1 grammar SimpleC; 2 options { 3 output=AST; 4 language=ObjC; 5 } 6 7 tokens { 8 VAR_DEF; 9 ARG_DEF; 10 FUNC_HDR; 11 FUNC_DECL; 12 FUNC_DEF; 13 BLOCK; 14 } 15 16 program 17 : declaration+ 18 ; 19 20 declaration 21 : variable 22 | functionHeader K_SEMICOLON -> ^(FUNC_DECL functionHeader) 23 | functionHeader block -> ^(FUNC_DEF functionHeader block) 24 ; 25 26 variable 27 : type declarator K_SEMICOLON -> ^(VAR_DEF type declarator) 28 ; 29 30 declarator 31 : K_ID 32 ; 33 34 functionHeader 35 : type K_ID K_LCURVE ( formalParameter ( K_COMMA formalParameter )* )? K_RCURVE 36 -> ^(FUNC_HDR type K_ID formalParameter+) 37 ; 38 39 formalParameter 40 : type declarator -> ^(ARG_DEF type declarator) 41 ; 42 43 type 44 : K_INT_TYPE 45 | K_CHAR 46 | K_VOID 47 | K_ID 48 ; 49 50 block 51 : lc=K_LCURLY 52 variable* 53 stat* 54 K_RCURLY 55 -> ^(BLOCK[$lc,@"BLOCK"] variable* stat*) 56 ; 57 58 stat: forStat 59 | expr K_SEMICOLON! 60 | block 61 | assignStat K_SEMICOLON! 62 | K_SEMICOLON! 63 ; 64 65 forStat 66 : K_FOR K_LCURVE start=assignStat K_SEMICOLON expr K_SEMICOLON next=assignStat K_RCURVE block 67 -> ^(K_FOR $start expr $next block) 68 ; 69 70 assignStat 71 : K_ID K_EQ expr -> ^(K_EQ K_ID expr) 72 ; 73 74 expr: condExpr 75 ; 76 77 condExpr 78 : aexpr ( (K_EQEQ^ | K_LT^) aexpr )? 79 ; 80 81 aexpr 82 : atom ( K_PLUS^ atom )* 83 ; 84 85 atom 86 : K_ID 87 | K_INT 88 | K_LCURVE expr K_RCURVE -> expr 89 ; 90 91 K_FOR : 'for' ; 92 K_CHAR: 'char'; 93 K_INT_TYPE : 'int' ; 94 K_VOID: 'void'; 95 96 K_ID : ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'0'..'9'|'_')* 97 ; 98 99 K_INT : anInt+=('0'..'9')+ {NSLog(@"\%@", $anInt);} 100 ; 101 102 K_LCURVE : '('; 103 K_RCURVE : ')'; 104 K_PLUS : '+' ; 105 K_COMMA : ','; 106 K_SEMICOLON : ';'; 107 K_LT : '<' ; 108 K_EQ : '=' ; 109 K_EQEQ : '==' ; 110 K_LCURLY : '{'; 111 K_RCURLY : '}'; 112 113 WS : ( ' ' 114 | '\t' 115 | '\r' 116 | '\n' 117 )+ 118 { $channel=HIDDEN; } 119 ; 120