1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73 | program : functions { treeRoot = mProgram($1); }
;
functions : functions function { $$ = connectFunctions($1, $2); }
| function { $$ = $1 }
;
function : BASIC_TYPE ID '(' formals ')' '{' decls stmnts '}' { $$ = mFunction(connectVariables($4, $7), $8, $2.strVal, $1.type, yylineno); }
;
formals : formals_non_emtpty { $$ = $1 }
| {$$ = NULL;}
;
formals_non_emtpty : formals_non_emtpty ',' formal { $$ = connectVariables($1, $3); }
| formal { $$ = $1 }
;
formal : BASIC_TYPE ID { $$ = mVariable(kFormal, $2.strVal, $1.type, yylineno); }
;
decls : decls decl { $$ = connectVariables($1, $2); }
| { $$ = NULL; }
;
decl : BASIC_TYPE idents ';' { $$ = addType($2, $1.type); }
;
idents : idents ',' ident { $$ = connectVariables($1, $3); }
| ident { $$ = $1 }
;
ident : ID { $$ = mVariable(kLocal, $1.strVal, 0, yylineno); }
;
stmnts : stmnts stmnt { $$ = connectStmnts($1, $2); }
| stmnt { $$ = $1 }
;
stmnt : ID '=' expr ';' { $$ = mAssign($1.strVal, $3, yylineno); }
| IF '(' expr ')' stmnt ELSE stmnt { $$ = mIf($3, $5, $7, yylineno); }
| IF '(' expr ')' stmnt { $$ = mIf($3, $5, NULL, yylineno); }
| WHILE '(' expr ')' stmnt { $$ = mWhile($3, $5, yylineno); }
| RETURN expr ';' { $$ = mReturn($2, yylineno); }
| READ ID ';' { $$ = mRead($2.strVal, yylineno); }
| WRITE expr ';' { $$ = mWrite($2, yylineno); }
| '{' stmnts '}' { $$ = $2 }
| ID '(' actuals ')' ';' { $$ = mFuncCallStmnt($3, $1.strVal, yylineno); }
;
expr : MINUSOP expr %prec UNOP { $$ = mUnary($1.opType, $2, yylineno); }
| NOTOP expr { $$ = mUnary($1.opType, $2, yylineno); }
| expr PLUSOP expr { $$ = mBinary($1, $2.opType, $3, yylineno); }
| expr MINUSOP expr { $$ = mBinary($1, $2.opType, $3, yylineno); }
| expr MULOP expr { $$ = mBinary($1, $2.opType, $3, yylineno); }
| expr ANDOP expr { $$ = mBinary($1, $2.opType, $3, yylineno); }
| expr OROP expr { $$ = mBinary($1, $2.opType, $3, yylineno); }
| expr RELOP expr { $$ = mBinary($1, $2.opType, $3, yylineno); }
|'(' expr ')' { $$ = $2 }
| ID '(' actuals ')' { $$ = mFuncCallExpr($3, $1.strVal, yylineno); }
| ID { $$ = mRValue($1.strVal, yylineno); }
| INT_CONST { $$ = mIntConst($1.intVal, yylineno); }
| BOOL_CONST { $$ = mBoolConst($1.intVal, yylineno); }
| STRING_CONST { $$ = mStringConst($1.strVal, yylineno); }
;
actuals : exprs { $$ = $1 }
| {$$ = NULL;}
;
exprs : exprs ',' expr { connectActuals($1, mActual($3)) }
| expr { $$ = mActual($1); }
;
|