1

I'm trying to write a program in ANTLR (Java) concerning simplifying regular expression. I have already written some code (grammar file contents below)

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)*
            ;

My next goal is to write down tree grammar code, which is able to simplify regular expressions (e.g. a|a -> a , etc.). I have done some coding (see text below), but I have troubles with defining rule that treats nodes as subtrees (in order to simplify following kind of expressions e.g.: (a|a)|(a|a) to a, etc.)

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.
;

Small driver class:

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());

Can anyone help me with solving this case? Thanks in advance and regards.

4
  • I'm parsing following set of regex: "a***;a|a;(ab)****;ab|ab;ab|aa;" There are 3 errors: 1. CommonTree cannot be resolved, 2. i.tree cannot be resolved or is not a field, 3. i cannot be resolved.
    – szefu
    Nov 28, 2012 at 18:44
  • I have updated grammar with kleene_exp and added small test driver class
    – szefu
    Nov 28, 2012 at 21:06
  • Glancing over the tree-pattern docs, I see some things missing: filter=true, which is mandatory, AFAIK. Perhaps you should re-read that again?
    – Bart Kiers
    Nov 28, 2012 at 21:45
  • still the same...:/ In particular I'm interested in following code snippet: "(...) ^('|' x=.* y=.*) {do something with $x and $y} ;". Is there possibility to replace "." with something that matches subtree? {In curly braces I would like to compare two subtrees in order to simplify expressions like: (a|a)|(a|a)}
    – szefu
    Nov 28, 2012 at 23:46

1 Answer 1

1

Now, I'm no expert, but in your tree grammar:

  1. add filter=true
  2. change the second line of bottomup rule to:
    ^('|' i=. j=. {i.toStringTree().equals(j.toStringTree()) }? ) -> $i }

If I'm not mistaken by using i=.* you're allowing i to be non-existent and you'll get a NullPointerException on conversion to a String.

Both i and j are of type CommonTree because you've set it up this way: ASTLabelType = CommonTree, so you should call i.toStringTree().

And since it's Java and you're comparing Strings, use equals().

Also to make the expression in curly brackets a predicate, you need a question mark after the closing one.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.