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