[antlr-interest] Ambiguous parse tree generated

Sam Harwell sam at tunnelvisionlabs.com
Tue Oct 30 16:00:46 PDT 2012


It's probably best to make sub-rules for this:

style : dot_style* hash_style*;
dot_style : Dot Identifier;
hash_style : Hash Identifier;

Works equally well for the other example you gave.

style : dot_style*;
dot_style : Dot Identifier hash_style?;
hash_style : Hash Identifier;

Sam

-----Original Message-----
From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-bounces at antlr.org] On Behalf Of Gerald Rosenberg
Sent: Tuesday, October 30, 2012 2:59 AM
To: antlr-interest
Subject: [antlr-interest] Ambiguous parse tree generated

I have a rule

style  :  ( Dot Identifier )* ( Hash Identifier )* ;

AntlrV4 generates a context with a list of Dot, a list of Hash, and a list of Identifier.  While both Identifier's are syntactically identical, they are semantically distinct.  In this particular case, the list of Dot can be used to partition the list of Identifier.

However, if I change the rule to the preferred form

style  :   ( Dot Identifier ( Hash Identifier )? )*  ;

I still get the same single lists of Dot, Hash, and Identifier. Only way to determine the Dot vs Hash associated Identifier's is by inspection of the token stream?  Adding labels did not change the generated code.

What am I missing?

List: http://www.antlr.org/mailman/listinfo/antlr-interest
Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address




More information about the antlr-interest mailing list