This repository has been archived by the owner on Nov 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
ParserGenerator.java
317 lines (279 loc) · 8.59 KB
/
ParserGenerator.java
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
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Hashtable;
import java.util.LinkedList;
/**
* Class generates the parse table.
*
* @author Logan Blyth, James McCarty, & Robert Rayborn
*
*/
public class ParserGenerator
{
private Hashtable<String, NonterminalRule> nonterminalRuleHT;
private Hashtable<String, TerminalRule> terminalRuleHT;
private LinkedList<NonterminalRule> nonterminalRules;
private LinkedList<TerminalRule> terminalRules;
private ParseTable parseTable;
/**
* Sets up class variables
* @param terminals - names of the terminals used
* @param nonterminals - names of the nonterminals used
* @param start - name of the starting nonterminal
*/
public ParserGenerator(String[] terminals, String[] nonterminals, String start)
{
// create variables
this.nonterminalRuleHT = new Hashtable<String, NonterminalRule>();
this.terminalRuleHT = new Hashtable<String, TerminalRule>();
this.nonterminalRules = new LinkedList<NonterminalRule>();
this.terminalRules = new LinkedList<TerminalRule>();
// add terminals to hash table
for(String terminal : terminals)
{
if (terminal.equals(EpsilonRule.getEpsilonRule().getSymbol()))
this.terminalRuleHT.put(terminal, EpsilonRule.getEpsilonRule());
else
this.terminalRuleHT.put(terminal, new TerminalRule(terminal));
}
// add nonterminals to hash table
for(String nonterminal : nonterminals)
{
this.nonterminalRuleHT.put(nonterminal, new NonterminalRule(nonterminal));
}
// set up terminal dollar rule and add it to the hash table
TerminalRule dollarRule = new TerminalRule("$");
this.terminalRuleHT.put("$", dollarRule);
// set up starting rule
NonterminalRule startingRule = this.nonterminalRuleHT.get(start);
startingRule.getFollow().add(dollarRule);
// create parse table
this.parseTable = new ParseTable(startingRule, dollarRule);
}
public void addGrammarRule(String symbol, String[] productionRuleStrings)
{
if( !this.nonterminalRuleHT.containsKey(symbol) ) // the symbol doesn't have a grammar rule yet
{
throw new RuntimeException("\n Error Parsing Rules. \n" +
"Symbol " + symbol + " is not in the grammar.");
}
else if(symbol.equals("$"))
{
throw new RuntimeException("\n Error Parsing Rules. \n" +
"Symbol $ is a reserved symbol.");
}
// generate a production rule for the new symbol
LinkedList<GrammarRule> productionRule = new LinkedList<GrammarRule>();
for(String productionRuleString : productionRuleStrings)
{
if(this.nonterminalRuleHT.containsKey(productionRuleString))
productionRule.add(this.nonterminalRuleHT.get(productionRuleString));
else if(this.terminalRuleHT.containsKey(productionRuleString))
productionRule.add(this.terminalRuleHT.get(productionRuleString));
else
throw new RuntimeException("\n Error Parsing Rules. \n"
+ "Symbol " + productionRuleString + " is not in the grammar.");
}
// add the production rule to the symbol's grammar rule
this.nonterminalRuleHT.get(symbol).addProductionRule(productionRule);
}
public void buildParseTable()
{
this.stepIIa();
this.stepIIb();
}
public ParseTable getParseTable()
{
return this.parseTable;
}
//=======PRINTING FUNCTIONS=======================
public void printHash()
{
for( int i = 0 ; i < this.nonterminalRules.size() ; i++ )
{
System.out.println(this.nonterminalRules.get(i).detailedToString());
}
System.out.println();
}
public void printFirst()
{
for(NonterminalRule nonterminal : this.nonterminalRules)
{
nonterminal.printFirst();
}
System.out.println();
}
public void printFollow()
{
for(NonterminalRule nonterminal : this.nonterminalRules)
{
nonterminal.printFollow();
}
System.out.println();
}
public void printParseTable()
{
for(NonterminalRule nonterminal : this.nonterminalRules)
{
System.out.println("NONTERM: "+nonterminal.getSymbol());
for(TerminalRule terminal : this.terminalRules)
{
System.out.println(" TERM: "+terminal.getSymbol());
System.out.println(" "+this.parseTable.get(nonterminal, terminal));
}
System.out.println();
}
System.out.println();
}
//================================================================
//============== Step II(a) stuff ================================
//================================================================
/**
* Runs all the step IIa stuff
*/
public void stepIIa()
{
this.convertHashTablesToLists();
if(ParserDriver.verboseOn())
{
System.out.println("Current Grammar Rules:");
this.printHash();
}
this.removeSelfLeftRecursion();
if(ParserDriver.verboseOn())
{
System.out.println("Recursion Removed; New Grammar Rules:");
this.printHash();
}
this.removeCommonPrefixes();
if(ParserDriver.verboseOn())
{
System.out.println("Common Prefixes Removed; New Grammar Rules:");
this.printHash();
}
}
public void convertHashTablesToLists()
{
this.nonterminalRules.addAll(this.nonterminalRuleHT.values());
this.terminalRules.addAll(this.terminalRuleHT.values());
// no longer need hash tables
this.nonterminalRuleHT.clear();
this.terminalRuleHT.clear();
}
public void removeSelfLeftRecursion()
{
NonterminalRule newRule, current;
for(int i = 0 ; i < this.nonterminalRules.size() ; i++)
{
current = this.nonterminalRules.get(i);
newRule = current.removeSelfLeftRecursion();
if(newRule != null){
this.nonterminalRules.addLast(newRule);
}
}
}
public void removeCommonPrefixes()
{
LinkedList<NonterminalRule> newGrammarRules;
for(int i = 0 ; i < this.nonterminalRules.size() ; i++) // can't do for each since list is changing
{
newGrammarRules = this.nonterminalRules.get(i).removeCommonPrefixes();
if(newGrammarRules != null){
this.nonterminalRules.addAll(newGrammarRules);
}
}
}
//================================================================
//============== Step II(b) stuff ================================
//================================================================
/**
* Runs all the step IIb stuff
*/
public void stepIIb()
{
this.constructFirst();
if(ParserDriver.verboseOn())
{
System.out.println("Constructed First Set:");
this.printFirst();
}
this.constructFollow();
if(ParserDriver.verboseOn())
{
System.out.println("Constructed Follow Set:");
this.printFollow();
}
this.createParseTable();
}
public void constructFirst()
{
boolean wasChanged = true;
while(wasChanged)
{
wasChanged = false;
for(NonterminalRule nonterminal : this.nonterminalRules)
{
wasChanged = nonterminal.constructFirst(wasChanged);
}
}
}
public void constructFollow()
{
boolean wasChanged = true;
while(wasChanged)
{
wasChanged = false;
for(NonterminalRule nonterminal : this.nonterminalRules)
{
wasChanged = nonterminal.constructFollow(wasChanged);
}
}
}
public void createParseTable()
{
this.terminalRules.remove(EpsilonRule.getEpsilonRule());
for(NonterminalRule nonterminal : this.nonterminalRules)
{
for(TerminalRule terminal : this.terminalRules)
{
this.parseTable.put(nonterminal, terminal, nonterminal.getProductionRuleForTerminal(terminal));
}
if(nonterminal.getProductionRuleForTerminal(EpsilonRule.getEpsilonRule()) != null)
{
LinkedList<TerminalRule> followList = nonterminal.getFollow();
for(TerminalRule followEntry : followList)
{
this.parseTable.put(nonterminal, followEntry, EpsilonRule.getEpsilonList());
}
}
}
}
public void saveParseTable(String fileName) throws IOException
{
FileWriter fstream = new FileWriter(fileName);
BufferedWriter out = new BufferedWriter(fstream);
// Line 1
for(TerminalRule terminal : this.terminalRules)
{
out.write(","+terminal.getSymbol());
}
out.write("\n");
// Other lines
LinkedList<GrammarRule> productionRule;
for(NonterminalRule nonterminal : this.nonterminalRules)
{
out.write(nonterminal.getSymbol());
for(TerminalRule terminal : this.terminalRules)
{
productionRule = this.parseTable.get(nonterminal,terminal);
if(productionRule == null)
out.write(",");
else
out.write(","+productionRule.toString().replace(",", " "));
}
out.write("\n");
}
out.close();
}
}