aboutsummaryrefslogtreecommitdiff
path: root/grammar/grammar.go
diff options
context:
space:
mode:
authorRyo Nihei <nihei.dev@gmail.com>2021-08-29 23:40:10 +0900
committerRyo Nihei <nihei.dev@gmail.com>2021-08-29 23:40:10 +0900
commitbb85dcc57cc3c0fff6cc9dc09540d58fef400d6f (patch)
tree2fc6a20d0c644a79d454a48c49baa26423fbd10d /grammar/grammar.go
parentAdd describe command to print a description file (diff)
downloadurubu-bb85dcc57cc3c0fff6cc9dc09540d58fef400d6f.tar.gz
urubu-bb85dcc57cc3c0fff6cc9dc09540d58fef400d6f.tar.xz
Add precedences and associativities to the description file
Diffstat (limited to 'grammar/grammar.go')
-rw-r--r--grammar/grammar.go15
1 files changed, 13 insertions, 2 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