[antlr-interest] For the documentation, perhaps

Thomas Brandon tbrandonau at gmail.com
Thu Aug 9 01:18:52 PDT 2007


On 8/9/07, Josh Scholar <joshscholar at nightstudies.net> wrote:
> This wasn't exactly what I was expecting, so maybe it should be
> documented ( I did buy the book by the way - but I probably skimmed
> too much):
>
> If you set a variable inside of an EBNF iterator but use it outside of
> that iterator, it only gets the final value.  Maybe attempting that
> should be an error.
>
> For instance, the following grammar, given "[a,b,c . d];" as input
> returns (LIST A C (DOT D)) instead of (LIST A B C (DOT D)).
>
> grammar simplelistgrammar;
>
> options {
> output=AST;
> }
>
> tokens {LIST;DOT;}
>
> prog    :        statement *;
> statement       :               atom? ';'! ;
>
> #****the problem is $rest:
> list    :h='['  ( head=atom (',' rest=atom)* ('.' dot=atom)? )?   ']'
> ->^(LIST $head? $rest* ^(DOT $dot)?) ;

This is the correct behaviour. You've told ANTLR you only want a
single rest. You want to use += instead, telling ANTLR you want to
collect all rests, like:
list    :h='['  ( head=atom (',' rest+=atom)* ('.' dot=atom)? )?   ']'
 ->^(LIST $head? $rest* ^(DOT $dot)?) ;
Then it should behave as you expect.
>
> atom    :SYMBOL|list;
> SYMBOL :  ('a'..'z'|'A'..'Z')+;
> WS  :   (' '|'\t')+ {$channel=HIDDEN;} ;
>
> ......
> What works is
> ...
> sublist : (','! atom)*;
> list    :h='['  ( head=atom  rest=sublist ('.' dot=atom)? )?   ']'
> ->^(LIST $head? $rest ^(DOT
> $dot)?) ;
> ...
>
> I wouldn't sure that second one would work even for something "[X.Y];"
> where "sublist" is empty, but it does.  The rewrite rules are smart
> enough to avoid creating a link for an empty rule.
Actually I think here ANTLR will create a label for sublist and add
all it's children into your tree, but in such a case it has no
children. A slightly cleaner way is:
sublist : (','! atom)+;
list    :h='['  ( head=atom  (rest=sublist)? ('.' dot=atom)? )?   ']'
 ->^(LIST $head? $rest? ^(DOT $dot)?) ;

> It might be complicated to make the parser understand some sort of
> algebra of EBNF operators so that when you use a variable outside of
> iterator it collects the values from the iteration into their
> respective variables so that the original example just works.
>
It would be hard to make ANTLR guess what you wanted, instead
correctly tell it want you want and it will do it ;).

Tom.
> Joshua Scholar
>


More information about the antlr-interest mailing list