[antlr-interest] AST with optional parameters

John B. Brodie jbb at acm.org
Tue Jun 7 05:22:31 PDT 2011


Greetings!

On Tue, 2011-06-07 at 07:41 -0400, David Smith wrote:
> I'm parsing a grammar in which the semicolon on the end of a line is 
> optional. So two of the statement rules might be:
>     |   (ID GETS expr SEMI) => ID GETS expr SEMI -> ^(GETS ID expr SEMI)
>     |   (ID GETS) => ID GETS expr -> ^(GETS ID expr)
> Since this occurs with a number of different assignment statements, I 
> would really like to collapse this into one rule that looks something 
> like this:
>     |   (ID GETS) => ID GETS e=expr (s=SEMI)? -> ^(GETS ID $e $s)
> but every implementation I can think of either refuses to generate 
> the grammar or, as in the case above, generates the grammar but 
> decides that the variable 's' is unknown.
> Is there a any way to achieve this?

Have you tried:

    |   (ID GETS) => ID GETS e=expr (s=SEMI)? -> ^(GETS ID $e $s?)

is the semicolon really necessary in the tree? (e.g. does the presence
of the semicolon actually change the meaning of of the statement?)

perhaps:

    | (ID GETS) => ID GETS^ expr SEMI!?


Hope this helps...
   -jbb




More information about the antlr-interest mailing list