1
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
3
* Copyright (C) 1998-2004 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
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
21
package weka.core.parser.JFlex;
25
* Stores a regular expression of rules section in a JFlex-specification.
27
* This base class has no content other than its type.
29
* @author Gerwin Klein
30
* @version JFlex 1.4.1, $Revision: 1.1 $, $Date: 2008/05/09 09:14:10 $
35
* The type of the regular expression. This field will be
36
* filled with values from class sym.java (generated by cup)
42
* Create a new regular expression of the specified type.
44
* @param type a value from the cup generated class sym.
48
public RegExp(int type) {
55
* Returns a String-representation of this regular expression
56
* with the specified indentation.
58
* @param tab a String that should contain only space characters and
59
* that is inserted in front of standard String-representation
62
public String print(String tab) {
63
return tab+toString();
68
* Returns a String-representation of this regular expression
70
public String toString() {
71
return "type = "+type;
76
* Find out if this regexp is a char class or equivalent to one.
78
* @param macros for macro expansion
79
* @return true if the regexp is equivalent to a char class.
81
public boolean isCharClass(Macros macros) {
93
binary = (RegExp2) this;
94
return binary.r1.isCharClass(macros) && binary.r2.isCharClass(macros);
97
unary = (RegExp1) this;
98
return macros.getDefinition((String) unary.content).isCharClass(macros);
100
default: return false;
105
* The approximate number of NFA states this expression will need (only
106
* works correctly after macro expansion and without negation)
108
* @param macros macro table for expansion
110
public int size(Macros macros) {
117
binary = (RegExp2) this;
118
return binary.r1.size(macros) + binary.r2.size(macros) + 2;
121
binary = (RegExp2) this;
122
return binary.r1.size(macros) + binary.r2.size(macros);
125
unary = (RegExp1) this;
126
content = (RegExp) unary.content;
127
return content.size(macros) + 2;
130
unary = (RegExp1) this;
131
content = (RegExp) unary.content;
132
return content.size(macros) + 2;
135
unary = (RegExp1) this;
136
content = (RegExp) unary.content;
137
return content.size(macros);
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)
147
unary = (RegExp1) this;
148
content = (RegExp) unary.content;
149
return content.size(macros) * content.size(macros) * 3;
154
unary = (RegExp1) this;
155
return ((String) unary.content).length()+1;
166
unary = (RegExp1) this;
167
return macros.getDefinition((String) unary.content).size(macros);
170
throw new Error("unknown regexp type "+type);