aboutsummaryrefslogtreecommitdiff
path: root/grammar/lalr1_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'grammar/lalr1_test.go')
-rw-r--r--grammar/lalr1_test.go31
1 files changed, 23 insertions, 8 deletions
diff --git a/grammar/lalr1_test.go b/grammar/lalr1_test.go
index 5ac84cd..67ab7d0 100644
--- a/grammar/lalr1_test.go
+++ b/grammar/lalr1_test.go
@@ -8,12 +8,6 @@ import (
"github.com/nihei9/vartan/spec"
)
-type expectedLALR1State struct {
- kernelItems []*lrItem
- nextStates map[symbol][]*lrItem
- reducibleProds []*production
-}
-
func TestGenLALR1Automaton(t *testing.T) {
// This grammar belongs to LALR(1) class, not SLR(1).
src := `
@@ -54,7 +48,7 @@ id: "[A-Za-z0-9_]+";
if err != nil {
t.Fatalf("failed to create a LALR1 automaton: %v", err)
}
- if lr0 == nil {
+ if automaton == nil {
t.Fatalf("genLALR1Automaton returns nil without any error")
}
@@ -101,7 +95,7 @@ id: "[A-Za-z0-9_]+";
},
}
- expectedStates := []expectedLALR1State{
+ expectedStates := []expectedLRState{
{
kernelItems: expectedKernels[0],
nextStates: map[symbol][]*lrItem{
@@ -220,6 +214,10 @@ id: "[A-Za-z0-9_]+";
t.Fatalf("kernel item not found; want: %v, got: %v", eKItem.id, kItem.id)
}
+ if len(kItem.lookAhead.symbols) != len(eKItem.lookAhead.symbols) {
+ t.Errorf("look-ahead symbols are mismatched; want: %v symbols, got: %v symbols", len(eKItem.lookAhead.symbols), len(kItem.lookAhead.symbols))
+ }
+
for eSym := range eKItem.lookAhead.symbols {
if _, ok := kItem.lookAhead.symbols[eSym]; !ok {
t.Errorf("look-ahead symbol not found: %v", eSym)
@@ -258,6 +256,23 @@ id: "[A-Za-z0-9_]+";
t.Errorf("reducible production was not found: %v", eProd.id)
}
}
+
+ if len(state.emptyProdItems) != len(eState.emptyProdItems) {
+ t.Errorf("empty production item is mismatched; want: %v, got: %v", len(eState.emptyProdItems), len(state.emptyProdItems))
+ }
+ for _, eItem := range eState.emptyProdItems {
+ found := false
+ for _, item := range state.emptyProdItems {
+ if item.id != eItem.id {
+ continue
+ }
+ found = true
+ break
+ }
+ if !found {
+ t.Errorf("empty production item not found: %v", eItem.id)
+ }
+ }
}
})
}