forked from goccmack/gocc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
first.go
155 lines (137 loc) · 3.8 KB
/
first.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
//Copyright 2013 Vastech SA (PTY) LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package first
import (
"fmt"
"strings"
"github.com/Desuuuu/gocc/internal/ast"
"github.com/Desuuuu/gocc/internal/parser/symbols"
)
// FirstSets represents a map of id->production.
type FirstSets struct {
firstSets map[string]SymbolSet
symbols *symbols.Symbols
}
const EMPTY = "empty"
// GetFirstSets returns the FirstSets of the Grammar.
func GetFirstSets(g *ast.Grammar, symbols *symbols.Symbols) *FirstSets {
firstSets := &FirstSets{
firstSets: make(map[string]SymbolSet),
symbols: symbols,
}
if g.SyntaxPart == nil {
return firstSets
}
for again := true; again; {
again = false
for _, prod := range g.SyntaxPart.ProdList {
switch {
case len(prod.Body.Symbols) == 0:
if firstSets.AddToken(prod.Id, EMPTY) {
again = true
}
case symbols.IsTerminal(prod.Body.Symbols[0].SymbolString()):
if firstSets.AddToken(prod.Id, prod.Body.Symbols[0].SymbolString()) {
again = true
}
default:
first := FirstS(firstSets, stringList(prod.Body.Symbols))
if !first.Equal(firstSets.GetSet(prod.Id)) {
if firstSets.AddSet(prod.Id, first) {
again = true
}
}
}
}
}
return firstSets
}
func stringList(symbols []ast.SyntaxSymbol) []string {
sl := make([]string, len(symbols))
for i, sym := range symbols {
sl[i] = sym.SymbolString()
}
return sl
}
func (this *FirstSets) AddSet(prodName string, terminals SymbolSet) (symbolsAdded bool) {
for symbol := range terminals {
if this.AddToken(prodName, symbol) {
symbolsAdded = true
}
}
return
}
func (this *FirstSets) AddToken(prodName string, terminal string) (symbolAdded bool) {
set, ok := this.firstSets[prodName]
if !ok {
set = make(SymbolSet)
this.firstSets[prodName] = set
}
if _, contain := set[terminal]; !contain {
set[terminal] = true
symbolAdded = true
}
return
}
// GetSet will return the SymbolSet corresponding to `prodName` or nil.
func (this *FirstSets) GetSet(prodName string) SymbolSet {
if set, ok := this.firstSets[prodName]; ok {
return set
}
return nil
}
// String will return the string representing the FirstSets.
func (this *FirstSets) String() string {
buf := new(strings.Builder)
for _, nt := range this.symbols.NTList() {
set := this.firstSets[nt]
fmt.Fprintf(buf, "%s: %s\n", nt, set)
}
return buf.String()
}
// First returns the first `SymbolSet` given the ast.SyntaxSymbol.
func First(fs *FirstSets, sym string) SymbolSet {
if fs.symbols.IsTerminal(sym) {
return SymbolSet{sym: true}
}
return fs.GetSet(sym)
}
/*
Returns First of the string, xyz, e.g.: for the item,
X : w • xyz
Let x, y, z be strings from the union of T and NT.
First(xy...z) =
First(x) if First(x) does not contain ϵ
First(x) + First(y) if First(x) contains ϵ but First(y) does not contain ϵ
...
First(x) + First(y) + ... + First(z)
*/
func FirstS(firstSets *FirstSets, symbols []string) (first SymbolSet) {
first = make(SymbolSet)
if len(symbols) == 0 {
return
}
fst := First(firstSets, symbols[0])
first.AddSet(fst)
_, containEmpty := fst["empty"]
for i := 1; i < len(symbols) && containEmpty; i++ {
fst = First(firstSets, symbols[i])
first.AddSet(fst)
_, containEmpty = fst["empty"]
}
if !containEmpty {
delete(first, "empty")
}
return
}