10
10
import static util .Grammar .EndToken ;
11
11
import static util .Grammar .StartRule ;
12
12
import static util .LRParser .ActionType .Accept ;
13
+ import static util .LRParser .ActionType .First ;
13
14
import static util .LRParser .ActionType .Reduce ;
14
15
import static util .LRParser .ActionType .Shift ;
15
16
@@ -32,7 +33,7 @@ public abstract class LRParser<S extends State, I extends LR0Item> {
32
33
protected ActionGoToTable actionGoToTable = new ActionGoToTable ();
33
34
34
35
public enum ActionType {
35
- Accept ( "a " ), Shift ("s" ), Reduce ("r" );
36
+ First ( "f " ), Shift ("s" ), Reduce ("r" ), Accept ( "a " );
36
37
String sigla ;
37
38
ActionType (String sigla ) {
38
39
this .sigla = sigla ;
@@ -112,7 +113,7 @@ public String getLog() {
112
113
public boolean accept (String line ) {
113
114
log .setLength (0 );
114
115
String [] tokens = (line + " " + EndToken ).split ("\\ s+" );
115
- Stack <String > symbols = new Stack <String >() {
116
+ Stack <String > symbols = new Stack <>() {
116
117
private static final long serialVersionUID = 1L ;
117
118
@ Override public String toString () { return join ("" , this ); }
118
119
};
@@ -129,9 +130,9 @@ public boolean accept(String line) {
129
130
String format1 = format ("%%%dd %%-%ds " , sSize , tSize );
130
131
String format2 = format ("%%-%ds - %%%dd: %%-%ds | %%-8s | %%s\n " , aSize , sSize , rSize );
131
132
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 );
135
136
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" ));
136
137
log .append (" " .repeat (sSize + tSize + 2 ));
137
138
loop : do {
@@ -141,23 +142,26 @@ public boolean accept(String line) {
141
142
return true ;
142
143
143
144
case Shift :
145
+ states .push (state );
144
146
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 ));
147
151
if (grammar .isTerminal (token )) break ;
148
152
log .append (format (format1 , state , token ));
149
153
break loop ;
150
154
151
155
case Reduce :
152
156
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 ));
155
159
log .append (format (format2 , action .type + " " + action .operand , state , reduce (rule ), symbols , states ));
156
160
}
157
161
log .append (format (format1 , state , token ));
158
162
action = actionGoToTable .get (state , token );
159
163
}
160
- while (index < tokens . length && action != null );
164
+ while (action != null && index < tokens . length );
161
165
log .append ("Rejected." );
162
166
return false ;
163
167
}
0 commit comments