[antlr-interest] ANTLR Grammar Operator Precedences

Kay Roepke kroepke at classdump.org
Sun Nov 12 21:50:50 PST 2006


On 13. Nov 2006, at 5:46 , Randall R Schulz wrote:

> Hi,
>
> On Sunday 12 November 2006 20:09, you wrote:
>> On 13. Nov 2006, at 5:03 , Randall R Schulz wrote:
>>> Is the precedence of the operators in ANTLR v3.0's grammar
>>> documented somewhere? I couldn't find anything on the Wiki.
>>
>> no, there isn't a list, unfortunately.
>> if you suspect a parser error to be responsible, consider running
>> ANTLR with
>> java org.antlr.Tool -Xgrtree yougrammar.g
>
> On what basis would I suspect an error when I don't know what's the
> correct behavior?

oh, boy. somehow i get the creepy feeling that i come across as an  
ass today.

i was merely judging by your previous posts that you suspect an error.

FOO: 'A' | 'B' | 'C' ;
vs.
FOO: ('A')|('B')|('C');

shouldn't make a difference, other than that the latter produces  
worse, but
semantically equivalent code.

>
>> that will print out the grammar AST. it could be huge, but at the
>> moment this
>> is the single most helpful tool we have.
>
> Surely someone knows what the relative priority of the concatenation,
> alternation and closure operators are, no? It seems kind of important.

well, afaik there is no written documentation of this, short of the  
grammar itself.
i can make one up at the spot, but surely that would bring in even  
more trouble today.
seems to be a bad day for communication :(

ok, here we go (descending priority):
set operators (range:'..',set negation:'~' only one allowed)
closure operators ('+','*','?' only one allowed)
concatenation (TERMINAL 'literal' TERMINAL)
alternation ('|')

this is probably not complete, and may be hideously wrong, but so what.
when in doubt, use parens. as always.

examples:
FOO: 'a'..'z'+;			// same as ('a'..'z')+
FOO: 'a'|'b'|'c'+;		// same as ('a')|('b')|('c'+)
FOO: ('a'|'b'|'c')+;		// to change priority
FOO: TOKEN 'literal' | BAR;	// same as (TOKEN 'literal')|(BAR)
FOO: ~'a'+;			// same as (~'a')+
FOO: (options {greedy=false;}: . )* BAR;	// options are for valid for  
all alts within the ()

hth,

-k
-- 
Kay Röpke
http://classdump.org/






More information about the antlr-interest mailing list