-
Notifications
You must be signed in to change notification settings - Fork 0
/
grammar.h
222 lines (164 loc) · 5.84 KB
/
grammar.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
// ****************************************************************************
// ****************************************************************************
// grammar.h
// ****************************************************************************
//
// ****************************************************************************
// ****************************************************************************
// ****************************************************************************
// Defines
// ****************************************************************************
#ifndef H_GRAMMAR
#define H_GRAMMAR
// ****************************************************************************
// ProdEle Struct
// ****************************************************************************
struct ProdEle {
union {
Terminal* m_term;
NonTerm* m_nonTerm;
};
const bool m_isTerm;
ProdEle(Terminal* term);
ProdEle(NonTerm* nTerm);
const comString& toString();
};
// ****************************************************************************
// Production Struct
// ****************************************************************************
class Production : public ProdEleVec {
public:
Production();
~Production();
void add(Terminal* t) {append(new ProdEle(t));}
void add(NonTerm* n) {append(new ProdEle(n));}
void addTScheme(TransScheme* tScheme) {m_tSchemes.append(tScheme);}
void setDeclarator() {m_declarator = true;}
bool isDeclarator() {return m_declarator;}
void setFuncDeclarator() {m_fDeclarator = true;}
bool isFuncDeclarator() {return m_fDeclarator;}
void setFuncInvocation() {m_funcInv = true;}
bool isFuncInvocation() {return m_funcInv;}
comString& printable(comString& buf);
bool matches(Production* o);
TSVec& getTransSchemes() {return m_tSchemes;}
private:
TSVec m_tSchemes;
bool m_declarator;
bool m_fDeclarator;
bool m_funcInv;
};
// ****************************************************************************
// Terminal Struct
// ****************************************************************************
class Terminal {
public:
Token::Type m_tType;
const comString m_spelling;
Terminal(Token::Type type);
Terminal(Token::Type type,
const comString& spelling);
bool matches(const Token* token);
};
// ****************************************************************************
// NonTerm Class
// ****************************************************************************
class NonTerm {
public:
NonTerm(const comString& name,
bool scopeEdge = false);
void addProduction(Production* production);
void addProduction(Terminal* term);
void addProduction(NonTerm* nTerm);
const comString& getName() const {return m_name;}
ProdVec& getProductions() {return m_productions;}
bool isScopeEdge() const {return m_scopeEdge;}
static void dumpGrammar();
private:
ProdVec m_productions;
const comString m_name;
bool m_scopeEdge;
static NTermVec m_nTerms;
};
// ****************************************************************************
// ParseTree Class
// ****************************************************************************
class ParseTree {
public:
ParseTree(NonTerm* nTerm);
ParseTree(Terminal* term,
Token* token);
~ParseTree();
void typeCheck();
void addChild(ParseTree* child);
void cullChildren();
ParseTree* getChild(UINT i) {return m_children[i];}
UINT getNumChildren() {return m_numChildren;}
void setProduction(Production* production) {m_production = production;}
Production* getProduction() {return m_production;}
void addVarDef(const comString& identifier,
Translator::Type type,
bool found = false);
VarDef* findVarDef(const comString& identifier);
void addFuncDef(FuncDef* def,
bool found = false);
FuncDef* findFuncDef(const comString& identifier);
bool isTerminal() {return m_prodEle.m_isTerm;}
Terminal* getTerminal() {return m_prodEle.m_term;}
NonTerm* getNonTerm() {return m_prodEle.m_nonTerm;}
void print() {print("tree.out");}
void print(const comString& filename);
const comString& getDesc() {return m_prodEle.toString();}
TransScheme* getScheme() {return m_scheme;}
Token* getToken() {return m_token;}
void forceScopeEdge() {m_scopeEdge = true;}
void forceVarDef(VarDef* def) {m_varDefs.append(def);}
private:
void print(UINT level,
FILE* file);
void typeCheck(UINT& lastLine);
ParseTree* m_children[PT_MAX_CHILDREN];
ParseTree* m_parent;
ProdEle m_prodEle;
UINT m_numChildren;
Production* m_production;
Token* m_token;
Translator::Type m_type;
TransScheme* m_scheme;
bool m_scopeEdge;
VDVec m_varDefs;
FDVec m_funcDefs;
friend class Translator;
};
// ****************************************************************************
// VarDef class
// ****************************************************************************
class VarDef {
public:
VarDef(const comString& rName,
const comString& oName,
Translator::Type type);
static void printVarDefHeader(FILE* file);
const comString m_preName;
const comString m_postName;
Translator::Type m_type;
static VDVec m_varDefs;
};
// ****************************************************************************
// FuncDef class
// ****************************************************************************
class FuncDef {
public:
FuncDef(const comString& rName,
const comString& oName,
Translator::Type type,
ParseTree* tree);
static void printFuncDefHeader(FILE* file);
const comString m_preName;
const comString m_postName;
const Translator::Type m_type;
VDVec m_params;
ParseTree* m_definition;
static FDVec m_funcDefs;
};
#endif