forked from simoneguidi94/gopapageno
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathopp.go
319 lines (254 loc) · 9 KB
/
opp.go
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
package gopapageno
import (
"context"
"fmt"
)
type OPParser struct {
g *Grammar
concurrency int
reductionStrategy ReductionStrategy
pools struct {
stacks []*Pool[stack[*Token]]
nonterminals []*Pool[Token]
// These are only used when reducing using a single sweep.
sweepInput *Pool[stack[Token]]
sweepStack *Pool[stack[*Token]]
}
workers []*oppWorker
results []*OPPStack
}
func NewOPParser(g *Grammar, src []byte, opts *RunOptions) *OPParser {
p := &OPParser{
g: g,
concurrency: opts.Concurrency,
reductionStrategy: opts.ReductionStrategy,
workers: make([]*oppWorker, opts.Concurrency),
results: make([]*OPPStack, opts.Concurrency),
}
srcLen := len(src)
stackPoolBaseSize := stacksCount[*Token](src, p.concurrency, opts.AvgTokenLength)
ntPoolBaseSize := int(float64(srcLen/opts.AvgTokenLength/p.concurrency) * 1.5)
// Initialize memory pools for stacks.
p.pools.stacks = make([]*Pool[stack[*Token]], p.concurrency)
// Initialize pools to hold pointers to tokens generated by the reduction steps.
p.pools.nonterminals = make([]*Pool[Token], p.concurrency)
for thread := 0; thread < p.concurrency; thread++ {
p.pools.stacks[thread] = NewPool(stackPoolBaseSize, WithConstructor(newStack[*Token]))
p.pools.nonterminals[thread] = NewPool[Token](ntPoolBaseSize)
}
if p.concurrency > 1 && (p.reductionStrategy == ReductionSweep || p.reductionStrategy == ReductionMixed) {
inputPoolBaseSize := stacksCount[Token](src, p.concurrency, opts.AvgTokenLength)
p.pools.sweepInput = NewPool(inputPoolBaseSize, WithConstructor(newStack[Token]))
p.pools.sweepStack = NewPool(stackPoolBaseSize, WithConstructor(newStack[*Token]))
}
for thread := 0; thread < p.concurrency; thread++ {
p.workers[thread] = &oppWorker{
parser: p,
id: thread,
ntPool: p.pools.nonterminals[thread],
}
}
return p
}
type oppWorker struct {
parser *OPParser
id int
ntPool *Pool[Token]
}
func (p *OPParser) Parse(ctx context.Context, tokensLists []*LOS[Token]) (*Token, error) {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
p.concurrency = len(tokensLists)
resultCh := make(chan parseResult[OPPStack])
errCh := make(chan error, 1)
// First parallel pass of the algorithm.
for thread := 0; thread < p.concurrency; thread++ {
var nextToken *Token
// If the thread is not the last, also take the first token of the next stack for lookahead.
if thread < p.concurrency-1 {
nextInputListIter := tokensLists[thread+1].HeadIterator()
nextToken = nextInputListIter.Next()
}
s := NewOPPStack(p.pools.stacks[thread])
go p.workers[thread].parse(ctx, s, tokensLists[thread], nextToken, false, resultCh, errCh)
}
if err := collectResults[OPPStack](p.results, resultCh, errCh, p.concurrency); err != nil {
return nil, err
}
//If the number of threads is greater than one, results must be combined and work should continue.
reductionPasses := 0
// Reduction phase
for p.concurrency--; p.concurrency >= 1; p.concurrency-- {
if p.reductionStrategy == ReductionSweep || (p.reductionStrategy == ReductionMixed && reductionPasses >= 2) {
// Nullifies the previous p.Concurrency--
p.concurrency++
// Create the final input by joining together the stacks from the previous step.
stack := p.results[0].Combine()
input := p.CombineSweepLOS(p.pools.sweepInput, p.results[1:])
// Sets correct Concurrency level for final sweep.
p.concurrency = 1
go p.workers[0].parse(ctx, stack, input, nil, true, resultCh, errCh)
if err := collectResults[OPPStack](p.results, resultCh, errCh, 1); err != nil {
cancel()
return nil, err
}
} else {
for i := 0; i < p.concurrency; i++ {
stackLeft := p.results[i]
stackRight := p.results[i+1]
stack := stackLeft.Combine()
// TODO: I should find a way to make this work without creating a new LOS for the inputs.
// Unfortunately the new stack depends on the content of tokensLists[i] since its elements are stored there.
// We can't erase the old input easily to reuse its storage.
// TODO: Maybe allocate 2 * c LOS so that we can alternate?
input := stackRight.CombineLOS(tokensLists[i].pool)
go p.workers[i].parse(ctx, stack, input, nil, true, resultCh, errCh)
}
if err := collectResults[OPPStack](p.results, resultCh, errCh, p.concurrency); err != nil {
cancel()
return nil, err
}
reductionPasses++
}
}
root, err := p.results[0].LastNonterminal()
if err != nil {
return nil, err
}
return root, nil
}
func (p *OPParser) CombineSweepLOS(pool *Pool[stack[Token]], stacks []*OPPStack) *LOS[Token] {
input := NewLOS[Token](pool)
for i := 0; i < p.concurrency-1; i++ {
iterator := stacks[i].HeadIterator()
//Ignore the first token.
iterator.Next()
for token := iterator.Next(); token != nil; token = iterator.Next() {
input.Push(*token)
}
}
return input
}
// parse implements both OPP and AOPP strategies.
func (w *oppWorker) parse(ctx context.Context, stack *OPPStack, tokens *LOS[Token], nextToken *Token, finalPass bool, resultCh chan<- parseResult[OPPStack], errCh chan<- error) {
tokensIt := tokens.HeadIterator()
// If the thread is the first, push a # onto the stack
// Otherwise, push the first inputToken onto the stack
if !finalPass {
if w.id == 0 {
stack.Push(&Token{
Type: TokenTerm,
Value: nil,
Precedence: PrecEmpty,
Next: nil,
Child: nil,
})
} else {
t := tokensIt.Next()
t.Precedence = PrecEmpty
stack.Push(t)
}
// If the thread is the last, push a # onto the tokens m
// Otherwise, push onto the tokens m the first inputToken of the next tokens m
if w.id == w.parser.concurrency-1 {
tokens.Push(Token{
Type: TokenTerm,
Value: nil,
Precedence: PrecEmpty,
Next: nil,
Child: nil,
})
} else if nextToken != nil {
tokens.Push(*nextToken)
}
}
var pos int
var lhsToken *Token
var rhs []TokenType
var rhsTokens []*Token
rhsBuf := make([]TokenType, w.parser.g.MaxRHSLength)
rhsTokensBuf := make([]*Token, w.parser.g.MaxRHSLength)
newNonTerm := Token{
Type: TokenEmpty,
Value: nil,
Precedence: PrecEmpty,
Next: nil,
Child: nil,
}
// Iterate over the tokens
// If this is the first worker, start reading from the input stack, otherwise begin with the last
// token of the previous stack.
for inputToken := tokensIt.Next(); inputToken != nil; {
//If the current inputToken is a non-terminal, push it onto the stack with no precedence relation
if !inputToken.Type.IsTerminal() {
inputToken.Precedence = PrecEmpty
stack.Push(inputToken)
inputToken = tokensIt.Next()
continue
}
//Find the first terminal on the stack and get the precedence between it and the current tokens inputToken
firstTerminal := stack.FirstTerminal()
var prec Precedence
if firstTerminal == nil {
prec = w.parser.g.precedence(TokenTerm, inputToken.Type)
} else {
prec = w.parser.g.precedence(firstTerminal.Type, inputToken.Type)
}
// If it's equal in precedence or yields, push the inputToken onto the stack with its precedence relation.
if prec == PrecEquals || prec == PrecYields {
inputToken.Precedence = prec
stack.Push(inputToken)
inputToken = tokensIt.Next()
} else if prec == PrecTakes || prec == PrecAssociative {
//If there are no tokens yielding precedence on the stack, push inputToken onto the stack.
//Otherwise, perform a reduction
if stack.YieldingPrecedence() == 0 {
inputToken.Precedence = prec
stack.Push(inputToken)
inputToken = tokensIt.Next()
} else {
pos = w.parser.g.MaxRHSLength - 1
var token *Token
// Pop tokens from the stack until one that yields precedence is reached, saving them in rhsBuf
for token = stack.Pop(); token.Precedence != PrecYields && token.Precedence != PrecAssociative; token = stack.Pop() {
rhsTokensBuf[pos] = token
rhsBuf[pos] = token.Type
pos--
}
rhsTokensBuf[pos] = token
rhsBuf[pos] = token.Type
//Pop one last token, if it's a non-terminal add it to rhsBuf, otherwise ignore it (push it again onto the stack)
token = stack.Pop()
if token.Type.IsTerminal() {
stack.Push(token)
} else {
pos--
rhsTokensBuf[pos] = token
rhsBuf[pos] = token.Type
stack.UpdateFirstTerminal()
}
//Obtain the actual rhs from the buffers
rhsTokens = rhsTokensBuf[pos:]
rhs = rhsBuf[pos:]
//Find corresponding lhs and ruleNum
lhs, ruleNum := w.parser.g.findRuleMatch(rhs)
if lhs == TokenEmpty {
errCh <- fmt.Errorf("could not find match for rhs %v", rhs)
return
}
newNonTerm.Type = lhs
lhsToken = w.ntPool.Get()
*lhsToken = newNonTerm
//Execute the semantic action
w.parser.g.Func(ruleNum, RuleSimple, lhsToken, rhsTokens, w.id)
//Push the new nonterminal onto the stack
stack.Push(lhsToken)
}
} else {
//If there's no precedence relation, abort the parsing
errCh <- fmt.Errorf("no precedence relation found")
return
}
}
resultCh <- parseResult[OPPStack]{w.id, stack}
}