4. Grammar Syntax header {…} /** doc comment */ kind grammar name ; options {…} tokens {…} scopes… action rules … /** doc comment */ rule[String s, int z] returns [int x, int y] throws E options {…} scopes init {…} : | ; exceptions ^(root child1 … childN) Trees Note: No inheritance
5.
6. Example Grammar grammar SimpleParser; program : variable* method+ ; variable: "int" ID (‘=‘ expr)? ';’ ; method : "method" ID '(' ')' '{' variable* statement+ '}' ; statement : ID ‘=‘ expr ';' | "return" expr ';' ; expr : ID | INT ; ID : ('a'..'z'|'A'..'Z')+ ; INT : '0'..'9'+ ; WS : (' '|''|'')+ {channel=99;} ;
7. Using the parser CharStream in = new ANTLRFileStream(“inputfile”); SimpleParserLexer lexer = new SimpleParserLexer(in); CommonTokenStream tokens = new CommonTokenStream(lexer); SimpleParser p = new SimpleParser(tokens); p.program(); // invoke start rule
8.
9. Recursion Warnings a : a A | B ; t.g:2:5: Alternative 1 discovers infinite left-recursion to a from a t.g:2:5: Alternative 1: after matching input such as B decision cannot predict what comes next due to recursion overflow to c from b // with -Im 0 (secret internal parameter) a : b | B ; b : c ; c : B b ;
10.
11.
12.
13. Example Error Recovery int i = 0; method foo( { int j = i; i = 4 } [program, method]: line 2:12 mismatched token: [@14,23:23='{',<14>,2:12]; expecting type ')' [program, method, statement]: line 5:0 mismatched token: [@31,46:46='}',<15>,5:0]; expecting type ';' int i = 0; method foo() ) { int j = i; i = = 4; } [program, method]: line 2:13 mismatched token: [@15,24:24=')',<13>,2:13]; expecting type '{' [program, method, statement, expr]: line 4:6 mismatched token: [@32,47:47='=',<6>,4:6]; expecting set null Note: I put in two errors each so you’ll see it continues properly One token insertion One token deletion
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29. Sample code gen templates /** Dump the elements one per line and stick in debugging * location() trigger in front. */ element() ::= << <if(debug)> dbg.location(<it.line>,<it.pos>);<> <endif> <it.el><> >> /** match a token optionally with a label in front */ tokenRef(token,label,elementIndex) ::= << <if(label)> <label>=input.LT(1);<> <endif> match(input,<token>,FOLLOW_<token>_in_<ruleName><elementIndex>); >>