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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T12:58:54+00:00 2026-06-04T12:58:54+00:00

How can I make correct rules for parsing if-then[-else] case? Here is some grammar:

  • 0

How can I make correct rules for parsing if-then[-else] case?
Here is some grammar:

{
 module TestGram (tparse) where
}

%tokentype    { String  }
%token one    { "1"     } 
       if     { "if"    }
       then   { "then"  }
       else   { "else"  }

%name tparse  

%%

statement : if one then statement else statement {"if 1 then ("++$4++") else ("++$6++")"}
          | if one then statement                {"if 1 then ("++$4++")"}
          | one                                  {"1"}


{
 happyError = error "parse error"
}

This grammar parses the following expression correctly:

> tparse ["if","1","then","if","1","then","1","else","1"]
"if 1 then (if 1 then (1) else (1))"

But compiling raises a warning about shift/reduce conflict. The documentation to the happy contains an example of such conflict:
http://www.haskell.org/happy/doc/html/sec-conflict-tips.html

There are two solutions shown, the first is to change the recursion type (not clear how to do it in this case). And the second is not to change anything. This option is ok for me, but I need a consultation.

  • 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-04T12:58:55+00:00Added an answer on June 4, 2026 at 12:58 pm

    Note that it is possible to resolve this with a grammar in LALR(1) without an S/R conflict:

    stmt: open
        | closed
    
    open: if one then stmt             {"if 1 then ("++$4++")"}
        | if one then closed else open {"if 1 then ("++$4++") else ("++$6++")"}
    
    closed: one                            {"1"}
          | if one then closed else closed {"if 1 then ("++$4++") else ("++$6++")"}
    

    The idea comes from this page on resolving the dangling else/if-else ambiguity.

    The basic notion is that we class statements as “open” or “closed”: open statements are those which have at least one if that isn’t paired with a following else; closed are those which have no if at all, or which do have them, but they’re all paired with else.

    Parsing if one then if one then one else one thus parses:

    • . if — shift
    • if . one — shift
    • if one . then — shift
    • if one then . if — shift
    • if one then if . one — shift
    • if one then if one . then — shift
    • if one then if one then . one — shift
    • if one then if one then (one) . else — reduce by closed rule 1
    • if one then if one then closed . else — shift
    • if one then if one then closed else . one — shift
    • if one then if one then closed else (one) . — reduce by closed rule 1
    • if one then (if one then closed else closed) . — reduce by closed rule 2
    • if one then (closed) . — reduce by stmt rule 2
    • (if one then stmt) . — reduce by open rule 1
    • (open) . — reduce by stmt rule 1
    • stmt . — stop

    (When a reduce occurs, I’ve stated which reduction rule occurs, and have put parentheses around the tokens being reduced.)

    We can see that the parser has no ambiguity in LALR(1) (or rather, Happy or bison will tell us ;-)), and that following the rules produces the correct interpretation, with the inner if being reduced together with the else.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I can not locate the correct method to make the first item in a
How can make a link within a facebox window that redirects it to another
I can make a single row IKImageBrowserView by setting the [imageBrowser setContentResizingMask:NSViewWidthSizable]; but in
I can make a variable's name from two variables' value: $a = 'tea'; $b
I can make a log in for easily, so that's not the problem. What
I can make batch or vbs file on windows and run. this can automate
Can someone tell me how I can make a table be 100% height in
Can someone tell me how I can make the following output? I have a
Just wondering how I can make my app open automatically at login, but make
How I can make a Makefile, because it's the best way when you distribute

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.