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
/
ParseTable.java
99 lines (85 loc) · 2.67 KB
/
ParseTable.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
import java.util.Hashtable;
import java.util.LinkedList;
/**
* Data structure for the parse table
*
* @author Logan Blyth, James McCarty, & Robert Rayborn
*
*/
public class ParseTable
{
/**
* Hash table that contains all the entries in the table
* Key is given by the concatenation of the nonterminal symbol string
* and the terminal symbol string.
*/
private Hashtable<String,LinkedList<GrammarRule>> entries;
/**
* The starting rule of the grammar
*/
private NonterminalRule start;
private TerminalRule dollar;
public ParseTable(NonterminalRule start, TerminalRule dollar)
{
entries = new Hashtable<String,LinkedList<GrammarRule>>();
this.start = start;
this.dollar = dollar;
}
/**
* Adds the grammar rule to the slot corresponding to the keys. Does not add if the input rule is null.
* @param nonterminalKey - nonterminal rule.
* @param terminalKey - terminal rule.
* @param value - production rule list.
*/
public void put(NonterminalRule nonterminalKey, TerminalRule terminalKey, LinkedList<GrammarRule> value)
{
if(value==null)
return;
this.entries.put(nonterminalKey.getSymbol()+terminalKey.getSymbol(), value);
}
/**
* Gets the production rule list at the slot corresponding to the keys.
* @param nonterminalKey - nonterminal rule.
* @param terminalKey - terminal rule.
* @return production rule list.
*/
public LinkedList<GrammarRule> get(NonterminalRule nonterminalKey, TerminalRule terminalKey)
{
return this.entries.get(nonterminalKey.getSymbol()+terminalKey.getSymbol());
}
/**
* Gets the production rule list at the slot corresponding to the keys.
* @param nonterminalKey - nonterminal rule.
* @param terminalKey - terminal string.
* @return production rule list.
*/
public LinkedList<GrammarRule> get(NonterminalRule nonterminalKey, String terminalString)
{
return this.entries.get(nonterminalKey.getSymbol()+terminalString);
}
/**
* Removes the production rule list at the slot corresponding to the keys.
* @param nonterminalKey - nonterminal list.
* @param terminalKey - terminal list.
* @return production rule list.
*/
public LinkedList<GrammarRule> remove(NonterminalRule nonterminalKey, TerminalRule terminalKey)
{
return this.entries.remove(nonterminalKey.getSymbol()+terminalKey.getSymbol());
}
//====== Getters & Setters ======
public NonterminalRule getStart()
{
return start;
}
public void setStart(NonterminalRule start)
{
this.start = start;
}
public TerminalRule getDollar() {
return dollar;
}
public void setDollar(TerminalRule dollar) {
this.dollar = dollar;
}
}