Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to force ANTLR to generate NoViableAltException?
    primarykey
    data
    text
    <p>I'm working with antlr 3.2. I have a simple grammar that consists of atoms (which are either the characters "0" or "1"), and a rule which accumulates a comma separated list of them into a list.</p> <p>When I pass in "00" as input, I don't get an error, which surprises me because this should not be valid input:</p> <pre><code>C:\Users\dan\workspace\antlrtest\test&gt;java -cp antlr-3.2.jar org.antlr.Tool Test.g C:\Users\dan\workspace\antlrtest\test&gt;javac -cp antlr-3.2.jar *.java C:\Users\dan\workspace\antlrtest\test&gt;java -cp .;antlr-3.2.jar TestParser [0] </code></pre> <p>How can I force a error to be generated in this case? It's particularly puzzling because when I use the interpreter in ANTLRWorks on this input, it <em>does</em> show a NoViableAltException.</p> <p>I find that if I change the grammar to require, say, a semicolon at the end, an error <em>is</em> generated, but that solution isn't available to me in the real grammar I am working on.</p> <p>Here is the grammar, which is self-contained and runnable:</p> <pre><code>grammar Test; @parser::members { public static void main(String[] args) throws Exception { String text = "00"; ANTLRStringStream in = new ANTLRStringStream(text); TestLexer lexer = new TestLexer(in); CommonTokenStream tokens = new CommonTokenStream(lexer); System.out.println(new TestParser(tokens).mainRule()); } } mainRule returns [List&lt;String&gt; words] @init{$words = new ArrayList&lt;String&gt;();} : w=atom {$words.add($w.text);} (',' w=atom {$words.add($w.text);} )* ; atom: '0' | '1'; WS : ( '\t' | ' ' | '\r' | '\n'| '\u000C' )+ { $channel = HIDDEN; } ; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload