Skip to content

Commit

Permalink
Merge pull request #3 from ironmeld/fix-follow-epsilon
Browse files Browse the repository at this point in the history
Fix problems with follow sets, epsilon, and the LR0 action table.
  • Loading branch information
amirhossein-hkh authored Feb 22, 2020
2 parents 1371bf1 + e81d26a commit 40f1271
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/lr0/LR0Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ private boolean createActionTableForLR0() {
for (int i = 0; i < canonicalCollection.size(); i++) {
for (LR0Item item : canonicalCollection.get(i).getItems()) {
if (item.getDotPointer() == item.getRightSide().length) {
if (item.getLeftSide().equals("s'")) {
if (item.getLeftSide().equals("S'")) {
actionTable[i].put("$", new Action(ActionType.ACCEPT, 0));
} else {
HashSet<String> terminals = grammar.getTerminals();
Expand Down
7 changes: 6 additions & 1 deletion src/lr1/LR1State.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,12 @@ private void closure(Grammar grammar) {
}
HashSet<Rule> rules = grammar.getRuledByLeftVariable(item.getCurrent());
for(Rule rule : rules){
temp.add(new LR1Item(rule.getLeftSide(),rule.getRightSide(),0,lookahead));
String[] rhs = rule.getRightSide();
int finished = 0;
if (rhs.length == 1 && rhs[0].equals("epsilon")) {
finished = 1;
}
temp.add(new LR1Item(rule.getLeftSide(),rhs,finished,lookahead));
}
}
}
Expand Down
21 changes: 12 additions & 9 deletions src/util/Grammar.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ public Grammar(String s) {
for (String rule : rulesRightSide) {
String[] rightSide = rule.trim().split("\\s+");
for (String terminal : rightSide) {
terminals.add(terminal);
if (!terminal.equals("epsilon")) {
terminals.add(terminal);
}
}

if (line == 0) {
Expand Down Expand Up @@ -123,18 +125,19 @@ private void computeFollowSet() {
for (Rule rule : rules) {
for (int i = 0; i < rule.getRightSide().length; i++) {
if (rule.getRightSide()[i].equals(variable)) {
HashSet<String> first;
if (i == rule.getRightSide().length - 1) {
fallowSets.get(variable).addAll(fallowSets.get(rule.leftSide));
first = fallowSets.get(rule.leftSide);
} else {
HashSet<String> first = computeFirst(rule.getRightSide(), i + 1);
first = computeFirst(rule.getRightSide(), i + 1);
if (first.contains("epsilon")) {
first.remove("epsilon");
first.addAll(fallowSets.get(rule.leftSide));
}
if (!fallowSets.get(variable).containsAll(first)) {
isChange = true;
fallowSets.get(variable).addAll(first);
}
}
if (!fallowSets.get(variable).containsAll(first)) {
isChange = true;
fallowSets.get(variable).addAll(first);
}
}
}
Expand All @@ -151,7 +154,7 @@ public HashSet<String> computeFirst(String[] string, int index) {
if (index == string.length) {
return first;
}
if (terminals.contains(string[index])) {
if (terminals.contains(string[index]) || string[index].equals("epsilon")) {
first.add(string[index]);
return first;
}
Expand All @@ -164,8 +167,8 @@ public HashSet<String> computeFirst(String[] string, int index) {

if (first.contains("epsilon")) {
if (index != string.length - 1) {
first.addAll(computeFirst(string, index + 1));
first.remove("epsilon");
first.addAll(computeFirst(string, index + 1));
}
}
return first;
Expand Down

0 comments on commit 40f1271

Please sign in to comment.