1tree grammar SimpleCTP; 2options { 3 tokenVocab = SimpleC; 4 language = ObjC; 5 ASTLabelType = ANTLRCommonTree; 6} 7 8scope Symbols 9{ 10ANTLRCommonTree *tree; 11} 12 13program 14 : declaration+ 15 ; 16 17declaration 18 : variable 19 | ^(FUNC_DECL functionHeader) 20 | ^(FUNC_DEF functionHeader block) 21 ; 22 23variable 24 : ^(VAR_DEF type declarator) 25 ; 26 27declarator 28 : K_ID 29 ; 30 31functionHeader 32 : ^(FUNC_HDR type K_ID formalParameter+) 33 ; 34 35formalParameter 36 : ^(ARG_DEF type declarator) 37 ; 38 39type 40 : K_INT_TYPE 41 | K_CHAR 42 | K_VOID 43 | K_ID 44 ; 45 46block 47 : ^(BLOCK variable* stat*) 48 ; 49 50stat: forStat 51 | expr 52 | block 53 ; 54 55forStat 56 : ^(K_FOR expr expr expr block) 57 ; 58 59expr: ^(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 66atom 67 : K_ID 68 | K_INT 69 ; 70