1
|
1 |
"{ Package: 'stx:goodies/smaCC' }"
|
|
2 |
|
|
3 |
"{ NameSpace: SmaCC }"
|
|
4 |
|
|
5 |
Set subclass:#SmaCCItemSet
|
|
6 |
instanceVariableNames:'type derivedFrom id'
|
|
7 |
classVariableNames:''
|
|
8 |
poolDictionaries:''
|
|
9 |
category:'SmaCC-Parser Generator'
|
|
10 |
!
|
|
11 |
|
|
12 |
SmaCCItemSet comment:'SmaCCItemSet represents a LALR(1) or a LR(1) item set.
|
|
13 |
|
|
14 |
Instance Variables:
|
|
15 |
type <Symbol> #LALR1 or #LR1
|
|
16 |
|
|
17 |
'
|
|
18 |
!
|
|
19 |
|
|
20 |
|
|
21 |
!SmaCCItemSet class methodsFor:'instance creation'!
|
|
22 |
|
|
23 |
new: anInteger
|
|
24 |
^(super new: anInteger)
|
|
25 |
initialize;
|
|
26 |
yourself
|
|
27 |
! !
|
|
28 |
|
|
29 |
!SmaCCItemSet methodsFor:'accessing'!
|
|
30 |
|
|
31 |
action: aGrammarSymbol prefer: aClass
|
|
32 |
| action actionItem eachAction |
|
|
33 |
action := SmaCCRejectAction new.
|
|
34 |
self do:
|
|
35 |
[:each |
|
|
36 |
eachAction := each action: aGrammarSymbol.
|
|
37 |
action class = SmaCCRejectAction
|
|
38 |
ifTrue:
|
|
39 |
[action := eachAction.
|
|
40 |
actionItem := each]
|
|
41 |
ifFalse:
|
|
42 |
[(eachAction = action or: [eachAction class = SmaCCRejectAction])
|
|
43 |
ifFalse:
|
|
44 |
[eachAction class = SmaCCShiftAction | (action class = SmaCCShiftAction)
|
|
45 |
ifTrue:
|
|
46 |
["Shift/Reduce conflict -- check precedence"
|
|
47 |
|
|
48 |
(aGrammarSymbol precedence isNil
|
|
49 |
or: [each precedence isNil or: [actionItem precedence isNil]])
|
|
50 |
ifTrue:
|
|
51 |
["No precedence, choose shift"
|
|
52 |
|
|
53 |
eachAction class = SmaCCShiftAction
|
|
54 |
ifTrue:
|
|
55 |
[self
|
|
56 |
raiseXReduceNotification: each
|
|
57 |
with: actionItem
|
|
58 |
type: 'Shift/Reduce Conflict'.
|
|
59 |
action := eachAction.
|
|
60 |
actionItem := each]
|
|
61 |
ifFalse:
|
|
62 |
[self
|
|
63 |
raiseXReduceNotification: actionItem
|
|
64 |
with: each
|
|
65 |
type: 'Shift/Reduce Conflict']]
|
|
66 |
ifFalse:
|
|
67 |
[(each precedence > actionItem precedence
|
|
68 |
or: [each precedence = actionItem precedence and: [eachAction class = aClass]])
|
|
69 |
ifTrue:
|
|
70 |
[self
|
|
71 |
raiseXReduceNotification: each
|
|
72 |
with: actionItem
|
|
73 |
type: 'Shift/Reduce Conflict (handled by precedence rules)'.
|
|
74 |
action := eachAction.
|
|
75 |
actionItem := each]
|
|
76 |
ifFalse:
|
|
77 |
[(each precedence = actionItem precedence and: [SmaCCRejectAction = aClass])
|
|
78 |
ifTrue:
|
|
79 |
[self
|
|
80 |
raiseXReduceNotificationWithNoPreferredItem: actionItem
|
|
81 |
with: each
|
|
82 |
type: 'Shift/Reduce Conflict (handled by precedence rules)'.
|
|
83 |
^aClass new]
|
|
84 |
ifFalse:
|
|
85 |
[self
|
|
86 |
raiseXReduceNotification: actionItem
|
|
87 |
with: each
|
|
88 |
type: 'Shift/Reduce Conflict (handled by precedence rules)']]]]
|
|
89 |
ifFalse:
|
|
90 |
["Reduce/Reduce conflict -- just pick one and proceed"
|
|
91 |
|
|
92 |
self
|
|
93 |
raiseXReduceNotification: actionItem
|
|
94 |
with: each
|
|
95 |
type: 'Reduce/Reduce Conflict']]]].
|
|
96 |
^action
|
|
97 |
!
|
|
98 |
|
|
99 |
derivedFrom
|
|
100 |
^ derivedFrom
|
|
101 |
|
|
102 |
"Created: / 14-02-2008 / 13:26:17 / janfrog"
|
|
103 |
!
|
|
104 |
|
|
105 |
derivedFrom:something
|
|
106 |
derivedFrom := something.
|
|
107 |
|
|
108 |
"Created: / 14-02-2008 / 13:26:17 / janfrog"
|
|
109 |
!
|
|
110 |
|
|
111 |
id
|
|
112 |
^ id
|
|
113 |
|
|
114 |
"Created: / 14-02-2008 / 13:28:54 / janfrog"
|
|
115 |
!
|
|
116 |
|
|
117 |
id:something
|
|
118 |
id := something.
|
|
119 |
|
|
120 |
"Created: / 14-02-2008 / 13:28:54 / janfrog"
|
|
121 |
!
|
|
122 |
|
|
123 |
mergeWith: anItemSet
|
|
124 |
self == anItemSet ifTrue: [^self].
|
|
125 |
anItemSet do: [:each | self add: each]
|
|
126 |
!
|
|
127 |
|
|
128 |
moveOn: aGrammarSymbol
|
|
129 |
| anItemSet |
|
|
130 |
anItemSet := self class new.
|
|
131 |
anItemSet type: type.
|
|
132 |
anItemSet derivedFrom: self.
|
|
133 |
self
|
|
134 |
do: [:each | each nextSymbol == aGrammarSymbol ifTrue: [anItemSet add: each moveNext]].
|
|
135 |
^anItemSet
|
|
136 |
|
|
137 |
"Modified: / 14-02-2008 / 13:26:51 / janfrog"
|
|
138 |
!
|
|
139 |
|
|
140 |
raiseXReduceNotification: preferredItem with: otherItem type: aString
|
|
141 |
| stream |
|
|
142 |
stream := WriteStream on: (String new: 200).
|
|
143 |
stream
|
|
144 |
nextPutAll: preferredItem printString;
|
|
145 |
nextPutAll: ' *****';
|
|
146 |
cr;
|
|
147 |
nextPutAll: otherItem printString.
|
|
148 |
Smalltalk isSmalltalkX ifTrue:[
|
|
149 |
SmaCCCompilationNotification raiseSignal: aString with: stream contents
|
|
150 |
] ifFalse:[
|
|
151 |
SmaCCCompilationNotification signal: aString with: stream contents
|
|
152 |
]
|
|
153 |
|
|
154 |
!
|
|
155 |
|
|
156 |
raiseXReduceNotificationWithNoPreferredItem: firstItem with: secondItem type: aString
|
|
157 |
| stream |
|
|
158 |
stream := WriteStream on: (String new: 200).
|
|
159 |
stream
|
|
160 |
nextPutAll: firstItem printString;
|
|
161 |
cr;
|
|
162 |
nextPutAll: secondItem printString.
|
|
163 |
Smalltalk isSmalltalkX ifTrue:[
|
|
164 |
SmaCCCompilationNotification raiseSignal: aString with: stream contents
|
|
165 |
] ifFalse:[
|
|
166 |
SmaCCCompilationNotification signal: aString with: stream contents
|
|
167 |
]
|
|
168 |
|
|
169 |
! !
|
|
170 |
|
|
171 |
!SmaCCItemSet methodsFor:'adding'!
|
|
172 |
|
|
173 |
add: aLR1Item
|
|
174 |
| index item |
|
|
175 |
aLR1Item == nil ifTrue: [^aLR1Item].
|
|
176 |
Smalltalk isSmalltalkX ifTrue:[
|
|
177 |
index := self findKeyOrNil: aLR1Item.
|
|
178 |
item := keyArray basicAt: index.
|
|
179 |
item == nil
|
|
180 |
ifTrue:
|
|
181 |
[keyArray basicAt: index put: aLR1Item.
|
|
182 |
tally := tally + 1.
|
|
183 |
self performClosureOn: aLR1Item]
|
|
184 |
ifFalse: [item mergeWith: aLR1Item].
|
|
185 |
] ifFalse:[
|
|
186 |
index := self findElementOrNil: aLR1Item.
|
|
187 |
item := self basicAt: index.
|
|
188 |
item == nil
|
|
189 |
ifTrue:
|
|
190 |
[self privateAt: index put: aLR1Item.
|
|
191 |
self performClosureOn: aLR1Item]
|
|
192 |
ifFalse: [item mergeWith: aLR1Item].
|
|
193 |
].
|
|
194 |
^aLR1Item
|
|
195 |
! !
|
|
196 |
|
|
197 |
!SmaCCItemSet methodsFor:'comparing'!
|
|
198 |
|
|
199 |
= anItemSet
|
|
200 |
self class = anItemSet class
|
|
201 |
ifTrue:
|
|
202 |
[type == #LALR1
|
|
203 |
ifTrue: [^self isLALREqualTo: anItemSet]
|
|
204 |
ifFalse: [^self isLREqualTo: anItemSet]].
|
|
205 |
^false
|
|
206 |
!
|
|
207 |
|
|
208 |
hash
|
|
209 |
^self inject: self class hash into: [:sum :each | sum bitXor: each hash]
|
|
210 |
! !
|
|
211 |
|
|
212 |
!SmaCCItemSet methodsFor:'copying'!
|
|
213 |
|
|
214 |
copyEmpty: aSize
|
|
215 |
^(super copyEmpty: aSize)
|
|
216 |
type: type;
|
|
217 |
yourself
|
|
218 |
! !
|
|
219 |
|
|
220 |
!SmaCCItemSet methodsFor:'initialize-release'!
|
|
221 |
|
|
222 |
initialize
|
|
223 |
(self class superclass canUnderstand: #initialize)
|
|
224 |
ifTrue: [super initialize].
|
|
225 |
type := #LALR1
|
|
226 |
!
|
|
227 |
|
|
228 |
type: aSymbol
|
|
229 |
type := aSymbol
|
|
230 |
! !
|
|
231 |
|
|
232 |
!SmaCCItemSet methodsFor:'printing'!
|
|
233 |
|
|
234 |
printOn:aStream
|
|
235 |
id ifNotNil:
|
|
236 |
[aStream nextPutAll: id printString asText allBold].
|
|
237 |
derivedFrom ifNotNil:
|
|
238 |
[derivedFrom id ifNotNil:
|
|
239 |
[aStream
|
|
240 |
nextPutAll:' (derived from ';
|
|
241 |
nextPutAll:derivedFrom id printString asText allBold;
|
|
242 |
nextPut:$)]].
|
|
243 |
aStream cr.
|
|
244 |
|
|
245 |
self do:[:each |
|
|
246 |
aStream
|
|
247 |
tab;
|
|
248 |
nextPutAll:each printString;
|
|
249 |
cr
|
|
250 |
]
|
|
251 |
|
|
252 |
"Modified: / 14-02-2008 / 13:34:50 / janfrog"
|
|
253 |
! !
|
|
254 |
|
|
255 |
!SmaCCItemSet methodsFor:'private'!
|
|
256 |
|
|
257 |
itemFor: anObject
|
|
258 |
Smalltalk isSmalltalkX ifTrue:[
|
|
259 |
^keyArray basicAt: (self findKeyOrNil: anObject)
|
|
260 |
].
|
|
261 |
^self basicAt: (self findElementOrNil: anObject)
|
|
262 |
!
|
|
263 |
|
|
264 |
performClosureOn: aLR1Item
|
|
265 |
| nextSymbol rest first followers |
|
|
266 |
nextSymbol := aLR1Item nextSymbol.
|
|
267 |
nextSymbol isTerminal ifTrue: [^self].
|
|
268 |
rest := aLR1Item rest.
|
|
269 |
first := rest firstTerminals.
|
|
270 |
followers := SmaCCSymbolSet new.
|
|
271 |
(first includes: SmaCCSymbol empty)
|
|
272 |
ifTrue:
|
|
273 |
[first remove: SmaCCSymbol empty.
|
|
274 |
followers baseOn: aLR1Item followers].
|
|
275 |
followers addAll: first.
|
|
276 |
nextSymbol productionsDo:
|
|
277 |
[:each |
|
|
278 |
self add: (SmaCCLR1Item
|
|
279 |
symbol: nextSymbol
|
|
280 |
rhs: each
|
|
281 |
follow: followers)]
|
|
282 |
! !
|
|
283 |
|
|
284 |
!SmaCCItemSet methodsFor:'public'!
|
|
285 |
|
|
286 |
isLALREqualTo: anItemSet
|
|
287 |
^anItemSet size = self size
|
|
288 |
and: [self allSatisfy: [:each | anItemSet includes: each]]
|
|
289 |
!
|
|
290 |
|
|
291 |
isLREqualTo: anItemSet
|
|
292 |
^anItemSet size = self size and:
|
|
293 |
[self allSatisfy:
|
|
294 |
[:each |
|
|
295 |
| item |
|
|
296 |
(item := anItemSet itemFor: each) notNil and: [each isLR1EqualTo: item]]]
|
|
297 |
! !
|
|
298 |
|
|
299 |
!SmaCCItemSet class methodsFor:'documentation'!
|
|
300 |
|
|
301 |
version
|
|
302 |
^ '$Header: /opt/data/cvs/stx/goodies/smaCC/SmaCC__SmaCCItemSet.st,v 1.2 2008-02-17 10:30:37 vranyj1 Exp $'
|
15
|
303 |
!
|
|
304 |
|
|
305 |
version_SVN
|
|
306 |
^ '$Id$'
|
1
|
307 |
! !
|