ANTLR Hands on
Environments & Installation
Install JDK
Reference:
In Ubuntu:
|
|
In CentOS:
|
|
In Mac OS:
Download jdk-8uversion-macosx-amd64.dmg
and double click on it.
Check Java Version
|
|
Download ANTLR
|
|
Add this to the CLASSPATH
:
|
|
Verify:
|
|
Tutorials
Refer:
links
- $A Lexer$: This reads an input character or byte stream (i.e. characters, binary data, etc.), divides it into tokens using patterns you specify, and generates a token stream as output. It can also flag some tokens such as whitespace and comments as hidden using a protocol that ANTLR parsers automatically understand and respect.
- $A Parser$: This reads a token stream (normally generated by a lexer), and matches phrases in your language via the rules (patterns) you specify, and typically performs some semantic action for each phrase (or sub-phrase) matched. Each match could invoke a custom action, write some text via StringTemplate, or generate an Abstract Syntax Tree for additional processing.
Create SimpleCalc.g
:
|
|
Run this file:
|
|
Then, we can find three generated files:
SimpleCalc.tokens
, SimpleCalcParser.java
and SimpleCalcLexer.java