[antlr-interest] Re: java15.g: Can't parse only conditionalExpression
atripp54321
atripp at comcast.net
Tue Nov 23 08:52:22 PST 2004
--- In antlr-interest at yahoogroups.com, Jens Theeß <j.theess at t...> wrote:
> Hi,
>
> I'm developing a tool that parses a textual form of UML sequence
> diagrams with some embedded java. The embedded java includes compound
> statements and conditional expressions. Before integrating the java
> parser (java15.g) into the sequence diagram parser, I tested the java
> parser stand-alone on some input it will encounter in the sequence
> diagram. The setup is as follows:
>
> JavaLexer lexer = new JavaLexer(r);
> lexer.setFilename(f);
> JavaRecognizer parser = new JavaRecognizer(lexer);
> parser.setFilename(f);
> parser.conditionalExpression(); // or
> parser.compoundStatement().
>
> The call to parser.compoundStatement() does fine with following input:
>
> {
> testfiles.A a = new testfiles.A();
> sdVariables.put("testfiles.A", "a", a);
> }
>
> However, a call to parser.conditionalExpression() fails on following
input:
>
> 1 == 2
>
> or
>
> d.startsWith("ha")
>
> What am I missing? Isn't the input a valid conditional expression?
>
> Thanks,
> Jens
Yea, I've always had this problem too. The problem (I think)
is that whenever you try calling any parser method other
than the top-level one (compilationUnit), you run the risk of
the parser trying to check "lookahead" tokens that don't exist.
What I do to get around it is to have a function that first
appends ";;" before parsing an expression. The semicolons
stop the lookahead from failing, and they're not part of the
generated AST.
That might be a nice enhancement to ANTLR - make sure
that any parser method can be called (not sure if that's
possible though)
Andy
Yahoo! Groups Links
<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/antlr-interest/
<*> To unsubscribe from this group, send an email to:
antlr-interest-unsubscribe at yahoogroups.com
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/
More information about the antlr-interest
mailing list