| 1 | //Copyright 2013 Vastech SA (PTY) LTD |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package first |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
| 19 | "strings" |
| 20 | |
| 21 | "github.com/goccmack/gocc/internal/ast" |
| 22 | "github.com/goccmack/gocc/internal/parser/symbols" |
| 23 | ) |
| 24 | |
| 25 | // FirstSets represents a map of id->production. |
| 26 | type FirstSets struct { |
| 27 | firstSets map[string]SymbolSet |
| 28 | symbols *symbols.Symbols |
| 29 | } |
| 30 | |
| 31 | const EMPTY = "empty" |
| 32 | |
| 33 | // GetFirstSets returns the FirstSets of the Grammar. |
| 34 | func GetFirstSets(g *ast.Grammar, symbols *symbols.Symbols) *FirstSets { |
| 35 | firstSets := &FirstSets{ |
| 36 | firstSets: make(map[string]SymbolSet), |
| 37 | symbols: symbols, |
| 38 | } |
| 39 | |
| 40 | if g.SyntaxPart == nil { |
| 41 | return firstSets |
| 42 | } |
| 43 | |
| 44 | for again := true; again; { |
| 45 | again = false |
| 46 | for _, prod := range g.SyntaxPart.ProdList { |
| 47 | switch { |
| 48 | case len(prod.Body.Symbols) == 0: |
| 49 | if firstSets.AddToken(prod.Id, EMPTY) { |
| 50 | again = true |
| 51 | } |
| 52 | case symbols.IsTerminal(prod.Body.Symbols[0].SymbolString()): |
| 53 | if firstSets.AddToken(prod.Id, prod.Body.Symbols[0].SymbolString()) { |
| 54 | again = true |
| 55 | } |
| 56 | default: |
| 57 | first := FirstS(firstSets, stringList(prod.Body.Symbols)) |
| 58 | if !first.Equal(firstSets.GetSet(prod.Id)) { |
| 59 | if firstSets.AddSet(prod.Id, first) { |
| 60 | again = true |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | return firstSets |
| 68 | } |
| 69 | |
| 70 | func stringList(symbols []ast.SyntaxSymbol) []string { |
| 71 | sl := make([]string, len(symbols)) |
| 72 | for i, sym := range symbols { |
| 73 | sl[i] = sym.SymbolString() |
| 74 | } |
| 75 | return sl |
| 76 | } |
| 77 | |
| 78 | func (this *FirstSets) AddSet(prodName string, terminals SymbolSet) (symbolsAdded bool) { |
| 79 | for symbol := range terminals { |
| 80 | if this.AddToken(prodName, symbol) { |
| 81 | symbolsAdded = true |
| 82 | } |
| 83 | } |
| 84 | return |
| 85 | } |
| 86 | |
| 87 | func (this *FirstSets) AddToken(prodName string, terminal string) (symbolAdded bool) { |
| 88 | set, ok := this.firstSets[prodName] |
| 89 | if !ok { |
| 90 | set = make(SymbolSet) |
| 91 | this.firstSets[prodName] = set |
| 92 | } |
| 93 | if _, contain := set[terminal]; !contain { |
| 94 | set[terminal] = true |
| 95 | symbolAdded = true |
| 96 | } |
| 97 | return |
| 98 | } |
| 99 | |
| 100 | // GetSet will return the SymbolSet corresponding to `prodName` or nil. |
| 101 | func (this *FirstSets) GetSet(prodName string) SymbolSet { |
| 102 | if set, ok := this.firstSets[prodName]; ok { |
| 103 | return set |
| 104 | } |
| 105 | return nil |
| 106 | } |
| 107 | |
| 108 | // String will return the string representing the FirstSets. |
| 109 | func (this *FirstSets) String() string { |
| 110 | buf := new(strings.Builder) |
| 111 | for _, nt := range this.symbols.NTList() { |
| 112 | set := this.firstSets[nt] |
| 113 | fmt.Fprintf(buf, "%s: %s\n", nt, set) |
| 114 | } |
| 115 | return buf.String() |
| 116 | } |
| 117 | |
| 118 | // First returns the first `SymbolSet` given the ast.SyntaxSymbol. |
| 119 | func First(fs *FirstSets, sym string) SymbolSet { |
| 120 | if fs.symbols.IsTerminal(sym) { |
| 121 | return SymbolSet{sym: true} |
| 122 | } |
| 123 | return fs.GetSet(sym) |
| 124 | } |
| 125 | |
| 126 | /* |
| 127 | Returns First of the string, xyz, e.g.: for the item, |
| 128 | |
| 129 | X : w • xyz |
| 130 | |
| 131 | Let x, y, z be strings from the union of T and NT. |
| 132 | First(xy...z) = |
| 133 | First(x) if First(x) does not contain ϵ |
| 134 | First(x) + First(y) if First(x) contains ϵ but First(y) does not contain ϵ |
| 135 | ... |
| 136 | First(x) + First(y) + ... + First(z) |
| 137 | */ |
| 138 | func FirstS(firstSets *FirstSets, symbols []string) (first SymbolSet) { |
| 139 | first = make(SymbolSet) |
| 140 | if len(symbols) == 0 { |
| 141 | return |
| 142 | } |
| 143 | fst := First(firstSets, symbols[0]) |
| 144 | first.AddSet(fst) |
| 145 | _, containEmpty := fst["empty"] |
| 146 | for i := 1; i < len(symbols) && containEmpty; i++ { |
| 147 | fst = First(firstSets, symbols[i]) |
| 148 | first.AddSet(fst) |
| 149 | _, containEmpty = fst["empty"] |
| 150 | } |
| 151 | if !containEmpty { |
| 152 | delete(first, "empty") |
| 153 | } |
| 154 | return |
| 155 | } |