Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8910463
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T03:41:44+00:00 2026-06-15T03:41:44+00:00

I’m trying to write a program in ANTLR (Java) concerning simplifying regular expression. I

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T03:41:45+00:00Added an answer on June 15, 2026 at 3:41 am

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have just tried to save a simple *.rtf file with some websites and
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have been unable to fix a problem with Java Unicode and encoding. The
I have thousands of HTML files to process using Groovy/Java and I need to
I am trying to loop through a bunch of documents I have to put
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I am trying to understand how to use SyndicationItem to display feed which is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.