1 tree grammar SimpleCTP; 2 options { 3 tokenVocab = SimpleC; 4 language = ObjC; 5 ASTLabelType = ANTLRCommonTree; 6 } 7 8 scope Symbols 9 { 10 ANTLRCommonTree *tree; 11 } 12 13 program 14 : declaration+ 15 ; 16 17 declaration 18 : variable 19 | ^(FUNC_DECL functionHeader) 20 | ^(FUNC_DEF functionHeader block) 21 ; 22 23 variable 24 : ^(VAR_DEF type declarator) 25 ; 26 27 declarator 28 : K_ID 29 ; 30 31 functionHeader 32 : ^(FUNC_HDR type K_ID formalParameter+) 33 ; 34 35 formalParameter 36 : ^(ARG_DEF type declarator) 37 ; 38 39 type 40 : K_INT_TYPE 41 | K_CHAR 42 | K_VOID 43 | K_ID 44 ; 45 46 block 47 : ^(BLOCK variable* stat*) 48 ; 49 50 stat: forStat 51 | expr 52 | block 53 ; 54 55 forStat 56 : ^(K_FOR expr expr expr block) 57 ; 58 59 expr: ^(K_EQEQ expr expr) 60 | ^(K_LT expr expr) 61 | ^(K_PLUS expr expr) 62 | ^(K_EQ K_ID e=expr) { NSLog(@"assigning \%@ to variable \%@", $e.text, $K_ID.text); } 63 | atom 64 ; 65 66 atom 67 : K_ID 68 | K_INT 69 ; 70