Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
255 views
in Technique[技术] by (71.8m points)

JavaCC - parse a step of an XPATH expression

I'm trying to write a JavaCC script for a (simple) XPath parser and I'm having problems with the part to parse individual steps.

My idea of the grammar is this:

Step ::= ( AxisName "::" )? NodeTest ( "[" Predicate "]" )*

I have transformed it into the following script snippet:

Step Step() :
{
    Token t;

    Step step;

    Axis axis;
    NodeTest nodeTest;
    Expression predicate;
}
{
    { axis = Axis.child; }

    (
        t = <IDENTIFIER>
        { axis = Axis.valueOf(t.image); }

        <COLON>
        <COLON>
    )?

    t = <IDENTIFIER>
    { nodeTest = new NodeNameTest(t.image); }

    { step = new Step(axis, nodeTest); }

    (       
        <OPEN_PAR>

        predicate = Expression()

        { step.addPredicate(predicate); }

        <CLOSE_PAR>
    )*

    { return step; }
}

This, however, doesn't work. Given the following expression:

p

it throws the following error:

Exception in thread "main" java.lang.IllegalArgumentException: No enum constant cz.dusanrychnovsky.generator.expression.Axis.p
    at java.lang.Enum.valueOf(Unknown Source)
    at cz.dusanrychnovsky.generator.expression.Axis.valueOf(Axis.java:3)
    at cz.dusanrychnovsky.generator.parser.XPathParser.Step(XPathParser.java:123)
    at cz.dusanrychnovsky.generator.parser.XPathParser.RelativeLocationPath(XPathParser.java:83)
    at cz.dusanrychnovsky.generator.parser.XPathParser.AbsoluteLocationPath(XPathParser.java:66)
    at cz.dusanrychnovsky.generator.parser.XPathParser.Start(XPathParser.java:23)
    at cz.dusanrychnovsky.generator.parser.XPathParser.parse(XPathParser.java:16)
    at cz.dusanrychnovsky.generator.Main.main(Main.java:24)

I believe that what happens is that the parser sees an identifier on the input so it takes the axis branch even though no colons will follow, which the parser cannot know at that time.

What is the best way to fix this? Should I somehow increase the lookahead value for the Step rule, and if that's the case, then how exactly would I do that? Or do I need to rewrite the rule somehow?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Two choices:

(   LOOKAHEAD(3)
    t = <IDENTIFIER>
    { axis = Axis.valueOf(t.image); }

    <COLON>
    <COLON>
)?

or

(   LOOKAHEAD( <IDENTIFIER> <COLON> <COLON> )
    t = <IDENTIFIER>
    { axis = Axis.valueOf(t.image); }

    <COLON>
    <COLON>
)?

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...