5

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

Guy Coder
  • 24,501
  • 8
  • 71
  • 136
leaf
  • 95
  • 5

1 Answers1

6

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.

Guy Coder
  • 24,501
  • 8
  • 71
  • 136