KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > javacc > parser > LookaheadWalk


1 /*
2  * Copyright © 2002 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
3  * California 95054, U.S.A. All rights reserved. Sun Microsystems, Inc. has
4  * intellectual property rights relating to technology embodied in the product
5  * that is described in this document. In particular, and without limitation,
6  * these intellectual property rights may include one or more of the U.S.
7  * patents listed at http://www.sun.com/patents and one or more additional
8  * patents or pending patent applications in the U.S. and in other countries.
9  * U.S. Government Rights - Commercial software. Government users are subject
10  * to the Sun Microsystems, Inc. standard license agreement and applicable
11  * provisions of the FAR and its supplements. Use is subject to license terms.
12  * Sun, Sun Microsystems, the Sun logo and Java are trademarks or registered
13  * trademarks of Sun Microsystems, Inc. in the U.S. and other countries. This
14  * product is covered and controlled by U.S. Export Control laws and may be
15  * subject to the export or import laws in other countries. Nuclear, missile,
16  * chemical biological weapons or nuclear maritime end uses or end users,
17  * whether direct or indirect, are strictly prohibited. Export or reexport
18  * to countries subject to U.S. embargo or to entities identified on U.S.
19  * export exclusion lists, including, but not limited to, the denied persons
20  * and specially designated nationals lists is strictly prohibited.
21  */

