File: Syntax.cpp

package info (click to toggle)
storm-lang 0.7.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 52,004 kB
  • sloc: ansic: 261,462; cpp: 140,405; sh: 14,891; perl: 9,846; python: 2,525; lisp: 2,504; asm: 860; makefile: 678; pascal: 70; java: 52; xml: 37; awk: 12
file content (277 lines) | stat: -rw-r--r-- 6,808 bytes parent folder | download | duplicates (3)
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#include "stdafx.h"
#include "Syntax.h"
#include "Stack.h"
#include "Item.h"

namespace storm {
	namespace syntax {
		namespace glr {

			RuleInfo::RuleInfo() : productions(null), followSet(null), followRules(null), firstRules(null) {}

			void RuleInfo::push(Nat id) {
				if (!productions)
					productions = new (this) Array<Nat>();
				productions->push(id);
			}

			void RuleInfo::follows(Regex regex) {
				if (!followSet)
					followSet = new (this) Set<Regex>();
				followSet->put(regex);
			}

			void RuleInfo::follows(Nat rule) {
				if (!followRules)
					followRules = new (this) Set<Nat>();
				followRules->put(rule);
			}

			void RuleInfo::followsFirst(Nat rule) {
				if (!firstRules)
					firstRules = new (this) Set<Nat>();
				firstRules->put(rule);
			}

			Set<Regex> *RuleInfo::first(Syntax *syntax) {
				Set<Regex> *r = new (syntax) Set<Regex>();
				if (!productions || compFirst)
					return r;

				compFirst = true;

				for (Nat i = 0; i < productions->count(); i++) {
					Item z(syntax, productions->at(i));

					if (z.end()) {
						r->put(follows(syntax));
					} else if (z.isRule(syntax)) {
						RuleInfo *info = syntax->ruleInfo(z.nextRule(syntax));
						if (!info->compFirst)
							r->put(info->first(syntax));
					} else {
						r->put(z.nextRegex(syntax));
					}
				}

				compFirst = false;

				return r;
			}

			Set<Regex> *RuleInfo::follows(Syntax *syntax) {
				if (!followSet)
					followSet = new (this) Set<Regex>();
				if (compFollows)
					return followSet;
				compFollows = true;

				typedef Set<Nat>::Iter Iter;

				if (followRules) {
					for (Iter i = followRules->begin(), end = followRules->end(); i != end; ++i) {
						RuleInfo *info = syntax->ruleInfo(i.v());
						followSet->put(info->follows(syntax));
					}
					followRules = null;
				}

				if (firstRules) {
					for (Iter i = firstRules->begin(), end = firstRules->end(); i != end; ++i) {
						RuleInfo *info = syntax->ruleInfo(i.v());
						followSet->put(info->first(syntax));
					}
					firstRules = null;
				}

				compFollows = false;
				return followSet;
			}


			/**
			 * Syntax.
			 */

			Syntax::Syntax() {
				rLookup = new (this) Map<Rule *, Nat>();
				pLookup = new (this) Map<Production *, Nat>();
				rules = new (this) Array<Rule *>();
				ruleProds = new (this) Array<RuleInfo *>();
				repRuleProds = new (this) Array<RuleInfo *>();
				productions = new (this) Array<Production *>();
				parentIds = new (this) Map<Nat, ParentReq>();
				prodParents = new (this) Array<ParentReq>();
			}

			Nat Syntax::add(Rule *rule) {
				Nat id = rLookup->get(rule, rules->count());
				if (id < rules->count())
					return id;

				// New rule!
				rules->push(rule);
				ruleProds->push(new (this) RuleInfo());
				rLookup->put(rule, id);
				return id;
			}

