-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
185 lines (164 loc) · 4.25 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package main
import (
"bufio"
"fmt"
"os"
"regexp"
"strings"
"github.com/mbotezatu/slr1-table/grammar"
"github.com/mbotezatu/slr1-table/slr1"
"github.com/olekukonko/tablewriter"
)
func main() {
// var nonTerm = regexp.MustCompile(`[A-Z]{1}[a-zA-Z0-9]*`)
// var term = regexp.MustCompile(`[a-z]{1}[a-zA-Z0-9]*`)
var imp = regexp.MustCompile(`->`)
var prod = regexp.MustCompile(`^((\s*?[A-Z]{1}[a-zA-Z0-9]*\s*?->(\s*?|[A-Z]{1}[a-zA-Z0-9]*|[a-z]{1}[a-zA-Z0-9]*)*?)|(\s*))$`)
var file *os.File
if len(os.Args) > 1 {
var err error
file, err = os.Open(os.Args[1])
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
defer func() {
if err := file.Close(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}()
} else {
file = os.Stdin
}
s := bufio.NewScanner(file)
cfg := grammar.NewCFG()
stopAssign := false
firstRule := true
for s.Scan() {
if prod.MatchString(s.Text()) {
if stopAssign {
continue
}
lrhs := imp.Split(s.Text(), -1)
lrhs[0] = strings.TrimSpace(lrhs[0])
if len(lrhs) < 2 {
continue
}
rhsSymbols := strings.Fields(lrhs[1])
if len(rhsSymbols) == 0 {
rhsSymbols = append(rhsSymbols, "")
}
rule := grammar.NewProduction(lrhs[0], rhsSymbols)
cfg.Rules[lrhs[0]] = append(cfg.Rules[lrhs[0]], rule)
if firstRule {
cfg.StartRule = rule
firstRule = false
}
cfg.NonTerminals.Add(lrhs[0])
for _, v := range rule.RHS() {
if grammar.IsTerminal(v) {
cfg.Terminals.Add(v)
} else {
cfg.NonTerminals.Add(v)
}
}
} else {
fmt.Fprintf(os.Stderr, "%s does not match the production regexp.\n", s.Text())
stopAssign = true
}
}
if stopAssign {
os.Exit(1)
}
if cfg.StartRule == nil {
fmt.Fprint(os.Stderr, "No grammar defined.\n")
os.Exit(1)
}
firstSets, err := grammar.First(cfg)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
fmt.Println("First Sets:")
for k, v := range firstSets {
if k == "" {
k = "λ"
}
fmt.Printf("\t%s: { ", k)
next := v.Iterator()
for elem, ok := next(); ok; elem, ok = next() {
if elem == "" {
elem = "λ"
}
fmt.Printf("%s ", elem)
}
fmt.Println("}")
}
followSets := grammar.Follow(cfg, firstSets)
fmt.Println("Follow Sets: ")
for k, v := range followSets {
fmt.Printf("\t%s: { ", k)
next := v.Iterator()
for elem, ok := next(); ok; elem, ok = next() {
fmt.Printf("%s ", elem)
}
fmt.Println("}")
}
fmt.Println()
actionTable, gotoTable, err := slr1.GenSLR1Table(cfg, followSets)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
actionTableWriter := tablewriter.NewWriter(os.Stdout)
var actionTableHeader []string
next := cfg.Terminals.Iterator()
actionTableHeader = append(actionTableHeader, "State")
for elem, ok := next(); ok; elem, ok = next() {
if elem != "" {
actionTableHeader = append(actionTableHeader, elem)
}
}
actionTableHeader = append(actionTableHeader, "$")
actionTableWriter.Append(actionTableHeader)
actionTableWriter.SetRowLine(true)
prtActionTable := make([][]string, len(actionTable))
for i := 0; i < len(actionTable); i++ {
prtActionTable[i] = make([]string, len(actionTableHeader))
prtActionTable[i][0] = fmt.Sprintf("%d", i)
for j := 1; j < len(actionTableHeader); j++ {
prtActionTable[i][j] = actionTable[i][actionTableHeader[j]]
}
}
actionTableWriter.AppendBulk(prtActionTable)
fmt.Println("ACTION Table:")
actionTableWriter.Render()
fmt.Println()
gotoTableWriter := tablewriter.NewWriter(os.Stdout)
gotoTableHeader := make([]string, cfg.NonTerminals.Len()+1)
next = cfg.NonTerminals.Iterator()
gotoTableHeader[0] = "State"
i := 1
for elem, ok := next(); ok; elem, ok = next() {
gotoTableHeader[i] = elem
i++
}
gotoTableWriter.Append(gotoTableHeader)
gotoTableWriter.SetRowLine(true)
prtGotoTable := make([][]string, len(gotoTable))
for i := 0; i < len(gotoTable); i++ {
prtGotoTable[i] = make([]string, len(gotoTableHeader))
prtGotoTable[i][0] = fmt.Sprintf("%d", i)
for j := 1; j < len(gotoTableHeader); j++ {
var state string
if gotoTable[i][gotoTableHeader[j]] > 0 {
state = fmt.Sprintf("%d", gotoTable[i][gotoTableHeader[j]])
}
prtGotoTable[i][j] = state
}
}
gotoTableWriter.AppendBulk(prtGotoTable)
fmt.Println("GOTO Table:")
gotoTableWriter.Render()
}