1 | /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
---|
2 | * JFlex 1.4.3 *
|
---|
3 | * Copyright (C) 1998-2009 Gerwin Klein <[email protected]> *
|
---|
4 | * All rights reserved. *
|
---|
5 | * *
|
---|
6 | * This program is free software; you can redistribute it and/or modify *
|
---|
7 | * it under the terms of the GNU General Public License. See the file *
|
---|
8 | * COPYRIGHT for more information. *
|
---|
9 | * *
|
---|
10 | * This program is distributed in the hope that it will be useful, *
|
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
---|
13 | * GNU General Public License for more details. *
|
---|
14 | * *
|
---|
15 | * You should have received a copy of the GNU General Public License along *
|
---|
16 | * with this program; if not, write to the Free Software Foundation, Inc., *
|
---|
17 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
|
---|
18 | * *
|
---|
19 | * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
---|
20 | package JFlex;
|
---|
21 |
|
---|
22 | import java.util.*;
|
---|
23 |
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * Stores all rules of the specification for later access in RegExp -> NFA
|
---|
27 | *
|
---|
28 | * @author Gerwin Klein
|
---|
29 | * @version JFlex 1.4.3, $Revision: 433 $, $Date: 2009-01-31 19:52:34 +1100 (Sat, 31 Jan 2009) $
|
---|
30 | */
|
---|
31 | public class RegExps {
|
---|
32 |
|
---|
33 | /** the spec line in which a regexp is used */
|
---|
34 | Vector /* of Integer */ lines;
|
---|
35 |
|
---|
36 | /** the lexical states in wich the regexp is used */
|
---|
37 | Vector /* of Vector of Integer */ states;
|
---|
38 |
|
---|
39 | /** the regexp */
|
---|
40 | Vector /* of RegExp */ regExps;
|
---|
41 |
|
---|
42 | /** the action of a regexp */
|
---|
43 | Vector /* of Action */ actions;
|
---|
44 |
|
---|
45 | /** flag if it is a BOL regexp */
|
---|
46 | Vector /* of Boolean */ BOL;
|
---|
47 |
|
---|
48 | /** the lookahead expression */
|
---|
49 | Vector /* of RegExp */ look;
|
---|
50 |
|
---|
51 | /** the forward DFA entry point of the lookahead expression */
|
---|
52 | Vector /* of Integer */ look_entry;
|
---|
53 |
|
---|
54 | /** Count of many general lookahead expressions there are.
|
---|
55 | * Need 2*gen_look_count additional DFA entry points. */
|
---|
56 | int gen_look_count;
|
---|
57 |
|
---|
58 | public RegExps() {
|
---|
59 | states = new Vector();
|
---|
60 | regExps = new Vector();
|
---|
61 | actions = new Vector();
|
---|
62 | BOL = new Vector();
|
---|
63 | look = new Vector();
|
---|
64 | lines = new Vector();
|
---|
65 | look_entry = new Vector();
|
---|
66 | }
|
---|
67 |
|
---|
68 | public int insert(int line, Vector stateList, RegExp regExp, Action action,
|
---|
69 | Boolean isBOL, RegExp lookAhead) {
|
---|
70 | if (Options.DEBUG) {
|
---|
71 | Out.debug("Inserting regular expression with statelist :"+Out.NL+stateList); //$NON-NLS-1$
|
---|
72 | Out.debug("and action code :"+Out.NL+action.content+Out.NL); //$NON-NLS-1$
|
---|
73 | Out.debug("expression :"+Out.NL+regExp); //$NON-NLS-1$
|
---|
74 | }
|
---|
75 |
|
---|
76 | states.addElement(stateList);
|
---|
77 | regExps.addElement(regExp);
|
---|
78 | actions.addElement(action);
|
---|
79 | BOL.addElement(isBOL);
|
---|
80 | look.addElement(lookAhead);
|
---|
81 | lines.addElement(new Integer(line));
|
---|
82 | look_entry.addElement(null);
|
---|
83 |
|
---|
84 | return states.size()-1;
|
---|
85 | }
|
---|
86 |
|
---|
87 | public int insert(Vector stateList, Action action) {
|
---|
88 |
|
---|
89 | if (Options.DEBUG) {
|
---|
90 | Out.debug("Inserting eofrule with statelist :"+Out.NL+stateList); //$NON-NLS-1$
|
---|
91 | Out.debug("and action code :"+Out.NL+action.content+Out.NL); //$NON-NLS-1$
|
---|
92 | }
|
---|
93 |
|
---|
94 | states.addElement(stateList);
|
---|
95 | regExps.addElement(null);
|
---|
96 | actions.addElement(action);
|
---|
97 | BOL.addElement(null);
|
---|
98 | look.addElement(null);
|
---|
99 | lines.addElement(null);
|
---|
100 | look_entry.addElement(null);
|
---|
101 |
|
---|
102 | return states.size()-1;
|
---|
103 | }
|
---|
104 |
|
---|
105 | public void addStates(int regNum, Vector newStates) {
|
---|
106 | Enumeration s = newStates.elements();
|
---|
107 |
|
---|
108 | while (s.hasMoreElements())
|
---|
109 | ((Vector)states.elementAt(regNum)).addElement(s.nextElement());
|
---|
110 | }
|
---|
111 |
|
---|
112 | public int getNum() {
|
---|
113 | return states.size();
|
---|
114 | }
|
---|
115 |
|
---|
116 | public boolean isBOL(int num) {
|
---|
117 | return ((Boolean) BOL.elementAt(num)).booleanValue();
|
---|
118 | }
|
---|
119 |
|
---|
120 | public RegExp getLookAhead(int num) {
|
---|
121 | return (RegExp) look.elementAt(num);
|
---|
122 | }
|
---|
123 |
|
---|
124 | public boolean isEOF(int num) {
|
---|
125 | return BOL.elementAt(num) == null;
|
---|
126 | }
|
---|
127 |
|
---|
128 | public Vector getStates(int num) {
|
---|
129 | return (Vector) states.elementAt(num);
|
---|
130 | }
|
---|
131 |
|
---|
132 | public RegExp getRegExp(int num) {
|
---|
133 | return (RegExp) regExps.elementAt(num);
|
---|
134 | }
|
---|
135 |
|
---|
136 | public int getLine(int num) {
|
---|
137 | return ((Integer) lines.elementAt(num)).intValue();
|
---|
138 | }
|
---|
139 |
|
---|
140 | public int getLookEntry(int num) {
|
---|
141 | return ((Integer) look_entry.elementAt(num)).intValue();
|
---|
142 | }
|
---|
143 |
|
---|
144 | public void checkActions() {
|
---|
145 | if ( actions.elementAt(actions.size()-1) == null ) {
|
---|
146 | Out.error(ErrorMessages.NO_LAST_ACTION);
|
---|
147 | throw new GeneratorException();
|
---|
148 | }
|
---|
149 | }
|
---|
150 |
|
---|
151 | public Action getAction(int num) {
|
---|
152 | while ( num < actions.size() && actions.elementAt(num) == null )
|
---|
153 | num++;
|
---|
154 |
|
---|
155 | return (Action) actions.elementAt(num);
|
---|
156 | }
|
---|
157 |
|
---|
158 | public int NFASize(Macros macros) {
|
---|
159 | int size = 0;
|
---|
160 | Enumeration e = regExps.elements();
|
---|
161 | while (e.hasMoreElements()) {
|
---|
162 | RegExp r = (RegExp) e.nextElement();
|
---|
163 | if (r != null) size += r.size(macros);
|
---|
164 | }
|
---|
165 | e = look.elements();
|
---|
166 | while (e.hasMoreElements()) {
|
---|
167 | RegExp r = (RegExp) e.nextElement();
|
---|
168 | if (r != null) size += r.size(macros);
|
---|
169 | }
|
---|
170 | return size;
|
---|
171 | }
|
---|
172 |
|
---|
173 | public void checkLookAheads() {
|
---|
174 | for (int i=0; i < regExps.size(); i++)
|
---|
175 | lookAheadCase(i);
|
---|
176 | }
|
---|
177 |
|
---|
178 | /**
|
---|
179 | * Determine which case of lookahead expression regExpNum points to (if any).
|
---|
180 | * Set case data in corresponding action.
|
---|
181 | * Increment count of general lookahead expressions for entry points
|
---|
182 | * of the two additional DFAs.
|
---|
183 | * Register DFA entry point in RegExps
|
---|
184 | *
|
---|
185 | * Needs to be run before adding any regexps/rules to be able to reserve
|
---|
186 | * the correct amount of space of lookahead DFA entry points.
|
---|
187 | *
|
---|
188 | * @param regExpNum the number of the regexp in RegExps.
|
---|
189 | */
|
---|
190 | private void lookAheadCase(int regExpNum) {
|
---|
191 | if ( getLookAhead(regExpNum) != null ) {
|
---|
192 | RegExp r1 = getRegExp(regExpNum);
|
---|
193 | RegExp r2 = getLookAhead(regExpNum);
|
---|
194 |
|
---|
195 | Action a = getAction(regExpNum);
|
---|
196 |
|
---|
197 | int len1 = SemCheck.length(r1);
|
---|
198 | int len2 = SemCheck.length(r2);
|
---|
199 |
|
---|
200 | if (len1 >= 0) {
|
---|
201 | a.setLookAction(Action.FIXED_BASE,len1);
|
---|
202 | }
|
---|
203 | else if (len2 >= 0) {
|
---|
204 | a.setLookAction(Action.FIXED_LOOK,len2);
|
---|
205 | }
|
---|
206 | else if (SemCheck.isFiniteChoice(r2)) {
|
---|
207 | a.setLookAction(Action.FINITE_CHOICE,0);
|
---|
208 | }
|
---|
209 | else {
|
---|
210 | a.setLookAction(Action.GENERAL_LOOK,0);
|
---|
211 | look_entry.setElementAt(new Integer(gen_look_count), regExpNum);
|
---|
212 | gen_look_count++;
|
---|
213 | }
|
---|
214 | }
|
---|
215 | }
|
---|
216 |
|
---|
217 | }
|
---|