I recently purchased The Definitive ANTLR Reference and I am excited to begin using ANTLR.
In the first chapter, this grammar is shown:
grammar T;
options {
language = Java;
}
r : 'call' ID ';' {System.out.println("invoke " + $ID.text);} ;
ID : 'a'..'z'+ ;
WS : (' '|'\n'|'\r')+ {$channel=HIDDEN;} ;
I copied this grammar down into a file, (.g extension), generated the Lexer and Parser, and created a main class like so:
import org.antlr.runtime.*;
public final class Test {
public static void main(String[] args) throws Exception {
ANTLRInputStream input = new ANTLRInputStream(System.in);
TLexer lexer = new TLexer(input);
CommonTokenStream tokens = new CommonTokenStream(lexer);
TParser parser = new TParser(tokens);
parser.r();
}
}
There are no real errors, but when I run the main class and enter:
call foo;
Nothing happens. "invoke foo" should be outputted to the screen, but nothing happens. I don't want to go on in the book without completing any one exercise. I'm using ANTLR 3.4 in Eclipse if it matters. Sorry if this seems like an easy question, but I'm new to ANTLR.
Thanks,
Omer
ANTLR (ANother Tool for Language Recognition) is a tool for processing structured text. It does this by giving us access to language processing primitives like lexers, grammars, and parsers as well as the runtime to process text against them. It's often used to build tools and frameworks.
It turns out ANTLR4 lets you generate parser code in a variety of languages: Java, C#, Python, Go, C++, Swift, JavaScript and even TypeScript!
ANTLR v4. ANTLR (ANother Tool for Language Recognition) is a powerful parser generator for reading, processing, executing, or translating structured text or binary files.
ANTLR 3 and ANTLR 4 are free software, published under a three-clause BSD License. Prior versions were released as public domain software. Documentation, derived from Parr's book The Definitive ANTLR 4 Reference, is included with the BSD-licensed ANTLR 4 source.
You need to enter the EOF character.
For UNIX based systems it is Ctrl-D
.
For Windows based systems it is Ctrl-Z
.
EDIT
Since you are entering input via the console, and ANTLR is reading the data as a stream, it needs the EOF. Later in the book you will be entering data via a file and the file's EOF will end the stream. You can also save the input to a file, and then pipe the input from the file into the command.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With