ANTLR no viable alternative at input '<EOF>' -


I am still on the path to learning with ANTLR. I have created a grammar and for the most part I hope , But I need to be able to run quietly (no output in stdout or stderr)

Grammar

  Grammar MyPredicate; Option {output = AST; } Paras: Expression EOF; Expression: Fields WS? OPERATOR_BINARY WS? value; OPERATOR_BINARY: '=' | '& Lt;' | '& Gt;' | '& Lt; = '| '& Gt; = '| '! = '| 'is'; Price: VALUE_STRING | VALUE_NUMERIC | VALUE_BOOLEAN; VALUE_STRING: '' '' | ('' '' '' '' '' '))' '' 'VALUE_NUMERIC: (' 0 '..' 9 ') + ('. '(' 0 '..' 9 ') (' One '..' 'z' | 'a' .. 'jade' | '_') ('a' .. '' '' right '' 'wrong'; field: FIELD_NAME ; FIELD_NAME: Z '|' A '..' Z '|' 0 '..' 9 '|' _ ') *; ESC_SEQ:' \\ '(' B '|' T '|' N '|' F '|' R '|' '' | '' \\ '); WS: (' '' \ t '|' \ r '|' '\ n') {skip ();};  

Java

  import org.antlr.runtime. *; Import org.antlr.runtime.tree. *; Import org .antlr.stringtemplate. *; Public class main {public static zero principal (string [] args) Exceptions {MyPredicateParser parser = New MyPredicateParser (New GeneralProtestream (new MyPredicateLexer (new ANTLRStringStream (args [0]))); MyPredicateParser.parse_return r = parser.parse (); parser.parse (); if (r.tree! = Null) {System.out.println ( (Tree) r.tree) .toStringTree ()); (CommonTree) r.tree) .sanityCheckParentAndChildIndexes (); }}  

input

  a = 1  

output

  line 0: -1 unmatched input '& lt; EOF & gt; Expect FIELD_NAME = 1 Zero  

I'm not sure why I found an EOF error, which I think is to parse my grammar correctly, and I evaluate the "pars" parser After getting the error, but that node is looking for EOF. Using the ANLR 3.2

The problem is that you are calling parse () < / Code> Remove the line twice in your main category:

  parser.parse ();  

Except only:

  MyPredicateParser.parse_return r = parser.parse ();  

In place.


Comments