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 | Expect(4);
|
---|
132 | while (StartOf(1)) {
|
---|
133 | Get();
|
---|
134 | }
|
---|
135 | Expect(5);
|
---|
136 | Expect(13);
|
---|
137 | }
|
---|
138 |
|
---|
139 | void TerminalDecl() {
|
---|
140 | Expect(1);
|
---|
141 | Expect(4);
|
---|
142 | while (StartOf(1)) {
|
---|
143 | Get();
|
---|
144 | }
|
---|
145 | Expect(5);
|
---|
146 | if (la.kind == 16) {
|
---|
147 | Get();
|
---|
148 | ConstraintDef();
|
---|
149 | }
|
---|
150 | Expect(13);
|
---|
151 | }
|
---|
152 |
|
---|
153 | void RuleDef() {
|
---|
154 | Expect(1);
|
---|
155 | Expect(4);
|
---|
156 | while (StartOf(1)) {
|
---|
157 | Get();
|
---|
158 | }
|
---|
159 | Expect(5);
|
---|
160 | Expect(21);
|
---|
161 | if (la.kind == 14) {
|
---|
162 | SemDecl();
|
---|
163 | }
|
---|
164 | SynExpr();
|
---|
165 | Expect(13);
|
---|
166 | }
|
---|
167 |
|
---|
168 | void SemDecl() {
|
---|
169 | Expect(14);
|
---|
170 | Expect(4);
|
---|
171 | while (StartOf(1)) {
|
---|
172 | Get();
|
---|
173 | }
|
---|
174 | Expect(5);
|
---|
175 | }
|
---|
176 |
|
---|
177 | void SemAction() {
|
---|
178 | Expect(15);
|
---|
179 | Expect(4);
|
---|
180 | while (StartOf(1)) {
|
---|
181 | Get();
|
---|
182 | }
|
---|
183 | Expect(5);
|
---|
184 | }
|
---|
185 |
|
---|
186 | void ConstraintDef() {
|
---|
187 | while (la.kind == 1) {
|
---|
188 | ConstraintRule();
|
---|
189 | }
|
---|
190 | }
|
---|
191 |
|
---|
192 | void ConstraintRule() {
|
---|
193 | Expect(1);
|
---|
194 | Expect(17);
|
---|
195 | SetDefinition();
|
---|
196 | }
|
---|
197 |
|
---|
198 | void SetDefinition() {
|
---|
199 | if (la.kind == 18) {
|
---|
200 | Get();
|
---|
201 | Expect(4);
|
---|
202 | while (StartOf(1)) {
|
---|
203 | Get();
|
---|
204 | }
|
---|
205 | Expect(5);
|
---|
206 | } else if (la.kind == 19) {
|
---|
207 | Get();
|
---|
208 | Expect(4);
|
---|
209 | while (StartOf(1)) {
|
---|
210 | Get();
|
---|
211 | }
|
---|
212 | Expect(5);
|
---|
213 | Expect(20);
|
---|
214 | Expect(4);
|
---|
215 | while (StartOf(1)) {
|
---|
216 | Get();
|
---|
217 | }
|
---|
218 | Expect(5);
|
---|
219 | } else SynErr(32);
|
---|
220 | }
|
---|
221 |
|
---|
222 | void SynExpr() {
|
---|
223 | SynTerm();
|
---|
224 | while (la.kind == 22) {
|
---|
225 | Get();
|
---|
226 | SynTerm();
|
---|
227 | }
|
---|
228 | }
|
---|
229 |
|
---|
230 | void SynTerm() {
|
---|
231 | SynFact();
|
---|
232 | while (StartOf(2)) {
|
---|
233 | SynFact();
|
---|
234 | }
|
---|
235 | }
|
---|
236 |
|
---|
237 | void SynFact() {
|
---|
238 | switch (la.kind) {
|
---|
239 | case 1: {
|
---|
240 | Get();
|
---|
241 | Expect(4);
|
---|
242 | while (StartOf(1)) {
|
---|
243 | Get();
|
---|
244 | }
|
---|
245 | Expect(5);
|
---|
246 | break;
|
---|
247 | }
|
---|
248 | case 23: {
|
---|
249 | Get();
|
---|
250 | break;
|
---|
251 | }
|
---|
252 | case 24: {
|
---|
253 | Get();
|
---|
254 | SynExpr();
|
---|
255 | Expect(25);
|
---|
256 | break;
|
---|
257 | }
|
---|
258 | case 26: {
|
---|
259 | Get();
|
---|
260 | SynExpr();
|
---|
261 | Expect(27);
|
---|
262 | break;
|
---|
263 | }
|
---|
264 | case 28: {
|
---|
265 | Get();
|
---|
266 | SynExpr();
|
---|
267 | Expect(29);
|
---|
268 | break;
|
---|
269 | }
|
---|
270 | case 15: {
|
---|
271 | SemAction();
|
---|
272 | break;
|
---|
273 | }
|
---|
274 | default: SynErr(33); break;
|
---|
275 | }
|
---|
276 | }
|
---|
277 |
|
---|
278 |
|
---|
279 |
|
---|
280 | public void Parse() {
|
---|
281 | la = new Token();
|
---|
282 | la.val = "";
|
---|
283 | Get();
|
---|
284 | GPDefSyntaxAnalyzer();
|
---|
285 | Expect(0);
|
---|
286 |
|
---|
287 | }
|
---|
288 |
|
---|
289 | static readonly bool[,] set = {
|
---|
290 | {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},
|
---|
291 | {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},
|
---|
292 | {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}
|
---|
293 |
|
---|
294 | };
|
---|
295 | } // end Parser
|
---|
296 |
|
---|
297 |
|
---|
298 | public class Errors {
|
---|
299 | public int count = 0; // number of errors detected
|
---|
300 | public System.IO.TextWriter errorStream = Console.Out; // error messages go to this stream
|
---|
301 | public string errMsgFormat = "-- line {0} col {1}: {2}"; // 0=line, 1=column, 2=text
|
---|
302 |
|
---|
303 | public virtual void SynErr (int line, int col, int n) {
|
---|
304 | string s;
|
---|
305 | switch (n) {
|
---|
306 | case 0: s = "EOF expected"; break;
|
---|
307 | case 1: s = "ident expected"; break;
|
---|
308 | case 2: s = "\"PROBLEM\" expected"; break;
|
---|
309 | case 3: s = "\"CODE\" expected"; break;
|
---|
310 | case 4: s = "\"<<\" expected"; break;
|
---|
311 | case 5: s = "\">>\" expected"; break;
|
---|
312 | case 6: s = "\"INIT\" expected"; break;
|
---|
313 | case 7: s = "\"NONTERMINALS\" expected"; break;
|
---|
314 | case 8: s = "\"TERMINALS\" expected"; break;
|
---|
315 | case 9: s = "\"RULES\" expected"; break;
|
---|
316 | case 10: s = "\"MAXIMIZE\" expected"; break;
|
---|
317 | case 11: s = "\"MINIMIZE\" expected"; break;
|
---|
318 | case 12: s = "\"END\" expected"; break;
|
---|
319 | case 13: s = "\".\" expected"; break;
|
---|
320 | case 14: s = "\"LOCAL\" expected"; break;
|
---|
321 | case 15: s = "\"SEM\" expected"; break;
|
---|
322 | case 16: s = "\"CONSTRAINTS\" expected"; break;
|
---|
323 | case 17: s = "\"IN\" expected"; break;
|
---|
324 | case 18: s = "\"SET\" expected"; break;
|
---|
325 | case 19: s = "\"RANGE\" expected"; break;
|
---|
326 | case 20: s = "\"..\" expected"; break;
|
---|
327 | case 21: s = "\"=\" expected"; break;
|
---|
328 | case 22: s = "\"|\" expected"; break;
|
---|
329 | case 23: s = "\"EPS\" expected"; break;
|
---|
330 | case 24: s = "\"(\" expected"; break;
|
---|
331 | case 25: s = "\")\" expected"; break;
|
---|
332 | case 26: s = "\"[\" expected"; break;
|
---|
333 | case 27: s = "\"]\" expected"; break;
|
---|
334 | case 28: s = "\"{\" expected"; break;
|
---|
335 | case 29: s = "\"}\" expected"; break;
|
---|
336 | case 30: s = "??? expected"; break;
|
---|
337 | case 31: s = "invalid GPDefSyntaxAnalyzer"; break;
|
---|
338 | case 32: s = "invalid SetDefinition"; break;
|
---|
339 | case 33: s = "invalid SynFact"; break;
|
---|
340 |
|
---|
341 | default: s = "error " + n; break;
|
---|
342 | }
|
---|
343 | errorStream.WriteLine(errMsgFormat, line, col, s);
|
---|
344 | count++;
|
---|
345 | }
|
---|
346 |
|
---|
347 | public virtual void SemErr (int line, int col, string s) {
|
---|
348 | errorStream.WriteLine(errMsgFormat, line, col, s);
|
---|
349 | count++;
|
---|
350 | }
|
---|
351 |
|
---|
352 | public virtual void SemErr (string s) {
|
---|
353 | errorStream.WriteLine(s);
|
---|
354 | count++;
|
---|
355 | }
|
---|
356 |
|
---|
357 | public virtual void Warning (int line, int col, string s) {
|
---|
358 | errorStream.WriteLine(errMsgFormat, line, col, s);
|
---|
359 | }
|
---|
360 |
|
---|
361 | public virtual void Warning(string s) {
|
---|
362 | errorStream.WriteLine(s);
|
---|
363 | }
|
---|
364 | } // Errors
|
---|
365 |
|
---|
366 |
|
---|
367 | public class FatalError: Exception {
|
---|
368 | public FatalError(string m): base(m) {}
|
---|
369 | }
|
---|
370 | } |
---|