[antlr-interest] very simple doubt about EXPR grammar

John B. Brodie jbb at acm.org
Thu Oct 14 13:17:02 PDT 2010


Greetings!
On Thu, 2010-10-14 at 09:31 -0300, Leonardo K. Shikida wrote:
> Hi Kevin
> 
> You´re right. So I´ve changed the grammar to include a stopword (semicolon).
> 
> Still the same problem.
> 
> 1-1+1; generates a NoViableAltException

very strange...

> 
> while
> 
> 1+1-1; does not
> 
> This is very strange because according to the rule
> 
> expr
>     :   e=multExpr
>         (   '+' multExpr
>         |   '-' multExpr
>         |   '*' multExpr
>         |   '/' multExpr
>         )*
>     ;
> 
> it does not matter what symbol comes. In fact, for all other
> combinations of symbols in the same expression, only those starting
> with 1-1 throws the exception.
> 
> 1*1-1; OK
> 1*1/1; OK
> 1-1-1; NOT OK
> 1*1+1; OK

unable to reproduce. attached please find a complete test grammar
including a test driver that contains your grammar.

this test grammar parses all four of the above without any problem.

(does your test input happen to (incorrectly) include a blank(s)? your
lexer accepts white space but your parser does not....)

> 
> and so on...
> 
> Can anyone help me? Is it an ANTLR bug or am I missing something here
> in this grammar?
> 
> Thanks in advance
> 
> Leo.
> 
> >>>>>>>>>>>>>>>>>
> 
> grammar Expr;
> 
> @header {
> }
> 
> @members {
> }
> 
> 
> stat:   comp ';'
>     ;
> 
> comp
>     :   e=expr
>         (   '>' expr
>         |   '<' expr
>         |   '=' expr
>         )*
>     ;
> 
> expr
>     :   e=multExpr
>         (   '+' multExpr
>         |   '-' multExpr
>         |   '*' multExpr
>         |   '/' multExpr
>         )*
>     ;
> 
> multExpr
>     	:	atom
>     	(    	atom
>     	)*
>     ;
> 
> atom
>     :   INT
>     |   ID
>     |  '(' comp ')'
>     ;
> 
> ID  :   ('a'..'z'|'_')+ ;
> INT :   '0'..'9'+ ;
> WS  :   (' '|'\t')+  ;
> 
> []
> 
> Leonardo K. Shikida
> 
> 
> 
> 
> 
> On Wed, Oct 13, 2010 at 3:14 PM, Kevin J. Cummings
> <cummings at kjchome.homeip.net> wrote:
> > On 10/13/2010 01:29 PM, Leonardo K. Shikida wrote:
> >> Hi
> >>
> >> This is something stupid, I guess. I have a grammar like this below
> >> and I would like to know why
> >>
> >> "1+1-1" works
> >>
> >> and
> >>
> >> "1-1+1" does not work (NoViableAltException)
> >
> > NoViableAltException is thrown in your stat rule when it can't predict
> > an INT, ID, (, or NEWLINE in the lookahead.  Does your test case end in
> > a NEWLINE?
> >
> >> Thanks
> >>
> >> Leo K.
> >
> > --
> > Kevin J. Cummings
> > kjchome at rcn.com
> > cummings at kjchome.homeip.net
> > cummings at kjc386.framingham.ma.us
> > Registered Linux User #1232 (http://counter.li.org)
> >

-------------- next part --------------
grammar Test;

options {
   output = AST;
   ASTLabelType = CommonTree;
}

@members {
   private static final String [] x = new String[] {
      "1*1-1;", // OK
      "1*1/1;", // Ok
      "1-1-1;", // NOT OK
      "1*1+1;"  //  OK
   };

   public static void main(String [] args) {
      for( int i = 0; i < x.length; ++i ) {
         try {
            System.out.println("about to parse:`"+x[i]+"`");
            TestLexer lexer = new TestLexer(new ANTLRStringStream(x[i]));
            CommonTokenStream tokens = new CommonTokenStream(lexer);
            System.out.println("tokens:"+tokens.toString());

            TestParser parser = new TestParser(tokens);
            TestParser.start_return p_result = parser.start();

            CommonTree ast = p_result.tree;
            if( ast == null ) {
               System.out.println("resultant tree: is NULL");
            } else {
               System.out.println("resultant tree: " + ast.toStringTree());
            }
            System.out.println();
         } catch(Exception e) {
            e.printStackTrace();
         }
      }
   }
}

start : stat EOF!;

stat:   comp ';'
    ;

comp
    :   e=expr
        (   '>' expr
        |   '<' expr
        |   '=' expr
        )*
    ;

expr
    :   e=multExpr
        (   '+' multExpr
        |   '-' multExpr
        |   '*' multExpr
        |   '/' multExpr
        )*
    ;

multExpr
        :       atom
        (       atom
        )*
    ;

atom
    :   INT
    |   ID
    |  '(' comp ')'
    ;

ID  :   ('a'..'z'|'_')+ ;
INT :   '0'..'9'+ ;
WS  :   (' '|'\t')+  ;


More information about the antlr-interest mailing list