From bb85dcc57cc3c0fff6cc9dc09540d58fef400d6f Mon Sep 17 00:00:00 2001 From: Ryo Nihei Date: Sun, 29 Aug 2021 23:40:10 +0900 Subject: Add precedences and associativities to the description file --- grammar/grammar.go | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) (limited to 'grammar/grammar.go') 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 -- cgit v1.2.3