KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ro > infoiasi > donald > compiler > parser > AbstractLR1Parser


1 package ro.infoiasi.donald.compiler.parser;
2
3 import ro.infoiasi.donald.compiler.cfg.*;
4 import java.io.*;
5 import java.util.*;
6
7 public abstract class AbstractLR1Parser extends AbstractParser {
8     public AbstractLR1Parser(ParserSpec spec) {
9         super(spec, true);
10     }
11
12     public AbstractLR1Parser(String JavaDoc parserSpecPath)
13             throws IOException, SpecParseException {
14         super(parserSpecPath, true);
15     }
16
17     protected int stateNo;
18     protected ParseAction[][] actionTable;
19     protected Integer JavaDoc[][] gotoTable;
20     protected Integer JavaDoc startStateIndex;
21
22     public ParseAction getAction(int state, Terminal a) {
23         return actionTable[state][a.getIndex()];
24     }
25     
26     public int getGoto(int state, NonTerminal x) {
27         Integer JavaDoc newState = gotoTable[state][x.getIndex()];
28         if (newState != null) {
29             return newState.intValue();
30         } else {
31             return -1;
32         }
33     }
34     
35     public Production getStartProduction() {
36         return sp;
37     }
38
39     public int getStateNo() {
40         return stateNo;
41     }
42     
43     public int getStartState() {
44         return startStateIndex.intValue();
45     }
46
47     public interface ParseAction {
48         boolean isShiftAction();
49         boolean isReduceAction();
50     }
51
52     public class ShiftAction implements ParseAction {
53         private Integer JavaDoc stateIndex;
54         public ShiftAction(Integer JavaDoc stateIndex) {
55             this.stateIndex = stateIndex;
56         }
57         public Integer JavaDoc getStateIndex() {
58             return stateIndex;
59         }
60         public boolean isShiftAction() {
61             return true;
62         }
63         public boolean isReduceAction() {
64             return false;
65         }
66         public String JavaDoc toString() {
67             return stateIndex.toString();
68         }
69     }
70
71     public class ReduceAction implements ParseAction {
72         private Production production;
73         public ReduceAction(Production production) {
74             this.production = production;
75         }
76         public Production getProduction() {
77             return production;
78         }
79         public boolean isShiftAction() {
80             return false;
81         }
82         public boolean isReduceAction() {
83             return true;
84         }
85         public String JavaDoc toString() {
86             return production.toString();
87         }
88     }
89
90     public List parse()
91             throws IOException, SyntaxError {
92         List pi = new LinkedList();
93         LinkedList stack = new LinkedList();
94         stack.add(startStateIndex);
95         Terminal a = lexer.nextToken().getSymbol();
96
97         while (true) {
98             if (DEBUG) {
99                 System.out.print("Stack: ");
100                 Iterator stackIt = stack.iterator();
101                 while (stackIt.hasNext()) {
102                     Integer JavaDoc stateIndex = (Integer JavaDoc)stackIt.next();
103                     System.out.print(stateIndex.intValue()+" ");
104                 }
105                 System.out.println();
106             }
107
108             Integer JavaDoc stateIndex = (Integer JavaDoc)stack.getLast();
109             ParseAction pa = actionTable[stateIndex.intValue()][a.getIndex()];
110             if (pa == null) {
111                 throw new SyntaxError(a);
112             }
113             if (pa.isShiftAction()) {
114                 ShiftAction sa = (ShiftAction)pa;
115                 stack.add(sa.getStateIndex());
116                 a = lexer.nextToken().getSymbol();
117             } else {
118                 ReduceAction ra = (ReduceAction)pa;
119                 Production prod = ra.getProduction();
120                 if (prod == sp) {
121                     return pi; // accept
122
}
123                 for (int i = 0; i<prod.getRHS().size(); i++) {
124                     stack.removeLast();
125                 }
126                 stateIndex = (Integer JavaDoc)stack.getLast();
127                 stack.addLast(gotoTable[stateIndex.intValue()][prod.getLHS().getIndex()]);
128                 pi.add(prod);
129             }
130         }
131     }
132 }
133
Popular Tags