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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:22:41+00:00 2026-05-25T18:22:41+00:00

I am writing a java program which would act upon the input which would

  • 0

I am writing a java program which would act upon the input which would be in scheme kind

of language.

Something like

(+ (+ a b))

Say I now want to check the syntax like if there are 2 brackets which have been opened, there have to be 2 other closing brackets. I am not sure how to achieve this with a regex. Can you help me out

  • 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-25T18:22:41+00:00Added an answer on May 25, 2026 at 6:22 pm

    Regular expressions can’t pair parentheses to an arbitrary depth. Scheme syntax is not regular.

    http://en.wikipedia.org/wiki/Regular_language#The_number_of_words_in_a_regular_language

    Thus, a non-regularity of some language L’ can be proved by counting the words in L’. Consider, for example, the Dyck language of strings of balanced parentheses. The number of words of length 2n in the Dyck language is equal to the Catalan number …, which is not of the form p(n)λn, witnessing the non-regularity of the Dyck language.

    You’re going to have to tokenize it and then walk over tokens counting paren depth and make sure the depth is zero at the end and never goes negative.

    For a simple language that has parentheses spaces and identifiers made from repetitions of the letter ‘a’, you might do

    Patter token = Pattern.compile("[() ]|a+|.", Pattern.DOT_ALL);
    Matcher m = token.matcher(sourceCode);
    int parenDepth = 0;
    while (m.find()) {
      char ch = m.group().charAt(0);
      switch (ch) {
        case '(':
          ++parenDepth;
          break;
        case ')':
          if (parenDepth == 0) {
            fail("Too many close parens");
          }
          --parenDepth;
          break;
      }
    }
    if (parenDepth != 0) {
      fail(parenDepth + " unclosed lists");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently writing a program in which I would like to access the variable
You are writing a Tetris program in Java. How would you set up your
I am writing a Java currency exchange program, in which I program 3 foreign
I am writing a program in Java, in which I define a class class
I'm writing a Java program for my work-study program which relies on the RXTX
I am writing a java program that will run tasks in parallel, which I
I am writing a small program in java which will draw a path on
For a uni assignment I'm writing a java program which needs to save and
I am writing a java program that needs a file open dialog. The file
I am writing this java program to find all the prime numbers up to

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.