[antlr-interest] Left recursion removal

Johannes Luber jaluber at gmx.de
Tue May 8 05:01:11 PDT 2007


Loring Craymer wrote:
> Unless I missed something, these two rules are
> identical, except that element_access is incorrectly
> defined (the three occurrences of primary_expression
> with suffixes are in fact just primary expression
> variants).  Take a closer look to be sure, then change
> occurrences of "element_access" to
> "primary_expression" and delete the element_access
> rule.
> 
> --Loring

Thanks for your idea! Using the primary_expression variant without left
recursion removal I saw that primary_expression has two additional
alternatives: "array_creation_expression" and "element_access '['
expression_list ']'". Simply replacing the element_access reference with
primary_expression would allow array_creation_expression directly in
front of "'[' expression_list ']'" instead having always either "'.'
identifier type_argument_list?", "'(' argument_list? ')'", "'++'" or
"'--'" between those subrules. Unfortunately every other alternative
requires that no tokens may be inserted in this place. That means that
the following rule isn't quite equivalent:

primary_expression
    :   array_creation_expression
    |   literal
    |   simple_name
    |   parenthesized_expression
    |   primary_expression '.' identifier type_argument_list?
    |   predefined_type '.' identifier type_argument_list?
    |   qualified_alias_member '.' identifier type_argument_list?
    |   primary_expression '(' argument_list? ')'
    |   primary_expression ('.' identifier type_argument_list? | '('
argument_list? ')' | '++' | '--')? '[' expression_list ']'
    |   this_access
    |   base_access
    |   primary_expression '++'
    |   primary_expression '--'
    |   object_creation_expression
    |   delegate_creation_expression
    |   typeof_expression
    |   checked_expression
    |   unchecked_expression
    |   default_value_expression
    |   anonymous_method_expression
    |   sizeof_expression
    |   {isUnsafeContext}?=> pointer_member_access
    |   {isUnsafeContext}?=> pointer_element_access
    |   {allowOrcas}?=> anonymous_object_creation_expression
    ;

How do I check best that array_creation_expression has to be used as
left-most token when the optional subrule "('.' identifier
type_argument_list? | '(' argument_list? ')' | '++' | '--')?" has been
used (and the opposite - no occurrence of the optional subrule requires
no array_creation_expression)? Can I do it with actions or do I have to
check it in another pass over the grammar (which would be doable but
require one pass more than already planned)?

Best regards,
Johannes Luber


More information about the antlr-interest mailing list