diff options
Diffstat (limited to 'grammar')
-rw-r--r-- | grammar/grammar.go | 15 | ||||
-rw-r--r-- | grammar/parsing_table.go | 30 |
2 files changed, 42 insertions, 3 deletions
diff --git a/grammar/grammar.go b/grammar/grammar.go index edfb6af..b87d368 100644 --- a/grammar/grammar.go +++ b/grammar/grammar.go @@ -33,8 +33,9 @@ const ( // precAndAssoc represents precedence and associativities of terminal symbols and productions. // We use the priority of the production to resolve shift/reduce conflicts. type precAndAssoc struct { - // termPrec represents the precedence of the terminal symbols. - termPrec map[symbolNum]int + // termPrec and termAssoc represent the precedence of the terminal symbols. + termPrec map[symbolNum]int + termAssoc map[symbolNum]assocType // prodPrec and prodAssoc represent the precedence and the associativities of the production. // These values are inherited from the right-most symbols in the RHS of the productions. @@ -51,6 +52,15 @@ func (pa *precAndAssoc) terminalPrecedence(sym symbolNum) int { return prec } +func (pa *precAndAssoc) terminalAssociativity(sym symbolNum) assocType { + assoc, ok := pa.termAssoc[sym] + if !ok { + return assocTypeNil + } + + return assoc +} + func (pa *precAndAssoc) productionPredence(prod productionNum) int { prec, ok := pa.prodPrec[prod] if !ok { @@ -896,6 +906,7 @@ func (b *GrammarBuilder) genPrecAndAssoc(symTab *symbolTable, prods *productionS return &precAndAssoc{ termPrec: termPrec, + termAssoc: termAssoc, prodPrec: prodPrec, prodAssoc: prodAssoc, }, nil diff --git a/grammar/parsing_table.go b/grammar/parsing_table.go index fe5a619..a82ef60 100644 --- a/grammar/parsing_table.go +++ b/grammar/parsing_table.go @@ -347,6 +347,19 @@ func (b *lrTableBuilder) genDescription(tab *ParsingTable, gram *Grammar) (*spec term.Pattern = pat } + 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 } } @@ -382,11 +395,26 @@ func (b *lrTableBuilder) genDescription(tab *ParsingTable, gram *Grammar) (*spec } } - prods[p.num.Int()] = &spec.Production{ + 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 } } |