[antlr-interest] Resolving ambiguities between rules

Terence Parr parrt at cs.usfca.edu
Mon Mar 7 08:50:52 PST 2005


On Mar 7, 2005, at 3:39 AM, togol machillan wrote:

> Hello,
>
> How do I resolve ambiguities between rules when a certain starting 
> part of the rules are the same, like the following two rules.
>
> p_singleline_list { std::string k = "";}
> 		    : (k = keyword) {std::cout << "With single line list --- Keyword 
> is " << k << std::endl;}
> 		     (VALUE SLASH)+ (VALUE NEWLINE|AMPERSAND NEWLINE (VALUE 
> (SLASH)?)+ NEWLINE)  ;
>
>
> p_with_sentences { std::string k = "";}
> 	     :(k = keyword) {std::cout << "Single line para  with 
> sentences--- Keyword is " << k << std::endl;}
> 	     (VALUE|FIGURE|VALUE EQUAL (VALUE|FIGURE))+ NEWLINE (sentence)*;
>
> Here the parser always tries to match with the first rule and throws 
> in an unexpected token error when the structure should match the 
> second rule. Is there any way to prioritise rule matching? Is it 
> possible to use syntactic perdicates to differentiate two rules rather 
> two alternatives of the same rule?

Hi.  The key is in the rule that calls these.  That is where the 
decision is made about which to call.  You may have to move keyword 
rule reference into that rule and pass k downwards as an argument into 
these rules.

Or just use a syntactic predicate.

Terence





More information about the antlr-interest mailing list