Skip to content

Commit fa00152

Browse files
committed
Reviewed accept method
1 parent fdec385 commit fa00152

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

src/util/LRParser.java

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import static util.Grammar.EndToken;
1111
import static util.Grammar.StartRule;
1212
import static util.LRParser.ActionType.Accept;
13+
import static util.LRParser.ActionType.First;
1314
import static util.LRParser.ActionType.Reduce;
1415
import static util.LRParser.ActionType.Shift;
1516

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

3435
public enum ActionType {
35-
Accept("a"), Shift("s"), Reduce("r");
36+
First("f"), Shift("s"), Reduce("r"), Accept("a");
3637
String sigla;
3738
ActionType(String sigla) {
3839
this.sigla = sigla;
@@ -112,7 +113,7 @@ public String getLog() {
112113
public boolean accept(String line) {
113114
log.setLength(0);
114115
String[] tokens = (line + " " + EndToken).split("\\s+");
115-
Stack<String> symbols = new Stack<String>() {
116+
Stack<String> symbols = new Stack<>() {
116117
private static final long serialVersionUID = 1L;
117118
@Override public String toString() { return join("", this); }
118119
};
@@ -129,9 +130,9 @@ public boolean accept(String line) {
129130
String format1 = format("%%%dd %%-%ds ", sSize, tSize);
130131
String format2 = format("%%-%ds - %%%dd: %%-%ds | %%-8s | %%s\n", aSize, sSize, rSize);
131132

132-
String token = "";
133-
int index = -1, state = -1;
134-
Action action = new Action(Shift, 0);
133+
String token = null;
134+
int index = -1, state = 0;
135+
Action action = new Action(First, state);
135136
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"));
136137
log.append(" ".repeat(sSize + tSize + 2));
137138
loop: do {
@@ -141,23 +142,26 @@ public boolean accept(String line) {
141142
return true;
142143

143144
case Shift:
145+
states.push(state);
144146
symbols.push(token);
145-
state = states.push(action.operand);
146-
log.append(format(format2, action.type, state, token=tokens[index += 1], symbols, states));
147+
case First:
148+
state = action.operand;
149+
token = tokens[index += 1];
150+
log.append(format(format2, action.type, state, token, symbols, states));
147151
if (grammar.isTerminal(token)) break;
148152
log.append(format(format1, state, token));
149153
break loop;
150154

151155
case Reduce:
152156
Rule rule = grammar.get(action.operand);
153-
for (int i=0; i<rule.rhs.length; i+=1) { symbols.pop(); states.pop(); }
154-
state = states.push(actionGoToTable.get(states.peek(), symbols.push(rule.lhs)));
157+
symbols.pop(); for (int i=0; i<rule.rhs.length-1; i+=1) { states.pop(); symbols.pop(); }
158+
state = actionGoToTable.get(states.peek(), symbols.push(rule.lhs));
155159
log.append(format(format2, action.type + " " + action.operand, state, reduce(rule), symbols, states));
156160
}
157161
log.append(format(format1, state, token));
158162
action = actionGoToTable.get(state, token);
159163
}
160-
while (index < tokens.length && action != null);
164+
while (action != null && index < tokens.length);
161165
log.append("Rejected.");
162166
return false;
163167
}

0 commit comments

Comments
 (0)