-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCS315f22_team23.yacc
203 lines (156 loc) · 4.99 KB
/
CS315f22_team23.yacc
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
/* FanC++.y */
%{
#include <stdio.h>
%}
%token BEG END RETURN IF ELSE WHILE FOR DIGIT INT_ID
%token FLOAT_ID STRING_ID CHAR_ID BOOL_ID STRING
%token COMMENT TRUE FALSE INT FLOAT CHAR IDENT ASSIGN
%token PLUS MINUS EQ MUL DIV SC LB RB LP RP LSB RSB DOT COMMA
%token HASHTAG AND OR DOL QM COLON STR_IDENT LT GT LTE GTE NEQ
%token CHECK_CONNECTION CONNECT_TO_URL READ_TEMP_DATA
%token READ_HUMIDITY_DATA READ_PRESSURE_DATA READ_QUALITY_DATA
%token READ_LIGHT_DATA READ_SOUND_DATA READ_TIMER_DATA
%token SEND_DATA_TO_CONNECTION RECEIVE_DATA_FROM_CONNECTION
%token SWITCH_OFF SWITCH_ON IN OUT NL
%%
program: BEG stmt_list END {printf("program valid\n"); return 0;};
stmt_list: stmt
|stmt_list stmt
;
stmt: matched |unmatched SC |COMMENT | error
matched:IF LP logic_expr RP LB matched RB SC ELSE LB matched RB SC
|loop_stmt SC
|assign_stmt SC
|declaration_stmt SC
|return_stmt SC
|call_stmt SC
|ternary_stmt
;
unmatched: IF LP logic_expr RP LB stmt_list RB
| IF LP logic_expr RP LB matched RB SC ELSE LB unmatched SC RB
;
loop_stmt: while_stmt
| for_stmt
;
while_stmt: WHILE LP logic_expr RP LB stmt_list RB ;
for_stmt: FOR LP assign_stmt SC logic_expr SC
assign_stmt RP LB stmt_list RB
| FOR LP var_declaration SC logic_expr SC
assign_stmt RP LB stmt_list RB
;
return_stmt: RETURN expr
|RETURN literal
;
ternary_stmt: logic_expr QM stmt stmt;
logic_expr: INT comparison_op INT
|DIGIT comparison_op DIGIT
|FLOAT comparison_op FLOAT
|literal comparison_op literal
|identifier comparison_op val
|identifier comparison_op literal
;
comparison_op: LT|GT|EQ|LTE|GTE|NEQ;
literal: STRING|CHAR|TRUE|FALSE;
assign_stmt: identifier ASSIGN expr
|identifier ASSIGN literal
|identifier ASSIGN call_stmt
;
expr: expr OR and_term
|and_term
;
and_term: and_term AND add_sub_term
|add_sub_term
;
add_sub_term: add_sub_term op1 mul_div_term
|mul_div_term
;
mul_div_term: mul_div_term op2 factor
|factor
;
factor: LP expr RP
|val
;
val: identifier
|FLOAT
|INT
|DIGIT
;
call_stmt:primitive_function
|non_primitive_function
;
non_primitive_function: identifier LP function_input RP;
function_input: identifier COMMA function_input
|literal COMMA function_input
|INT COMMA function_input
|FLOAT COMMA function_input
|identifier
|INT
|FLOAT
|literal
;
primitive_function: input
|output
|check_connection
|read_temp_data
|read_humidity_data
|read_pressure_data
|read_quality_data
|read_light_data
|url_connection
|read_sound_data
|read_timer_data
|switch_on
|switch_off
|send_data_to_connection
|receive_data_from_connection
;
url_connection: CONNECT_TO_URL LP STRING RP;
check_connection: CHECK_CONNECTION LP STRING RP;
read_temp_data: READ_TEMP_DATA LP RP;
read_humidity_data: READ_HUMIDITY_DATA LP RP;
read_pressure_data: READ_PRESSURE_DATA LP RP;
read_quality_data: READ_QUALITY_DATA LP RP;
read_light_data: READ_LIGHT_DATA LP RP;
read_sound_data: READ_SOUND_DATA LP RP;
read_timer_data: READ_TIMER_DATA LP RP;
input: IN DOL;
output: OUT DOL output_body;
switch_on: SWITCH_ON LP switch_id_list RP;
switch_off: SWITCH_OFF LP switch_id_list RP;
send_data_to_connection: SEND_DATA_TO_CONNECTION LP STRING COMMA INT RP
|SEND_DATA_TO_CONNECTION LP STRING COMMA identifier RP;
|SEND_DATA_TO_CONNECTION LP STRING COMMA DIGIT RP;
receive_data_from_connection: RECEIVE_DATA_FROM_CONNECTION LP STRING RP;
output_body:expr|literal;
switch_id_list: DIGIT | DIGIT COMMA switch_id_list;
op1:PLUS|MINUS;
op2:MUL|DIV;
identifier: IDENT;
declaration_stmt:var_declaration
|function_decleration;
var_declaration:type_id ident_list
|type_id assign_stmt;
type_id:INT_ID|CHAR_ID|BOOL_ID|STRING_ID|FLOAT_ID;
ident_list:identifier
|ident_list COMMA identifier
;
function_decleration: function_header LB stmt_list RB;
function_header: identifier LP param_list RP;
param_list: /*empty*/ | type_id identifier
| type_id identifier COMMA param_list
;
%%
#include "lex.yy.c"
int lineno = 1;
int error = 0;
main() {
yyparse();
if (error == 0) {
printf("Input program is valid.\n");
}
return 0;
}
yyerror( char *s ) {
error = -1;
fprintf( stderr, "%s on line %d!\n", s, lineno);
}