1// @@ANTLR Tool Options@@: -trace 2tree grammar t047treeparserWalker; 3options { 4 language=JavaScript; 5 tokenVocab=t047treeparser; 6 ASTLabelType=CommonTree; 7} 8 9program 10 : declaration+ 11 ; 12 13declaration 14 : variable 15 | ^(FUNC_DECL functionHeader) 16 | ^(FUNC_DEF functionHeader block) 17 ; 18 19variable returns [res] 20 : ^(VAR_DEF type declarator) 21 { 22 $res = $declarator.text; 23 } 24 ; 25 26declarator 27 : ID 28 ; 29 30functionHeader 31 : ^(FUNC_HDR type ID formalParameter+) 32 ; 33 34formalParameter 35 : ^(ARG_DEF type declarator) 36 ; 37 38type 39 : 'int' 40 | 'char' 41 | 'void' 42 | ID 43 ; 44 45block 46 : ^(BLOCK variable* stat*) 47 ; 48 49stat: forStat 50 | expr 51 | block 52 ; 53 54forStat 55 : ^('for' expr expr expr block) 56 ; 57 58expr: ^(EQEQ expr expr) 59 | ^(LT expr expr) 60 | ^(PLUS expr expr) 61 | ^(EQ ID expr) 62 | atom 63 ; 64 65atom 66 : ID 67 | INT 68 ; 69