Debian Codegraph @

ipl/packs/tcll1/parsell1.icn

raw
1 # parse using tables produced by tcLL1
2 # (written by Dr. Thomas W. Christopher)
3 #
4 record Token(type,body,line,column)
5
6 link readll1
7
8 procedure parseLL1(ll1)
9 local predictionStack
10 local x,y,z,top,cur,errLine,errColumn
11 predictionStack:=[ll1.start,ll1.eoi]
12 cur := &null
13 repeat {
14 if not(top := pop(predictionStack)) then return
15 while member(ll1.actions,top) do {
16 outAction(top)
17 if not(top := pop(predictionStack)) then return
18 }
19 /cur := scan()
20 if top == cur.type then {
21 outToken(cur)
22 cur:=&null
23 if top == ll1.eoi then break
24 } else if x:=\ll1.sel[top] & y:=\x[cur.type] then {
25 every z:=y[*y to 1 by -1] do push(predictionStack,z)
26 } else if y:=\ll1.deflt[top] then {
27 every z:=y[*y to 1 by -1] do push(predictionStack,z)
28 } else {
29 #panic mode error recovery
30 reportParseError(cur)
31 errLine:=cur.line
32 errColumn:=cur.column
33 push(predictionStack,top)
34 repeat {
35 while not member(ll1.fiducials,cur.type) &
36 cur.type~==ll1.eoi do {
37 #write("scanning past ",cur.body)
38 cur := scan()
39 }
40 if x:=!predictionStack &
41 (x==cur.type) |
42 member(\ll1.firstFiducials[x], cur.type)
43 then break
44 else cur := scan()
45 }
46 repeat {
47 top := pop(predictionStack) |
48 stop("system error in panic mode")
49 #write("pruning stack ",top)
50 if top==cur.type then {
51 push(predictionStack,top)
52 break
53 }
54 if member(ll1.actions,top) then {
55 outAction(top)
56 } else if member(ll1.terminals,top) then {
57 outError(top,errLine,errColumn)
58 } else if member(\ll1.firstFiducials[top],cur.type)
59 then {
60 push(predictionStack,top)
61 break
62 } else {
63 predictionStack := ll1.minLengRHS[top] |||
64 predictionStack
65 }
66 }
67 }
68 }
69 return
70 end
71