-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsyntax_analysis.h
200 lines (167 loc) · 3.69 KB
/
syntax_analysis.h
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
#pragma once
#ifndef syntax_analysis_h
#define syntax_analysis_h
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include "lexical_analysis.h"
extern enum{ INT, REAL, STRING, VOID, PTR };
void next()
{
get_next_token();
if (token == '{') {
ed[depth + 1] = ed[depth];
depth++;
}
else if (token == '}')depth--;
}
void match(int tok) {
if (token == tok) {
next();
}
else {
printf("%d: expected token: %d\n", line, tok);
exit(-1);
}
}
void function_body()
{
// type func_name (...) {...}
// -->| |<--
// ... {
// 1. local declarations
// 2. statements
// }
*++text = ENT;
int type;
while (token != '}')
{
if (token == Int || token == Real || token == String || token == Void)
{
while (token != ';')
{
if (token == Int) { match(Int); type == INT; }
else if (token == String) { match(String); type == STRING; }
else if (token == Real) { match(Real); type == REAL; }
else if (token == Void) { match(Void); type == VOID; }
while (token == Mul) {
match(Mul);
type = type + PTR;
}
if (token != Id) {
printf("%d: bad paramater declaration\n", line);
exit(-1);
}
if (cur_id->class_ == Var) {
printf("%d: duplicate parameter declaration\n", line);
exit(-1);
}
match(Id);
cur_id->class_ = Var;
cur_id->addr = (int)data_;
data_ = (void *)((int)data_ + sizeof(int));
if (token == ',')match(',');
}
match(';');
}
}
}
void function_parameter()
{
int type;
while (token != ')')
{
if (token == Int) { match(Int); type == INT; }
else if (token == String) { match(String); type == STRING; }
else if (token == Real) { match(Real); type == REAL; }
else if (token == Void) { match(Void); type == VOID; }
//pointer type
while (token == Mul) {
match(Mul);
type = type + PTR;
}
//paramater name
if (token != Id) {
printf("%d: bad paramater declaration\n", line);
exit(-1);
}
if (cur_id->class_ == Var) {
printf("%d: duplicate parameter declaration\n", line);
exit(-1);
}
match(Id);
cur_id->class_ = Var;
cur_id->addr = (int)data_;
data_ = (void *)((int)data_ + sizeof(int));
if (token == ',')match(',');
}
}
void function_declaration()
{
// type func_name (...) {...}
// | this part
match('(');
function_parameter();
match(')');
match('{');
function_body();
//match("}"); //this '}' has been matched in function_body
}
void global_declaration()
{
//EBNF:
// global_declaration ::= enum_decl | variable_decl | function_decl
//
// enum_decl ::= 'enum' [id] '{' id ['=' 'num'] {',' id ['=' 'num'} '}'
//
// variable_decl ::= type {'*'} id { ',' {'*'} id } ';'
//
// function_decl ::= type {'*'} id '(' parameter_decl ')' '{' body_decl '}'
int type;
int i;
if (token == Int) { match(Int); type == INT; }
else if (token == String) { match(String); type == STRING; }
else if (token == Real){ match(Real); type == REAL; }
else if (token == Void){ match(Void); type == VOID; }
while (token != ';' && token != '}')
{
while (token == MUL) { //index
match(MUL);
type = type + PTR;
}
if (token != Id) {
//invalid declaration
printf("%d: bad global declaration\n", line);
exit(-1);
}
if (cur_id->class_) {
printf("%d: duplicate global declaration\n", line);
exit(-1);
}
match(Id);
cur_id->class_ = type;
if (token == '(')
{
cur_id->class_ = Fun;
cur_id->addr = (int)text;
function_declaration();
}
else
{
cur_id->class_ = Var;
cur_id->addr = (int)data_;
data_ = (void *)((int)data_ + sizeof(int));
}
if (token == ',') match(',');
}
get_next_token();
}
void program()
{
next();
while (token > 0) {
global_declaration();
}
}
#endif