blob: 993867f7c8cebaed50bcf9d92a508fd1e1f4d6e6 (
plain)
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
|
%language "c++"
%require "3.2"
%code requires {
#include <string>
#include <memory>
#include "Location.hh"
#include "Node.hh"
namespace pcc {
class Driver;
}
}
%locations
%param {pcc::Driver& driver}
%code {
#include "Driver.hh"
#define YY_DECL pcc::Parser::symbol_type yylex(pcc::Driver& driver)
YY_DECL;
}
%define api.namespace {pcc}
%define api.parser.class {Parser}
%define api.location.type {pcc::Location}
%define api.value.type variant
%define api.token.constructor
%define parse.error verbose
%define parse.assert
%token COLON SEMICOLON COMMA DOT ASSIGN LBRACKET RBRACKET LPARENTHESIS RPARENTHESIS
%token PROGRAM IDENTIFIER CONST VAR BEGINS ENDS FUNCTION FILE_END
%token IF THEN ELSE WHILE DO
%token BOOLEAN CHAR INTEGER REAL STRING
%token ADD SUB MUL REAL_DIV DIV MOD EXP LT LE GT GE EQ NE
%token AND NOT OR XOR
%token INTEGER_LITERAL REAL_LITERAL STRING_LITERAL BOOLEAN_LITERAL CHAR_LITERAL
%type <std::string> IDENTIFIER
%type <std::shared_ptr<pcc::BooleanLiteralNode>> BOOLEAN_LITERAL
%type <std::shared_ptr<pcc::IntegerLiteralNode>> INTEGER_LITERAL
%type <std::shared_ptr<pcc::RealLiteralNode>> REAL_LITERAL
%type <std::shared_ptr<pcc::StringLiteralNode>> STRING_LITERAL
%%
/* ==================[global part]================== */
program
: program_header decls functions
;
program_header
: PROGRAM IDENTIFIER SEMICOLON
;
decls
: decls decl SEMICOLON
|
;
decl
: VAR var_decls SEMICOLON
;
functions
: functions function SEMICOLON
|
;
/* ==================[var part]================== */
var_decls
: var_decls SEMICOLON var_decl
| var_decl
;
var_decl
: vars COLON type
;
vars
: vars COMMA IDENTIFIER
| IDENTIFIER
type
: INTEGER
| REAL
| BOOLEAN
| CHAR
| STRING
;
/* ==================[function part]================== */
function
: function_header decls statement_block
;
function_header
: FUNCTION IDENTIFIER LPARENTHESIS var_decls RPARENTHESIS COLON type SEMICOLON
;
/* ==================[statements part]================== */
statements
: statements SEMICOLON statement
| statement
;
statement
: open_statement
| closed_statement
;
open_statement
: IF expression THEN statement
| IF expression THEN closed_statement ELSE open_statement
| WHILE expression DO open_statement
;
closed_statement
: normal_statement
| IF expression THEN closed_statement ELSE closed_statement
| WHILE expression DO closed_statement
;
normal_statement
: statement_block
| empty_statement
| assign_statement
| expression
;
statement_block
: BEGINS statements ENDS
;
empty_statement
:
;
assign_statement
: lvalue ASSIGN expression
;
/* ==================[expression part]================== */
expression
: l1_expression l1_operator expression
| l1_expression
;
l1_expression
: l1_expression l2_operator l2_expression
| l2_expression
;
l2_expression
: l2_expression l3_operator l3_expression
| l3_expression
;
l3_expression
: l3_expression l4_operator l4_expression
| l4_expression
l4_expression
: lvalue
| function_call
| literal
| LPARENTHESIS expression RPARENTHESIS
| l5_operator l4_expression
;
l1_operator
: LT
| LE
| GT
| GE
| EQ
| NE
;
l2_operator
: ADD
| SUB
| OR
| XOR
;
l3_operator
: MUL
| REAL_DIV
| DIV
| MOD
| AND
;
l4_operator
: EXP
;
l5_operator
: NOT
| ADD
| SUB
;
lvalue
: IDENTIFIER
| IDENTIFIER LBRACKET expression RBRACKET
| IDENTIFIER DOT IDENTIFIER
;
function_call
: IDENTIFIER LPARENTHESIS arguments RPARENTHESIS
| IDENTIFIER LPARENTHESIS RPARENTHESIS
;
arguments
: arguments COMMA expression
| expression
;
literal
: BOOLEAN_LITERAL
| INTEGER_LITERAL
| REAL_LITERAL
| STRING_LITERAL
;
%%
namespace pcc {
void Parser::error (const location_type& l, const std::string& m)
{
std::cerr << l << ":" << m << '\n';
}
}
|