-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathg3.y
76 lines (60 loc) · 1.16 KB
/
g3.y
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
%{
#include <iostream>
using namespace std;
extern int yylex();
int yyerror(const char*);
%}
%token FUNCTION VARIABLE NUMBER FOR IF VAR LOGICAL_EXP COND_OP WHILE PRE_POST_FIX_OP
%start programSegment
%left '+' '-'
%left '*' '/'
%%
programSegment: command
| programSegment command
;
command: decl ';'
| exp ';'
| ifCond
| forLoop
| whileLoop
| assign ';'
| functionDecl
// command-> every possible declaration form programSegment
// programsegment : command | programsegment commmand
// sep
ifCond: IF '(' cond ')' '{' programSegment '}'
;
forLoop: FOR '(' exp '=' exp ';' cond ';' VARIABLE PRE_POST_FIX_OP ')' '{' programSegment '}'
;
whileLoop: WHILE '(' cond ')' '{' programSegment '}'
;
functionDecl: FUNCTION VARIABLE '(' headerDecl ')' '{' programSegment '}'
;
headerDecl: VARIABLE ',' headerDecl
| VARIABLE
;
decl: VAR VARIABLE
;
cond: exp COND_OP exp
| exp
;
assign: VARIABLE '=' exp
;
exp: LOGICAL_EXP
| NUMBER
| VARIABLE
| VARIABLE PRE_POST_FIX_OP
| PRE_POST_FIX_OP VARIABLE
| exp '+' exp
| exp '-' exp
| exp '*' exp
| exp '/' exp
| '(' exp ')'
;
%%
int main() {
yyparse();
}
int yyerror(const char* s) {
cout << s << endl;
}