Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit e5b1077

Browse files
author
Alexey
committedMar 13, 2022
Added AST parsing
1 parent 1975480 commit e5b1077

File tree

2 files changed

+70
-60
lines changed

2 files changed

+70
-60
lines changed
 

‎lexer.l

+2-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ UNKNOWN_TOKEN = .
6060
"return" { System.out.println("[token at line " + yyline + " = \"" + yytext() + "\" : KW_RETURN]"); return ParserLexer.KW_RETURN; }
6161
"print" { System.out.println("[token at line " + yyline + " = \"" + yytext() + "\" : KW_PRINT]"); return ParserLexer.KW_PRINT; }
6262
"funct" { System.out.println("[token at line " + yyline + " = \"" + yytext() + "\" : KW_FUNCT]"); return ParserLexer.KW_FUNCT; }
63-
63+
"in" { System.out.println("[token at line " + yyline + " = \"" + yytext() + "\" : KW_IN]"); return ParserLexer.KW_IN; }
64+
6465

6566

6667
/* boolean literals */

‎parser.y

+68-59
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,33 @@
99
import java.io.Reader;
1010
import java.io.IOException;
1111
import java.io.EOFException;
12+
import syntax_tree.*;
1213
}
1314

1415

1516
%code {
17+
18+
Program root;
19+
1620
public static void main(String args[]) throws IOException {
1721
ParserLexer lexer = new ParserLexer(System.in);
1822
parser p = new parser(lexer);
19-
if(p.parse())
23+
if(p.parse()) {
2024
System.out.println("---------------------\n Parser Completed Successfully!");
25+
System.out.println(p.root.toString());
26+
}
2127
return;
2228
}
2329
}
2430

2531

2632

2733
//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
2935
%token KW_ELSE KW_FOR KW_LOOP KW_IN KW_WHILE KW_FUNCT KW_RETURN KW_PRINT IDENTIFIER
3036

3137
//Boolean
32-
%token BOOLEAN_LITERAL
38+
%token BOOLEAN_LITERAL
3339

3440
// Separators
3541
%token LPAREN RPAREN LBRACE RBRACE LBRACK RBRACK SEMICOLON COMMA DOT
@@ -47,7 +53,9 @@
4753

4854
/* Declare types for the grammar's non-terminals. */
4955

50-
%left KW_RETURN KW_PRINT
56+
%left IDENTIFIER
57+
58+
%left KW_RETURN KW_PRINT KW_IN
5159

5260
%left COMMA
5361

@@ -65,37 +73,41 @@
6573
%left ASSIGN
6674

6775
%left LPAREN LBRACE LBRACK
68-
%start Prog
76+
%start Program
6977

7078
//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); }
7585

7686
;
7787

78-
Body :
79-
Declaration SEMICOLON
88+
IDENTIFIER_T : IDENTIFIER { $$ = new Identifier($1); } ;
89+
90+
Body :
91+
Declaration SEMICOLON
8092
| Assignment SEMICOLON
8193
| Expression SEMICOLON
8294
| PrintStatement SEMICOLON
8395
| Statement
8496
| ReturnStatement
8597
;
8698

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); }
90102
;
91103

92-
ReturnStatement : KW_RETURN Expression ;
104+
ReturnStatement : KW_RETURN Expression { $$ = new ReturnStatement($2); } ;
93105

94-
PrintStatement : KW_PRINT Expression ;
106+
PrintStatement : KW_PRINT Expression { $$ = new PrintStatement($2); } ;
95107

96-
Assignment : IDENTIFIER ASSIGN Expression ;
108+
Assignment : IDENTIFIER_T ASSIGN Expression { $$ = new Assignment($1, $3); } ;
97109

98-
Expression : IDENTIFIER
110+
Expression : IDENTIFIER_T
99111
| LPAREN Expression RPAREN
100112
| Relation
101113
| Value
@@ -104,72 +116,69 @@ Expression : IDENTIFIER
104116
| ArrayAccess
105117
;
106118

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); }
118130
;
119131

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); }
124136
;
125137

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); }
131142
| ArrayValue
132143
| DictValue
133144
;
134145

135-
ArrayValue : LBRACK ArrayValues RBRACK ;
146+
ArrayValue : LBRACK ArrayValues RBRACK { $$ = new ArrayValue($2); } ;
136147

137-
DictValue : LBRACE DictValues RBRACE ;
148+
DictValue : LBRACE DictValues RBRACE { $$ = new DictValue($2); } ;
138149

139-
DictValues : %empty
140-
| Assignment COMMA DictValues
141-
;
150+
DictValues : %empty { $$ = new DictValues(); }
151+
| Assignment COMMA DictValues { $$ = new DictValues($3, $1); }
152+
;
142153

143-
ArrayValues : %empty
144-
| Expression COMMA ArrayValues
154+
ArrayValues : %empty { $$ = new ArrayValues(); }
155+
| Expression COMMA ArrayValues { $$ = new ArrayValues($3, $1); }
145156
;
146-
147-
FunctionDef : KW_FUNCT LPAREN Params RPAREN KW_IS Body KW_END ;
148157

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); } ;
150161

151-
Params: %empty
152-
| IDENTIFIER COMMA Params
153-
} IDENTIFIER
162+
Params: %empty { $$ = new Params(); }
163+
| IDENTIFIER_T COMMA Params { $$ = new Params($3, $1); }
154164
;
155165

156-
Args: %empty
157-
| Expression COMMA Args
158-
| Expression
166+
Args: %empty { $$ = new Args(); }
167+
| Expression COMMA Args { $$ = new Args($3, $1); }
159168
;
160169

161170
Statement : IfStatement
162171
| ForStatement
163172
| WhileStatement
164173
;
165174

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); }
168177
;
169178

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); } ;
171180

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); } ;
173182

174183
%%
175184

@@ -190,7 +199,7 @@ class ParserLexer implements parser.Lexer {
190199

191200
@Override
192201
public Object getLVal() {
193-
return null;
202+
return yylex.yytext();
194203
}
195204

196205
@Override

0 commit comments

Comments
 (0)
Please sign in to comment.