[antlr-interest] How to remove the rules ambiguity here?

Johannes Luber jaluber at gmx.de
Sun Oct 28 10:37:47 PDT 2007


Hi!

I've been trying to remove a certain ambiguity with the rule
compilation_unit in the attached grammar, which is a reduced variant of
my normal grammar. The problem stems from an interaction between the
following rules:

compilation_unit
    :   extern_alias_directive* using_directive*
        global_attribute_section* namespace_member_declaration* EOF
    ;

global_attribute_section
    :   OPEN_BRACKET global_attribute_target_specifier attribute_list
        COMMA? CLOSE_BRACKET
    ;

namespace_member_declaration
    :   namespace_declaration
    |   type_declaration
    ;

type_declaration
    :   class_declaration
    |   struct_declaration
    |   interface_declaration
    |   enum_declaration
    |   delegate_declaration
    ;

qualified_alias_member
    :   IDENTIFIER DOUBLE_COLON IDENTIFIER type_argument_list?
    ;

delegate_declaration
    :   attribute_section* delegate_modifier* DELEGATE return_type
        IDENTIFIER type_parameter_list? OPEN_PARENS
        formal_parameter_list? CLOSE_PARENS
        type_parameter_constraints_clause* SEMICOLON
    ;

attribute_section
    :   OPEN_BRACKET attribute_target_specifier? attribute_list
        COMMA? CLOSE_BRACKET
    ;

compilation_unit references both global_attribute_section and
namespace_member_declaration. The latter one calls finally
attribute_section, which is near-identical to global_attribute_section.
Even if the actual values always differ for each attribute rule, ANTLR
consider them identical due to the use of contextual_keyword["keyword"].
The use of syntactic predicates doesn't seem to have an influence here.

Can I remove them and trust on ANTLRs ability to to tell the options
apart? Or do I have to merge the global_attribute_section with
attribute_section and look later in a second phase, if those attributes
were allowed here? Or can I safely use a flag to tell the parser "Here
aren't global attributes allowed!"?

Johannes
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: CSharp3ParserTest.g
Url: http://www.antlr.org/pipermail/antlr-interest/attachments/20071028/c8b2634a/attachment.pl 
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: CSharp3Lexer.tokens
Url: http://www.antlr.org/pipermail/antlr-interest/attachments/20071028/c8b2634a/attachment-0001.pl 


More information about the antlr-interest mailing list