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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T16:05:58+00:00 2026-06-02T16:05:58+00:00

I writing a simple grammar to recognize some expressions. Here, I’m posting a simpler

  • 0

I writing a simple grammar to recognize some expressions.
Here, I’m posting a simpler version of it, that I wrote just to simplify my explanation. This simpler version can recognize expressions like:

  1. this is a text
  2. [n]this is another text[/n]
  3. [n][n]this is a compound
    expression[/n][/n]

My problem is when I sumbmit a expression like:
[i]this should generate just a recognition exception[/n]

A recognition exception is thrown, but the parser enters in a infinte recursion, because it matches ‘[‘, but when it not matches ‘i’ it loses itself. I think that is happening because my text component of the grammar can not contain square brackets. So, I’m posting the grammar.

grammar ErrorTest;

expression
    :    rawText EOF
    |    command EOF
    ;

rawText
    :    word+
    ;

word
    :    ESPACE* TEXT ESPACE*
    ;

command 
    :    simpleCommand
    |    compoundCommand
    ;

simpleCommand
    :    HELP
    ;

compoundCommand
    :    rawText
    |    BEGIN compoundCommand END
    ;

HELP   : '[help]';

BEGIN  : '[n]';
END    : '[/n]';

ESPACE : ' ';
TEXT   : ~(' '|'['|']')*;

How can I solve it?

  • 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-02T16:05:59+00:00Added an answer on June 2, 2026 at 4:05 pm

    word matches the empty string because in

    word
        :    ESPACE* TEXT ESPACE*
        ;
    

    TEXT matches the empty string which causes

    rawText
        :    word+
        ;
    

    to loop infinitely.

    Change

    TEXT   : ~(' '|'['|']')*;
    

    to

    TEXT   : ~(' '|'['|']')+;
    

    which will make your grammar only finitely ambiguous.

    The way to think about this is that rawText can match the empty string in many ways

    1. Zero TEXT tokens
    2. One TEXT token with length 0.
    3. Two TEXT tokens with length 0.
    4. Three TEXT tokens with length 0.
    5. …

    This manifests when you have a syntactic error ([i]) because it tries each of these alternatives to see if any of them resolve the error.


    To get rid of any quadratic behavior, you should really make it completely unambiguous.

    rawText : ign (word (ign word)*)? ign;
    ign     : ESPACE*;
    word    : TEXT;
    

    The problem with the naive fix is that rawText can match "foo" in several ways:

    1. TEXT("foo")
    2. TEXT("fo"), ESPACE(""), TEXT("o")
    3. TEXT("f"), ESPACE(""), TEXT("oo")
    4. TEXT("f"), ESPACE(""), TEXT("o"), ESPACE(""), TEXT("o")
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I writing simple application that need to make some record of the incoming sound.
I'm writing a parser for a very simple grammar in javacc. It's beginning to
I'm looking into writing simple graphics code in Android and I've noticed some synchronized()
I am writing simple site that requires users and profiles to be handled. The
I'm writing simple GUI using wxPyhon and faced some problems. My application does simple
I'm writing an interpreter in Python for a very simple language grammar. I have
I'm writing a simple parser for my compilers class (just a proof of concept
I'm writing simple Android app to filter some events from user's calendar. The questions
I've been recently writing simple game in C++ with SFML. Here's my question: In
I'm writing a simple graphics engine that draws textures on the screen using OpenGL

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.