[antlr-interest] Why is ANTLR getting lost?

Jon Schewe jpschewe at mtu.net
Wed Feb 20 04:14:49 PST 2008


Ok, it's actually failing in testConstraints as well.  I just can't get 
a reasonable error value out of antlr.  Is there a good way to check for 
an error from ANTLR in such a case?  The constraints() method is 
returning non-null and not throwing an exception, however I'm getting a 
call to emit with "no viable alternative at input...".

Jon Schewe wrote:
> I've got a piece of a grammar that is causing ANTLR to get lost and 
> I'm not sure.  I've written unit tests with JUnit to call the 
> different rules to parse specific pieces of code with the input slowly 
> getting larger and it fails when I add the rule "constraintDecls".  
> Grammar is attached, one should just need to look at the first few rules.
>
> The rule constraintDecls calls constraints, which calls 
> constraintDecl.  Here is are two test methods, the first one passes 
> the second one fails:
>  /**
>   * @throws RecognitionException
>   */
>  @Test
>  public void testConstraints0() throws RecognitionException {
>    final String data = "constraint3: 5.0 <= 6 + 6 * a[1 + 3] * c - 1 
> <= 10.0;";
>
>    final GHOCESLexer lexer = new GHOCESLexer(new 
> ANTLRStringStream(data));
>    final CommonTokenStream tokens = new CommonTokenStream(lexer);
>    final GHOCESParser parser = new GHOCESParser(tokens);
>    final GHOCESParser.constraints_return r = parser.constraints();
>    Assert.assertNotNull(r);
>  }
>
>  /**
>   * @throws RecognitionException
>   */
>  @Test
>  public void testConstraintDecls0() throws RecognitionException {
>    final String data = "Constraints constraint3: 5.0 <= 6 + 6 * a[1 + 
> 3] * c - 1 <= 10.0;";
>
>    final GHOCESLexer lexer = new GHOCESLexer(new 
> ANTLRStringStream(data));
>    final CommonTokenStream tokens = new CommonTokenStream(lexer);
>    final GHOCESParser parser = new GHOCESParser(tokens);
>    final GHOCESParser.constraintDecls_return r = 
> parser.constraintDecls();
>    Assert.assertNotNull(r);
>  }
> Error is (I force an exception in emit to be sure what the call stack 
> is):
> 203 [main] ERROR 
> com.honeywell.htc.schedinfra.pconstraint.parser.GHOCESParser - line 
> 25:17 no viable alternative at input '1'
> java.lang.RuntimeException
>    at 
> com.honeywell.htc.schedinfra.pconstraint.parser.GHOCESParser.emitErrorMessage(GHOCESParser.java:124) 
>
>    at 
> org.antlr.runtime.BaseRecognizer.displayRecognitionError(BaseRecognizer.java:152) 
>
>    at 
> org.antlr.runtime.BaseRecognizer.reportError(BaseRecognizer.java:144)
>    at 
> com.honeywell.htc.schedinfra.pconstraint.parser.GHOCESParser.constraintDecl(GHOCESParser.java:2523) 
>
>    at 
> com.honeywell.htc.schedinfra.pconstraint.parser.GHOCESParser.constraints(GHOCESParser.java:1940) 
>
>    at 
> com.honeywell.htc.schedinfra.pconstraint.parser.GHOCESParser.constraintDecls(GHOCESParser.java:700) 
>
>    at 
> com.honeywell.htc.schedinfra.pconstraint.parser.GHOCESParser.program(GHOCESParser.java:176) 
>
>    at 
> com.honeywell.htc.schedinfra.pconstraint.GHOCES.parseFile(GHOCES.java:401) 
>
>    at 
> com.honeywell.htc.schedinfra.pconstraint.parser.test.TestParser.testSubscripts(TestParser.java:116) 
>
>    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
>    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
>    at java.lang.reflect.Method.invoke(Unknown Source)
>    at junit.framework.TestCase.runTest(TestCase.java:168)
>    at junit.framework.TestCase.runBare(TestCase.java:134)
>    at junit.framework.TestResult$1.protect(TestResult.java:110)
>    at junit.framework.TestResult.runProtected(TestResult.java:128)
>    at junit.framework.TestResult.run(TestResult.java:113)
>    at junit.framework.TestCase.run(TestCase.java:124)
>    at junit.framework.TestSuite.runTest(TestSuite.java:232)
>    at junit.framework.TestSuite.run(TestSuite.java:227)
>    at 
> org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:81) 
>
>    at 
> org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:38) 
>
>    at 
> org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) 
>
>    at 
> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460) 
>
>    at 
> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673) 
>
>    at 
> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386) 
>
>    at 
> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196) 
>
> 219 [main] INFO 
> com.honeywell.htc.schedinfra.pconstraint.parser.test.TestParser - 
> Running testSummation
> 219 [main] INFO 
> com.honeywell.htc.schedinfra.pconstraint.parser.test.TestParser - 
> Running testAddExpr0
> 235 [main] INFO 
> com.honeywell.htc.schedinfra.pconstraint.parser.test.TestParser - 
> Running testConstraintDecl0
> 235 [main] ERROR 
> com.honeywell.htc.schedinfra.pconstraint.parser.GHOCESParser - line 
> 1:30 no viable alternative at input '1'
>



More information about the antlr-interest mailing list