-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdroneX.yacc
212 lines (175 loc) · 4.9 KB
/
droneX.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
204
205
206
207
208
209
210
211
212
%{ int yylex(void); extern int yylineno; %}
%token MAIN
%token LP
%token RP
%token LB
%token RB
%token GREATERTHAN
%token GREATEROREQUAL
%token LESSTHAN
%token LESSOREQUAL
%token EQUAL
%token NOTEQUAL
%token ASSIGNMENT_OP
%token OR
%token AND
%token WHILE
%token FOR
%token ELSEIF
%token IF
%token ELSE
%token COMMENT_LINE
%token COMMENT_MULTI_LINE
%token CONSTANT
%token DOT
%token FUNCTION
%token READALTITUDE
%token READTEMPERATURE
%token READACCELERATION
%token TURNONCAMERA
%token TURNOFFCAMERA
%token TAKEPICTURE
%token READTIMESTAMP
%token CONNECTTOBASE
%token IDENTIFIER
%token NUMBER
%token STRING
%token FLOAT
%token INTEGER
%token BOOLEAN
%token RETURN
%token PRINT
%token INPUT
%token FUNCTION
%token CONNECT
%token DISCONNECT
%token RECEIVE
%token COMMA
%token NOT
%token TO
%token ENDSTMT
%token FUNCTION_START
%token FUNCTION_RETURN
%token FUNCTION_CALL
%token PLUS
%token MINUS
%token MULTIPLICATION
%token DIVISION
%token MODULUS;
%start program
%%
program: main;
main: LP RP LB statements RB;
statements: stmt
| stmt statements
;
stmt: loops
| cond_stmt
| comments
| assign_st ENDSTMT
| expr ENDSTMT
| connection_stmt ENDSTMT
| function_declaration ENDSTMT
| function_call ENDSTMT
| function_return ENDSTMT
| print ENDSTMT
| ENDSTMT
;
comments: COMMENT_LINE
| COMMENT_MULTI_LINE
;
assign_st: IDENTIFIER ASSIGNMENT_OP const
| IDENTIFIER ASSIGNMENT_OP IDENTIFIER
| IDENTIFIER ASSIGNMENT_OP expr
| IDENTIFIER ASSIGNMENT_OP logic_expr
| IDENTIFIER ASSIGNMENT_OP function_call
| IDENTIFIER ASSIGNMENT_OP input
| IDENTIFIER ASSIGNMENT_OP functions
;
loops: while_loop
| for_loop
;
while_loop: WHILE LP logic_expr RP LB statements RB
;
for_loop: FOR LP IDENTIFIER ASSIGNMENT_OP NUMBER TO NUMBER ENDSTMT RP LB statements RB
| FOR LP IDENTIFIER ASSIGNMENT_OP NUMBER TO IDENTIFIER ENDSTMT RP LB statements RB
| FOR LP IDENTIFIER ASSIGNMENT_OP IDENTIFIER TO NUMBER ENDSTMT RP LB statements RB
| FOR LP IDENTIFIER ASSIGNMENT_OP IDENTIFIER TO IDENTIFIER ENDSTMT RP LB statements RB
| FOR LP IDENTIFIER ASSIGNMENT_OP IDENTIFIER TO expr ENDSTMT RP LB statements RB
| FOR LP IDENTIFIER ASSIGNMENT_OP expr TO IDENTIFIER ENDSTMT RP LB statements RB
| FOR LP IDENTIFIER ASSIGNMENT_OP NUMBER TO expr ENDSTMT RP LB statements RB
| FOR LP IDENTIFIER ASSIGNMENT_OP expr TO NUMBER ENDSTMT RP LB statements RB
| FOR LP IDENTIFIER ASSIGNMENT_OP expr TO expr ENDSTMT RP LB statements RB
cond_stmt: if_stmt;
if_stmt: IF LP logic_expr RP LB statements RB ELSE LB statements RB
| IF LP logic_expr RP LB statements RB
;
function_call: FUNCTION_CALL RP
| FUNCTION_CALL multi_params RP
;
function_declaration: FUNCTION_START LP RP LB statements RB
| FUNCTION_START LP multi_params RP LB statements RB
;
function_return: FUNCTION_RETURN expr
| FUNCTION_RETURN logic_expr
| FUNCTION_RETURN const
| FUNCTION_RETURN IDENTIFIER
;
multi_params: IDENTIFIER
| const
| IP
| IDENTIFIER COMMA multi_params
| const COMMA multi_params
;
print: PRINT const RP
| PRINT IDENTIFIER RP
| PRINT expr RP
| PRINT logic_expr RP
;
input: INPUT RP
| INPUT STRING RP
;
expr: expr_base
| expr_base op operand
;
expr_base: operand op LP expr_base RP
| operand op operand
| LP expr_base RP
| expr_base op operand op operand
| expr_base op LP expr_base RP
;
logic_expr: operand comparison_op operand
| LP logic_expr RP
| logic_expr boolean_op operand comparison_op operand
| logic_expr boolean_op LP operand comparison_op operand RP
;
functions: READALTITUDE
| READTEMPERATURE
| READACCELERATION
| TURNONCAMERA
| TURNOFFCAMERA
| TAKEPICTURE
| READTIMESTAMP
| CONNECTTOBASE
;
operand: IDENTIFIER | const | function_call | functions;
connection_stmt: receive_IP
| IDENTIFIER comparison_op IP CONNECT
| IDENTIFIER comparison_op IP DISCONNECT
;
receive_IP: IDENTIFIER RECEIVE IP;
arithmetic_op: PLUS | MINUS | MULTIPLICATION | DIVISION | MODULUS;
comparison_op: LESSTHAN | GREATERTHAN | LESSOREQUAL | GREATEROREQUAL | EQUAL | NOTEQUAL;
boolean_op: AND | OR | NOT;
const: NUMBER | STRING | FLOAT | BOOLEAN;
IP: IDENTIFIER DOT IDENTIFIER DOT IDENTIFIER DOT IDENTIFIER;
op: boolean_op | arithmetic_op;
%%
#include "lex.yy.c"
int yyerror(char* s){
fprintf(stderr, "%s at line %d\n",s, yylineno);
return 1;
}
int main(){
return yyparse();
}