1

我正在尝试用ANTLR (Java) 编写一个关于简化正则表达式的程序。我已经写了一些代码(下面的语法文件内容)

grammar Regexp_v7;
options{
    language = Java;
    output = AST;
    ASTLabelType = CommonTree;
    backtrack = true;
}

tokens{
    DOT;
    REPEAT;
    RANGE;
    NULL;
} 

fragment
    ZERO
            :    '0'
            ;

fragment
    DIGIT
            :    '1'..'9'
            ;

fragment
    EPSILON
            :    '@'
            ;

fragment
    FI
            :    '%'
            ;

    ID
            :    EPSILON
            |    FI
            |    'a'..'z'
            |    'A'..'Z'
            ;

 NUMBER
            :    ZERO
            |    DIGIT (ZERO | DIGIT)*
            ;

 WHITESPACE
            :    ('\r' | '\n' | ' ' | '\t' ) + {$channel = HIDDEN;}
            ;

list
            :    (reg_exp ';'!)*
            ;

term
            :        ID -> ID
            |    '('! reg_exp ')'!
            ;

repeat_exp
            :    term ('{' range_exp '}')+ -> ^(REPEAT term (range_exp)+)
            |    term -> term
            ;

range_exp
            :    NUMBER ',' NUMBER -> ^(RANGE NUMBER NUMBER)
            |    NUMBER (',') -> ^(RANGE NUMBER NULL)
            |    ',' NUMBER -> ^(RANGE NULL NUMBER)
            |    NUMBER -> ^(RANGE NUMBER NUMBER)
            ;
kleene_exp
            :    repeat_exp ('*'^)*
            ;
concat_exp
            :    kleene_exp (kleene_exp)+ -> ^(DOT kleene_exp (kleene_exp)+)
            |    kleene_exp -> kleene_exp
            ;

reg_exp
            :    concat_exp ('|'^ concat_exp)*
            ;

我的下一个目标是写下能够简化正则表达式的树语法代码(例如 a|a -> a 等)。我已经做了一些编码(见下文),但我在定义将节点视为子树的规则时遇到了麻烦(为了简化以下类型的表达式,例如:(a|a)|(a|a) to a 等。 )

tree grammar Regexp_v7Walker;

options{
    language = Java;
    tokenVocab = Regexp_v7;
    ASTLabelType = CommonTree;
    output=AST;
    backtrack = true;
}

tokens{
    NULL;
}

bottomup
            : ^('*' ^('*' e=.)) -> ^('*' $e)    //a** -> a*
            | ^('|' i=.* j=.* {$i.tree.toStringTree() == $j.tree.toStringTree()} ) 
            -> $i // There are 3 errors while this line is up and running: 
                  // 1. CommonTree cannot be resolved, 
                  // 2. i.tree cannot be resolved or is not a field,
                  // 3. i cannot be resolved.
;

小司机班:

public class Regexp_Test_v7 {
    public static void main(String[] args) throws RecognitionException {
        CharStream stream = new ANTLRStringStream("a***;a|a;(ab)****;ab|ab;ab|aa;");
        Regexp_v7Lexer lexer = new Regexp_v7Lexer(stream);
        CommonTokenStream tokenStream = new CommonTokenStream(lexer);
        Regexp_v7Parser parser = new Regexp_v7Parser(tokenStream);
        list_return list = parser.list();
        CommonTree t = (CommonTree) list.getTree();
        System.out.println("Original tree: " + t.toStringTree());
        CommonTreeNodeStream nodes = new CommonTreeNodeStream(t);
        Regexp_v7Walker s = new Regexp_v7Walker(nodes);
        t = (CommonTree)s.downup(t);
        System.out.println("Simplified tree: " + t.toStringTree());

谁能帮我解决这个案子?在此先感谢和问候。

4

1 回答 1

1

现在,我不是专家,但在你的树语法中:

  1. 添加filter=true
  2. 将规则的第二行更改bottomup为:
    ^('|' i=. j=. {i.toStringTree().equals(j.toStringTree()) }? ) -> $i }

如果我没记错的话,i=.*你就i可以不存在了,你会得到一个NullPointerException转换为String.

两者ij都是类型CommonTree,因为您已经这样设置它:ASTLabelType = CommonTree,所以您应该调用i.toStringTree().

由于它是 Java 并且您正在比较字符串,因此请使用equals().

此外,为了使大括号中的表达式成为谓词,您需要在结束后加一个问号。

于 2012-11-30T18:31:17.070 回答