【发布时间】:2015-02-08 13:46:07
【问题描述】:
所以,我正在为学校做一个正则表达式解析器,它创建一个负责匹配的对象层次结构。我决定做面向对象的,因为这样更容易想象语法的实现。所以,这些是我组成正则表达式的类。这一切都在 Java 中,但我认为如果你精通任何面向对象的语言,你可以跟随。
我们需要实现的唯一运算符是Union (+)、Kleene-Star (*)、表达式的连接(ab 或可能是 (a+b)c),当然还有括号,如串联的例子。这就是我现在已经实现的,我让它像一个魅力一样工作,主要是有点开销。
父类Regexp.java
public abstract class Regexp {
//Print out the regular expression it's holding
//Used for debugging purposes
abstract public void print();
//Checks if the string matches the expression it's holding
abstract public Boolean match(String text);
//Adds a regular expression to be operated upon by the operators
abstract public void add(Regexp regexp);
/*
*To help the main with the overhead to help it decide which regexp will
*hold the other
*/
abstract public Boolean isEmpty();
}
有一个最简单的正则表达式,Base.java,它保存一个字符,如果字符串与字符匹配则返回 true。
public class Base extends Regexp{
char c;
public Base(char c){
this.c = c;
}
public Base(){
c = null;
}
@Override
public void print() {
System.out.println(c);
}
//If the string is the char, return true
@Override
public Boolean match(String text) {
if(text.length() > 1) return false;
return text.startsWith(""+c);
}
//Not utilized, since base is only contained and cannot contain
@Override
public void add(Regexp regexp) {
}
@Override
public Boolean isEmpty() {
return c == null;
}
}
括号 Paren.java,用于在其中保存正则表达式。这里没有什么花哨的东西,但说明了匹配的工作原理。
public class Paren extends Regexp{
//Member variables: What it's holding and if it's holding something
private Regexp regexp;
Boolean empty;
//Parenthesis starts out empty
public Paren(){
empty = true;
}
//Unless you create it with something to hold
public Paren(Regexp regexp){
this.regexp = regexp;
empty = false;
}
//Print out what it's holding
@Override
public void print() {
regexp.print();
}
//Real simple; either what you're holding matches the string or it doesn't
@Override
public Boolean match(String text) {
return regexp.match(text);
}
//Pass something for it to hold, then it's not empty
@Override
public void add(Regexp regexp) {
this.regexp = regexp;
empty = false;
}
//Return if it's holding something
@Override
public Boolean isEmpty() {
return empty;
}
}
一个 Union.java,它是两个可以匹配的正则表达式。如果其中一个匹配,则整个 Union 是匹配的。
public class Union extends Regexp{
//Members
Regexp lhs;
Regexp rhs;
//Indicating if there's room to push more stuff in
private Boolean lhsEmpty;
private Boolean rhsEmpty;
public Union(){
lhsEmpty = true;
rhsEmpty = true;
}
//Can start out with something on the left side
public Union(Regexp lhs){
this.lhs = lhs;
lhsEmpty = false;
rhsEmpty = true;
}
//Or with both members set
public Union(Regexp lhs, Regexp rhs) {
this.lhs = lhs;
this.rhs = rhs;
lhsEmpty = false;
rhsEmpty = false;
}
//Some stuff to help me see the unions format when I'm debugging
@Override
public void print() {
System.out.println("(");
lhs.print();
System.out.println("union");
rhs.print();
System.out.println(")");
}
//If the string matches the left side or right side, it's a match
@Override
public Boolean match(String text) {
if(lhs.match(text) || rhs.match(text)) return true;
return false;
}
/*
*If the left side is not set, add the member there first
*If not, and right side is empty, add the member there
*If they're both full, merge it with the right side
*(This is a consequence of left-to-right parsing)
*/
@Override
public void add(Regexp regexp) {
if(lhsEmpty){
lhs = regexp;
lhsEmpty = false;
}else if(rhsEmpty){
rhs = regexp;
rhsEmpty = false;
}else{
rhs.add(regexp);
}
}
//If it's not full, it's empty
@Override
public Boolean isEmpty() {
return (lhsEmpty || rhsEmpty);
}
}
连接,Concat.java,它基本上是一个链接在一起的正则表达式列表。这个很复杂。
public class Concat extends Regexp{
/*
*The list of regexps is called product and the
*regexps inside called factors
*/
List<Regexp> product;
public Concat(){
product = new ArrayList<Regexp>();
}
public Concat(Regexp regexp){
product = new ArrayList<Regexp>();
pushRegexp(regexp);
}
public Concat(List<Regexp> product) {
this.product = product;
}
//Adding a new regexp pushes it into the list
public void pushRegexp(Regexp regexp){
product.add(regexp);
}
//Loops over and prints them
@Override
public void print() {
for(Regexp factor: product){
factor.print();
}
}
/*
*Builds up a substring approaching the input string.
*When it matches, it builds another substring from where it
*stopped. If the entire string has been pushed, it checks if
*there's an equal amount of matches and factors.
*/
@Override
public Boolean match(String text) {
ArrayList<Boolean> bools = new ArrayList<Boolean>();
int start = 0;
ListIterator<Regexp> itr = product.listIterator();
Regexp factor = itr.next();
for(int i = 0; i <= text.length(); i++){
String test = text.substring(start, i);
if(factor.match(test)){
start = i;
bools.add(true);
if(itr.hasNext())
factor = itr.next();
}
}
return (allTrue(bools) && (start == text.length()));
}
private Boolean allTrue(List<Boolean> bools){
return product.size() == bools.size();
}
@Override
public void add(Regexp regexp) {
pushRegexp(regexp);
}
@Override
public Boolean isEmpty() {
return product.isEmpty();
}
}
再次,我已经让这些工作对我的开销、标记化和所有这些好东西感到满意。现在我要介绍Kleene-star操作。它匹配文本中出现的任何数量,甚至是 0。因此,ba* 将匹配 b、ba、baa、baaa 等,而 (ba)* 将匹配 ba、baba、bababa 等。是否有可能将我的正则表达式扩展到此,或者您是否看到了解决此问题的另一种方法?
PS:还有 getter、setter 和其他各种支持函数我没有写出来,但这主要是为了让你快速了解这些类是如何工作的。
【问题讨论】:
标签: java regex parsing polymorphism abstract-class