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

  • SEARCH
  • Home
  • 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 6221803
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:11:52+00:00 2026-05-24T08:11:52+00:00

i want to parse something like this in my lexer: ( begin expression )

  • 0

i want to parse something like this in my lexer:

( begin expression )

where expressions are also surrounded by brackets. it isn’t important what is in the expression, i just want to have all what’s between the (begin and the matching ) as a token. an example would be:

(begin 
    (define x (+ 1 2)))

so the text of the token should be

(define x (+ 1 2)))

something like

PROGRAM : LPAREN BEGIN .* RPAREN;

does (obviously) not work because as soon as he sees a “)”, he thinks the rule is over, but i need the matching bracket for this.

how can i do that?

  • 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-05-24T08:11:53+00:00Added an answer on May 24, 2026 at 8:11 am

    Inside lexer rules, you can invoke rules recursively. So, that’s one way to solve this. Another approach would be to keep track of the number of open- and close parenthesis and let a gated semantic predicate loop as long as your counter is more than zero.

    A demo:

    T.g

    grammar T;
    
    parse
      :  BeginToken {System.out.println("parsed :: " + $BeginToken.text);} EOF
      ;
    
    BeginToken 
    @init{int open = 1;}
      :  '(' 'begin' ( {open > 0}?=>              // keep reapeating `( ... )*` as long as open > 0
                         ( ~('(' | ')')           // match anything other than parenthesis
                         | '('          {open++;} // match a '(' in increase the var `open`
                         | ')'          {open--;} // match a ')' in decrease the var `open`
                         )
                     )*
      ;
    

    Main.java

    import org.antlr.runtime.*;
    
    public class Main {
      public static void main(String[] args) throws Exception {
        String input = "(begin (define x (+ (- 1 3) 2)))";
        TLexer lexer = new TLexer(new ANTLRStringStream(input));
        TParser parser = new TParser(new CommonTokenStream(lexer));
        parser.parse();
      }
    }
    
    java -cp antlr-3.3-complete.jar org.antlr.Tool T.g
    javac -cp antlr-3.3-complete.jar *.java
    java -cp .:antlr-3.3-complete.jar Main
    
    parsed :: (begin (define x (+ (- 1 3) 2)))
    

    Note that you’ll need to beware of string literals inside your source that might include parenthesis:

    BeginToken
    @init{int open = 1;}
      :  '(' 'begin' ( {open > 0}?=>              // ...
                         ( ~('(' | ')' | '"')     // ...
                         | '('          {open++;} // ...
                         | ')'          {open--;} // ...
                         |  '"' ...               // TODO: define a string literal here
                         )
                     )*
      ;
    

    or comments that may contain parenthesis.

    The suggestion with the predicate uses some language specific code (Java, in this case). An advantage of calling a lexer rule recursively is that you don’t have custom code in your lexer:

    BeginToken 
      :  '(' Spaces? 'begin' Spaces? NestedParens Spaces? ')'
      ;
    
    fragment NestedParens
      :  '(' ( ~('(' | ')') | NestedParens )* ')'
      ;
    
    fragment Spaces
      :  (' ' | '\t')+
      ;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to parse a html content that have something like this: <div id=sometext>Lorem<br>
I want to parse something like this: {{word1|word2|word3|word4|...}} {{word1|word2|word3}} ... with preg_match_all . I
I am currently using NSXMLParser mathods to parse my data something like this :
I want to do something like this in my geometry shader: uniform int maxOutputVert;
I want to know if it's possible to do something like this: `readfile(base64_decode_only_img_src_tags(mypage.html)); I've
I have want to do something like this to mimic a form post: $.ajax({
hi i have xml file whitch i want to parse, it looks something like
I want to do something like this: {{user.name.toLowerCase()}} but I get this error: Error:
I want to parse a config file sorta thing, like so: [KEY:Value] [SUBKEY:SubValue] Now
Can I do something like this in Python? for (i = 0; i <

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.