Skip to content

Commit

Permalink
Reviewed accept method
Browse files Browse the repository at this point in the history
  • Loading branch information
GiacomoCau committed Dec 11, 2021
1 parent fdec385 commit fa00152
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions src/util/LRParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import static util.Grammar.EndToken;
import static util.Grammar.StartRule;
import static util.LRParser.ActionType.Accept;
import static util.LRParser.ActionType.First;
import static util.LRParser.ActionType.Reduce;
import static util.LRParser.ActionType.Shift;

Expand All @@ -32,7 +33,7 @@ public abstract class LRParser<S extends State, I extends LR0Item> {
protected ActionGoToTable actionGoToTable = new ActionGoToTable();

public enum ActionType {
Accept("a"), Shift("s"), Reduce("r");
First("f"), Shift("s"), Reduce("r"), Accept("a");
String sigla;
ActionType(String sigla) {
this.sigla = sigla;
Expand Down Expand Up @@ -112,7 +113,7 @@ public String getLog() {
public boolean accept(String line) {
log.setLength(0);
String[] tokens = (line + " " + EndToken).split("\\s+");
Stack<String> symbols = new Stack<String>() {
Stack<String> symbols = new Stack<>() {
private static final long serialVersionUID = 1L;
@Override public String toString() { return join("", this); }
};
Expand All @@ -129,9 +130,9 @@ public boolean accept(String line) {
String format1 = format("%%%dd %%-%ds ", sSize, tSize);
String format2 = format("%%-%ds - %%%dd: %%-%ds | %%-8s | %%s\n", aSize, sSize, rSize);

String token = "";
int index = -1, state = -1;
Action action = new Action(Shift, 0);
String token = null;
int index = -1, state = 0;
Action action = new Action(First, state);
log.append(format(format("%%%ds %%-%ds %%-%ds - %%%ds: %%-%ds | %%-8s | %%s\n\n", sSize, tSize, aSize, sSize, rSize), "st", "tk", "action", "ns", "tk/rl", "symbols", "states"));
log.append(" ".repeat(sSize + tSize + 2));
loop: do {
Expand All @@ -141,23 +142,26 @@ public boolean accept(String line) {
return true;

case Shift:
states.push(state);
symbols.push(token);
state = states.push(action.operand);
log.append(format(format2, action.type, state, token=tokens[index += 1], symbols, states));
case First:
state = action.operand;
token = tokens[index += 1];
log.append(format(format2, action.type, state, token, symbols, states));
if (grammar.isTerminal(token)) break;
log.append(format(format1, state, token));
break loop;

case Reduce:
Rule rule = grammar.get(action.operand);
for (int i=0; i<rule.rhs.length; i+=1) { symbols.pop(); states.pop(); }
state = states.push(actionGoToTable.get(states.peek(), symbols.push(rule.lhs)));
symbols.pop(); for (int i=0; i<rule.rhs.length-1; i+=1) { states.pop(); symbols.pop(); }
state = actionGoToTable.get(states.peek(), symbols.push(rule.lhs));
log.append(format(format2, action.type + " " + action.operand, state, reduce(rule), symbols, states));
}
log.append(format(format1, state, token));
action = actionGoToTable.get(state, token);
}
while (index < tokens.length && action != null);
while (action != null && index < tokens.length);
log.append("Rejected.");
return false;
}
Expand Down

0 comments on commit fa00152

Please sign in to comment.