| 1 |
# Read in parse tables produced by TCLL1 |
| 2 |
# (written by Thomas W. Christopher) |
| 3 |
# |
| 4 |
link xcode #xcode is provided by the Icon Programming Library |
| 5 |
invocable all |
| 6 |
|
| 7 |
record LL1(sel,deflt, |
| 8 |
terminals,actions, |
| 9 |
fiducials,firstFiducials, |
| 10 |
minLengRHS, |
| 11 |
start,eoi) |
| 12 |
|
| 13 |
procedure readLL1(filename) |
| 14 |
local g,s,f |
| 15 |
f:=open(filename) | fail |
| 16 |
s:=xdecode(f) | fail |
| 17 |
g:=unpackLL1(s) |
| 18 |
close(f) |
| 19 |
return g |
| 20 |
end |
| 21 |
|
| 22 |
procedure unpackLL1(h) |
| 23 |
local startSymbol, |
| 24 |
eoiSymbol, |
| 25 |
rhsList, |
| 26 |
selIn, |
| 27 |
defltIn, |
| 28 |
termList, |
| 29 |
actionList, |
| 30 |
fiducList, |
| 31 |
firstFiduc, |
| 32 |
minLengRhs |
| 33 |
|
| 34 |
local r,i,n,t,s, |
| 35 |
actionSet,terminalSet, |
| 36 |
defaultTable,selTable, |
| 37 |
fiducialSet,firstFiducials, |
| 38 |
minLengRHS |
| 39 |
|
| 40 |
# the following must be in the same order they were listed in |
| 41 |
# return statement of genLL1() in module "ll1.icn". With the |
| 42 |
# exception of rhsList, they are in the same order as in record |
| 43 |
# LL1. |
| 44 |
|
| 45 |
rhsList := get(h) |
| 46 |
selIn := get(h) |
| 47 |
defltIn := get(h) |
| 48 |
termList:= get(h) |
| 49 |
actionList:=get(h) |
| 50 |
fiducList:=get(h) |
| 51 |
firstFiduc:=get(h) |
| 52 |
minLengRhs:=get(h) |
| 53 |
startSymbol := get(h)[1] |
| 54 |
eoiSymbol := get(h)[1] |
| 55 |
|
| 56 |
every r:= !rhsList & i := 1 to *r do r[i]:=r[i][1] |
| 57 |
|
| 58 |
actionSet:=set() |
| 59 |
every insert(actionSet,(!actionList)[1]) |
| 60 |
terminalSet:=set() |
| 61 |
every insert(terminalSet,(!termList)[1]) |
| 62 |
defaultTable:=table() |
| 63 |
every n:=key(defltIn) do defaultTable[n[1]]:=defltIn[n] |
| 64 |
selTable:=table() |
| 65 |
every n:=key(selIn) do { |
| 66 |
/selTable[n[1]] := t := table() |
| 67 |
every s:= key(selIn[n]) do { |
| 68 |
t[s[1]] := selIn[n][s] |
| 69 |
} |
| 70 |
} |
| 71 |
fiducialSet:=set() |
| 72 |
every insert(fiducialSet,(!fiducList)[1]) |
| 73 |
firstFiducials:=table() |
| 74 |
every n:=key(firstFiduc) & |
| 75 |
s:=firstFiduc[n] do { |
| 76 |
firstFiducials[n[1]]:=set() |
| 77 |
every insert(firstFiducials[n[1]],(!s)[1]) |
| 78 |
} |
| 79 |
minLengRHS:=table() |
| 80 |
every n:=key(minLengRhs) do |
| 81 |
minLengRHS[n[1]]:=minLengRhs[n] |
| 82 |
|
| 83 |
return LL1(selTable,defaultTable, |
| 84 |
terminalSet,actionSet, |
| 85 |
fiducialSet,firstFiducials, |
| 86 |
minLengRHS, |
| 87 |
startSymbol,eoiSymbol) |
| 88 |
|
| 89 |
end |
| 90 |
|
| 91 |
procedure showStructure(h, indent) |
| 92 |
local t,i |
| 93 |
/indent:="" |
| 94 |
i := indent||" " |
| 95 |
case type(h) of { |
| 96 |
"string": write(indent,"\"",h,"\"") |
| 97 |
"list": {write(indent,"[") |
| 98 |
every showStructure(!h,i) |
| 99 |
write(indent,"]") |
| 100 |
} |
| 101 |
"table":{write(indent,"table") |
| 102 |
t := sort(h,3) |
| 103 |
while showStructure(get(t),i) do { |
| 104 |
write(indent,"->") |
| 105 |
showStructure(get(t),i) |
| 106 |
write(indent,"---") |
| 107 |
} |
| 108 |
write(indent,"end table") |
| 109 |
} |
| 110 |
"set": {write(indent,"{") |
| 111 |
every showStructure(!h,i) |
| 112 |
write(indent,"}") |
| 113 |
} |
| 114 |
} |
| 115 |
return |
| 116 |
end |
| 117 |
|
| 118 |
procedure showLL1(g) |
| 119 |
write("start symbol") |
| 120 |
showStructure( g.start) |
| 121 |
write("eoi symbol") |
| 122 |
showStructure( g.eoi) |
| 123 |
write("action set") |
| 124 |
showStructure( g.actions) |
| 125 |
write("terminal set") |
| 126 |
showStructure( g.terminals) |
| 127 |
write("default table") |
| 128 |
showStructure( g.deflt) |
| 129 |
write("selection table") |
| 130 |
showStructure( g.sel) |
| 131 |
write("fiducial set") |
| 132 |
showStructure( g.fiducials) |
| 133 |
write("first fiducials") |
| 134 |
showStructure( g.firstFiducials) |
| 135 |
write("minimum length RHSs") |
| 136 |
showStructure( g.minLengRHS) |
| 137 |
return |
| 138 |
end |
| 139 |
|
| 140 |
|