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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T12:42:49+00:00 2026-06-07T12:42:49+00:00

can somebody help me with writing correct grammar rules for nested if statements? In

  • 0

can somebody help me with writing correct grammar rules for nested if statements?
In my language, I am able to write constructions like this:

(IF CONDITION)
    some statements
    (IF CONDITION)
        some statements 
    (ELSE IF CONDITION) 
        some statements
    (ELSE IF CONDITION) 
        some statements
    (ELSE IF CONDITION) 
        some statements 
    (ELSE) 
        some statements
    (END) 
    some statements 
(ELSE IF CONDITION) 
    some statements 
(ELSE) 
    some statements 
(END)

I wrote lexer so left and right parenthesis are RULE_OPEN and RULE_CLOSE tokens,
“IF” is IF token,
“END” is END token,
“ELSE” is ELSE token,
“CONDITION” is CONDITION token.

Assume that “some statements” may be anything that is allowed in my language (like in common programming language). It is important that it is possible to nest IF statements possibly infinitely.

Hope this is clear, please let me know if I explained it badly.

No matter how I am trying. I am always getting shift/reduce conflicts and parser is not accepting correct input.

Even though, I have successfully wrote rules without else-if. When I add rules for else-if, code starts to be very complicated for me.

Below is my sucessfull approach without else-if part (I listed only relevant rules):

statements: statement

statements: statements statement

statement: code
| data_out
| rule

rule: inline_if_statement
| block_if_statement

block_if_statement: RULE_OPEN IF CONDITION RULE_CLOSE statements RULE_OPEN END RULE_CLOSE

block_if_statement: RULE_OPEN IF CONDITION  RULE_CLOSE statements block_else_statement

block_else_statement: RULE_OPEN ELSE RULE_CLOSE statements RULE_OPEN END RULE_CLOSE 

block_else_statement: empty

empty : 

I think that this is very common problem in parsing and I hope that somebody here have already solved it 🙂
Thanks for helping!

  • 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-07T12:42:51+00:00Added an answer on June 7, 2026 at 12:42 pm

    Generally, a single shift-reduce conflict for a language that has an optional ELSE clause can be tolerated. Pete Jinks offers a couple of alternative formulations that can resolve the conflict.

    A strategy for specifying ELSE-IF constructions: treat this like any other recursively-defined repeating block:

    running_else_if_statement : RULE_OPEN IF CONDITION RULE_CLOSE statements else_if_blocks
        RULE_OPEN ELSE RULE_CLOSE statements RULE_OPEN END RULE_CLOSE
                              ;
    
    else_if_blocks : else_if_block
                   | else_if_blocks else_if_block
                   ;
    
    else_if_block : RULE_OPEN ELSE_IF CONDITION RULE_CLOSE statements 
                  ;
    

    As a side note on style: most practitioners consistently combine all alternatives for a production with pipes, the way you have done with

    statement : code
              | data_out
              | rule
              ;
    

    It’s confusing to read:

    statements : statement
               ;
    
    statements : statements statement
               ;
    

    Most prefer:

    statements : statement
               | statements statement
               ;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can somebody help me with this. There is HTML code: <h3> <label> <input type=checkbox
Heyy can somebody help me ? i have this issue that still do know
Can somebody help me convert this VB function to Perl (PHP, Ruby or Python
Can somebody help me understand how this pointed corner was actually generated in css
Can somebody help me construct this regular expression, please... Given the following strings... "April
Can somebody help on this one. I am trying to set the name of
Can somebody help me, I want to use in jQuery something like ScrollTo. When
can somebody help me to write a sql select to perform a task. So
Can somebody help me with this: this is my code: //js var TestPlugin={ test:function(name,successCallback,failureCallback){
I can't find the download link from this plugin: http://plugins.jquery.com/project/jgfeed Can somebody help me?

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.