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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
|
package grammar
import (
"fmt"
mlcompiler "github.com/nihei9/maleeni/compiler"
mlspec "github.com/nihei9/maleeni/spec"
"github.com/nihei9/vartan/spec"
)
type astActionEntry struct {
position int
expansion bool
}
type Grammar struct {
lexSpec *mlspec.LexSpec
skipLexKinds []mlspec.LexKind
productionSet *productionSet
augmentedStartSymbol symbol
symbolTable *symbolTable
astActions map[productionID][]*astActionEntry
}
func NewGrammar(root *spec.RootNode) (*Grammar, error) {
symTab := newSymbolTable()
anonPat2Sym := map[string]symbol{}
var lexSpec *mlspec.LexSpec
var skip []mlspec.LexKind
{
entries := []*mlspec.LexEntry{}
anonPats := []string{}
for _, prod := range root.Productions {
if isLexicalProduction(prod) {
_, err := symTab.registerTerminalSymbol(prod.LHS)
if err != nil {
return nil, err
}
var modes []mlspec.LexModeName
if prod.Directive != nil {
dir := prod.Directive
switch dir.Name {
case "mode":
if len(dir.Parameters) == 0 {
return nil, fmt.Errorf("'mode' directive needs an ID parameter")
}
for _, param := range dir.Parameters {
if param.ID == "" {
return nil, fmt.Errorf("'mode' directive needs an ID parameter")
}
modes = append(modes, mlspec.LexModeName(param.ID))
}
default:
return nil, fmt.Errorf("invalid directive name '%v'", dir.Name)
}
}
alt := prod.RHS[0]
var push mlspec.LexModeName
var pop bool
if alt.Directive != nil {
dir := alt.Directive
switch dir.Name {
case "skip":
if len(dir.Parameters) > 0 {
return nil, fmt.Errorf("'skip' directive needs no parameter")
}
skip = append(skip, mlspec.LexKind(prod.LHS))
case "push":
if len(dir.Parameters) != 1 || dir.Parameters[0].ID == "" {
return nil, fmt.Errorf("'push' directive needs an ID parameter")
}
push = mlspec.LexModeName(dir.Parameters[0].ID)
case "pop":
if len(dir.Parameters) > 0 {
return nil, fmt.Errorf("'pop' directive needs no parameter")
}
pop = true
default:
return nil, fmt.Errorf("invalid directive name '%v'", dir.Name)
}
}
entries = append(entries, &mlspec.LexEntry{
Modes: modes,
Kind: mlspec.LexKind(prod.LHS),
Pattern: mlspec.LexPattern(alt.Elements[0].Pattern),
Push: push,
Pop: pop,
})
continue
}
for _, alt := range prod.RHS {
for _, elem := range alt.Elements {
if elem.Pattern == "" {
continue
}
exist := false
for _, p := range anonPats {
if p == elem.Pattern {
exist = true
break
}
}
if exist {
continue
}
anonPats = append(anonPats, elem.Pattern)
}
}
}
var anonEntries []*mlspec.LexEntry
for i, p := range anonPats {
kind := fmt.Sprintf("__%v__", i+1)
sym, err := symTab.registerTerminalSymbol(kind)
if err != nil {
return nil, err
}
anonPat2Sym[p] = sym
anonEntries = append(anonEntries, &mlspec.LexEntry{
Kind: mlspec.LexKind(kind),
Pattern: mlspec.LexPattern(p),
})
}
// Anonymous patterns take precedence over explicitly defined lexical specifications.
entries = append(anonEntries, entries...)
for _, fragment := range root.Fragments {
entries = append(entries, &mlspec.LexEntry{
Fragment: true,
Kind: mlspec.LexKind(fragment.LHS),
Pattern: mlspec.LexPattern(fragment.RHS),
})
}
lexSpec = &mlspec.LexSpec{
Entries: entries,
}
}
prods := newProductionSet()
var augStartSym symbol
astActs := map[productionID][]*astActionEntry{}
{
startProd := root.Productions[0]
augStartText := fmt.Sprintf("%s'", startProd.LHS)
var err error
augStartSym, err = symTab.registerStartSymbol(augStartText)
if err != nil {
return nil, err
}
startSym, err := symTab.registerNonTerminalSymbol(startProd.LHS)
if err != nil {
return nil, err
}
p, err := newProduction(augStartSym, []symbol{
startSym,
})
if err != nil {
return nil, err
}
prods.append(p)
for _, prod := range root.Productions {
if isLexicalProduction(prod) {
continue
}
_, err := symTab.registerNonTerminalSymbol(prod.LHS)
if err != nil {
return nil, err
}
}
for _, prod := range root.Productions {
if isLexicalProduction(prod) {
continue
}
lhsSym, ok := symTab.toSymbol(prod.LHS)
if !ok {
return nil, fmt.Errorf("symbol '%v' is undefined", prod.LHS)
}
for _, alt := range prod.RHS {
altSyms := make([]symbol, len(alt.Elements))
for i, elem := range alt.Elements {
var sym symbol
if elem.Pattern != "" {
var ok bool
sym, ok = anonPat2Sym[elem.Pattern]
if !ok {
return nil, fmt.Errorf("pattern '%v' is undefined", elem.Pattern)
}
} else {
var ok bool
sym, ok = symTab.toSymbol(elem.ID)
if !ok {
return nil, fmt.Errorf("symbol '%v' is undefined", elem.ID)
}
}
altSyms[i] = sym
}
p, err := newProduction(lhsSym, altSyms)
if err != nil {
return nil, err
}
prods.append(p)
if alt.Directive != nil {
dir := alt.Directive
switch dir.Name {
case "ast":
if len(dir.Parameters) != 1 || dir.Parameters[0].Tree == nil {
return nil, fmt.Errorf("'ast' directive needs a tree parameter")
}
param := dir.Parameters[0]
lhsText, ok := symTab.toText(p.lhs)
if !ok || param.Tree.Name != lhsText {
return nil, fmt.Errorf("a name of a tree structure must be the same ID as an LHS of a production; LHS: %v", lhsText)
}
astAct := make([]*astActionEntry, len(param.Tree.Children))
for i, c := range param.Tree.Children {
if c.Position > len(alt.Elements) {
return nil, fmt.Errorf("a position must be less than or equal to the length of an alternative; alternative length: %v", len(alt.Elements))
}
if c.Expansion {
offset := c.Position - 1
elem := alt.Elements[offset]
if elem.Pattern != "" {
return nil, fmt.Errorf("the expansion symbol cannot be applied to a pattern ($%v: %v)", c.Position, elem.Pattern)
}
elemSym, ok := symTab.toSymbol(elem.ID)
if !ok {
// If the symbol was not found, it's a bug.
return nil, fmt.Errorf("a symbol corresponding to a position ($%v: %v) was not found", c.Position, elem.ID)
}
if elemSym.isTerminal() {
return nil, fmt.Errorf("the expansion symbol cannot be applied to a terminal symbol ($%v: %v)", c.Position, elem.ID)
}
}
astAct[i] = &astActionEntry{
position: c.Position,
expansion: c.Expansion,
}
}
astActs[p.id] = astAct
default:
return nil, fmt.Errorf("invalid directive name '%v'", dir.Name)
}
}
}
}
}
return &Grammar{
lexSpec: lexSpec,
skipLexKinds: skip,
productionSet: prods,
augmentedStartSymbol: augStartSym,
symbolTable: symTab,
astActions: astActs,
}, nil
}
func isLexicalProduction(prod *spec.ProductionNode) bool {
if len(prod.RHS) == 1 && len(prod.RHS[0].Elements) == 1 && prod.RHS[0].Elements[0].Pattern != "" {
return true
}
return false
}
func Compile(gram *Grammar) (*spec.CompiledGrammar, error) {
lexSpec, err := mlcompiler.Compile(gram.lexSpec, mlcompiler.CompressionLevel(mlcompiler.CompressionLevelMax))
if err != nil {
return nil, err
}
kind2Term := make([][]int, len(lexSpec.Modes))
skip := make([][]int, len(lexSpec.Modes))
for modeNum, spec := range lexSpec.Specs {
if modeNum == 0 {
kind2Term[0] = nil
skip[0] = nil
continue
}
k2tRec := make([]int, len(spec.Kinds))
skipRec := make([]int, len(spec.Kinds))
for n, k := range spec.Kinds {
if n == 0 {
k2tRec[0] = symbolNil.num().Int()
continue
}
sym, ok := gram.symbolTable.toSymbol(k.String())
if !ok {
return nil, fmt.Errorf("terminal symbol '%v' (in '%v' mode) is not found in a symbol table", k, lexSpec.Modes[modeNum])
}
k2tRec[n] = sym.num().Int()
for _, sk := range gram.skipLexKinds {
if k != sk {
continue
}
skipRec[n] = 1
}
}
kind2Term[modeNum] = k2tRec
skip[modeNum] = skipRec
}
terms, err := gram.symbolTable.getTerminalTexts()
if err != nil {
return nil, err
}
nonTerms, err := gram.symbolTable.getNonTerminalTexts()
if err != nil {
return nil, err
}
firstSet, err := genFirstSet(gram.productionSet)
if err != nil {
return nil, err
}
followSet, err := genFollowSet(gram.productionSet, firstSet)
if err != nil {
return nil, err
}
lr0, err := genLR0Automaton(gram.productionSet, gram.augmentedStartSymbol)
if err != nil {
return nil, err
}
tab, err := genSLRParsingTable(lr0, gram.productionSet, followSet, len(terms), len(nonTerms))
if err != nil {
return nil, err
}
action := make([]int, len(tab.actionTable))
for i, e := range tab.actionTable {
action[i] = int(e)
}
goTo := make([]int, len(tab.goToTable))
for i, e := range tab.goToTable {
goTo[i] = int(e)
}
lhsSyms := make([]int, len(gram.productionSet.getAllProductions())+1)
altSymCounts := make([]int, len(gram.productionSet.getAllProductions())+1)
astActEnties := make([][]int, len(gram.productionSet.getAllProductions())+1)
for _, p := range gram.productionSet.getAllProductions() {
lhsSyms[p.num] = p.lhs.num().Int()
altSymCounts[p.num] = p.rhsLen
astAct, ok := gram.astActions[p.id]
if !ok {
continue
}
astActEntry := make([]int, len(astAct))
for i, e := range astAct {
if e.expansion {
astActEntry[i] = e.position * -1
} else {
astActEntry[i] = e.position
}
}
astActEnties[p.num] = astActEntry
}
return &spec.CompiledGrammar{
LexicalSpecification: &spec.LexicalSpecification{
Lexer: "maleeni",
Maleeni: &spec.Maleeni{
Spec: lexSpec,
KindToTerminal: kind2Term,
Skip: skip,
},
},
ParsingTable: &spec.ParsingTable{
Action: action,
GoTo: goTo,
StateCount: tab.stateCount,
InitialState: tab.InitialState.Int(),
StartProduction: productionNumStart.Int(),
LHSSymbols: lhsSyms,
AlternativeSymbolCounts: altSymCounts,
Terminals: terms,
TerminalCount: tab.terminalCount,
NonTerminals: nonTerms,
NonTerminalCount: tab.nonTerminalCount,
EOFSymbol: symbolEOF.num().Int(),
},
ASTAction: &spec.ASTAction{
Entries: astActEnties,
},
}, nil
}
|