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 9025495
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T06:17:09+00:00 2026-06-16T06:17:09+00:00

I’m trying to match balanced parentheses such that, a PARAMS tree is created if

  • 0

I’m trying to match balanced parentheses such that, a PARAMS tree is created if a match is made, else the LPARAM and RPARAM tokens are simply added as atoms to the tree…

tokens
{
    LIST;    
    PARAMS;
}

start   : list -> ^(LIST list);

list    : (expr|atom)+;

expr : LPARAM list? RPARAM -> ^(PARAMS list?);

atom :  INT | LPARAM | RPARAM;

INT :   '0'..'9'+;
LPARAM  :   '(';
RPARAM  :   ')';

At the moment, it will never create a PARAMS tree, because in the rule expr it will always see the end RPARAM as an atom, rather than the the closing token for that rule.

So at the moment, something like 1 2 3 (4) 5 is added to a LIST tree as a flat list of tokens, rather than the required grouping.

I’ve handled adding tokens as atoms to a tree before, but they never were able to start another rule, as LPARAM does here.

Do I need some sort of syntatic/semantic predicate here?

  • 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-16T06:17:11+00:00Added an answer on June 16, 2026 at 6:17 am

    Here is a simple approach that comes with a couple of constraints. I think these conform to the expected behavior that you mentioned in the comments.

    • An unmatched LPARAM never appears inside a child list
    • An unmatched RPARAM never appears inside a child list

    Grammar:

    start   : root+ EOF -> ^(LIST root+ );
    
    root    : expr
            | LPARAM
            | RPARAM
            ;
            
    expr    : list
            | atom
            ;           
            
    list    : LPARAM expr+ RPARAM -> ^(LIST expr+)
            ;
    
    atom    : INT
            ;
    

    Rule root matches mismatched LPARAMs and RPARAMs. Rules list and atom only care about themselves.

    This solution is relatively fragile because rule root requires expr to be listed before LPARAM and RPARAM. Even so, maybe this is enough to solve your problem.

    Test case 1 : no lists

    Input: 1 2 3

    Output:

    1 2 3

    Test case 2 : one list

    Input: 1 (2) 3

    Output:

    1 (2) 3

    Test case 3 : two lists

    Input: (1) 2 (3)

    Output:

    (1) 2 (3)

    Test case 4 : no lists, mismatched lefts

    Input: ((1 2 3

    Output:

    ((1 2 3

    Test case 5 : two lists, mismatched lefts

    Input: ((1 (2) (3)

    Output:

    ((1 (2) (3)

    Test case 6 : no lists, mismatched rights

    Input: 1 2 3))

    Output:

    1 2 3))

    Test case 7 : two lists, mismatched rights

    Input: (1) (2) 3))

    Output:

    (1) (2) 3))

    Test case 8 : two lists, mixed mismatched lefts

    Input: ((1 (2) ( (3)

    Output:
    ((1 (2) ( (3)

    Test case 9 : two lists, mixed mismatched rights

    Input: (1) ) (2) 3))

    Output:

    (1) ) (2) 3))


    Here’s a slightly more complicated grammar that operates on [] and () pairs. I think the solution is going to get exponentially worse as you add pairs, but hey, it’s fun! You may also be hitting the limitation of what you can do with grammar-driven AST building.

    start   : root+ EOF -> ^(LIST root+ )
            ;
            
    root    : expr
            | LPARAM
            | RPARAM
            | LSQB
            | RSQB
            ;       
    expr    : plist
            | slist
            | atom
            ;           
            
    plist   : LPARAM pexpr* RPARAM -> ^(LIST pexpr*)
            ;
            
    pexpr   : slist
            | atom
            | LSQB
            | RSQB
            ;       
            
    slist   : LSQB sexpr* RSQB -> ^(LIST sexpr*)
            ;
            
    sexpr   : plist
            | atom
            | LPARAM
            | RPARAM
            ;               
            
    atom    : INT;
    
    INT     : ('0'..'9')+;
    LPARAM  : '(';
    RPARAM  : ')';
    LSQB    : '[';
    RSQB    : ']';
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to create an if statement in PHP that prevents a single post
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I
I am trying to render a haml file in a javascript response like so:
I am doing a simple coin flipping experiment for class that involves flipping a
I have a French site that I want to parse, but am running into
I'm trying to select an H1 element which is the second-child in its group
I know there's a lot of other questions out there that deal with this

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.