line 1:0 no viable alternative at input '<EOF>'

1,572 views
Skip to first unread message

Gaurav Singh

unread,
Oct 9, 2015, 3:11:42 AM10/9/15
to antlr-discussion
Hello experts,

I am new to ANTLR4 and really excited to learn this awesome tool. 

I am going through  'The Definitive ANTLR 4 Reference' guide step-by-step, but stuck in section 4.1 : Arithmetic Expression Language. Used the grammar Expr.g4 with some modification:

grammar Expr;

prog : stat+;

stat: expr NEWLINE
| ID '=' expr NEWLINE
| NEWLINE
;
expr : expr ('*'|'|') expr //modified
| expr ('+'|'-') expr //modified
| ID
| INT
| '(' expr ')'
;
ID : [a-zA-Z]+;

INT : [0-9]+;

NEWLINE :'\r'? '\n' ;

WS : [ \t]+ -> skip;

With 'grun' everything is working fine for following inputs (using Windows command prompt):

a. 123
b. (a+b)

But when I tried the generated Parser/Lexer with my own java code:

import java.io.InputStream;

import org.antlr.v4.runtime.ANTLRInputStream;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.tree.ParseTree;


public class ExpressionTester {

public static void main(String[] args) {
String fileName = null;
if(args.length >0) fileName = args[0];
try {
InputStream input = System.in;

if(fileName != null) input = new FileInputStream(fileName);

ANTLRInputStream charStream = new ANTLRInputStream(input);
ExprLexer lexer = new ExprLexer(charStream);
CommonTokenStream tokenStream = new CommonTokenStream(lexer);
ExprParser parser = new ExprParser(tokenStream);
ParseTree tree = parser.prog();
System.out.println(tree.toStringTree(parser));
} catch (Exception e) {
e.printStackTrace();
}
}

}


 By calling the main() using command line : java Expressiontester 1
^Z

Output : 
      line 1:0 no viable alternative at input '<EOF>'
      <prog stat>

Kindly help. The problem arise when I pass the file t.expr as argument.

Thanx in advance.
       
Expr.g4
ExpressionTester.java

Gaurav Singh

unread,
Oct 9, 2015, 4:43:58 AM10/9/15
to antlr-discussion
I got the glitch. I was passing the input as an argument to main() through command line.
It is working perfectly when I did like :

 java Expressiontester
(a+b)
^Z

output : (prog (stat (expr( (expr (expr a) + (expr b)) )) \r\n))
Reply all
Reply to author
Forward
0 new messages