[antlr-interest] Syntactic predicates and loops

Monty Zukowski monty at codetransform.com
Fri Apr 30 09:57:11 PDT 2004


On Apr 30, 2004, at 9:18 AM, Noel F Bryson wrote:

> I'm trying to build a grammar which includes something like the
> following rules, which are intended to parse declaration-lists e.g. T1
> a,b,c,T2 d,e,f,T3 g,h (where T1,T2 are types and a-h are variables).
>
> type: identifier;
> variable: identifier;
>
> list_of_variables: variable (COMMA variable)*;
>
> declaration_list: type list_of_variables
>                       ( COMMA type list_of_variables)*;
>
>
> I have simplified the rules to illustrate my problem - which is that
> I get an ambiguity in list_of_variables:
>
> sand2.g:35: warning:nondeterminism upon
> sand2.g:35:     k==1:COMMA
> sand2.g:35:     k==2:identifier
> sand2.g:35:     k==3:EOF
> sand2.g:35:     between alt 1 and exit branch of block
>
> This occurs because ANTLR sees that, when it meets a COMMA, it can
> either match using the loop in list_of_variables, or exit and match
> using the loop in list_of_declarations. hence it can't tell if T2 is a
> variable name or a type name.
>
> Since 'type' and 'variable' are actually more complex than I have shown
> here, and themselves involve loops at the end, I don't think I can
> resolve this be factoring or increasing the look-ahead.
>
>
> I'd like to be able to resolve this by getting list_of_variables to use
> a syntactic predicate to lookahead through the identifier to see if
> there is a COMMA (or EOF) after it. I'm new to this field, and would be
> grateful if someone could show me how to do this - ANTLR ignores my
> feeble efforts e.g.
>
> list_of_variables: variable ((COMMA identifier COMMA identifier
> identifier) => COMMA variable)*;
>
What goes wrong with this attempt?  It looks like it should work, 
though ANTLR will complain of a superflous syntactic predicate since 
there is only one alternative in the ()* closure.

Monty Zukowski

ANTLR & Java Consultant -- http://www.codetransform.com
ANSI C/GCC transformation toolkit -- 
http://www.codetransform.com/gcc.html
Embrace the Decay -- http://www.codetransform.com/EmbraceDecay.html



 
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