Search code examples
antlr3gunit

Decision can match input such as "{'A'..'Z', '_', 'a'..'z'}" using multiple alternatives: 1, 3


I am a startbie for this antlr 3.5. I understood that left recursion is accepted in ant;r 4.0 and not in 3.5, I am getting ambigious error warning for my grammar . I am just verifying my email using this grammar, can some one fix this grammar

        grammar HelloWorld;

        options
        {
          // antlr will generate java lexer and parser
          language = Java;
          // generated parser should create abstract syntax tree
          output = AST;
          backtrack = true;
        }

        //as the generated lexer will reside in com.nuwaza.aqua.antlr
        //package, we have to add package declaration on top of it
        @lexer::header {
          package com.nuwaza.aqua.antlr;
        }

        //as the generated parser will reside in org.meri.antlr_step_by_step.parsers 
        //package, we have to add package declaration on top of it
        @parser::header {
          package com.nuwaza.aqua.antlr;
        }



        // ***************** parser rules:
        //our grammar accepts only salutation followed by an end symbol
        expression : EmailId At Domain Dot Web EOF;


        // ***************** lexer rules:
        //the grammar must contain at least one lexer rule
        EmailId: (Domain)+;   
        At : '@';
        Domain:(Identifier)+;
        Dot: DotOperator;
        Web:(Identifier)+|(DotOperator)+|(Identifier)+;
        /*Space
          :
          (
            ' '
            | '\t'
            | '\r'
            | '\n'
            | '\u000C'
          )

           {
            skip();
           }
          ;*/
        Identifier
          :
          (
            'a'..'z'
            | 'A'..'Z'
            | '_'
          )
          (
            'a'..'z'
            | 'A'..'Z'
            | '_'
            | Digit
          )*
          ; 
          fragment
        Digit
          :
          '0'..'9'
          ;

          fragment DotOperator:'.';

Solution

  • I assume that your problem is in your rule: Identifier. If I were you, I would do something like:

    Identifier : ID (ID |Digit)*; 
    
    fragment ID : ('a'..'z' | 'A'..'Z' | '_');
    

    I hope this would help you. ;)