Web Images Videos Maps News Shopping Gmail more »
Recently Visited Groups | Help | Sign in
Google Groups Home
Difficulty with if/else precedence
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  4 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Jonathan Ragan-Kelley  
View profile  
 More options Jan 19 2006, 4:02 am
From: "Jonathan Ragan-Kelley" <katok...@gmail.com>
Date: Thu, 19 Jan 2006 01:02:21 -0800
Local: Thurs, Jan 19 2006 4:02 am
Subject: Difficulty with if/else precedence
I'm parsing a C-style language and I'm facing a dangling-else ambiguity
problem:

if (rel1) if (rel2) stmt1 else stmt2

My statement nonterminal includes the following productions seemingly
relevant here:

statement
    ->assignexpression SEMICOLON
    |  L_BRKT statements? R_BRKT
    |  SEMICOLON
    |  %prec(CLOSEDIF) IF relation statement ELSE statement
    |  %prec(OPENIF) IF relation statement

The key here is that I am trying to resolve the ambiguity by defining:

%nonassoc CLOSEDIF OPENIF

to prioritize closely-match if/else pairs over more distantly dangling
potential pairs, as per the example in
[http://lava.net/~newsham/pyggy/html/node29.html].

WITHOUT the precedence rules for the two IF productions, the grammar
behaves exactly as expected, except for the above ambiguity.  However,
if I introduce these rules, parsing:

if (rel1) stmt1 else if (rel2) stmt2

fails on the second if.  Other sorts of if/else statements (that don't
involve 'ELSE statement' -> 'ELSE IF relation statement') parse fine,
and the else precedence example is handled correctly.

I fundamentally fail to see how introducing these precedence rules
should alter this.  Interestingly, swapping the higher/lower precedence
(to effectively group ambiguous ELSE sub-statements with the earliest,
rather than the latest unmatched IF) disambiguates in the opposite way
as expected, *and* the parse works for the oddly problematic "else if"
inputs.

Any pointers much appreciated, as I am very confused.  I am working
furiously towards a publication deadline in a few days and had totally
taken for granted that my grammar has been working for ages until I
just this evening ran into my first input in a very long time with
bracket-less if/else trees revealing the initial common ambiguity.

Thanks.
-jrk


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Tim Newsham  
View profile  
 More options Jan 19 2006, 1:15 pm
From: Tim Newsham <news...@lava.net>
Date: Thu, 19 Jan 2006 08:15:38 -1000 (HST)
Local: Thurs, Jan 19 2006 1:15 pm
Subject: Re: Difficulty with if/else precedence

> if (rel1) if (rel2) stmt1 else stmt2

> My statement nonterminal includes the following productions seemingly
> relevant here:

> statement
>    ->assignexpression SEMICOLON
>    |  L_BRKT statements? R_BRKT
>    |  SEMICOLON
>    |  %prec(CLOSEDIF) IF relation statement ELSE statement
>    |  %prec(OPENIF) IF relation statement

> The key here is that I am trying to resolve the ambiguity by defining:

> %nonassoc CLOSEDIF OPENIF

See test3.pyg in the examples directory for exactly this scenerio.

The %nonassoc precedence rule is too strong here.  It says that the
OPENIF is %right and %left of CLOSEDIF, meaning it can never be
derived at the left-most or the right-most non-terminal of the
CLOSEDIF production.  Ie. the "statement" in CLOSEDIF cannot be
an OPENIF statement.

What you want is the %pref rule (%pref CLOSEDIF OPENIF) which resolves
the ambiguity by preferring CLOSEDIF to OPENIF when constructing the
parse tables (ie. in a state that can perform an action to make
progress on the CLOSEDIF production and a diffferent action to make
progress on the OPENIF production, the OPENIF action is discarded).

> to prioritize closely-match if/else pairs over more distantly dangling
> potential pairs, as per the example in
> [http://lava.net/~newsham/pyggy/html/node29.html].

Ugh!  I thought I had updated the documentation reguarding this.
node29.html is wrong for the reason's described above.

> -jrk

Tim Newsham
http://www.lava.net/~newsham/

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jonathan Ragan-Kelley  
View profile  
 More options Jan 19 2006, 3:52 pm
From: "Jonathan Ragan-Kelley" <katok...@gmail.com>
Date: Thu, 19 Jan 2006 12:52:37 -0800
Local: Thurs, Jan 19 2006 3:52 pm
Subject: Re: Difficulty with if/else precedence
Perfect -- seems to work just as expected, now.  Thanks for the rapid
response, as ever.
-jrk

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jonathan Ragan-Kelley  
View profile  
 More options Jan 19 2006, 3:52 pm
From: "Jonathan Ragan-Kelley" <katok...@gmail.com>
Date: Thu, 19 Jan 2006 12:52:38 -0800
Local: Thurs, Jan 19 2006 3:52 pm
Subject: Re: Difficulty with if/else precedence
Perfect -- seems to work just as expected, now.  Thanks for the rapid
response, as ever.
-jrk

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google