Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a package for computing follow sets of a grammar #74

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions internal/parser/first/first.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@ Returns First of the string, xyz, e.g.: for the item,
*/
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"]
Expand Down
145 changes: 145 additions & 0 deletions internal/parser/follow/follow.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
// Package follow implements routines for computation of follow sets for all the
// nonterminals in a given grammar.

package follow

import (
"bytes"
"fmt"

"github.com/goccmack/gocc/internal/ast"
"github.com/goccmack/gocc/internal/parser/first"
"github.com/goccmack/gocc/internal/parser/symbols"
)

type FollowSets struct {
followSets map[string]first.SymbolSet
symbols *symbols.Symbols
}

const (
EMPTY = "empty"
TERMINATOR = "$"
)

// GetFollowSets computes and returns the FollowSets of a Grammar.
func GetFollowSets(g *ast.Grammar, symbols *symbols.Symbols, firstSets *first.FirstSets) *FollowSets {
followSets := &FollowSets{
followSets: make(map[string]first.SymbolSet),
symbols: symbols,
}

if g.SyntaxPart == nil {
return followSets
}

// Place TERMINATOR in the follow set of the start symbol.
followSets.AddToken(g.SyntaxPart.ProdList[0].Id, TERMINATOR)

// The computation of follow sets is done using a set of rules mentioned
// in comments below. Rules 2 and 3 are interdependent, as a result of
// which computation of one rule might induce a change in follow sets,
// resulting in recomputation of the other rule. The computations are
// repeated until a fixed point is attained, after which the size of
// follow sets will remain constant and the value of variable `again`
// will be false. The fixed point will always exist since at the end of
// each computation the size of follow sets can either increase or
// remain constant, and will be bounded by the total number of terminals.
for again := true; again; {
again = false
for _, prod := range g.SyntaxPart.ProdList[0:] {
symbolCount := len(prod.Body.Symbols)
switch {
// (Rule 1) If there is a production A -> α B ß, then everything
// in first(ß) (except ε) is in follow(B).
// (Rule 2) If first(ß) contains ε, then follow(B) contains follow(A).
case symbolCount >= 2:
for k, v := range prod.Body.Symbols {
if !symbols.IsTerminal(v.SymbolString()) {
// beta represents ß.
beta := []string{}
for _, v := range prod.Body.Symbols[k+1:] {
beta = append(beta, v.SymbolString())
}
// firstBeta represents first(ß).
firstBeta := first.FirstS(firstSets, beta)
if _, contains := firstBeta[EMPTY]; !contains {
if !followSets.GetSet(v.SymbolString()).Equal(firstBeta) {
if followSets.AddSet(v.SymbolString(), firstBeta) {
again = true
}
}
} else {
// followA represents follow(A).
followA := followSets.GetSet(prod.Id)
B := prod.Body.Symbols[symbolCount-1]
// followB represents follow(B).
followB := followSets.GetSet(B.SymbolString())
if !followA.Equal(followB) {
if followSets.AddSet(B.SymbolString(), followA) {
again = true
}
}
}
}
}
// (Rule 3) If there is a production A -> α B, then follow(B) contains follow(A).
case !symbols.IsTerminal(prod.Body.Symbols[symbolCount-1].SymbolString()):
followA := followSets.GetSet(prod.Id)
B := prod.Body.Symbols[symbolCount-1]
followB := followSets.GetSet(B.SymbolString())
if !followA.Equal(followB) {
if followSets.AddSet(B.SymbolString(), followA) {
again = true
}
}
}
}
}

return followSets
}

// AddSet adds the follow-set of a nonterminal to FollowSets.
func (this *FollowSets) AddSet(prodName string, terminals first.SymbolSet) bool {
symbolsAdded := false
for symbol := range terminals {
if this.AddToken(prodName, symbol) {
symbolsAdded = true
}
}
return symbolsAdded
}

// AddToken adds a terminal to the FollowSet of a nonterminal.
func (this *FollowSets) AddToken(prodName string, terminal string) bool {
symbolAdded := false
set, ok := this.followSets[prodName]
if !ok {
set = make(first.SymbolSet)
this.followSets[prodName] = set
}
if _, contain := set[terminal]; !contain {
set[terminal] = true
symbolAdded = true
}
return symbolAdded
}

// GetSet returns the FollowSet for a nonterminal.
func (this *FollowSets) GetSet(prodName string) first.SymbolSet {
if set, ok := this.followSets[prodName]; ok {
return set
}
return nil
}

// String returns a string representing the FollowSets.
func (this *FollowSets) String() string {
buf := new(bytes.Buffer)
for _, nt := range this.symbols.NTList() {
set := this.followSets[nt]
fmt.Fprintf(buf, "%s: %s\n", nt, set)
}
return buf.String()
}
6 changes: 6 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
genLexer "github.com/goccmack/gocc/internal/lexer/gen/golang"
lexItems "github.com/goccmack/gocc/internal/lexer/items"
"github.com/goccmack/gocc/internal/parser/first"
"github.com/goccmack/gocc/internal/parser/follow"
lr1Action "github.com/goccmack/gocc/internal/parser/lr1/action"
lr1Items "github.com/goccmack/gocc/internal/parser/lr1/items"
"github.com/goccmack/gocc/internal/parser/symbols"
Expand Down Expand Up @@ -102,6 +103,11 @@ func main() {
io.WriteFileString(path.Join(cfg.OutDir(), "first.txt"), firstSets.String())
}

followSets := follow.GetFollowSets(g, gSymbols, firstSets)
if cfg.Verbose() {
io.WriteFileString(path.Join(cfg.OutDir(), "follow.txt"), followSets.String())
}

lr1Sets := lr1Items.GetItemSets(g, gSymbols, firstSets)
if cfg.Verbose() {
io.WriteFileString(path.Join(cfg.OutDir(), "LR1_sets.txt"), lr1Sets.String())
Expand Down