forked from simoneguidi94/gopapageno
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator.go
155 lines (128 loc) · 3.8 KB
/
generator.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
package generator
import (
"fmt"
"github.com/filgor84/gogopapageno/generator/regex"
)
func Generate(lexerFilename string, parserFilename string, outdir string) {
lexRules, cutPoints, lexCode := parseLexer(lexerFilename)
fmt.Printf("Lex rules (%d):\n", len(lexRules))
for _, r := range lexRules {
fmt.Println(r)
}
fmt.Printf("Cut points regex: %s\n", cutPoints)
fmt.Println("Lex code:")
fmt.Println(lexCode)
var dfa regex.Dfa
if len(lexRules) > 0 {
var nfa *regex.Nfa
success, result := regex.ParseString([]byte(lexRules[0].Regex), 1)
if success {
nfa = result.Value.(*regex.Nfa)
nfa.AddAssociatedRule(0)
} else {
fmt.Println("Error: could not parse the following regular expression:", lexRules[0].Regex)
return
}
for i := 1; i < len(lexRules); i++ {
var curNfa *regex.Nfa
success, result = regex.ParseString([]byte(lexRules[i].Regex), 1)
if success {
curNfa = result.Value.(*regex.Nfa)
curNfa.AddAssociatedRule(i)
nfa.Unite(*curNfa)
} else {
fmt.Println("Error: could not parse the following regular expression:", lexRules[i].Regex)
return
}
}
dfa = nfa.ToDfa()
/*ok, hasRuleNum, ruleNum := dfa.Check([]byte(" "))
if ok {
fmt.Println("Ok")
if hasRuleNum {
fmt.Println("RuleNum:", ruleNum)
} else {
fmt.Println("No rule")
}
} else {
fmt.Println("Not ok")
}*/
} else {
fmt.Println("Error: the lexer does not contain any rule")
return
}
var cutPointsDfa regex.Dfa
if cutPoints == "" {
cutPointsNfa := regex.NewEmptyStringNfa()
cutPointsDfa = cutPointsNfa.ToDfa()
} else {
var cutPointsNfa *regex.Nfa
success, result := regex.ParseString([]byte(cutPoints), 1)
if success {
cutPointsNfa = result.Value.(*regex.Nfa)
} else {
fmt.Println("Error: could not parse the following regular expression:", cutPoints)
return
}
cutPointsDfa = cutPointsNfa.ToDfa()
}
parserPreamble, axiom, rules := parseGrammar(parserFilename)
fmt.Println("Go preamble:")
fmt.Println(parserPreamble)
if axiom == "" {
fmt.Println("Error: the axiom is not defined")
return
} else {
fmt.Println("Axiom:", axiom)
}
fmt.Printf("Rules (%d):\n", len(rules))
for _, r := range rules {
fmt.Println(r)
}
nonterminals, terminals := inferTokens(rules)
fmt.Printf("Nonterminals (%d): %s\n", len(nonterminals), nonterminals)
fmt.Printf("Terminals (%d): %s\n", len(terminals), terminals)
if !checkAxiomUsage(rules, axiom) {
fmt.Println("Error: the axiom isn't used in any rule")
return
}
newRules, newNonterminals := deleteRepeatedRHS(nonterminals, terminals, axiom, rules)
fmt.Printf("New rules after elimination of repeated rhs (%d):\n", len(newRules))
for _, r := range newRules {
fmt.Println(r)
}
fmt.Printf("New nonterminals (%d): %s\n", len(newNonterminals), newNonterminals)
precMatrix, err := createPrecMatrix(newRules, newNonterminals, terminals)
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Println("Precedence matrix:")
fmt.Println(precMatrix)
sortedRules := sortRulesByRHS(newRules, newNonterminals, terminals)
fmt.Printf("Sorted rules (%d):\n", len(sortedRules))
for _, r := range sortedRules {
fmt.Println(r)
}
err = emitOutputFolder(outdir)
handleEmissionError(err)
err = emitLexerFunction(outdir, lexCode, lexRules)
handleEmissionError(err)
err = emitLexerAutomata(outdir, dfa, cutPointsDfa)
handleEmissionError(err)
err = emitTokens(outdir, newNonterminals, terminals)
handleEmissionError(err)
err = emitRules(outdir, sortedRules, newNonterminals, terminals)
handleEmissionError(err)
err = emitFunction(outdir, parserPreamble, sortedRules)
handleEmissionError(err)
err = emitPrecMatrix(outdir, terminals, precMatrix)
handleEmissionError(err)
err = emitCommonFiles(outdir)
handleEmissionError(err)
}
func handleEmissionError(e error) {
if e != nil {
fmt.Println(e.Error())
}
}