-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgrammar.y
More file actions
98 lines (84 loc) · 1.54 KB
/
Copy pathgrammar.y
File metadata and controls
98 lines (84 loc) · 1.54 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
// This is just a specification, the parser was handwritten.
%{
}%
%token <operator> PLUS "+"
%token <operator> MINUS "-"
%token <operator> MULTIPLY "*"
%token <operator> DIVIDE "/"
%token <operator> ASSIGNMENT "="
%token <operator> GREATER_THAN ">"
%token <operator> EQUALS "=="
%token <operator> OPENING_BRACKET "("
%token <operator> CLOSING_BRACKET ")"
%token <delimiter> SEMICOLON ";"
%token <delimiter> COMMA ","
%token <delimiter> OPENING_BRACE "{"
%token <delimiter> CLOSING_BRACE "}"
%token <keyword> FOR "for"
%token <keyword> WRITE "write"
%token <keyword> READ "read"
%token <keyword> INT "int"
%token <variable> [a-z]+
%token <constant> [0-9]+
%%
program:
declaration SEMICOLON
| declaration SEMICOLON statement
| statement
;
statement:
assignment SEMICOLON
| read SEMICOLON
| write SEMICOLON
| loop SEMICOLON
| assignment SEMICOLON statement
| read SEMICOLON statement
| write SEMICOLON statement
| loop SEMICOLON statement
;
loop:
FOR OPENING_BRACKET assignment SEMICOLON expression SEMICOLON assignment CLOSING_BRACKET OPENING_BRACE statement CLOSING_BRACE
;
assignment:
variable EQUALS expression
;
expression:
expression GREATER_THAN T1
| expression EQUALS T1
| T1
;
T1:
T1 PLUS T2
| T1 MINUS T2
| T2
;
T2:
T2 MULTIPLY T3
| T2 DIVIDE T3
| T3
;
T3:
OPENING_BRACKET expression CLOSING_BRACKET
| constant
| variable
;
write:
WRITE constant
| WRITE variable
;
read:
READ variable
;
declaration:
INT variable_list
;
variable_list:
variable
| variable COMMA variable_list
;
variable:
<variable>
;
constant:
<constant>
;