-
Notifications
You must be signed in to change notification settings - Fork 0
/
printtree.p
150 lines (136 loc) · 3.07 KB
/
printtree.p
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
procedure printList (aset : setref);
{ Writes out a set like:
elem1, elem2, elem3
}
begin
if aset <> nil then begin
with aset^ do begin
{first elem:}
printsymbol (output, tokenkind[contents], contents);
aset := nextelem;
end; {with}
{rest of elems:}
while aset <> nil do
with aset^ do begin
write (output, ', ');
printsymbol (output, tokenkind[contents], contents);
aset := nextelem;
end; {with-while}
write (output, ' ');
end; {if}
end; {printList}
procedure printSet (aset : setref);
begin
write (output, '[ ');
printList (aset);
write (output, ']');
end; {printSet}
procedure writeKind (p : nodeptr);
begin
with p^ do
case kind of
grammarkind :
write (output, '<$> ');
rulekind :
write (output, '<:> ');
termornonterm :
;
epsilon :
write (output, '<> ');
zeroormore :
write (output, '<*> ');
oneormore :
write (output, '<+> ');
condoneormore :
write (output, '</> ');
optional :
write (output, '<?> ');
factorsequence :
write (output, '<.> ');
alternative :
write (output, '<|> ');
illegal :
write (output, '@ ');
end; {case-with}
end; {writeKind}
procedure printtree (nodep : nodeptr; indent : cardinal);
var
p : nodeptr;
procedure writeState (nodep : nodeptr);
begin
with nodep^ do begin
write (output, '(');
case state of
yes : write (output, 'y');
no : write (output, 'n');
undecided : write (output, 'u');
end; {case}
write (output, ') ');
if kind = termornonterm then begin
if tokenkind[nameid] {= nonterm} then
if firstdet then printSet (firstset) else write (output, '?');
{else no action for terminal}
end
else
if firstdet then printSet (firstset) else write (output, '?');
end; {with}
end; {writeState}
begin {printtree}
if nodep <> nil then
with nodep^ do begin
if indent > 0 then
write (output, ' ' : indent);
writeKind (nodep);
case kind of
grammarkind :
begin
writeState (nodep);
writeln (output);
p := altp;
while p <> nil do begin
writeln (output);
printtree (p, indent + 3);
p := p^.succp;
end; {while}
end;
rulekind :
begin
printsymbol (output, nonterm, nameid);
write (output, ' ');
writeState (nodep);
write (output, ' ');
printSet (tokeninfo[nameid].followset);
writeln (output);
printtree (altp, indent + 3);
end;
termornonterm :
begin
printsymbol (output, tokenkind[nameid], nameid);
write (output, ' ');
writeState (nodep);
writeln (output);
end;
illegal,
epsilon :
begin
writeState (nodep);
writeln (output);
end;
optional,
oneormore,
zeroormore,
condoneormore,
alternative,
factorsequence :
begin
writeState (nodep);
writeln (output);
p := altp;
repeat
printtree (p, indent + 3);
p := p^.succp;
until p = nil;
end;
end; {case}
end; {with-if}
end; {printtree}