ANTLR no viable alternative to input '<EOF>'

I'm still on my way with ANTLR. I built the grammar and, for the most part, does what I expect, but I need it to be able to work quietly (without output to stdout or stderr).

Grammar

grammar MyPredicate; options { output=AST; } parse : expression EOF ; expression : field WS? OPERATOR_BINARY WS? value ; OPERATOR_BINARY : '=' | '<' | '>' | '<=' | '>=' | '!=' | 'has' ; value : VALUE_STRING | VALUE_NUMERIC | VALUE_BOOLEAN ; VALUE_STRING : '""' | '"' (ESC_SEQ | ~('\\'|'"'))+ '"' ; VALUE_NUMERIC : ('0'..'9')+ ('.' ('0'..'9')+)? ; VALUE_BOOLEAN : 'true' | 'false' ; field : FIELD_NAME ; FIELD_NAME : ('a'..'z'|'A'..'Z'|'_') ('a'..'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 void main(String[] args) throws Exception { MyPredicateParser parser = new MyPredicateParser(new CommonTokenStream(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(); } } } 

Enter

 a = 1 

Exit

 line 0:-1 mismatched input '<EOF>' expecting FIELD_NAME a = 1 null 

I am not sure why I get an EOF error. From what I understand, my grammar is parsed correctly, and I get an error after the parser is evaluated, but node is looking for EOF. Using ANTLR 3.2

+3
source share
1 answer

The problem is that you call parse() twice in your main class. Delete the line:

 parser.parse(); 

only:

 MyPredicateParser.parse_return r = parser.parse(); 

in place.

+13
source

Source: https://habr.com/ru/post/1316324/


All Articles