summaryrefslogtreecommitdiff
path: root/ipl/packs/tcll1/readll1.icn
blob: b1f42b032f20f9ff644894216dd21a686d06b699 (plain)
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
# Read in parse tables produced by TCLL1
#	(written by Thomas W. Christopher)
#
link xcode #xcode is provided by the Icon Programming Library
invocable all

record  LL1(sel,deflt,
	terminals,actions,
	fiducials,firstFiducials,
	minLengRHS,
	start,eoi)

procedure readLL1(filename)
local g,s,f
f:=open(filename) | fail
s:=xdecode(f) | fail
g:=unpackLL1(s)
close(f)
return g
end

procedure unpackLL1(h)
local	startSymbol,
	eoiSymbol,
	rhsList,
	selIn,
	defltIn,
	termList,
	actionList,
	fiducList,
	firstFiduc,
	minLengRhs

local r,i,n,t,s,
 actionSet,terminalSet,
 defaultTable,selTable,
 fiducialSet,firstFiducials,
 minLengRHS

# the following must be in the same order they were listed in
# return statement of genLL1() in module "ll1.icn". With the
# exception of rhsList, they are in the same order as in record
# LL1.

rhsList := get(h)
selIn := get(h)
defltIn := get(h)
termList:= get(h)
actionList:=get(h)
fiducList:=get(h)
firstFiduc:=get(h)
minLengRhs:=get(h)
startSymbol := get(h)[1]
eoiSymbol := get(h)[1]

every r:= !rhsList & i := 1 to *r do r[i]:=r[i][1]

actionSet:=set()
every insert(actionSet,(!actionList)[1])
terminalSet:=set()
every insert(terminalSet,(!termList)[1])
defaultTable:=table()
every n:=key(defltIn) do defaultTable[n[1]]:=defltIn[n]
selTable:=table()
every n:=key(selIn) do {
	/selTable[n[1]] := t := table()
	every s:= key(selIn[n]) do {
		t[s[1]] := selIn[n][s]
	}
}
fiducialSet:=set()
every insert(fiducialSet,(!fiducList)[1])
firstFiducials:=table()
every n:=key(firstFiduc) &
	s:=firstFiduc[n] do {
		firstFiducials[n[1]]:=set()
		every insert(firstFiducials[n[1]],(!s)[1])
}
minLengRHS:=table()
every n:=key(minLengRhs) do
	minLengRHS[n[1]]:=minLengRhs[n]

return LL1(selTable,defaultTable,
	terminalSet,actionSet,
	fiducialSet,firstFiducials,
	minLengRHS,
	startSymbol,eoiSymbol)

end

procedure showStructure(h, indent)
local t,i
/indent:=""
i := indent||" "
case type(h) of {
"string": write(indent,"\"",h,"\"")
"list":	{write(indent,"[")
	every showStructure(!h,i)
	write(indent,"]")
	}
"table":{write(indent,"table")
	t := sort(h,3)
	while showStructure(get(t),i) do {
		write(indent,"->")
		showStructure(get(t),i)
		write(indent,"---")
	  }
	write(indent,"end table")
	}
"set": {write(indent,"{")
	every showStructure(!h,i)
	write(indent,"}")
	}
}
return
end

procedure showLL1(g)
write("start symbol")
showStructure( g.start)
write("eoi symbol")
showStructure( g.eoi)
write("action set")
showStructure( g.actions)
write("terminal set")
showStructure( g.terminals)
write("default table")
showStructure( g.deflt)
write("selection table")
showStructure( g.sel)
write("fiducial set")
showStructure( g.fiducials)
write("first fiducials")
showStructure( g.firstFiducials)
write("minimum length RHSs")
showStructure( g.minLengRHS)
return
end