1 |
|
---|
2 | using System;
|
---|
3 |
|
---|
4 | namespace SyntaxAnalyzer {
|
---|
5 |
|
---|
6 |
|
---|
7 |
|
---|
8 | public class Parser {
|
---|
9 | public const int _EOF = 0;
|
---|
10 | public const int _ident = 1;
|
---|
11 | public const int maxT = 30;
|
---|
12 |
|
---|
13 | const bool T = true;
|
---|
14 | const bool x = false;
|
---|
15 | const int minErrDist = 2;
|
---|
16 |
|
---|
17 | public Scanner scanner;
|
---|
18 | public Errors errors;
|
---|
19 |
|
---|
20 | public Token t; // last recognized token
|
---|
21 | public Token la; // lookahead token
|
---|
22 | int errDist = minErrDist;
|
---|
23 |
|
---|
24 |
|
---|
25 |
|
---|
26 | public Parser(Scanner scanner) {
|
---|
27 | this.scanner = scanner;
|
---|
28 | errors = new Errors();
|
---|
29 | }
|
---|
30 |
|
---|
31 | void SynErr (int n) {
|
---|
32 | if (errDist >= minErrDist) errors.SynErr(la.line, la.col, n);
|
---|
33 | errDist = 0;
|
---|
34 | }
|
---|
35 |
|
---|
36 | public void SemErr (string msg) {
|
---|
37 | if (errDist >= minErrDist) errors.SemErr(t.line, t.col, msg);
|
---|
38 | errDist = 0;
|
---|
39 | }
|
---|
40 |
|
---|
41 | void Get () {
|
---|
42 | for (;;) {
|
---|
43 | t = la;
|
---|
44 | la = scanner.Scan();
|
---|
45 | if (la.kind <= maxT) { ++errDist; break; }
|
---|
46 |
|
---|
47 | la = t;
|
---|
48 | }
|
---|
49 | }
|
---|
50 |
|
---|
51 | void Expect (int n) {
|
---|
52 | if (la.kind==n) Get(); else { SynErr(n); }
|
---|
53 | }
|
---|
54 |
|
---|
55 | bool StartOf (int s) {
|
---|
56 | return set[s, la.kind];
|
---|
57 | }
|
---|
58 |
|
---|
59 | void ExpectWeak (int n, int follow) {
|
---|
60 | if (la.kind == n) Get();
|
---|
61 | else {
|
---|
62 | SynErr(n);
|
---|
63 | while (!StartOf(follow)) Get();
|
---|
64 | }
|
---|
65 | }
|
---|
66 |
|
---|
67 |
|
---|
68 | bool WeakSeparator(int n, int syFol, int repFol) {
|
---|
69 | int kind = la.kind;
|
---|
70 | if (kind == n) {Get(); return true;}
|
---|
71 | else if (StartOf(repFol)) {return false;}
|
---|
72 | else {
|
---|
73 | SynErr(n);
|
---|
74 | while (!(set[syFol, kind] || set[repFol, kind] || set[0, kind])) {
|
---|
75 | Get();
|
---|
76 | kind = la.kind;
|
---|
77 | }
|
---|
78 | return StartOf(syFol);
|
---|
79 | }
|
---|
80 | }
|
---|
81 |
|
---|
82 |
|
---|
83 | void GPDefSyntaxAnalyzer() {
|
---|
84 | Expect(2);
|
---|
85 | Expect(1);
|
---|
86 | if (la.kind == 3) {
|
---|
87 | Get();
|
---|
88 | Expect(4);
|
---|
89 | while (StartOf(1)) {
|
---|
90 | Get();
|
---|
91 | }
|
---|
92 | Expect(5);
|
---|
93 | }
|
---|
94 | if (la.kind == 6) {
|
---|
95 | Get();
|
---|
96 | Expect(4);
|
---|
97 | while (StartOf(1)) {
|
---|
98 | Get();
|
---|
99 | }
|
---|
100 | Expect(5);
|
---|
101 | }
|
---|
102 | Expect(7);
|
---|
103 | while (la.kind == 1) {
|
---|
104 | NonterminalDecl();
|
---|
105 | }
|
---|
106 | Expect(8);
|
---|
107 | while (la.kind == 1) {
|
---|
108 | TerminalDecl();
|
---|
109 | }
|
---|
110 | Expect(9);
|
---|
111 | while (la.kind == 1) {
|
---|
112 | RuleDef();
|
---|
113 | }
|
---|
114 | if (la.kind == 10) {
|
---|
115 | Get();
|
---|
116 | } else if (la.kind == 11) {
|
---|
117 | Get();
|
---|
118 | } else SynErr(31);
|
---|
119 | Expect(4);
|
---|
120 | while (StartOf(1)) {
|
---|
121 | Get();
|
---|
122 | }
|
---|
123 | Expect(5);
|
---|
124 | Expect(12);
|
---|
125 | Expect(1);
|
---|
126 | Expect(13);
|
---|
127 | }
|
---|
128 |
|
---|
129 | void NonterminalDecl() {
|
---|
130 | Expect(1);
|
---|
131 | if (la.kind == 4) {
|
---|
132 | Get();
|
---|
133 | while (StartOf(1)) {
|
---|
134 | Get();
|
---|
135 | }
|
---|
136 | Expect(5);
|
---|
137 | }
|
---|
138 | Expect(13);
|
---|
139 | }
|
---|
140 |
|
---|
141 | void TerminalDecl() {
|
---|
142 | Expect(1);
|
---|
143 | if (la.kind == 4) {
|
---|
144 | Get();
|
---|
145 | while (StartOf(1)) {
|
---|
146 | Get();
|
---|
147 | }
|
---|
148 | Expect(5);
|
---|
149 | }
|
---|
150 | if (la.kind == 16) {
|
---|
151 | Get();
|
---|
152 | ConstraintDef();
|
---|
153 | }
|
---|
154 | Expect(13);
|
---|
155 | }
|
---|
156 |
|
---|
157 | void RuleDef() {
|
---|
158 | Expect(1);
|
---|
159 | if (la.kind == 4) {
|
---|
160 | Get();
|
---|
161 | while (StartOf(1)) {
|
---|
162 | Get();
|
---|
163 | }
|
---|
164 | Expect(5);
|
---|
165 | }
|
---|
166 | Expect(21);
|
---|
167 | if (la.kind == 14) {
|
---|
168 | SemDecl();
|
---|
169 | }
|
---|
170 | SynExpr();
|
---|
171 | Expect(13);
|
---|
172 | }
|
---|
173 |
|
---|
174 | void SemDecl() {
|
---|
175 | Expect(14);
|
---|
176 | Expect(4);
|
---|
177 | while (StartOf(1)) {
|
---|
178 | Get();
|
---|
179 | }
|
---|
180 | Expect(5);
|
---|
181 | }
|
---|
182 |
|
---|
183 | void SemAction() {
|
---|
184 | Expect(15);
|
---|
185 | Expect(4);
|
---|
186 | while (StartOf(1)) {
|
---|
187 | Get();
|
---|
188 | }
|
---|
189 | Expect(5);
|
---|
190 | }
|
---|
191 |
|
---|
192 | void ConstraintDef() {
|
---|
193 | while (la.kind == 1) {
|
---|
194 | ConstraintRule();
|
---|
195 | }
|
---|
196 | }
|
---|
197 |
|
---|
198 | void ConstraintRule() {
|
---|
199 | Expect(1);
|
---|
200 | Expect(17);
|
---|
201 | SetDefinition();
|
---|
202 | }
|
---|
203 |
|
---|
204 | void SetDefinition() {
|
---|
205 | if (la.kind == 18) {
|
---|
206 | Get();
|
---|
207 | Expect(4);
|
---|
208 | while (StartOf(1)) {
|
---|
209 | Get();
|
---|
210 | }
|
---|
211 | Expect(5);
|
---|
212 | } else if (la.kind == 19) {
|
---|
213 | Get();
|
---|
214 | Expect(4);
|
---|
215 | while (StartOf(1)) {
|
---|
216 | Get();
|
---|
217 | }
|
---|
218 | Expect(5);
|
---|
219 | Expect(20);
|
---|
220 | Expect(4);
|
---|
221 | while (StartOf(1)) {
|
---|
222 | Get();
|
---|
223 | }
|
---|
224 | Expect(5);
|
---|
225 | } else SynErr(32);
|
---|
226 | }
|
---|
227 |
|
---|
228 | void SynExpr() {
|
---|
229 | SynTerm();
|
---|
230 | while (la.kind == 22) {
|
---|
231 | Get();
|
---|
232 | SynTerm();
|
---|
233 | }
|
---|
234 | }
|
---|
235 |
|
---|
236 | void SynTerm() {
|
---|
237 | SynFact();
|
---|
238 | while (StartOf(2)) {
|
---|
239 | SynFact();
|
---|
240 | }
|
---|
241 | }
|
---|
242 |
|
---|
243 | void SynFact() {
|
---|
244 | switch (la.kind) {
|
---|
245 | case 1: {
|
---|
246 | Get();
|
---|
247 | if (la.kind == 4) {
|
---|
248 | Get();
|
---|
249 | while (StartOf(1)) {
|
---|
250 | Get();
|
---|
251 | }
|
---|
252 | Expect(5);
|
---|
253 | }
|
---|
254 | break;
|
---|
255 | }
|
---|
256 | case 23: {
|
---|
257 | Get();
|
---|
258 | break;
|
---|
259 | }
|
---|
260 | case 24: {
|
---|
261 | Get();
|
---|
262 | SynExpr();
|
---|
263 | Expect(25);
|
---|
264 | break;
|
---|
265 | }
|
---|
266 | case 26: {
|
---|
267 | Get();
|
---|
268 | SynExpr();
|
---|
269 | Expect(27);
|
---|
270 | break;
|
---|
271 | }
|
---|
272 | case 28: {
|
---|
273 | Get();
|
---|
274 | SynExpr();
|
---|
275 | Expect(29);
|
---|
276 | break;
|
---|
277 | }
|
---|
278 | case 15: {
|
---|
279 | SemAction();
|
---|
280 | break;
|
---|
281 | }
|
---|
282 | default: SynErr(33); break;
|
---|
283 | }
|
---|
284 | }
|
---|
285 |
|
---|
286 |
|
---|
287 |
|
---|
288 | public void Parse() {
|
---|
289 | la = new Token();
|
---|
290 | la.val = "";
|
---|
291 | Get();
|
---|
292 | GPDefSyntaxAnalyzer();
|
---|
293 | Expect(0);
|
---|
294 |
|
---|
295 | }
|
---|
296 |
|
---|
297 | static readonly bool[,] set = {
|
---|
298 | {T,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x},
|
---|
299 | {x,T,T,T, T,x,T,T, T,T,T,T, T,T,T,T, T,T,T,T, T,T,T,T, T,T,T,T, T,T,T,x},
|
---|
300 | {x,T,x,x, x,x,x,x, x,x,x,x, x,x,x,T, x,x,x,x, x,x,x,T, T,x,T,x, T,x,x,x}
|
---|
301 |
|
---|
302 | };
|
---|
303 | } // end Parser
|
---|
304 |
|
---|
305 |
|
---|
306 | public class Errors {
|
---|
307 | public int count = 0; // number of errors detected
|
---|
308 | public System.IO.TextWriter errorStream = Console.Out; // error messages go to this stream
|
---|
309 | public string errMsgFormat = "-- line {0} col {1}: {2}"; // 0=line, 1=column, 2=text
|
---|
310 |
|
---|
311 | public virtual void SynErr (int line, int col, int n) {
|
---|
312 | string s;
|
---|
313 | switch (n) {
|
---|
314 | case 0: s = "EOF expected"; break;
|
---|
315 | case 1: s = "ident expected"; break;
|
---|
316 | case 2: s = "\"PROBLEM\" expected"; break;
|
---|
317 | case 3: s = "\"CODE\" expected"; break;
|
---|
318 | case 4: s = "\"<<\" expected"; break;
|
---|
319 | case 5: s = "\">>\" expected"; break;
|
---|
320 | case 6: s = "\"INIT\" expected"; break;
|
---|
321 | case 7: s = "\"NONTERMINALS\" expected"; break;
|
---|
322 | case 8: s = "\"TERMINALS\" expected"; break;
|
---|
323 | case 9: s = "\"RULES\" expected"; break;
|
---|
324 | case 10: s = "\"MAXIMIZE\" expected"; break;
|
---|
325 | case 11: s = "\"MINIMIZE\" expected"; break;
|
---|
326 | case 12: s = "\"END\" expected"; break;
|
---|
327 | case 13: s = "\".\" expected"; break;
|
---|
328 | case 14: s = "\"LOCAL\" expected"; break;
|
---|
329 | case 15: s = "\"SEM\" expected"; break;
|
---|
330 | case 16: s = "\"CONSTRAINTS\" expected"; break;
|
---|
331 | case 17: s = "\"IN\" expected"; break;
|
---|
332 | case 18: s = "\"SET\" expected"; break;
|
---|
333 | case 19: s = "\"RANGE\" expected"; break;
|
---|
334 | case 20: s = "\"..\" expected"; break;
|
---|
335 | case 21: s = "\"=\" expected"; break;
|
---|
336 | case 22: s = "\"|\" expected"; break;
|
---|
337 | case 23: s = "\"EPS\" expected"; break;
|
---|
338 | case 24: s = "\"(\" expected"; break;
|
---|
339 | case 25: s = "\")\" expected"; break;
|
---|
340 | case 26: s = "\"[\" expected"; break;
|
---|
341 | case 27: s = "\"]\" expected"; break;
|
---|
342 | case 28: s = "\"{\" expected"; break;
|
---|
343 | case 29: s = "\"}\" expected"; break;
|
---|
344 | case 30: s = "??? expected"; break;
|
---|
345 | case 31: s = "invalid GPDefSyntaxAnalyzer"; break;
|
---|
346 | case 32: s = "invalid SetDefinition"; break;
|
---|
347 | case 33: s = "invalid SynFact"; break;
|
---|
348 |
|
---|
349 | default: s = "error " + n; break;
|
---|
350 | }
|
---|
351 | errorStream.WriteLine(errMsgFormat, line, col, s);
|
---|
352 | count++;
|
---|
353 | }
|
---|
354 |
|
---|
355 | public virtual void SemErr (int line, int col, string s) {
|
---|
356 | errorStream.WriteLine(errMsgFormat, line, col, s);
|
---|
357 | count++;
|
---|
358 | }
|
---|
359 |
|
---|
360 | public virtual void SemErr (string s) {
|
---|
361 | errorStream.WriteLine(s);
|
---|
362 | count++;
|
---|
363 | }
|
---|
364 |
|
---|
365 | public virtual void Warning (int line, int col, string s) {
|
---|
366 | errorStream.WriteLine(errMsgFormat, line, col, s);
|
---|
367 | }
|
---|
368 |
|
---|
369 | public virtual void Warning(string s) {
|
---|
370 | errorStream.WriteLine(s);
|
---|
371 | }
|
---|
372 | } // Errors
|
---|
373 |
|
---|
374 |
|
---|
375 | public class FatalError: Exception {
|
---|
376 | public FatalError(string m): base(m) {}
|
---|
377 | }
|
---|
378 | } |
---|