aboutsummaryrefslogtreecommitdiff
path: root/grammar/grammar.go
diff options
context:
space:
mode:
authorRyo Nihei <nihei.dev@gmail.com>2021-08-21 16:18:56 +0900
committerRyo Nihei <nihei.dev@gmail.com>2021-08-21 16:19:49 +0900
commit94e2400aa8e6017165ab22ba5f2f70a6d0682f63 (patch)
tree83949f605bd729c83f8295db47aa065ad96e65bf /grammar/grammar.go
parentFix indents of a tree (diff)
downloadurubu-94e2400aa8e6017165ab22ba5f2f70a6d0682f63.tar.gz
urubu-94e2400aa8e6017165ab22ba5f2f70a6d0682f63.tar.xz
Resolve conflicts by default rules
When a shift/reduce conflict occurred, we prioritize the shift action, and when a reduce/reduce conflict occurred, we prioritize the production defined earlier in the grammar file.
Diffstat (limited to 'grammar/grammar.go')
-rw-r--r--grammar/grammar.go46
1 files changed, 16 insertions, 30 deletions
diff --git a/grammar/grammar.go b/grammar/grammar.go
index bca82e9..85eedcc 100644
--- a/grammar/grammar.go
+++ b/grammar/grammar.go
@@ -688,7 +688,7 @@ func Compile(gram *Grammar, opts ...CompileOption) (*spec.CompiledGrammar, error
return nil, err
}
- var tab *ParsingTable
+ var automaton *lr0Automaton
switch config.class {
case ClassSLR:
followSet, err := genFollowSet(gram.productionSet, firstSet)
@@ -701,44 +701,30 @@ func Compile(gram *Grammar, opts ...CompileOption) (*spec.CompiledGrammar, error
return nil, err
}
- slr := &lrTableBuilder{
- automaton: slr1.lr0Automaton,
- prods: gram.productionSet,
- termCount: len(terms),
- nonTermCount: len(nonTerms),
- symTab: gram.symbolTable,
- sym2AnonPat: gram.sym2AnonPat,
- }
- tab, err = slr.build()
-
- if config.descriptionFileName != "" {
- f, err := os.OpenFile(config.descriptionFileName, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
- if err != nil {
- return nil, err
- }
- defer f.Close()
-
- slr.write(f)
- }
-
- if err != nil {
- return nil, err
- }
+ automaton = slr1.lr0Automaton
case ClassLALR:
lalr1, err := genLALR1Automaton(lr0, gram.productionSet, firstSet)
if err != nil {
return nil, err
}
- lalr := &lrTableBuilder{
- automaton: lalr1.lr0Automaton,
+ automaton = lalr1.lr0Automaton
+ }
+
+ var tab *ParsingTable
+ {
+ b := &lrTableBuilder{
+ automaton: automaton,
prods: gram.productionSet,
termCount: len(terms),
nonTermCount: len(nonTerms),
symTab: gram.symbolTable,
sym2AnonPat: gram.sym2AnonPat,
}
- tab, err = lalr.build()
+ tab, err = b.build()
+ if err != nil {
+ return nil, err
+ }
if config.descriptionFileName != "" {
f, err := os.OpenFile(config.descriptionFileName, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
@@ -747,11 +733,11 @@ func Compile(gram *Grammar, opts ...CompileOption) (*spec.CompiledGrammar, error
}
defer f.Close()
- lalr.write(f)
+ b.writeDescription(f, tab)
}
- if err != nil {
- return nil, err
+ if len(b.conflicts) > 0 {
+ fmt.Fprintf(os.Stderr, "%v conflicts\n", len(b.conflicts))
}
}