-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathCoco.atg
477 lines (419 loc) · 21.5 KB
/
Coco.atg
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
/*-------------------------------------------------------------------------
Coco.ATG -- Attributed Grammar
Compiler Generator Coco/R,
Copyright (c) 1990, 2004 Hanspeter Moessenboeck, University of Linz
extended by M. Loeberbauer & A. Woess, Univ. of Linz
with improvements by Pat Terry, Rhodes University
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2, or (at your option) any
later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
As an exception, it is allowed to write an extension of Coco/R that is
used as a plugin in non-free software.
If not otherwise stated, any source code generated by Coco/R (other than
Coco/R itself) does not fall under the GNU General Public License.
-------------------------------------------------------------------------*/
/*-------------------------------------------------------------------------
compile with:
Coco Coco.ATG -namespace at.jku.ssw.Coco
-------------------------------------------------------------------------*/
$namespace=at.jku.ssw.Coco
using System.IO;
COMPILER Coco
const int id = 0;
const int str = 1;
public TextWriter trace; // other Coco objects referenced in this ATG
public Tab tab;
public DFA dfa;
public ParserGen pgen;
bool genScanner;
string tokenString; // used in declarations of literal tokens
string noString = "-none-"; // used in declarations of literal tokens
/*-------------------------------------------------------------------------*/
CHARACTERS
letter = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_".
digit = "0123456789".
cr = '\r'.
lf = '\n'.
tab = '\t'.
stringCh = ANY - '"' - '\\' - cr - lf.
charCh = ANY - '\'' - '\\' - cr - lf.
printable = '\u0020' .. '\u007e'.
hex = "0123456789abcdef".
TOKENS
ident = letter { letter | digit }.
number = digit { digit }.
string = '"' { stringCh | '\\' printable } '"'.
badString = '"' { stringCh | '\\' printable } (cr | lf).
char = '\'' ( charCh | '\\' printable { hex } ) '\''.
PRAGMAS
ddtSym = '$' { digit | letter }. (. tab.SetDDT(la.val); .)
optionSym = '$' letter { letter } '='
{ digit | letter
| '-' | '.' | ':'
}. (. tab.SetOption(la.val); .)
COMMENTS FROM "/*" TO "*/" NESTED
COMMENTS FROM "//" TO lf
IGNORE cr + lf + tab
/*-------------------------------------------------------------------------*/
PRODUCTIONS
Coco (. Symbol sym; Graph g, g1, g2; string gramName; CharSet s; int beg, line; .)
=
[ // using statements
ANY (. beg = t.pos; line = t.line; .)
{ ANY } (. pgen.usingPos = new Position(beg, la.pos, 0, line); .)
]
"COMPILER" (. genScanner = true;
tab.ignored = new CharSet(); .)
ident (. gramName = t.val;
beg = la.pos; line = la.line;
.)
{ ANY } (. tab.semDeclPos = new Position(beg, la.pos, 0, line); .)
[ "IGNORECASE" (. dfa.ignoreCase = true; .) ] /* pdt */
[ "CHARACTERS" { SetDecl }]
[ "TOKENS" { TokenDecl<Node.t> }]
[ "PRAGMAS" { TokenDecl<Node.pr> }]
{ "COMMENTS" (. bool nested = false; .)
"FROM" TokenExpr<out g1>
"TO" TokenExpr<out g2>
[ "NESTED" (. nested = true; .)
] (. dfa.NewComment(g1.l, g2.l, nested); .)
}
{ "IGNORE" Set<out s> (. tab.ignored.Or(s); .)
}
SYNC
"PRODUCTIONS" (. if (genScanner) dfa.MakeDeterministic();
tab.DeleteNodes();
.)
{ ident (. sym = tab.FindSym(t.val);
bool undef = sym == null;
if (undef) sym = tab.NewSym(Node.nt, t.val, t.line);
else {
if (sym.typ == Node.nt) {
if (sym.graph != null) SemErr("name declared twice");
} else SemErr("this symbol kind not allowed on left side of production");
sym.line = t.line;
}
bool noAttrs = sym.attrPos == null;
sym.attrPos = null;
.)
[ AttrDecl<sym> ] (. if (!undef)
if (noAttrs != (sym.attrPos == null))
SemErr("attribute mismatch between declaration and use of this symbol");
.)
[ SemText<out sym.semPos> ] WEAK
'='
Expression<out g> (. sym.graph = g.l;
tab.Finish(g);
.)
WEAK
'.'
}
"END" ident (. if (gramName != t.val)
SemErr("name does not match grammar name");
tab.gramSy = tab.FindSym(gramName);
if (tab.gramSy == null)
SemErr("missing production for grammar name");
else {
sym = tab.gramSy;
if (sym.attrPos != null)
SemErr("grammar symbol must not have attributes");
}
tab.noSym = tab.NewSym(Node.t, "???", 0); // noSym gets highest number
tab.SetupAnys();
tab.RenumberPragmas();
if (tab.ddt[2]) tab.PrintNodes();
if (errors.count == 0) {
Console.WriteLine("checking");
tab.CompSymbolSets();
if (tab.ddt[7]) tab.XRef();
if (tab.GrammarOk()) {
Console.Write("parser");
pgen.WriteParser();
if (genScanner) {
Console.Write(" + scanner");
dfa.WriteScanner();
if (tab.ddt[0]) dfa.PrintStates();
}
Console.WriteLine(" generated");
if (tab.ddt[8]) pgen.WriteStatistics();
}
}
if (tab.ddt[6]) tab.PrintSymbolTable();
.)
'.'
.
/*------------------------------------------------------------------------------------*/
SetDecl (. CharSet s; .)
=
ident (. string name = t.val;
CharClass c = tab.FindCharClass(name);
if (c != null) SemErr("name declared twice");
.)
'=' Set<out s> (. if (s.Elements() == 0) SemErr("character set must not be empty");
tab.NewCharClass(name, s);
.)
'.'
.
/*------------------------------------------------------------------------------------*/
Set<out CharSet s> (. CharSet s2; .)
=
SimSet<out s>
{ '+' SimSet<out s2> (. s.Or(s2); .)
| '-' SimSet<out s2> (. s.Subtract(s2); .)
}
.
/*------------------------------------------------------------------------------------*/
SimSet<out CharSet s> (. int n1, n2; .)
= (. s = new CharSet(); .)
( ident (. CharClass c = tab.FindCharClass(t.val);
if (c == null) SemErr("undefined name"); else s.Or(c.set);
.)
| string (. string name = t.val;
name = tab.Unescape(name.Substring(1, name.Length-2));
foreach (char ch in name)
if (dfa.ignoreCase) s.Set(char.ToLower(ch));
else s.Set(ch); .)
| Char<out n1> (. s.Set(n1); .)
[ ".." Char<out n2> (. for (int i = n1; i <= n2; i++) s.Set(i); .)
]
| "ANY" (. s = new CharSet(); s.Fill(); .)
)
.
/*--------------------------------------------------------------------------------------*/
Char<out int n>
=
char (. string name = t.val; n = 0;
name = tab.Unescape(name.Substring(1, name.Length-2));
if (name.Length == 1) n = name[0];
else SemErr("unacceptable character value");
if (dfa.ignoreCase && (char)n >= 'A' && (char)n <= 'Z') n += 32;
.)
.
/*------------------------------------------------------------------------------------*/
TokenDecl<int typ> (. string name; int kind; Symbol sym; Graph g; .)
=
Sym<out name, out kind> (. sym = tab.FindSym(name);
if (sym != null) SemErr("name declared twice");
else {
sym = tab.NewSym(typ, name, t.line);
sym.tokenKind = Symbol.fixedToken;
}
tokenString = null;
.)
SYNC
( '=' TokenExpr<out g> '.' (. if (kind == str) SemErr("a literal must not be declared with a structure");
tab.Finish(g);
if (tokenString == null || tokenString.Equals(noString))
dfa.ConvertToStates(g.l, sym);
else { // TokenExpr is a single string
if (tab.literals[tokenString] != null)
SemErr("token string declared twice");
tab.literals[tokenString] = sym;
dfa.MatchLiteral(tokenString, sym);
}
.)
| (. if (kind == id) genScanner = false;
else dfa.MatchLiteral(sym.name, sym);
.)
)
[ SemText<out sym.semPos> (. if (typ != Node.pr) SemErr("semantic action not allowed here"); .)
]
.
/*------------------------------------------------------------------------------------*/
AttrDecl<Symbol sym>
=
'<' (. int beg = la.pos; int col = la.col; int line = la.line; .)
{ ANY
| badString (. SemErr("bad string in attributes"); .)
}
'>' (. if (t.pos > beg)
sym.attrPos = new Position(beg, t.pos, col, line); .)
| "<." (. int beg = la.pos; int col = la.col; int line = la.line; .)
{ ANY
| badString (. SemErr("bad string in attributes"); .)
}
".>" (. if (t.pos > beg)
sym.attrPos = new Position(beg, t.pos, col, line); .)
.
/*------------------------------------------------------------------------------------*/
Expression<out Graph g> (. Graph g2; .)
=
Term<out g> (. bool first = true; .)
{ WEAK
'|'
Term<out g2> (. if (first) { tab.MakeFirstAlt(g); first = false; }
tab.MakeAlternative(g, g2);
.)
}
.
/*------------------------------------------------------------------------------------*/
Term<out Graph g> (. Graph g2; Node rslv = null; g = null; .)
=
( [ (. rslv = tab.NewNode(Node.rslv, null, la.line); .)
Resolver<out rslv.pos> (. g = new Graph(rslv); .)
]
Factor<out g2> (. if (rslv != null) tab.MakeSequence(g, g2);
else g = g2;
.)
{ Factor<out g2> (. tab.MakeSequence(g, g2); .)
}
| (. g = new Graph(tab.NewNode(Node.eps, null, 0)); .)
) (. if (g == null) // invalid start of Term
g = new Graph(tab.NewNode(Node.eps, null, 0));
.)
.
/*------------------------------------------------------------------------------------*/
Factor<out Graph g> (. string name; int kind; Position pos; bool weak = false;
g = null;
.)
=
( [ "WEAK" (. weak = true; .)
]
Sym<out name, out kind> (. Symbol sym = tab.FindSym(name);
if (sym == null && kind == str)
sym = tab.literals[name] as Symbol;
bool undef = sym == null;
if (undef) {
if (kind == id)
sym = tab.NewSym(Node.nt, name, 0); // forward nt
else if (genScanner) {
sym = tab.NewSym(Node.t, name, t.line);
dfa.MatchLiteral(sym.name, sym);
} else { // undefined string in production
SemErr("undefined string in production");
sym = tab.eofSy; // dummy
}
}
int typ = sym.typ;
if (typ != Node.t && typ != Node.nt)
SemErr("this symbol kind is not allowed in a production");
if (weak)
if (typ == Node.t) typ = Node.wt;
else SemErr("only terminals may be weak");
Node p = tab.NewNode(typ, sym, t.line);
g = new Graph(p);
.)
[ Attribs<p> (. if (kind != id) SemErr("a literal must not have attributes"); .)
] (. if (undef)
sym.attrPos = p.pos; // dummy
else if ((p.pos == null) != (sym.attrPos == null))
SemErr("attribute mismatch between declaration and use of this symbol");
.)
| '(' Expression<out g> ')'
| '[' Expression<out g> ']' (. tab.MakeOption(g); .)
| '{' Expression<out g> '}' (. tab.MakeIteration(g); .)
| SemText<out pos> (. Node p = tab.NewNode(Node.sem, null, 0);
p.pos = pos;
g = new Graph(p);
.)
| "ANY" (. Node p = tab.NewNode(Node.any, null, 0); // p.set is set in tab.SetupAnys
g = new Graph(p);
.)
| "SYNC" (. Node p = tab.NewNode(Node.sync, null, 0);
g = new Graph(p);
.)
) (. if (g == null) // invalid start of Factor
g = new Graph(tab.NewNode(Node.eps, null, 0));
.)
.
/*------------------------------------------------------------------------------------*/
Resolver<out Position pos>
=
"IF" "(" (. int beg = la.pos; int col = la.col; int line = la.line; .)
Condition (. pos = new Position(beg, t.pos, col, line); .)
.
/*------------------------------------------------------------------------------------*/
Condition = { "(" Condition | ANY } ")" .
/*------------------------------------------------------------------------------------*/
TokenExpr<out Graph g> (. Graph g2; .)
=
TokenTerm<out g> (. bool first = true; .)
{ WEAK
'|'
TokenTerm<out g2> (. if (first) { tab.MakeFirstAlt(g); first = false; }
tab.MakeAlternative(g, g2);
.)
}
.
/*------------------------------------------------------------------------------------*/
TokenTerm<out Graph g> (. Graph g2; .)
=
TokenFactor<out g>
{ TokenFactor<out g2> (. tab.MakeSequence(g, g2); .)
}
[ "CONTEXT"
'(' TokenExpr<out g2> (. tab.SetContextTrans(g2.l); dfa.hasCtxMoves = true;
tab.MakeSequence(g, g2); .)
')'
]
.
/*------------------------------------------------------------------------------------*/
TokenFactor<out Graph g> (. string name; int kind; .)
=
(. g = null; .)
( Sym<out name, out kind> (. if (kind == id) {
CharClass c = tab.FindCharClass(name);
if (c == null) {
SemErr("undefined name");
c = tab.NewCharClass(name, new CharSet());
}
Node p = tab.NewNode(Node.clas, null, 0); p.val = c.n;
g = new Graph(p);
tokenString = noString;
} else { // str
g = tab.StrToGraph(name);
if (tokenString == null) tokenString = name;
else tokenString = noString;
}
.)
| '(' TokenExpr<out g> ')'
| '[' TokenExpr<out g> ']' (. tab.MakeOption(g); tokenString = noString; .)
| '{' TokenExpr<out g> '}' (. tab.MakeIteration(g); tokenString = noString; .)
) (. if (g == null) // invalid start of TokenFactor
g = new Graph(tab.NewNode(Node.eps, null, 0)); .)
.
/*------------------------------------------------------------------------------------*/
Sym<out string name, out int kind>
= (. name = "???"; kind = id; .)
( ident (. kind = id; name = t.val; .)
| (string (. name = t.val; .)
| char (. name = "\"" + t.val.Substring(1, t.val.Length-2) + "\""; .)
) (. kind = str;
if (dfa.ignoreCase) name = name.ToLower();
if (name.IndexOf(' ') >= 0)
SemErr("literal tokens must not contain blanks"); .)
)
.
/*------------------------------------------------------------------------------------*/
Attribs<Node p>
=
'<' (. int beg = la.pos; int col = la.col; int line = la.line; .)
{ ANY
| badString (. SemErr("bad string in attributes"); .)
}
'>' (. if (t.pos > beg) p.pos = new Position(beg, t.pos, col, line); .)
| "<." (. int beg = la.pos; int col = la.col; int line = la.line; .)
{ ANY
| badString (. SemErr("bad string in attributes"); .)
}
".>" (. if (t.pos > beg) p.pos = new Position(beg, t.pos, col, line); .)
.
/*------------------------------------------------------------------------------------*/
SemText<out Position pos>
=
"(." (. int beg = la.pos; int col = la.col; int line = la.line; .)
{ ANY
| badString (. SemErr("bad string in semantic action"); .)
| "(." (. SemErr("missing end of previous semantic action"); .)
}
".)" (. pos = new Position(beg, t.pos, col, line); .)
.
END Coco.