22
23 package org.javacc.parser;
24
25 import java.util.Vector JavaDoc;
26
27 public class LookaheadWalk {
28
29   public static boolean considerSemanticLA;
30
31   public static Vector JavaDoc sizeLimitedMatches;
32
33   public static void vectorAppend(Vector JavaDoc vToAppendTo, Vector JavaDoc vToAppend) {
34     for (int i = 0; i < vToAppend.size(); i++) {
35       vToAppendTo.addElement(vToAppend.elementAt(i));
36     }
37   }
38
39   public static Vector JavaDoc genFirstSet(Vector JavaDoc partialMatches, Expansion exp) {
40     if (exp instanceof RegularExpression) {
41       Vector JavaDoc retval = new Vector JavaDoc();
42       for (int i = 0; i < partialMatches.size(); i++) {
43         MatchInfo m = (MatchInfo)partialMatches.elementAt(i);
44         MatchInfo mnew = new MatchInfo();
45         for (int j = 0; j < m.firstFreeLoc; j++) {
46           mnew.match[j] = m.match[j];
47         }
48         mnew.firstFreeLoc = m.firstFreeLoc;
49         mnew.match[mnew.firstFreeLoc++] = ((RegularExpression)exp).ordinal;
50         if (mnew.firstFreeLoc == MatchInfo.laLimit) {
51           sizeLimitedMatches.addElement(mnew);
52         } else {
53           retval.addElement(mnew);
54         }
55       }
56       return retval;
57     } else if (exp instanceof NonTerminal) {
58       NormalProduction prod = ((NonTerminal)exp).prod;
59       if (prod instanceof JavaCodeProduction) {
60         return new Vector JavaDoc();
61       } else {
62         return genFirstSet(partialMatches, prod.expansion);
63       }
64     } else if (exp instanceof Choice) {
65       Vector JavaDoc retval = new Vector JavaDoc();
66       Choice ch = (Choice)exp;
67       for (int i = 0; i < ch.choices.size(); i++) {
68         Vector JavaDoc v = genFirstSet(partialMatches, (Expansion)ch.choices.elementAt(i));
69         vectorAppend(retval, v);
70       }
71       return retval;
72     } else if (exp instanceof Sequence) {
73       Vector JavaDoc v = partialMatches;
74       Sequence seq = (Sequence)exp;
75       for (int i = 0; i < seq.units.size(); i++) {
76         v = genFirstSet(v, (Expansion)seq.units.elementAt(i));
77         if (v.size() == 0) break;
78       }
79       return v;
80     } else if (exp instanceof OneOrMore) {
81       Vector JavaDoc retval = new Vector JavaDoc();
82       Vector JavaDoc v = partialMatches;
83       OneOrMore om = (OneOrMore)exp;
84       while (true) {
85         v = genFirstSet(v, om.expansion);
86         if (v.size() == 0) break;
87         vectorAppend(retval, v);
88       }
89       return retval;
90     } else if (exp instanceof ZeroOrMore) {
91       Vector JavaDoc retval = new Vector JavaDoc();
92       vectorAppend(retval, partialMatches);
93       Vector JavaDoc v = partialMatches;
94       ZeroOrMore zm = (ZeroOrMore)exp;
95       while (true) {
96         v = genFirstSet(v, zm.expansion);
97         if (v.size() == 0) break;
98         vectorAppend(retval, v);
99       }
100       return retval;
101     } else if (exp instanceof ZeroOrOne) {
102       Vector JavaDoc retval = new Vector JavaDoc();
103       vectorAppend(retval, partialMatches);
104       vectorAppend(retval, genFirstSet(partialMatches, ((ZeroOrOne)exp).expansion));
105       return retval;
106     } else if (exp instanceof TryBlock) {
107       return genFirstSet(partialMatches, ((TryBlock)exp).exp);
108     } else if (considerSemanticLA &&
109                exp instanceof Lookahead &&
110                ((Lookahead)exp).action_tokens.size() != 0
111               ) {
112       return new Vector JavaDoc();
113     } else {
114       Vector JavaDoc retval = new Vector JavaDoc();
115       vectorAppend(retval, partialMatches);
116       return retval;
117     }
118   }
119
120   public static void vectorSplit(Vector JavaDoc toSplit, Vector JavaDoc mask, Vector JavaDoc partInMask, Vector JavaDoc rest) {
121     OuterLoop:
122     for (int i = 0; i < toSplit.size(); i++) {
123       for (int j = 0; j < mask.size(); j++) {
124         if (toSplit.elementAt(i) == mask.elementAt(j)) {
125           partInMask.addElement(toSplit.elementAt(i));
126           continue OuterLoop;
127         }
128       }
129       rest.addElement(toSplit.elementAt(i));
130     }
131   }
132
133   public static Vector JavaDoc genFollowSet(Vector JavaDoc partialMatches, Expansion exp, long generation) {
134     if (exp.myGeneration == generation) {
135       return new Vector JavaDoc();
136     }
137 //System.out.println("*** Parent: " + exp.parent);
138
exp.myGeneration = generation;
139     if (exp.parent == null) {
140       Vector JavaDoc retval = new Vector JavaDoc();
141       vectorAppend(retval, partialMatches);
142       return retval;
143     } else if (exp.parent instanceof NormalProduction) {
144       Vector JavaDoc parents = ((NormalProduction)exp.parent).parents;
145       Vector JavaDoc retval = new Vector JavaDoc();
146 //System.out.println("1; gen: " + generation + "; exp: " + exp);
147
for (int i = 0; i < parents.size(); i++) {
148         Vector JavaDoc v = genFollowSet(partialMatches, (Expansion)parents.elementAt(i), generation);
149         vectorAppend(retval, v);
150       }
151       return retval;
152     } else if (exp.parent instanceof Sequence) {
153       Sequence seq = (Sequence)exp.parent;
154       Vector JavaDoc v = partialMatches;
155       for (int i = exp.ordinal+1; i < seq.units.size(); i++) {
156         v = genFirstSet(v, (Expansion)seq.units.elementAt(i));
157         if (v.size() == 0) return v;
158       }
159       Vector JavaDoc v1 = new Vector JavaDoc();
160       Vector JavaDoc v2 = new Vector JavaDoc();
161       vectorSplit(v, partialMatches, v1, v2);
162       if (v1.size() != 0) {
163 //System.out.println("2; gen: " + generation + "; exp: " + exp);
164
v1 = genFollowSet(v1, seq, generation);
165       }
166       if (v2.size() != 0) {
167 //System.out.println("3; gen: " + generation + "; exp: " + exp);
168
v2 = genFollowSet(v2, seq, Expansion.nextGenerationIndex++);
169       }
170       vectorAppend(v2, v1);
171       return v2;
172     } else if (exp.parent instanceof OneOrMore || exp.parent instanceof ZeroOrMore) {
173       Vector JavaDoc moreMatches = new Vector JavaDoc();
174       vectorAppend(moreMatches, partialMatches);
175       Vector JavaDoc v = partialMatches;
176       while (true) {
177         v = genFirstSet(v, exp);
178         if (v.size() == 0) break;
179         vectorAppend(moreMatches, v);
180       }
181       Vector JavaDoc v1 = new Vector JavaDoc();
182       Vector JavaDoc v2 = new Vector JavaDoc();
183       vectorSplit(moreMatches, partialMatches, v1, v2);
184       if (v1.size() != 0) {
185 //System.out.println("4; gen: " + generation + "; exp: " + exp);
186
v1 = genFollowSet(v1, (Expansion)exp.parent, generation);
187       }
188       if (v2.size() != 0) {
189 //System.out.println("5; gen: " + generation + "; exp: " + exp);
190
v2 = genFollowSet(v2, (Expansion)exp.parent, Expansion.nextGenerationIndex++);
191       }
192       vectorAppend(v2, v1);
193       return v2;
194     } else {
195 //System.out.println("6; gen: " + generation + "; exp: " + exp);
196
return genFollowSet(partialMatches, (Expansion)exp.parent, generation);
197     }
198   }
199
200    public static void reInit()
201    {
202       considerSemanticLA = false;
203       sizeLimitedMatches = null;
204    }
205
206 }
207
Popular Tags