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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
|
package main
import (
"encoding/json"
"fmt"
"io"
"os"
"strings"
"text/template"
"github.com/nihei9/vartan/grammar"
spec "github.com/nihei9/vartan/spec/grammar"
"github.com/spf13/cobra"
)
func init() {
cmd := &cobra.Command{
Use: "show",
Short: "Print a report in a readable format",
Example: ` vartan show grammar-report.json`,
Args: cobra.ExactArgs(1),
RunE: runShow,
}
rootCmd.AddCommand(cmd)
}
func runShow(cmd *cobra.Command, args []string) error {
report, err := readReport(args[0])
if err != nil {
return err
}
err = writeReport(os.Stdout, report)
if err != nil {
return err
}
return nil
}
func readReport(path string) (*spec.Report, error) {
f, err := os.Open(path)
if err != nil {
return nil, fmt.Errorf("Cannot open the report %s: %w", path, err)
}
defer f.Close()
d, err := io.ReadAll(f)
if err != nil {
return nil, err
}
report := &spec.Report{}
err = json.Unmarshal(d, report)
if err != nil {
return nil, err
}
return report, nil
}
const reportTemplate = `# Conflicts
{{ printConflictSummary . }}
# Terminals
{{ range slice .Terminals 1 -}}
{{ printTerminal . }}
{{ end }}
# Productions
{{ range slice .Productions 1 -}}
{{ printProduction . }}
{{ end }}
# States
{{ range .States }}
## State {{ .Number }}
{{ range .Kernel -}}
{{ printItem . }}
{{ end }}
{{ range .Shift -}}
{{ printShift . }}
{{ end -}}
{{ range .Reduce -}}
{{ printReduce . }}
{{ end -}}
{{ range .GoTo -}}
{{ printGoTo . }}
{{ end }}
{{ range .SRConflict -}}
{{ printSRConflict . }}
{{ end -}}
{{ range .RRConflict -}}
{{ printRRConflict . }}
{{ end -}}
{{ end }}`
func writeReport(w io.Writer, report *spec.Report) error {
termName := func(sym int) string {
if report.Terminals[sym].Alias != "" {
return report.Terminals[sym].Alias
}
return report.Terminals[sym].Name
}
nonTermName := func(sym int) string {
return report.NonTerminals[sym].Name
}
termAssoc := func(sym int) string {
switch report.Terminals[sym].Associativity {
case "l":
return "left"
case "r":
return "right"
default:
return "no"
}
}
prodAssoc := func(prod int) string {
switch report.Productions[prod].Associativity {
case "l":
return "left"
case "r":
return "right"
default:
return "no"
}
}
fns := template.FuncMap{
"printConflictSummary": func(report *spec.Report) string {
var implicitlyResolvedCount int
var explicitlyResolvedCount int
for _, s := range report.States {
for _, c := range s.SRConflict {
if c.ResolvedBy == grammar.ResolvedByShift.Int() {
implicitlyResolvedCount++
} else {
explicitlyResolvedCount++
}
}
for _, c := range s.RRConflict {
if c.ResolvedBy == grammar.ResolvedByProdOrder.Int() {
implicitlyResolvedCount++
} else {
explicitlyResolvedCount++
}
}
}
var b strings.Builder
if implicitlyResolvedCount == 1 {
fmt.Fprintf(&b, "%v conflict occurred and resolved implicitly.\n", implicitlyResolvedCount)
} else if implicitlyResolvedCount > 1 {
fmt.Fprintf(&b, "%v conflicts occurred and resolved implicitly.\n", implicitlyResolvedCount)
}
if explicitlyResolvedCount == 1 {
fmt.Fprintf(&b, "%v conflict occurred and resolved explicitly.\n", explicitlyResolvedCount)
} else if explicitlyResolvedCount > 1 {
fmt.Fprintf(&b, "%v conflicts occurred and resolved explicitly.\n", explicitlyResolvedCount)
}
if implicitlyResolvedCount == 0 && explicitlyResolvedCount == 0 {
fmt.Fprintf(&b, "No conflict")
}
return b.String()
},
"printTerminal": func(term spec.Terminal) string {
var prec string
if term.Precedence != 0 {
prec = fmt.Sprintf("%2v", term.Precedence)
} else {
prec = " -"
}
var assoc string
if term.Associativity != "" {
assoc = term.Associativity
} else {
assoc = "-"
}
if term.Alias != "" {
return fmt.Sprintf("%4v %v %v %v (%v)", term.Number, prec, assoc, term.Name, term.Alias)
}
return fmt.Sprintf("%4v %v %v %v", term.Number, prec, assoc, term.Name)
},
"printProduction": func(prod spec.Production) string {
var prec string
if prod.Precedence != 0 {
prec = fmt.Sprintf("%2v", prod.Precedence)
} else {
prec = " -"
}
var assoc string
if prod.Associativity != "" {
assoc = prod.Associativity
} else {
assoc = "-"
}
var b strings.Builder
fmt.Fprintf(&b, "%v →", nonTermName(prod.LHS))
if len(prod.RHS) > 0 {
for _, e := range prod.RHS {
if e > 0 {
fmt.Fprintf(&b, " %v", termName(e))
} else {
fmt.Fprintf(&b, " %v", nonTermName(e*-1))
}
}
} else {
fmt.Fprintf(&b, " ε")
}
return fmt.Sprintf("%4v %v %v %v", prod.Number, prec, assoc, b.String())
},
"printItem": func(item spec.Item) string {
prod := report.Productions[item.Production]
var b strings.Builder
fmt.Fprintf(&b, "%v →", nonTermName(prod.LHS))
for i, e := range prod.RHS {
if i == item.Dot {
fmt.Fprintf(&b, " ・")
}
if e > 0 {
fmt.Fprintf(&b, " %v", termName(e))
} else {
fmt.Fprintf(&b, " %v", nonTermName(e*-1))
}
}
if item.Dot >= len(prod.RHS) {
fmt.Fprintf(&b, " ・")
}
return fmt.Sprintf("%4v %v", prod.Number, b.String())
},
"printShift": func(tran spec.Transition) string {
return fmt.Sprintf("shift %4v on %v", tran.State, termName(tran.Symbol))
},
"printReduce": func(reduce spec.Reduce) string {
var b strings.Builder
{
fmt.Fprintf(&b, "%v", termName(reduce.LookAhead[0]))
for _, a := range reduce.LookAhead[1:] {
fmt.Fprintf(&b, ", %v", termName(a))
}
}
return fmt.Sprintf("reduce %4v on %v", reduce.Production, b.String())
},
"printGoTo": func(tran spec.Transition) string {
return fmt.Sprintf("goto %4v on %v", tran.State, nonTermName(tran.Symbol))
},
"printSRConflict": func(sr spec.SRConflict) string {
var adopted string
switch {
case sr.AdoptedState != nil:
adopted = fmt.Sprintf("shift %v", *sr.AdoptedState)
case sr.AdoptedProduction != nil:
adopted = fmt.Sprintf("reduce %v", *sr.AdoptedProduction)
}
var resolvedBy string
switch sr.ResolvedBy {
case grammar.ResolvedByPrec.Int():
if sr.AdoptedState != nil {
resolvedBy = fmt.Sprintf("symbol %v has higher precedence than production %v", termName(sr.Symbol), sr.Production)
} else {
resolvedBy = fmt.Sprintf("production %v has higher precedence than symbol %v", sr.Production, termName(sr.Symbol))
}
case grammar.ResolvedByAssoc.Int():
if sr.AdoptedState != nil {
resolvedBy = fmt.Sprintf("symbol %v and production %v has the same precedence, and symbol %v has %v associativity", termName(sr.Symbol), sr.Production, termName(sr.Symbol), termAssoc(sr.Symbol))
} else {
resolvedBy = fmt.Sprintf("production %v and symbol %v has the same precedence, and production %v has %v associativity", sr.Production, termName(sr.Symbol), sr.Production, prodAssoc(sr.Production))
}
case grammar.ResolvedByShift.Int():
resolvedBy = fmt.Sprintf("symbol %v and production %v don't define a precedence comparison (default rule)", sr.Symbol, sr.Production)
default:
resolvedBy = "?" // This is a bug.
}
return fmt.Sprintf("shift/reduce conflict (shift %v, reduce %v) on %v: %v adopted because %v", sr.State, sr.Production, termName(sr.Symbol), adopted, resolvedBy)
},
"printRRConflict": func(rr spec.RRConflict) string {
var resolvedBy string
switch rr.ResolvedBy {
case grammar.ResolvedByProdOrder.Int():
resolvedBy = fmt.Sprintf("production %v and %v don't define a precedence comparison (default rule)", rr.Production1, rr.Production2)
default:
resolvedBy = "?" // This is a bug.
}
return fmt.Sprintf("reduce/reduce conflict (%v, %v) on %v: reduce %v adopted because %v", rr.Production1, rr.Production2, termName(rr.Symbol), rr.AdoptedProduction, resolvedBy)
},
}
tmpl, err := template.New("").Funcs(fns).Parse(reportTemplate)
if err != nil {
return err
}
err = tmpl.Execute(w, report)
if err != nil {
return err
}
return nil
}
|