ANTLR Andrew Pangborn & Zach Busser. ANTLR in a Nutshell ANother Tool for Language Recognition...

Preview:

Citation preview

ANTLRAndrew Pangborn & Zach Busser

ANTLR in a Nutshell• ANother Tool for Language Recognition

• generates lexers

• generates parsers (and parse trees)

• Java-based, but has runtime implementations in other languages like python, javascript, c, c#, obj-c

• An LL(*) parser

• EBNF grammar notation

ANTLR Features

• Hierarchical lexical rules

• Abstract Syntax Tree generation

• Inline code execution

• Tree grammars

ANTLR Pros

•Target languages besides Java – C, python, Javascript, etc.

•LL(*) - not limited to 1 lookahead token

•Simpler to debug than LALR grammars (no cryptic shift-reduce / reduce-reduce errors)

•ANTLRWorks

ANTLR Shortcomings

• An LL parser – cannot support left-recursive grammars like YACC or PJ2

• Recursive descent approach not great for efficiency

• No mechanism for specifying operator precedence

Getting started with ANTLR

• http://www.antlr.org/wiki/display/ANTLR3/FAQ+-+Getting+Started

Demo Time!

• A simple 4-function expression parser

• 4-function expression evaluator

• AST generation example

References

• http://www.corriganjc.net/weblog/archives/2007/06/the_antlr_trini.html

• http://www.antlr.org/wiki/display/ANTLR3/ANTLR+v3+documentation

Recommended