1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
3 * Copyright (C) 1998-2009 Gerwin Klein <lsf@jflex.de> *
4 * All rights reserved. *
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. *
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. *
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 *
19 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
26 * Stores all rules of the specification for later access in RegExp -> NFA
28 * @author Gerwin Klein
29 * @version $Revision: 1.4.3 $, $Date: 2009/12/21 15:58:48 $
31 public class RegExps
{
33 /** the spec line in which a regexp is used */
34 Vector
/* of Integer */ lines
;
36 /** the lexical states in wich the regexp is used */
37 Vector
/* of Vector of Integer */ states
;
40 Vector
/* of RegExp */ regExps
;
42 /** the action of a regexp */
43 Vector
/* of Action */ actions
;
45 /** flag if it is a BOL regexp */
46 Vector
/* of Boolean */ BOL
;
48 /** the lookahead expression */
49 Vector
/* of RegExp */ look
;
51 /** the forward DFA entry point of the lookahead expression */
52 Vector
/* of Integer */ look_entry
;
54 /** Count of many general lookahead expressions there are.
55 * Need 2*gen_look_count additional DFA entry points. */
59 states
= new Vector();
60 regExps
= new Vector();
61 actions
= new Vector();
65 look_entry
= new Vector();
68 public int insert(int line
, Vector stateList
, RegExp regExp
, Action action
,
69 Boolean isBOL
, RegExp lookAhead
) {
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$
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);
84 return states
.size()-1;
87 public int insert(Vector stateList
, Action action
) {
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$
94 states
.addElement(stateList
);
95 regExps
.addElement(null);
96 actions
.addElement(action
);
98 look
.addElement(null);
99 lines
.addElement(null);
100 look_entry
.addElement(null);
102 return states
.size()-1;
105 public void addStates(int regNum
, Vector newStates
) {
106 Enumeration s
= newStates
.elements();
108 while (s
.hasMoreElements())
109 ((Vector
)states
.elementAt(regNum
)).addElement(s
.nextElement());
112 public int getNum() {
113 return states
.size();
116 public boolean isBOL(int num
) {
117 return ((Boolean
) BOL
.elementAt(num
)).booleanValue();
120 public RegExp
getLookAhead(int num
) {
121 return (RegExp
) look
.elementAt(num
);
124 public boolean isEOF(int num
) {
125 return BOL
.elementAt(num
) == null;
128 public Vector
getStates(int num
) {
129 return (Vector
) states
.elementAt(num
);
132 public RegExp
getRegExp(int num
) {
133 return (RegExp
) regExps
.elementAt(num
);
136 public int getLine(int num
) {
137 return ((Integer
) lines
.elementAt(num
)).intValue();
140 public int getLookEntry(int num
) {
141 return ((Integer
) look_entry
.elementAt(num
)).intValue();
144 public void checkActions() {
145 if ( actions
.elementAt(actions
.size()-1) == null ) {
146 Out
.error(ErrorMessages
.NO_LAST_ACTION
);
147 throw new GeneratorException();
151 public Action
getAction(int num
) {
152 while ( num
< actions
.size() && actions
.elementAt(num
) == null )
155 return (Action
) actions
.elementAt(num
);
158 public int NFASize(Macros macros
) {
160 Enumeration e
= regExps
.elements();
161 while (e
.hasMoreElements()) {
162 RegExp r
= (RegExp
) e
.nextElement();
163 if (r
!= null) size
+= r
.size(macros
);
166 while (e
.hasMoreElements()) {
167 RegExp r
= (RegExp
) e
.nextElement();
168 if (r
!= null) size
+= r
.size(macros
);
173 public void checkLookAheads() {
174 for (int i
=0; i
< regExps
.size(); i
++)
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
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.
188 * @param regExpNum the number of the regexp in RegExps.
190 private void lookAheadCase(int regExpNum
) {
191 if ( getLookAhead(regExpNum
) != null ) {
192 RegExp r1
= getRegExp(regExpNum
);
193 RegExp r2
= getLookAhead(regExpNum
);
195 Action a
= getAction(regExpNum
);
197 int len1
= SemCheck
.length(r1
);
198 int len2
= SemCheck
.length(r2
);
201 a
.setLookAction(Action
.FIXED_BASE
,len1
);
203 else if (len2
>= 0) {
204 a
.setLookAction(Action
.FIXED_LOOK
,len2
);
206 else if (SemCheck
.isFiniteChoice(r2
)) {
207 a
.setLookAction(Action
.FINITE_CHOICE
,0);
210 a
.setLookAction(Action
.GENERAL_LOOK
,0);
211 look_entry
.setElementAt(new Integer(gen_look_count
), regExpNum
);