[antlr-interest] For the documentation, perhaps
Cameron Palmer
cameron.palmer at gmail.com
Thu Aug 9 01:39:45 PDT 2007
+= operator gets what you want. Page 132 and 148 of The Book.
prog : statement *;
statement : atom? ';'! ;
list : h='[' ( head=atom (',' rest+=atom)* ('.' dot=atom)? )? ']'
-> ^(LIST $head? $rest* ^(DOT $dot)?) ;
atom : SYMBOL | list;
SYMBOL : ('a'..'z'|'A'..'Z')+;
WS : (' '|'\t'|'\n')+ {$channel=HIDDEN;} ;
Cameron Palmer
University of North Texas
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)?) ;
>
> 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.
>
> 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.
>
> Joshua Scholar
>
More information about the antlr-interest
mailing list