aboutsummaryrefslogtreecommitdiff
path: root/grammar/parsing_table.go
blob: 53f692ea72d096388f316d478cfa87403028e40e (plain) (blame)
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
package grammar

import (
	"fmt"
	"sort"

	"github.com/nihei9/vartan/grammar/symbol"
	spec "github.com/nihei9/vartan/spec/grammar"
)

type ActionType string

const (
	ActionTypeShift  = ActionType("shift")
	ActionTypeReduce = ActionType("reduce")
	ActionTypeError  = ActionType("error")
)

type actionEntry int

const actionEntryEmpty = actionEntry(0)

func newShiftActionEntry(state stateNum) actionEntry {
	return actionEntry(state * -1)
}

func newReduceActionEntry(prod productionNum) actionEntry {
	return actionEntry(prod)
}

func (e actionEntry) isEmpty() bool {
	return e == actionEntryEmpty
}

func (e actionEntry) describe() (ActionType, stateNum, productionNum) {
	if e == actionEntryEmpty {
		return ActionTypeError, stateNumInitial, productionNumNil
	}
	if e < 0 {
		return ActionTypeShift, stateNum(e * -1), productionNumNil
	}
	return ActionTypeReduce, stateNumInitial, productionNum(e)
}

type GoToType string

const (
	GoToTypeRegistered = GoToType("registered")
	GoToTypeError      = GoToType("error")
)

type goToEntry uint

const goToEntryEmpty = goToEntry(0)

func newGoToEntry(state stateNum) goToEntry {
	return goToEntry(state)
}

func (e goToEntry) describe() (GoToType, stateNum) {
	if e == goToEntryEmpty {
		return GoToTypeError, stateNumInitial
	}
	return GoToTypeRegistered, stateNum(e)
}

type conflictResolutionMethod int

func (m conflictResolutionMethod) Int() int {
	return int(m)
}

const (
	ResolvedByPrec      conflictResolutionMethod = 1
	ResolvedByAssoc     conflictResolutionMethod = 2
	ResolvedByShift     conflictResolutionMethod = 3
	ResolvedByProdOrder conflictResolutionMethod = 4
)

type conflict interface {
	conflict()
}

type shiftReduceConflict struct {
	state      stateNum
	sym        symbol.Symbol
	nextState  stateNum
	prodNum    productionNum
	resolvedBy conflictResolutionMethod
}

func (c *shiftReduceConflict) conflict() {
}

type reduceReduceConflict struct {
	state      stateNum
	sym        symbol.Symbol
	prodNum1   productionNum
	prodNum2   productionNum
	resolvedBy conflictResolutionMethod
}

func (c *reduceReduceConflict) conflict() {
}

var (
	_ conflict = &shiftReduceConflict{}
	_ conflict = &reduceReduceConflict{}
)

type ParsingTable struct {
	actionTable      []actionEntry
	goToTable        []goToEntry
	stateCount       int
	terminalCount    int
	nonTerminalCount int

	// errorTrapperStates's index means a state number, and when `errorTrapperStates[stateNum]` is `1`,
	// the state has an item having the following form. The `α` and `β` can be empty.
	//
	// A → α・error β
	errorTrapperStates []int

	InitialState stateNum
}

func (t *ParsingTable) getAction(state stateNum, sym symbol.SymbolNum) (ActionType, stateNum, productionNum) {
	pos := state.Int()*t.terminalCount + sym.Int()
	return t.actionTable[pos].describe()
}

func (t *ParsingTable) getGoTo(state stateNum, sym symbol.SymbolNum) (GoToType, stateNum) {
	pos := state.Int()*t.nonTerminalCount + sym.Int()
	return t.goToTable[pos].describe()
}

func (t *ParsingTable) readAction(row int, col int) actionEntry {
	return t.actionTable[row*t.terminalCount+col]
}

func (t *ParsingTable) writeAction(row int, col int, act actionEntry) {
	t.actionTable[row*t.terminalCount+col] = act
}

func (t *ParsingTable) writeGoTo(state stateNum, sym symbol.Symbol, nextState stateNum) {
	pos := state.Int()*t.nonTerminalCount + sym.Num().Int()
	t.goToTable[pos] = newGoToEntry(nextState)
}

