[antlr-interest] Disambiguate following rules

Jost Baron Jost.Baron at gmx.de
Sun Oct 28 03:21:29 PDT 2012


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi!

The problem is that an expression might be an identifier, oder maybe
an assignment. So both possibilities may match the same input.

To resolve that, you would usually use left-factoring, but that seems
impractical here.

Another solution might be to remove the second alternative from the
var_decl rule. Why would you have an expression as variable name? I
don't know your language and reasons for that rule, of course.

Greetings,
Jost

On 10/28/2012 09:26 AM, forumer at smartmobili.com wrote:
> Hi,
> 
> I have a grammar with the following rules and that is using 
> backtrack option. Now when I remove backtrack I have a problem
> with the var_decl rule :
> 
> Decision can match input such as "IDENTIFIER" using multiple 
> alternatives: 1, 2
> 
> 
> Any idea about how to fix this ?
> 
> 
> 
> variable_declaration : VAR variable_declaration_list | 
> empty_statement ;
> 
> variable_declaration_list : var_decl ( ',' var_decl )* ;
> 
> 
> var_decl : identifier ( '=' expression )? | expression ;
> 
> /***EXPR*************************/ expression : 
> assignment_expression (',' assignment_expression )* ;
> 
> argument_expression_list : assignment_expression ( ',' 
> assignment_expression )* ;
> 
> assignment_expression : conditional_expression ( 
> assignment_operator assignment_expression )? ;
> 
> assignment_operator : '=' | '*=' | '/=' | '%=' | '+=' | '-=' | 
> '<<=' | '>>=' | '&=' | '^=' | '|=' | '>>>=' ;
> 
> constant_expression : conditional_expression ;
> 
> conditional_expression //: logical_or_expression  ( '?' 
> logical_or_expression ':' logical_or_expression )? : 
> logical_or_expression  ( '?' expression ':' expression )? ;
> 
> logical_or_expression : logical_and_expression ('||' 
> logical_and_expression )* ;
> 
> logical_and_expression : inclusive_or_expression ('&&' 
> inclusive_or_expression )* ;
> 
> inclusive_or_expression : exclusive_or_expression ('|' 
> exclusive_or_expression )* ;
> 
> exclusive_or_expression : and_expression ('^' and_expression)* ;
> 
> and_expression : equality_expression ('&' equality_expression)* ;
> 
> equality_expression /* === and !== for javascript */ : 
> relational_expression (('!=' | '==' | '===' | '!==' ) 
> relational_expression)* ;
> 
> relational_expression /* instanceof and in are for javascript */ : 
> shift_expression ( RELATION_OPERATOR shift_expression)* ;
> 
> RELATION_OPERATOR :  '<' | '>' | '<=' | '>=' | 'in' | 'instanceof' 
> // Don't tokenize these! ;
> 
> shift_expression : additive_expression (('<<' | '>>') 
> additive_expression)* ;
> 
> additive_expression : multiplicative_expression (('+' | '-') 
> multiplicative_expression)* ;
> 
> multiplicative_expression : unary_expression ( ('*' | 
> REGEX_OR_DIVIDE | '%') unary_expression )*    /* no cast
> expression for objj */ ;
> 
> unary_expression : postfix_expression | '++' unary_expression | 
> '--' unary_expression | unary_operator unary_expression   /* no 
> cast expression for objj */ ;
> 
> unary_operator //    : '&' | '*' | '+' | '-' | '~' | '!' : '+' | 
> '-' | '~' | '!' ;
> 
> postfix_expression : primary_expression ( '[' ']' | '[' expression 
> ']' | '(' ')' | '(' argument_expression_list ')' | '.' identifier
> | '++' | '--' )* ;
> 
> primary_expression : TRUE | FALSE | 'defined' '(' expression ')' /*
> for prepocessing */ | TYPEOF identifier                 /* 
> javascript */ | TYPEOF '(' identifier ')'         /* javascript */
>  | '[' expression? ']'     /* objj array? DIES */ | '[' expression 
> ',' expression ( ',' expression )* ']'   /* objj array? DIES */ | 
> message_expression        /*   '[' receiver message_selector ']' */
> | selector_expression | protocol_expression | encode_expression |
> '{' STRING_LITERAL ':' primary_expression ( ','  STRING_LITERAL ':'
> primary_expression )* '}'  /* js obj array? */ | '{' '}' /* js
> empty obj array? */ | constant | identifier | VAR identifier | '('
> expression ')' ;
> 
> 
> identifier : a=IDENTIFIER { /*System.out.println ($a.text);*/ } ;
> 
> IDENTIFIER : 
> ('_'|'A'..'Z'|'a'..'z')('_'|'A'..'Z'|'a'..'z'|'0'..'9')* ;
> 
> List: http://www.antlr.org/mailman/listinfo/antlr-interest 
> Unsubscribe: 
> http://www.antlr.org/mailman/options/antlr-interest/your-email-address
>
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://www.enigmail.net/

iQIcBAEBAgAGBQJQjQclAAoJEG6HPMAgWtVz8RoQAMxEGgo6CRYtzgF3ui1jMFLX
1N53lWwW+npzcRgUjgi11HhQOAnmCz6dTRGbHuOHGBFb9hOQZlQSkHlg3WhIjT4e
jAeVljmiyo41XRmNWdrsqyphog8uBygbtU+v+VX4MqXjaCkwOpHeUKv1EkDpW5xL
iZBsYhLKAkAY4et1zuJHByPgyWeORkS8FwSFmMjzh3jYDS+kHL9CM0qker1dlpUO
lb1kHQoQZ2Ze++3/UoMMrD3/ZW1sfQTkmy6msPnMhKOZyLFpV2zM3oU6ZibOTDP2
1kf5alKXbVWPkCGxlHqPGWGLe0oehRKDjDOVyVySRZZUpCg/4VJe3zgK8fxGyLyQ
kRkXxJ5nQi6vq5hiJcekgIbU/hHEF0LhoBkcGZjHlfMKWgpaUM8mWilYGmwAPA7x
0ichrk5wyIZQEsdAWnYuQUzs2dR/41N2Pg//5rW05DApI26SoZipP/FmIBmHLGGd
rn3w2da39kTt1kifqAUvhbMC+f3HFhuoh98VV3s7Z5uux24ba4+v9KUWJEu2wH98
jlbXkTUxgJU9VYKlw/M7DsoYLYvZKGKVvEflYzncgjS4O3C2H54GAn5wolaAAVIS
M5SIYjm12jt9ui5IiLiMm3JD6mqKT655qTNJYKSw+KTkud3FqXmbH8ovhXkSZEMM
aQOZIAfSyW1gGoQJf1Fe
=++LP
-----END PGP SIGNATURE-----


More information about the antlr-interest mailing list