9
9
import java.io.Reader ;
10
10
import java.io.IOException ;
11
11
import java.io.EOFException ;
12
+ import syntax_tree.* ;
12
13
}
13
14
14
15
15
16
%code {
17
+
18
+ Program root;
19
+
16
20
public static void main (String args[]) throws IOException {
17
21
ParserLexer lexer = new ParserLexer (System.in );
18
22
parser p = new parser (lexer);
19
- if (p.parse ())
23
+ if (p.parse ()) {
20
24
System.out .println (" ---------------------\n Parser Completed Successfully!" );
25
+ System.out .println (p.root .toString ());
26
+ }
21
27
return ;
22
28
}
23
29
}
24
30
25
31
26
32
27
33
// All keywords
28
- %token KW_IF KW_IS KW_VAR KW_END KW_TRUE KW_FALSE KW_THEN
34
+ %token KW_IF KW_IS KW_VAR KW_END KW_TRUE KW_FALSE KW_THEN
29
35
%token KW_ELSE KW_FOR KW_LOOP KW_IN KW_WHILE KW_FUNCT KW_RETURN KW_PRINT IDENTIFIER
30
36
31
37
// Boolean
32
- %token BOOLEAN_LITERAL
38
+ %token BOOLEAN_LITERAL
33
39
34
40
// Separators
35
41
%token LPAREN RPAREN LBRACE RBRACE LBRACK RBRACK SEMICOLON COMMA DOT
47
53
48
54
/* Declare types for the grammar's non-terminals. */
49
55
50
- %left KW_RETURN KW_PRINT
56
+ %left IDENTIFIER
57
+
58
+ %left KW_RETURN KW_PRINT KW_IN
51
59
52
60
%left COMMA
53
61
65
73
%left ASSIGN
66
74
67
75
%left LPAREN LBRACE LBRACK
68
- %start Prog
76
+ %start Program
69
77
70
78
// Grammar Definition ___________
71
- %%
72
- Prog :
73
- %empty {System.out.println(" EOF" );}
74
- | Body Prog
79
+ %%
80
+ Program : Prog { root = new Program($1 ); } ;
81
+
82
+ Prog :
83
+ %empty { $$ = new Program(); }
84
+ | Body Prog { $$ = new Program($2 , $1 ); }
75
85
76
86
;
77
87
78
- Body :
79
- Declaration SEMICOLON
88
+ IDENTIFIER_T : IDENTIFIER { $$ = new Identifier($1 ); } ;
89
+
90
+ Body :
91
+ Declaration SEMICOLON
80
92
| Assignment SEMICOLON
81
93
| Expression SEMICOLON
82
94
| PrintStatement SEMICOLON
83
95
| Statement
84
96
| ReturnStatement
85
97
;
86
98
87
- Declaration : KW_VAR IDENTIFIER
88
- | KW_VAR IDENTIFIER ASSIGN Expression
89
- | KW_VAR IDENTIFIER ASSIGN FunctionDef
99
+ Declaration : KW_VAR IDENTIFIER_T { $$ = new Declaration( $2 ); }
100
+ | KW_VAR IDENTIFIER_T ASSIGN Expression { $$ = new Declaration( $2 , $4 ); }
101
+ | KW_VAR IDENTIFIER_T ASSIGN FunctionDef { $$ = new Declaration( $2 , $4 , 0 ); }
90
102
;
91
103
92
- ReturnStatement : KW_RETURN Expression ;
104
+ ReturnStatement : KW_RETURN Expression { $$ = new ReturnStatement( $2 ); } ;
93
105
94
- PrintStatement : KW_PRINT Expression ;
106
+ PrintStatement : KW_PRINT Expression { $$ = new PrintStatement( $2 ); } ;
95
107
96
- Assignment : IDENTIFIER ASSIGN Expression ;
108
+ Assignment : IDENTIFIER_T ASSIGN Expression { $$ = new Assignment( $1 , $3 ); } ;
97
109
98
- Expression : IDENTIFIER
110
+ Expression : IDENTIFIER_T
99
111
| LPAREN Expression RPAREN
100
112
| Relation
101
113
| Value
@@ -104,72 +116,69 @@ Expression : IDENTIFIER
104
116
| ArrayAccess
105
117
;
106
118
107
- ArrayAccess : Expression LBRACK Expression RBRACK ;
108
-
109
- Relation : Expression LT Expression
110
- | Expression LTEQ Expression
111
- | Expression GT Expression
112
- | Expression GTEQ Expression
113
- | Expression EQ Expression
114
- | Expression TOKEN_AND Expression
115
- | Expression TOKEN_OR Expression
116
- | Expression TOKEN_XOR Expression
117
- | TOKEN_NOT Expression
119
+ ArrayAccess : Expression LBRACK Expression RBRACK { $$ = new ArrayAccess( $1 , $3 ); } ;
120
+
121
+ Relation : Expression LT Expression { $$ = new BinaryRelation( $1 , $3 , RelationOp.LT); }
122
+ | Expression LTEQ Expression { $$ = new BinaryRelation( $1 , $3 , RelationOp.LTEQ); }
123
+ | Expression GT Expression { $$ = new BinaryRelation( $1 , $3 , RelationOp.GT); }
124
+ | Expression GTEQ Expression { $$ = new BinaryRelation( $1 , $3 , RelationOp.GTEQ); }
125
+ | Expression EQ Expression { $$ = new BinaryRelation( $1 , $3 , RelationOp.EQ); }
126
+ | Expression TOKEN_AND Expression { $$ = new BinaryRelation( $1 , $3 , RelationOp.AND); }
127
+ | Expression TOKEN_OR Expression { $$ = new BinaryRelation( $1 , $3 , RelationOp.OR); }
128
+ | Expression TOKEN_XOR Expression { $$ = new BinaryRelation( $1 , $3 , RelationOp.XOR); }
129
+ | TOKEN_NOT Expression { $$ = new UnaryRelation( $2 , RelationOp.NOT); }
118
130
;
119
131
120
- Calc : Expression PLUS Expression
121
- | Expression MINUS Expression
122
- | Expression MULT Expression
123
- | Expression DIV Expression
132
+ Calc : Expression PLUS Expression { $$ = new CalcExpression( $1 , $3 , CalcOp.PLUS); }
133
+ | Expression MINUS Expression { $$ = new CalcExpression( $1 , $3 , CalcOp.MINUS); }
134
+ | Expression MULT Expression { $$ = new CalcExpression( $1 , $3 , CalcOp.MULT); }
135
+ | Expression DIV Expression { $$ = new CalcExpression( $1 , $3 , CalcOp.DIV); }
124
136
;
125
137
126
- Value : STRING
127
- | INTEGER_LITERAL
128
- | REAL_LITERAL
129
- | KW_TRUE
130
- | KW_FALSE
138
+ Value : STRING { $$ = new StringValue($1 ); }
139
+ | INTEGER_LITERAL { $$ = new IntegerValue($1 ); }
140
+ | REAL_LITERAL { $$ = new RealValue($1 ); }
141
+ | BOOLEAN_LITERAL { $$ = new BooleanValue($1 ); }
131
142
| ArrayValue
132
143
| DictValue
133
144
;
134
145
135
- ArrayValue : LBRACK ArrayValues RBRACK ;
146
+ ArrayValue : LBRACK ArrayValues RBRACK { $$ = new ArrayValue( $2 ); } ;
136
147
137
- DictValue : LBRACE DictValues RBRACE ;
148
+ DictValue : LBRACE DictValues RBRACE { $$ = new DictValue( $2 ); } ;
138
149
139
- DictValues : %empty
140
- | Assignment COMMA DictValues
141
- ;
150
+ DictValues : %empty { $$ = new DictValues(); }
151
+ | Assignment COMMA DictValues { $$ = new DictValues( $3 , $1 ); }
152
+ ;
142
153
143
- ArrayValues : %empty
144
- | Expression COMMA ArrayValues
154
+ ArrayValues : %empty { $$ = new ArrayValues(); }
155
+ | Expression COMMA ArrayValues { $$ = new ArrayValues( $3 , $1 ); }
145
156
;
146
-
147
- FunctionDef : KW_FUNCT LPAREN Params RPAREN KW_IS Body KW_END ;
148
157
149
- FunctionCall : IDENTIFIER LPAREN Args RPAREN ;
158
+ FunctionDef : KW_FUNCT LPAREN Params RPAREN KW_IS Prog KW_END { $$ = new FunctionDef($3 , $6 ); } ;
159
+
160
+ FunctionCall : IDENTIFIER_T LPAREN Args RPAREN { $$ = new FunctionCall($1 , $3 ); } ;
150
161
151
- Params : %empty
152
- | IDENTIFIER COMMA Params
153
- } IDENTIFIER
162
+ Params : %empty { $$ = new Params(); }
163
+ | IDENTIFIER_T COMMA Params { $$ = new Params($3 , $1 ); }
154
164
;
155
165
156
- Args : %empty
157
- | Expression COMMA Args
158
- | Expression
166
+ Args : %empty { $$ = new Args(); }
167
+ | Expression COMMA Args { $$ = new Args($3 , $1 ); }
159
168
;
160
169
161
170
Statement : IfStatement
162
171
| ForStatement
163
172
| WhileStatement
164
173
;
165
174
166
- IfStatement : KW_IF LPAREN Expression RPAREN KW_THEN Body KW_END
167
- | KW_IF LPAREN Expression RPAREN KW_THEN Body KW_ELSE Body KW_END
175
+ IfStatement : KW_IF LPAREN Expression RPAREN KW_THEN Prog KW_END { $$ = new IfStatement( $3 , $6 ); }
176
+ | KW_IF LPAREN Expression RPAREN KW_THEN Prog KW_ELSE Prog KW_END { $$ = new IfStatement( $3 , $6 , $8 ); }
168
177
;
169
178
170
- ForStatement : KW_FOR LPAREN IDENTIFIER KW_IN Expression RPAREN KW_LOOP Body KW_END ;
179
+ ForStatement : KW_FOR LPAREN IDENTIFIER_T KW_IN Expression RPAREN KW_LOOP Prog KW_END { $$ = new ForStatement( $3 , $5 , $8 ); } ;
171
180
172
- WhileStatement : KW_WHILE LPAREN Expression RPAREN KW_LOOP Body KW_END ;
181
+ WhileStatement : KW_WHILE LPAREN Expression RPAREN KW_LOOP Prog KW_END { $$ = new WhileStatement( $3 , $6 ); } ;
173
182
174
183
%%
175
184
@@ -190,7 +199,7 @@ class ParserLexer implements parser.Lexer {
190
199
191
200
@Override
192
201
public Object getLVal () {
193
- return null ;
202
+ return yylex. yytext () ;
194
203
}
195
204
196
205
@Override
0 commit comments