1
|
1 |
"{ Package: 'stx:goodies/smaCC' }"
|
|
2 |
|
|
3 |
"{ NameSpace: SmaCC }"
|
|
4 |
|
|
5 |
Object subclass:#SmaCCLR1Item
|
|
6 |
instanceVariableNames:'rhs symbol location followers'
|
|
7 |
classVariableNames:''
|
|
8 |
poolDictionaries:''
|
|
9 |
category:'SmaCC-Parser Generator'
|
|
10 |
!
|
|
11 |
|
|
12 |
SmaCCLR1Item comment:'SmaCCLR1Item represents an item in a SmaCCItemSet.
|
|
13 |
|
|
14 |
Instance Variables:
|
|
15 |
followers <SmaCCSymbolSet> all possible symbols that can follow this production in this state
|
|
16 |
location <Integer> the position in the rhs of the production we are at
|
|
17 |
rhs <SmaCCRHS> the rhs of the production
|
|
18 |
symbol <SmaCCSymbol> the symbol for the production'
|
|
19 |
!
|
|
20 |
|
|
21 |
|
|
22 |
!SmaCCLR1Item class methodsFor:'instance creation'!
|
|
23 |
|
|
24 |
symbol: productionGrammarSymbol rhs: aRHS follow: aGrammarSymbolCollection
|
|
25 |
^(self new)
|
|
26 |
symbol: productionGrammarSymbol
|
|
27 |
rhs: aRHS
|
|
28 |
follow: aGrammarSymbolCollection;
|
|
29 |
yourself
|
|
30 |
! !
|
|
31 |
|
|
32 |
!SmaCCLR1Item methodsFor:'accessing'!
|
|
33 |
|
|
34 |
action: aGrammarSymbol
|
|
35 |
location > rhs size
|
|
36 |
ifTrue:
|
|
37 |
[(symbol isStartingSymbol and: [aGrammarSymbol isEmptySymbol])
|
|
38 |
ifTrue: [^SmaCCAcceptAction new].
|
|
39 |
(followers includes: aGrammarSymbol)
|
|
40 |
ifTrue: [^SmaCCReduceAction symbol: symbol rhs: rhs]]
|
|
41 |
ifFalse: [(rhs at: location) = aGrammarSymbol ifTrue: [^SmaCCShiftAction new]].
|
|
42 |
^SmaCCRejectAction new
|
|
43 |
!
|
|
44 |
|
|
45 |
followers
|
|
46 |
^followers
|
|
47 |
!
|
|
48 |
|
|
49 |
mergeWith: aLRItem
|
|
50 |
followers mergeWith: aLRItem followers
|
|
51 |
!
|
|
52 |
|
|
53 |
moveNext
|
|
54 |
^(self copy)
|
|
55 |
followers: (followers class basedOn: followers);
|
|
56 |
location: location + 1;
|
|
57 |
yourself
|
|
58 |
!
|
|
59 |
|
|
60 |
nextSymbol
|
|
61 |
^location > rhs size
|
|
62 |
ifTrue: [SmaCCSymbol sentinel]
|
|
63 |
ifFalse: [rhs at: location]
|
|
64 |
!
|
|
65 |
|
|
66 |
precedence
|
|
67 |
"If we don't have a precedence, then assume we are the lowest precedence (which will cause a shift action)."
|
|
68 |
|
|
69 |
| max |
|
|
70 |
max := -1.
|
|
71 |
rhs do: [:each | max := max max: (each precedence ifNil: [max])].
|
|
72 |
^max
|
|
73 |
!
|
|
74 |
|
|
75 |
rest
|
|
76 |
| newRHS |
|
|
77 |
newRHS := SmaCCRHS new.
|
|
78 |
location + 1 to: rhs size do: [:each | newRHS add: (rhs at: each)].
|
|
79 |
^newRHS
|
|
80 |
! !
|
|
81 |
|
|
82 |
!SmaCCLR1Item methodsFor:'comparing'!
|
|
83 |
|
|
84 |
= anItem
|
|
85 |
^self class == anItem class and:
|
|
86 |
[location == anItem location
|
|
87 |
and: [rhs == anItem rhs and: [symbol == anItem symbol]]]
|
|
88 |
!
|
|
89 |
|
|
90 |
hash
|
|
91 |
^(symbol identityHash bitXor: (location bitShift: 14))
|
|
92 |
bitXor: rhs identityHash
|
|
93 |
! !
|
|
94 |
|
|
95 |
!SmaCCLR1Item methodsFor:'initialize-release'!
|
|
96 |
|
|
97 |
followers: aSymbolSet
|
|
98 |
followers := aSymbolSet
|
|
99 |
!
|
|
100 |
|
|
101 |
location: anInteger
|
|
102 |
location := anInteger
|
|
103 |
!
|
|
104 |
|
|
105 |
symbol: productionGrammarSymbol rhs: aRHS follow: aSymbolCollection
|
|
106 |
symbol := productionGrammarSymbol.
|
|
107 |
rhs := aRHS.
|
|
108 |
location := 1.
|
|
109 |
followers := aSymbolCollection
|
|
110 |
! !
|
|
111 |
|
|
112 |
!SmaCCLR1Item methodsFor:'printing'!
|
|
113 |
|
|
114 |
printOn: aStream
|
|
115 |
| i |
|
|
116 |
aStream
|
|
117 |
nextPut: $[;
|
|
118 |
nextPutAll: symbol printString;
|
|
119 |
nextPutAll: ' :'.
|
|
120 |
i := 1.
|
|
121 |
rhs do:
|
|
122 |
[:each |
|
|
123 |
i = location ifTrue: [aStream nextPutAll: ' . ' asText allBold].
|
|
124 |
aStream
|
|
125 |
space;
|
|
126 |
nextPutAll: each printString.
|
|
127 |
i := i + 1].
|
|
128 |
location > rhs size ifTrue: [aStream nextPutAll: ' . ' asText allBold].
|
|
129 |
aStream nextPut: $;.
|
|
130 |
followers printOn: aStream.
|
|
131 |
aStream nextPut: $]
|
|
132 |
|
|
133 |
"Modified: / 14-02-2008 / 12:28:48 / janfrog"
|
|
134 |
! !
|
|
135 |
|
|
136 |
!SmaCCLR1Item methodsFor:'private'!
|
|
137 |
|
|
138 |
location
|
|
139 |
^location
|
|
140 |
!
|
|
141 |
|
|
142 |
rhs
|
|
143 |
^rhs
|
|
144 |
!
|
|
145 |
|
|
146 |
symbol
|
|
147 |
^symbol
|
|
148 |
! !
|
|
149 |
|
|
150 |
!SmaCCLR1Item methodsFor:'public'!
|
|
151 |
|
|
152 |
isLR1EqualTo: aLRItem
|
|
153 |
^self followers size = aLRItem followers size
|
|
154 |
and: [self followers allSatisfy: [:each | aLRItem followers includes: each]]
|
|
155 |
! !
|
|
156 |
|
|
157 |
!SmaCCLR1Item class methodsFor:'documentation'!
|
|
158 |
|
|
159 |
version
|
|
160 |
^ '$Header: /opt/data/cvs/stx/goodies/smaCC/SmaCC__SmaCCLR1Item.st,v 1.2 2008-02-17 10:30:47 vranyj1 Exp $'
|
|
161 |
! !
|