title: ANTLR 4 : Bad grammar and 'no viable alternative at input'
body: <p>I'm using ANTLR 4 like that:</p>
  
  <pre><code>import org.antlr.v4.runtime.ANTLRInputStream;
  import org.antlr.v4.runtime.CharStream;
  import org.antlr.v4.runtime.CommonTokenStream;
  import org.antlr.v4.runtime.TokenStream;
  
  public class Builder
  {
      public static void main(String[] args)
      {
          CharStream input = new ANTLRInputStream("ON M1==2 &amp;&amp; M3 == 5 &amp;&amp; (M2 &gt; 1 || M5 &lt;= 5.0) "
                                                + "DO P5:42 P4:10");
          ExprLexer lexer = new ExprLexer(input);
          TokenStream tokens = new CommonTokenStream(lexer);
          ExprParser parser = new ExprParser(tokens);
          ExprParser.ExpressionContext uu = parser.expression();
          for (int i = 0; i &lt; uu.getChildCount(); ++i)
              System.out.println(uu.getChild(i));
      }
  }
  </code></pre>
  
  <p>And the following grammar:</p>
  
  <pre><code>grammar Expr;
  options
  {
    // antlr will generate java lexer and parser
    language = Java;
  
  }
  WS      : [ \t\r\n]+ -&gt; skip ;
  OP      : '&amp;&amp;' | '||';
  COMP    : '==' | '&lt;' | '&gt;' | '&lt;=' | '&gt;=' | '!=';
  INT     : [0-9]+;
  REAL    : INT '.' INT | INT;
  
  ACTION  : 'P' INT ':' INT;
  MEASURE : 'M' INT;
  
  // ***************** parser rules:
  cond       : MEASURE COMP REAL;
  condexpr   : '(' condexpr ')' | cond OP condexpr | cond;
  actionexpr : ACTION actionexpr | ACTION;
  expression : 'ON' condexpr 'DO' actionexpr;
  </code></pre>
  
  <p>I have the following output:</p>
  
  <pre><code>line 1:7 no viable alternative at input 'M1==2'
  ON
  [29]
  DO
  [31]
  </code></pre>
  
  <p>I think I have an error in my grammar, but I do not see it.
  Have you got an idea?</p>
  
  <p>For your help,
  Thanks by advance.</p>