[antlr-interest] another milestone for v3.0

Terence Parr parrt at cs.usfca.edu
Thu Mar 17 13:02:19 PST 2005


Howdy,

Just this morning I was able to get ANTLR to *interpret* the java.g 
grammar.  In other words, instead of generating a parser in some 
language, compiling it, and then running it on some java input, I was 
able to parse Java w/o generating code or compiling at all.  The result 
of interpretation is a parse tree always.  If there is a parse error, 
it blasts out of the interpreter, but leaves an error node in the tree 
indicating where the problem was.

The reason you care about this is because of the GUI dev environment.  
Imagine prototyping a language.  You're building the grammar and you 
want to constantly check a small sample input against your grammar.  
Generating code, compiling, and running it is a hassle.  With the GUI, 
you'll be able to immediately test input against the grammar or just a 
single rule etc... :)  Hooray!

BTW, the interpreter is actually pretty fast. :)

For the curious, here is what the code looks like that reads in the 
java.g grammar dynamically and interprets an input file.  Note that the 
java.g file is a combined parser/lexer and hence the call to get the 
lexer grammar from the original grammar.  This seems pretty clean to me 
for such a real world problem.

Grammar parser =
     new Grammar("java.g", new BufferedReader(new FileReader("java.g")));

String lexerGrammarText = parser.getLexerGrammar();
Grammar lexer = new Grammar();
lexer.importTokenVocabulary(parser); // make sure token types line up
lexer.setGrammarContent(lexerGrammarText);

CharStream input = new ANTLRFileStream(args[0]);
Interpreter lexEngine = new Interpreter(lexer, input);
CommonTokenStream tokens = new CommonTokenStream(lexEngine);
// parser tunes to channel one by default, ignores stuff on other 
channels.
// can't execute actions in the lexer so I have to force certain tokens 
to
// be ignored
tokens.setTokenTypeChannel(lexer.getTokenType("WS"), 99);
tokens.setTokenTypeChannel(lexer.getTokenType("SL_COMMENT"), 99);
tokens.setTokenTypeChannel(lexer.getTokenType("ML_COMMENT"), 99);

System.out.println("tokens="+tokens.toString()); // print all tokens
Interpreter parseEngine = new Interpreter(parser, tokens);
ParseTree t = parseEngine.parse("compilationUnit"); // interpret

System.out.println(t); // print parse tree

Ter
--
CS Professor & Grad Director, University of San Francisco
Creator, ANTLR Parser Generator, http://www.antlr.org
Cofounder, http://www.jguru.com



More information about the antlr-interest mailing list