diff options
author | Ryo Nihei <nihei.dev@gmail.com> | 2021-09-01 23:57:02 +0900 |
---|---|---|
committer | Ryo Nihei <nihei.dev@gmail.com> | 2021-09-02 00:01:35 +0900 |
commit | 8832b64b4227245e45f9a24d543c1b80168c489d (patch) | |
tree | 20ed014caa923254d8e7870241225d8c20f7d2b4 /grammar | |
parent | Remove the expected terminals field from the parsing table (diff) | |
download | cotia-8832b64b4227245e45f9a24d543c1b80168c489d.tar.gz cotia-8832b64b4227245e45f9a24d543c1b80168c489d.tar.xz |
Support LAC (lookahead correction)
Diffstat (limited to 'grammar')
-rw-r--r-- | grammar/grammar.go | 7 | ||||
-rw-r--r-- | grammar/parsing_table.go | 2 |
2 files changed, 9 insertions, 0 deletions
diff --git a/grammar/grammar.go b/grammar/grammar.go index efa0f35..7e86ea8 100644 --- a/grammar/grammar.go +++ b/grammar/grammar.go @@ -1038,9 +1038,12 @@ func Compile(gram *Grammar, opts ...CompileOption) (*spec.CompiledGrammar, error return nil, err } + var class string var automaton *lr0Automaton switch config.class { case ClassSLR: + class = "slr" + followSet, err := genFollowSet(gram.productionSet, firstSet) if err != nil { return nil, err @@ -1053,6 +1056,8 @@ func Compile(gram *Grammar, opts ...CompileOption) (*spec.CompiledGrammar, error automaton = slr1.lr0Automaton case ClassLALR: + class = "lalr" + lalr1, err := genLALR1Automaton(lr0, gram.productionSet, firstSet) if err != nil { return nil, err @@ -1064,6 +1069,7 @@ func Compile(gram *Grammar, opts ...CompileOption) (*spec.CompiledGrammar, error var tab *ParsingTable { b := &lrTableBuilder{ + class: config.class, automaton: automaton, prods: gram.productionSet, termCount: len(terms), @@ -1150,6 +1156,7 @@ func Compile(gram *Grammar, opts ...CompileOption) (*spec.CompiledGrammar, error }, }, ParsingTable: &spec.ParsingTable{ + Class: class, Action: action, GoTo: goTo, StateCount: tab.stateCount, diff --git a/grammar/parsing_table.go b/grammar/parsing_table.go index aa88ad5..28f6392 100644 --- a/grammar/parsing_table.go +++ b/grammar/parsing_table.go @@ -136,6 +136,7 @@ func (t *ParsingTable) writeGoTo(state stateNum, sym symbol, nextState stateNum) } type lrTableBuilder struct { + class Class automaton *lr0Automaton prods *productionSet termCount int @@ -552,6 +553,7 @@ func (b *lrTableBuilder) genDescription(tab *ParsingTable, gram *Grammar) (*spec } return &spec.Description{ + Class: string(b.class), Terminals: terms, NonTerminals: nonTerms, Productions: prods, |