-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimple.y
More file actions
223 lines (221 loc) · 7.16 KB
/
Simple.y
File metadata and controls
223 lines (221 loc) · 7.16 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
%{/*************************************************************************
Compiler for the Simple language
***************************************************************************/
/*=========================================================================
C Libraries, Symbol Table, Code Generator & other C code
=========================================================================*/
#include <stdio.h> /* For I/O */
#include <stdlib.h> /* For malloc here and in symbol table */
#include <string.h> /* For strcmp in symbol table */
#include "ST.h" /* Symbol Table */
#define YYDEBUG 1 /* For Debugging */
int errors; /* Error Count */
Env* current_Env = init_new_Env();
/*=========================================================================
SEMANTIC RECORDS
=========================================================================*/
%}
%union semrec /* The Semantic Records*/
{
char* id; /* Identifiers */
literal* literal_value;
}
/*=========================================================================
TOKENS
=========================================================================*/
%start program
%token <id> IDENTIFIER /* Simple identifier */
%token <lbls> IF WHILE SWITCH FOR DO /* For backpatching labels*/
%token <literal> LITERAL //直接量
%token SKIP THEN ELSE FI END
VAR //定义变量 var name={}|[]|
VAL //定义常量
FUNCTION //定义函数
NAN //
INFINITY //
_NULL //
_TRUE //
_FALSE //
RETURN YIELD CONTINUE BREAK GOTO CASE DEFAULT //控制语句
CATCH TRY FINALLY THROW //异常控制流
PACKAGE IMPORT //包管理系统
SUPER //基类构造函数
THIS //对象本身引用
NEW //
INTERFACE IMPLEMENTS //接口
PUBLIC PRIVATE PROTECTED //访问控制
CONSOLE //控制台IO
%token INTEGER READ WRITE LET IN
%token ASSGNOP
/*=========================================================================
OPERATOR PRECEDENCE
=========================================================================*/
%left '.' '[' ']' "new" '(' ')'
%left "++" "--"
%right '!' '~'
%nonassoc "typeof" "delete"
%right "**"
%left '*' '/' '%'
%left '+' '-'
%left ">>" "<<" ">>>"
%nonassoc '>' '<' "<=" ">="
"in" /*"instanceof"*/
%nonassoc "==" "===" "!=" "!=="
%left '&' '|' '^' "||" "&&"
%right '?' ':'
%nonassoc '=' "+=" "-=" "*=" "/=" "%="
"<<=" ">>=" ">>>="
"&=" "^=" "|="
/*=========================================================================
GRAMMAR RULES for the Simple language
=========================================================================*/
%%
program : stmts
;
declaration : /* empty */
| VAR ID__or_assign ';' { install( $2 ); }
| VAL ID__with_assign ';' { }
| function_declaration
;
ID__or_assign : /* empty */
| IDENTIFIER '=' right_value { }
| IDENTIFIER { }
| ID_or_assign ',' ID_or_assign { }
;
ID__with_assign : IDENTIFIER '=' right_value { }
| ID_or_assign ',' ID_or_assign { }
;
right_value : IDENTIFIER { }
| '[' array ']' { }
| '{' object '}' { }
| FUNCTION '(' function_params ')' '{' function_body '}'
| LITERAL { }
;
array : /* empty */
| array_not_empty
;
array_not_empty : right_value { }
| array ',' right_value { }
;
object : /* empty */
| object_not_empty
;
object_not_empty : IDENTIFIER ':' right_value { }
| object ',' IDENTIFIER ':' right_value { }
function_params : /* empty */
| function_params_not_empty
;
function_params_not_empty : IDENTIFIER
function_params_not_empty ',' IDENTIFIER
; |
function_declaration : FUNCTION IDENTIFIER '(' function_params ')''{' function_body '}'
function_body : /* empty */
| stmts
| stmts return_stmt ';'
;
return_stmt : "return;"
|"return" exp ';'
;
not_if_stmt : /* empty */
| declaration
| while_stmt
| do_stmt
| for_stmt
| switch_stmt
| out_stmt
| assign_stmt
| function_call_stmt
;
out_stmt : CONSOLE "<<" exp { } ;
in_stmt : CONSOLE ">>" left_value { }
assign_stmt : /* empty */
| left_value '=' exp { }
| in_stmt
| left_value "+=" exp
| left_value "-=" exp
| left_value "*=" exp
| left_value "/=" exp
| left_value "%=" exp
| left_value ">>=" exp
| left_value "<<=" exp
| left_value ">>>=" exp
| left_value "&=" exp
| left_value "|=" exp
| left_value "^=" exp
| "++" left_value
| "--" left_value
| left_value "++"
| left_value "--"
;
left_value : IDENTIFIER
| left_value '[' exp ']'
| left_value '.' IDENTIFIER
;
stmts : /* empty */
| stmts stmt { }
;
stmt :
| '{' stmts '}'
| if_matched_stmt
| if_open_stmt ;
if_open_stmt : IF '(' bool_exp ')' stmt
| IF '(' bool_exp ')' if_matched_stmt
ELSE if_open_stmt ;
if_matched_stmt : IF '(' bool_exp ')' if_matched_stmt ELSE if_matched_stmt
| not_if_stmt ;
bool_exp: '!' bool_exp
| '(' boo_exp_or ')'
| _TRUE
| _FALSE
| bool_stmt
;
boo_exp_or : boo_exp_or "||" bool_exp_and
| bool_exp_and ;
bool_exp_and : bool_exp_and "&&"
| bool_exp ;
bool_stmt : left_value "<" left_value
| left_value "<=" left_value
| left_value "==" left_value
| left_value "!=" left_value
| left_value "===" left_value
| left_value "!==" left_value
| left_value ">" left_value
| left_value ">=" left_value
| IDENTIFIER "in" left_value
exp : LITERAL { }
| IDENTIFIER { }
| exp '+' exp { }
| exp '-' exp { }
| exp '*' exp { }
| exp '/' exp { }
| exp '^' exp { }
| '(' exp ')' { $$ = $2; }
| '-' exp %prec { $$= -$2; }
| typeof_exp
;
typeof_exp : TYPEOF left_value
%%
/*=========================================================================
MAIN
=========================================================================*/
main( int argc, char *argv[] )
{ extern FILE *yyin;
++argv; --argc;
yyin = fopen( argv[0], "r" );
/*yydebug = 1;*/
errors = 0;
yyparse ();
printf ( "Parse Completed\n" );
if ( errors == 0 )
{
}
}
/*=========================================================================
YYERROR
=========================================================================*/
yyerror ( char *s ) /* Called by yyparse on error */
{
errors++;
printf ("%s\n", s);
}
/**************************** End Grammar File ***************************/