KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JFlex > RegExp


1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2  * JFlex 1.4.1 *
3  * Copyright (C) 1998-2004 Gerwin Klein <lsf@jflex.de> *
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
21 package JFlex;
22
23
24 /**
25  * Stores a regular expression of rules section in a JFlex-specification.
26  *
27  * This base class has no content other than its type.
28  *
29  * @author Gerwin Klein
30  * @version JFlex 1.4.1, $Revision: 2.4 $, $Date: 2004/11/06 23:03:32 $
31  */

32 public class RegExp {
33   
34   /**
35    * The type of the regular expression. This field will be
36    * filled with values from class sym.java (generated by cup)
37    */

38   int type;
39   
40
41   /**
42    * Create a new regular expression of the specified type.
43    *
44    * @param type a value from the cup generated class sym.
45    *
46    * @see JFlex.sym
47    */

48   public RegExp(int type) {
49     this.type = type;
50   }
51
52   
53
54   /**
55    * Returns a String-representation of this regular expression
56    * with the specified indentation.
57    *
58    * @param tab a String that should contain only space characters and
59    * that is inserted in front of standard String-representation
60    * pf this object.
61    */

62   public String JavaDoc print(String JavaDoc tab) {
63     return tab+toString();
64   }
65
66
67   /**
68    * Returns a String-representation of this regular expression
69    */

70   public String JavaDoc toString() {
71     return "type = "+type;
72   }
73
74
75   /**
76    * Find out if this regexp is a char class or equivalent to one.
77    *
78    * @param macros for macro expansion
79    * @return true if the regexp is equivalent to a char class.
80    */

81   public boolean isCharClass(Macros macros) {
82     RegExp1 unary;
83     RegExp2 binary;
84
85     switch (type) {
86     case sym.CHAR:
87     case sym.CHAR_I:
88     case sym.CCLASS:
89     case sym.CCLASSNOT:
90       return true;
91       
92     case sym.BAR:
93       binary = (RegExp2) this;
94       return binary.r1.isCharClass(macros) && binary.r2.isCharClass(macros);
95  
96     case sym.MACROUSE:
97       unary = (RegExp1) this;
98       return macros.getDefinition((String JavaDoc) unary.content).isCharClass(macros);
99      
100     default: return false;
101     }
102   }
103   
104   /**
105    * The approximate number of NFA states this expression will need (only
106    * works correctly after macro expansion and without negation)
107    *
108    * @param macros macro table for expansion
109    */

110   public int size(Macros macros) {
111     RegExp1 unary;
112     RegExp2 binary;
113     RegExp content;
114
115     switch ( type ) {
116     case sym.BAR:
117       binary = (RegExp2) this;
118       return binary.r1.size(macros) + binary.r2.size(macros) + 2;
119
120     case sym.CONCAT:
121       binary = (RegExp2) this;
122       return binary.r1.size(macros) + binary.r2.size(macros);
123       
124     case sym.STAR:
125       unary = (RegExp1) this;
126       content = (RegExp) unary.content;
127       return content.size(macros) + 2;
128
129     case sym.PLUS:
130       unary = (RegExp1) this;
131       content = (RegExp) unary.content;
132       return content.size(macros) + 2;
133       
134     case sym.QUESTION:
135       unary = (RegExp1) this;
136       content = (RegExp) unary.content;
137       return content.size(macros);
138
139     case sym.BANG:
140       unary = (RegExp1) this;
141       content = (RegExp) unary.content;
142       return content.size(macros) * content.size(macros);
143       // this is only a very rough estimate (worst case 2^n)
144
// exact size too complicated (propably requires construction)
145

146     case sym.TILDE:
147       unary = (RegExp1) this;
148       content = (RegExp) unary.content;
149       return content.size(macros) * content.size(macros) * 3;
150       // see sym.BANG
151

152     case sym.STRING:
153     case sym.STRING_I:
154       unary = (RegExp1) this;
155       return ((String JavaDoc) unary.content).length()+1;
156
157     case sym.CHAR:
158     case sym.CHAR_I:
159       return 2;
160
161     case sym.CCLASS:
162     case sym.CCLASSNOT:
163       return 2;
164
165     case sym.MACROUSE:
166       unary = (RegExp1) this;
167       return macros.getDefinition((String JavaDoc) unary.content).size(macros);
168     }
169
170     throw new Error JavaDoc("unknown regexp type "+type);
171   }
172 }
173
Popular Tags