type lrTableBuilder struct {
	automaton    *lr0Automaton
	prods        *productionSet
	termCount    int
	nonTermCount int
	symTab       *symbol.SymbolTableReader
	precAndAssoc *precAndAssoc

	conflicts []conflict
}

func (b *lrTableBuilder) build() (*ParsingTable, error) {
	var ptab *ParsingTable
	{
		initialState := b.automaton.states[b.automaton.initialState]
		ptab = &ParsingTable{
			actionTable:        make([]actionEntry, len(b.automaton.states)*b.termCount),
			goToTable:          make([]goToEntry, len(b.automaton.states)*b.nonTermCount),
			stateCount:         len(b.automaton.states),
			terminalCount:      b.termCount,
			nonTerminalCount:   b.nonTermCount,
			errorTrapperStates: make([]int, len(b.automaton.states)),
			InitialState:       initialState.num,
		}
	}

	for _, state := range b.automaton.states {
		if state.isErrorTrapper {
			ptab.errorTrapperStates[state.num] = 1
		}

		for sym, kID := range state.next {
			nextState := b.automaton.states[kID]
			if sym.IsTerminal() {
				b.writeShiftAction(ptab, state.num, sym, nextState.num)
			} else {
				ptab.writeGoTo(state.num, sym, nextState.num)
			}
		}

		for prodID := range state.reducible {
			reducibleProd, ok := b.prods.findByID(prodID)
			if !ok {
				return nil, fmt.Errorf("reducible production not found: %v", prodID)
			}

			var reducibleItem *lrItem
			for _, item := range state.items {
				if item.prod != reducibleProd.id {
					continue
				}

				reducibleItem = item
				break
			}
			if reducibleItem == nil {
				for _, item := range state.emptyProdItems {
					if item.prod != reducibleProd.id {
						continue
					}

					reducibleItem = item
					break
				}
				if reducibleItem == nil {
					return nil, fmt.Errorf("reducible item not found; state: %v, production: %v", state.num, reducibleProd.num)
				}
			}

			for a := range reducibleItem.lookAhead.symbols {
				b.writeReduceAction(ptab, state.num, a, reducibleProd.num)
			}
		}
	}

	return ptab, nil
}

// writeShiftAction writes a shift action to the parsing table. When a shift/reduce conflict occurred,
// we prioritize the shift action.
func (b *lrTableBuilder) writeShiftAction(tab *ParsingTable, state stateNum, sym symbol.Symbol, nextState stateNum) {
	act := tab.readAction(state.Int(), sym.Num().Int())
	if !act.isEmpty() {
		ty, _, p := act.describe()
		if ty == ActionTypeReduce {
			act, method := b.resolveSRConflict(sym.Num(), p)
			b.conflicts = append(b.conflicts, &shiftReduceConflict{
				state:      state,
				sym:        sym,
				nextState:  nextState,
				prodNum:    p,
				resolvedBy: method,
			})
			if act == ActionTypeShift {
				tab.writeAction(state.Int(), sym.Num().Int(), newShiftActionEntry(nextState))
			}
			return
		}
	}
	tab.writeAction(state.Int(), sym.Num().Int(), newShiftActionEntry(nextState))
}

// writeReduceAction writes a reduce action to the parsing table. When a shift/reduce conflict occurred,
// we prioritize the shift action, and when a reduce/reduce conflict we prioritize the action that reduces
// the production with higher priority. Productions defined earlier in the grammar file have a higher priority.
func (b *lrTableBuilder) writeReduceAction(tab *ParsingTable, state stateNum, sym symbol.Symbol, prod productionNum) {
	act := tab.readAction(state.Int(), sym.Num().Int())
	if !act.isEmpty() {
		ty, s, p := act.describe()
		switch ty {
		case ActionTypeReduce:
			if p == prod {
				return
			}

			b.conflicts = append(b.conflicts, &reduceReduceConflict{
				state:      state,
				sym:        sym,
				prodNum1:   p,
				prodNum2:   prod,
				resolvedBy: ResolvedByProdOrder,
			})
			if p < prod {
				tab.writeAction(state.Int(), sym.Num().Int(), newReduceActionEntry(p))
			} else {
				tab.writeAction(state.Int(), sym.Num().Int(), newReduceActionEntry(prod))
			}
		case ActionTypeShift:
			act, method := b.resolveSRConflict(sym.Num(), prod)
			b.conflicts = append(b.conflicts, &shiftReduceConflict{
				state:      state,
				sym:        sym,
				nextState:  s,
				prodNum:    prod,
				resolvedBy: method,
			})
			if act == ActionTypeReduce {
				tab.writeAction(state.Int(), sym.Num().Int(), newReduceActionEntry(prod))
			}
		}
		return
	}
	tab.writeAction(state.Int(), sym.Num().Int(), newReduceActionEntry(prod))
}

