[antlr-interest] Re: java15.g: Can't parse only conditionalExpression

Terence Parr parrt at cs.usfca.edu
Tue Nov 23 09:28:29 PST 2004



On Nov 23, 2004, at 8:52 AM, atripp54321 wrote:
>> 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)

ANTLR only thinks top-level rules (unreferenced by other rules) can be 
followed by "end of input" so it's a matter of doing a trick as Andy 
suggests or just adding a rule like:

conditionalExpressionTopLevel : conditionalExpression ;

Ter
--
CS Professor & Grad Director, University of San Francisco
Creator, ANTLR Parser Generator, http://www.antlr.org
Cofounder, http://www.jguru.com
Cofounder, http://www.knowspam.net enjoy email again!





 
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