			Nat Syntax::add(Production *p) {
				Nat id = pLookup->get(p, productions->count());
				if (id < productions->count())
					return id;

				// New production!
				productions->push(p);
				repRuleProds->push(null);
				pLookup->put(p, id);

				Nat ruleId = lookup(p->rule());
				ruleProds->at(ruleId)->push(id);

				// Check the parent id for this production.
				ParentReq req;
				if (p->parent) {
					Nat pId = lookup(p->parent);
					if (parentIds->has(pId)) {
						req = parentIds->get(pId);
					} else {
						req = ParentReq(engine(), parentIds->count());
						parentIds->put(pId, req);
					}
				}
				prodParents->push(req);

				addFollows(p);

				return id;
			}

			void Syntax::addFollows(Production *p) {
				addFollows(Item(this, p), p);
			}

			void Syntax::addFollows(const Item &start, Production *p) {
				for (Item at = start; !at.end(); at = at.next(p)) {
					if (at.pos == Item::specialPos && specialProd(at.id) == 0)
						addFollows(Item(this, baseProd(at.id) | prodRepeat), p);

					if (!at.isRule(this))
						continue;

					Nat rule = at.nextRule(this);
					if (specialRule(rule))
						// We compute these on the fly when we need them...
						continue;

					addFollows(ruleInfo(rule), at.next(p));
				}
			}

			void Syntax::addFollows(RuleInfo *into, const Item &next) {
				if (next.end()) {
					// The follow set of 'rule' is the follow set of this production.
					into->follows(next.rule(this));
				} else if (next.isRule(this)) {
					// The follow set of 'rule' is the first set of 'next.nextRule'
					into->followsFirst(next.nextRule(this));
				} else {
					// The follow set shall also include this regex.
					into->follows(next.nextRegex(this));
				}
			}

			Nat Syntax::lookup(Rule *r) {
				Nat id = rLookup->get(r, rules->count());
				if (id >= rules->count()) {
					// We need to add it...
					id = add(r);
				}
				return id;
			}

			Nat Syntax::lookup(Production *p) {
				Nat id = pLookup->get(p, productions->count());
				if (id >= productions->count()) {
					id = add(p);
				}
				return id;
			}

			RuleInfo *Syntax::ruleInfo(Nat rule) {
				switch (specialRule(rule)) {
				case ruleRepeat: {
					Nat prod = baseRule(rule);
					RuleInfo *info = repRuleProds->at(prod);
					if (!info) {
						info = new (this) RuleInfo();
						repRuleProds->at(prod) = info;

						info->push(prod | prodEpsilon);
						info->push(prod | prodRepeat);

						// We only need to consider the repeat production for the follow set.
						addFollows(info, Item(this, prod | prodRepeat).next(this));
						addFollows(info, special(prod).next(this));
					}

					return info;
				}
				case ruleESkip: {
					Nat prod = baseRule(rule);
					RuleInfo *info = new (this) RuleInfo();
					info->push(prod | prodESkip);
					// Note: these rules do not need a follow set, as they are treated specially.
					return info;
				}
				default:
					return ruleProds->at(rule);
				}
			}

			Str *Syntax::ruleName(Nat id) const {
				switch (specialRule(id)) {
				case ruleRepeat: {
					Production *p = productions->at(baseRule(id));
					return *p->rule()->identifier() + new (this) Str(L"'");
				}
				case ruleESkip:
					return TO_S(this, L"red" << baseRule(id));
				default:
					return rules->at(id)->identifier();
				}
			}

			Production *Syntax::production(Nat pid) const {
				return productions->at(baseProd(pid));
			}

			ParentReq Syntax::parentId(Nat rule) const {
				if (specialRule(rule) != 0)
					return ParentReq();
				else
					return parentIds->at(rule);
			}

			ParentReq Syntax::productionReq(Nat pid) const {
				if (specialProd(pid) != 0)
					return ParentReq();
				else
					return prodParents->at(baseProd(pid));
			}

			Bool Syntax::sameSyntax(Syntax *o) const {
				if (productions->count() != o->productions->count())
					return false;

				for (Nat i = 0; i < productions->count(); i++) {
					if (!o->pLookup->has(productions->at(i)))
						return false;
				}

				return true;
			}

		}
	}
}