Nothing Special   »   [go: up one dir, main page]

ANTLR

What is ANTLR?

ANTLR (ANother Tool for Language Recognition) is a powerful parser generator for reading, processing, executing, or translating structured text or binary files. It's widely used to build languages, tools, and frameworks. From a grammar, ANTLR generates a parser that can build and walk parse trees.

terence Terence Parr is a tech lead at Google and until 2022 was a professor of data science / computer science at Univ. of San Francisco. He is the maniac behind ANTLR and has been working on language tools since 1989.

Check out Terence impersonating a machine learning droid: explained.ai
Quick Start
To try ANTLR immediately, jump to the new ANTLR Lab!

To install locally, use antlr4-tools, which installs Java and ANTLR if needed and creates antlr4 and antlr4-parse executables:

$ pip install antlr4-tools

(Windows must add ..\LocalCache\local-packages\Python310\Scripts to the PATH). See the Getting Started doc. Paste the following grammar into file Expr.g4 and, from that directory, run the antlr4-parse command. Hit control-D on Unix (or control-Z on Windows) to indicate end-of-input. A window showing the parse tree will appear.

grammar Expr;
prog:   (expr NEWLINE)* ;
expr:   expr ('*'|'/') expr
    |   expr ('+'|'-') expr
    |   INT
    |   '(' expr ')'
    ;
NEWLINE : [\r\n]+ ;
INT     : [0-9]+ ;
$ antlr4-parse Expr.g4 prog -gui
10+20*30
^D
$ antlr4 Expr.g4  # gen code
$ ls ExprParser.java
ExprParser.java
sample3

Testimonials

Kudos. I'm actually really liking ANTLR! I have a pretty darn good velocity with a rapid prototyping project I am doing in my Google 20% time. For example, I just discovered a feature in rewrite rules that does exactly what I need (referencing previous rule ASTs, p. 174 in your book). It took me about 5 minutes to get this to work and remove an ugly wart from my grammar. Hats off! Guido van Rossum, Inventor of Python
ANTLR is an exceptionally powerful and flexible tool for parsing formal languages. At Twitter, we use it exclusively for query parsing in Twitter search. Our grammars are clean and concise, and the generated code is efficient and stable. The book is our go-to reference for ANTLR v4 -- engaging writing, clear descriptions and practical examples all in one place. Samuel Luckenbill, Senior Manager of Search Infrastructure, Twitter, inc.
Just wanted to take the opportunity to say thanks. ANTLR is a BIG improvement over yacc/lex, and your support for it most commendable. Managed to get my tired old brain around it in a day. Nice work! Brad Cox, Inventor of Objective-C
More...