| 1 |
|
| 2 |
record Token(type,body,line,column) |
| 3 |
|
| 4 |
link readll1 |
| 5 |
|
| 6 |
procedure parseLL1(ll1) |
| 7 |
local predictionStack |
| 8 |
local x,y,z,top,cur |
| 9 |
predictionStack:=[ll1.start,ll1.eoi] |
| 10 |
cur := scan() |
| 11 |
repeat { |
| 12 |
if not(top := pop(predictionStack)) then return |
| 13 |
if top == cur.type then { |
| 14 |
outToken(cur) |
| 15 |
if top == ll1.eoi then break |
| 16 |
cur := scan() |
| 17 |
} else if member(ll1.actions,top) then { |
| 18 |
outAction(top) |
| 19 |
} else if x:=\ll1.sel[top] & y:=\x[cur.type] then { |
| 20 |
every z:=y[*y to 1 by -1] do push(predictionStack,z) |
| 21 |
} else if y:=\ll1.deflt[top] then { |
| 22 |
every z:=y[*y to 1 by -1] do push(predictionStack,z) |
| 23 |
} else { |
| 24 |
#panic mode error recovery |
| 25 |
reportParseError(cur) |
| 26 |
push(predictionStack,top) |
| 27 |
repeat { |
| 28 |
while not member(ll1.fiducials,cur.type) & |
| 29 |
cur.type~==ll1.eoi do { |
| 30 |
#write("scanning past ",cur.body) |
| 31 |
cur := scan() |
| 32 |
} |
| 33 |
if x:=!predictionStack & |
| 34 |
(x==cur.type) | |
| 35 |
member(\ll1.firstFiducials[x], cur.type) |
| 36 |
then break |
| 37 |
else cur := scan() |
| 38 |
} |
| 39 |
repeat { |
| 40 |
top := pop(predictionStack) | |
| 41 |
stop("system error in panic mode") |
| 42 |
#write("pruning stack ",top) |
| 43 |
if top==cur.type then { |
| 44 |
push(predictionStack,top) |
| 45 |
break |
| 46 |
} |
| 47 |
if member(ll1.actions,top) then { |
| 48 |
outAction(top) |
| 49 |
} else if member(ll1.terminals,top) then { |
| 50 |
outError(top) |
| 51 |
} else if member(\ll1.firstFiducials[top],cur.type) |
| 52 |
then { |
| 53 |
push(predictionStack,top) |
| 54 |
break |
| 55 |
} else { |
| 56 |
predictionStack := ll1.minLengRHS[top] ||| |
| 57 |
predictionStack |
| 58 |
} |
| 59 |
} |
| 60 |
} |
| 61 |
} |
| 62 |
return |
| 63 |
end |
| 64 |
# |
| 65 |
# Copyright (C) 1994, T.W. Christopher and G.K. Thiruvathukal. |
| 66 |
# All rights reserved. The use of TLC is governed by conditions |
| 67 |
# similar to GNU Copyleft. Please consult the files distributed |
| 68 |
# with TLC for more information: COPYLEFT, WARRANTY, and README. |
| 69 |
# If the aforementioned files are missing, you can obtain them |
| 70 |
# from {tc,gkt}@iitmax.acc.iit.edu. |
| 71 |
# |
| 72 |
# |