func (b *lrTableBuilder) resolveSRConflict(sym symbol.SymbolNum, prod productionNum) (ActionType, conflictResolutionMethod) {
	symPrec := b.precAndAssoc.terminalPrecedence(sym)
	prodPrec := b.precAndAssoc.productionPredence(prod)
	if symPrec == 0 || prodPrec == 0 {
		return ActionTypeShift, ResolvedByShift
	}
	if symPrec == prodPrec {
		assoc := b.precAndAssoc.productionAssociativity(prod)
		if assoc != assocTypeLeft {
			return ActionTypeShift, ResolvedByAssoc
		}
		return ActionTypeReduce, ResolvedByAssoc
	}
	if symPrec < prodPrec {
		return ActionTypeShift, ResolvedByPrec
	}
	return ActionTypeReduce, ResolvedByPrec
}

func (b *lrTableBuilder) genReport(tab *ParsingTable, gram *Grammar) (*spec.Report, error) {
	var terms []*spec.Terminal
	{
		termSyms := b.symTab.TerminalSymbols()
		terms = make([]*spec.Terminal, len(termSyms)+1)

		for _, sym := range termSyms {
			name, ok := b.symTab.ToText(sym)
			if !ok {
				return nil, fmt.Errorf("failed to generate terminals: symbol not found: %v", sym)
			}

			term := &spec.Terminal{
				Number: sym.Num().Int(),
				Name:   name,
			}

			prec := b.precAndAssoc.terminalPrecedence(sym.Num())
			if prec != precNil {
				term.Precedence = prec
			}

			assoc := b.precAndAssoc.terminalAssociativity(sym.Num())
			switch assoc {
			case assocTypeLeft:
				term.Associativity = "l"
			case assocTypeRight:
				term.Associativity = "r"
			}

			terms[sym.Num()] = term
		}
	}

	var nonTerms []*spec.NonTerminal
	{
		nonTermSyms := b.symTab.NonTerminalSymbols()
		nonTerms = make([]*spec.NonTerminal, len(nonTermSyms)+1)
		for _, sym := range nonTermSyms {
			name, ok := b.symTab.ToText(sym)
			if !ok {
				return nil, fmt.Errorf("failed to generate non-terminals: symbol not found: %v", sym)
			}

			nonTerms[sym.Num()] = &spec.NonTerminal{
				Number: sym.Num().Int(),
				Name:   name,
			}
		}
	}

	var prods []*spec.Production
	{
		ps := gram.productionSet.getAllProductions()
		prods = make([]*spec.Production, len(ps)+1)
		for _, p := range ps {
			rhs := make([]int, len(p.rhs))
			for i, e := range p.rhs {
				if e.IsTerminal() {
					rhs[i] = e.Num().Int()
				} else {
					rhs[i] = e.Num().Int() * -1
				}
			}

			prod := &spec.Production{
				Number: p.num.Int(),
				LHS:    p.lhs.Num().Int(),
				RHS:    rhs,
			}

			prec := b.precAndAssoc.productionPredence(p.num)
			if prec != precNil {
				prod.Precedence = prec
			}

			assoc := b.precAndAssoc.productionAssociativity(p.num)
			switch assoc {
			case assocTypeLeft:
				prod.Associativity = "l"
			case assocTypeRight:
				prod.Associativity = "r"
			}

			prods[p.num.Int()] = prod
		}
	}

	var states []*spec.State
	{
		srConflicts := map[stateNum][]*shiftReduceConflict{}
		rrConflicts := map[stateNum][]*reduceReduceConflict{}
		for _, con := range b.conflicts {
			switch c := con.(type) {
			case *shiftReduceConflict:
				srConflicts[c.state] = append(srConflicts[c.state], c)
			case *reduceReduceConflict:
				rrConflicts[c.state] = append(rrConflicts[c.state], c)
			}
		}

		states = make([]*spec.State, len(b.automaton.states))
		for _, s := range b.automaton.states {
			kernel := make([]*spec.Item, len(s.items))
			for i, item := range s.items {
				p, ok := b.prods.findByID(item.prod)
				if !ok {
					return nil, fmt.Errorf("failed to generate states: production of kernel item not found: %v", item.prod)
				}

				kernel[i] = &spec.Item{
					Production: p.num.Int(),
					Dot:        item.dot,
				}
			}

			sort.Slice(kernel, func(i, j int) bool {
				if kernel[i].Production < kernel[j].Production {
					return true
				}
				if kernel[i].Production > kernel[j].Production {
					return false
				}
				return kernel[i].Dot < kernel[j].Dot
			})

			var shift []*spec.Transition
			var reduce []*spec.Reduce
			var goTo []*spec.Transition
			{
			TERMINALS_LOOP:
				for _, t := range b.symTab.TerminalSymbols() {
					act, next, prod := tab.getAction(s.num, t.Num())
					switch act {
					case ActionTypeShift:
						shift = append(shift, &spec.Transition{
							Symbol: t.Num().Int(),
							State:  next.Int(),
						})
					case ActionTypeReduce:
						for _, r := range reduce {
							if r.Production == prod.Int() {
								r.LookAhead = append(r.LookAhead, t.Num().Int())
								continue TERMINALS_LOOP
							}
						}
						reduce = append(reduce, &spec.Reduce{
							LookAhead:  []int{t.Num().Int()},
							Production: prod.Int(),
						})
					}
				}

				for _, n := range b.symTab.NonTerminalSymbols() {
					ty, next := tab.getGoTo(s.num, n.Num())
					if ty == GoToTypeRegistered {
						goTo = append(goTo, &spec.Transition{
							Symbol: n.Num().Int(),
							State:  next.Int(),
						})
					}
				}

				sort.Slice(shift, func(i, j int) bool {
					return shift[i].State < shift[j].State
				})
				sort.Slice(reduce, func(i, j int) bool {
					return reduce[i].Production < reduce[j].Production
				})
				sort.Slice(goTo, func(i, j int) bool {
					return goTo[i].State < goTo[j].State
				})
			}

			sr := []*spec.SRConflict{}
			rr := []*spec.RRConflict{}
			{
				for _, c := range srConflicts[s.num] {
					conflict := &spec.SRConflict{
						Symbol:     c.sym.Num().Int(),
						State:      c.nextState.Int(),
						Production: c.prodNum.Int(),
						ResolvedBy: c.resolvedBy.Int(),
					}

					ty, s, p := tab.getAction(s.num, c.sym.Num())
					switch ty {
					case ActionTypeShift:
						n := s.Int()
						conflict.AdoptedState = &n
					case ActionTypeReduce:
						n := p.Int()
						conflict.AdoptedProduction = &n
					}

					sr = append(sr, conflict)
				}

				sort.Slice(sr, func(i, j int) bool {
					return sr[i].Symbol < sr[j].Symbol
				})

				for _, c := range rrConflicts[s.num] {
					conflict := &spec.RRConflict{
						Symbol:      c.sym.Num().Int(),
						Production1: c.prodNum1.Int(),
						Production2: c.prodNum2.Int(),
						ResolvedBy:  c.resolvedBy.Int(),
					}

					_, _, p := tab.getAction(s.num, c.sym.Num())
					conflict.AdoptedProduction = p.Int()

					rr = append(rr, conflict)
				}

				sort.Slice(rr, func(i, j int) bool {
					return rr[i].Symbol < rr[j].Symbol
				})
			}

			states[s.num.Int()] = &spec.State{
				Number:     s.num.Int(),
				Kernel:     kernel,
				Shift:      shift,
				Reduce:     reduce,
				GoTo:       goTo,
				SRConflict: sr,
				RRConflict: rr,
			}
		}
	}

	return &spec.Report{
		Terminals:    terms,
		NonTerminals: nonTerms,
		Productions:  prods,
		States:       states,
	}, nil
}