Understanding Ambiguity in If-Else Grammars: An Explanation and Analysis

  • Thread starter Thread starter SpiffyEh
  • Start date Start date
Click For Summary

Discussion Overview

The discussion revolves around the ambiguity present in a specific If-Else Grammar used in programming languages. Participants explore the definition of ambiguity in grammars, attempt to generate sentences using the provided production rules, and discuss potential rewrites to eliminate the ambiguity.

Discussion Character

  • Exploratory
  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant seeks clarification on the meaning of ambiguity in grammars, referencing the definition that a grammar is ambiguous if it generates two or more distinct parse trees for the same sentence.
  • Another participant suggests generating sentences using the production rules to better understand the grammar.
  • Several participants discuss the ability to produce various forms of statements from the grammar, including nested if-then and if-then-else structures.
  • A participant provides examples of how to derive the same sentence through different production paths, illustrating the ambiguity.
  • There is a proposal to rewrite the grammar to remove ambiguity, with one participant suggesting a new structure involving blocks.
  • Concerns are raised about whether the rewritten grammar generates the same sentences as the original and whether it introduces new strings.
  • One participant questions if the new grammar still retains the original ambiguity, prompting further exploration of potential solutions.

Areas of Agreement / Disagreement

Participants express uncertainty about the ambiguity of the rewritten grammar and whether it successfully resolves the issues present in the original grammar. There is no consensus on a definitive solution to eliminate ambiguity.

Contextual Notes

Participants note the importance of generating sentences systematically to explore the grammar's properties, but some express confusion about the implications of their findings regarding ambiguity.

SpiffyEh
Messages
191
Reaction score
0

Homework Statement



Consider the following If-Else Grammar:


<stmt> => if <expr> then <stmt>

| if <expr> then <stmt> else <stmt>
| ... (assume other types of <stmt>s, not part of exercise


Show that this grammar is ambiguous.


Homework Equations





The Attempt at a Solution



I don't really understand what ambiguous means, I have the definition

A grammar is ambiguous if and only if it generates two or more distinct parse trees of same type (derivation) for same sentence

But I don't see how this works. Can someone please explain to me how this grammar could generate 2 or more distinct parse trees for the same derivation of a sentence? I just don't see how it could.
 
Physics news on Phys.org
Have you tried generating sentences using these production rule, to get an idea for the grammar?


(I suppose you can't actually generate a sentence, but you can at least generate various intermediates)
 
all i can generate is an if then and an if then else
 
Ah, that isn't all you can produce from a <stmt> symbol! Each of the two things you produced have more <stmt> symbols in them, so you can produce more than just two intermediates from <stmt> using those two production rules.
 
umm.. I can produce, if then if then as many times as needed, or if then if then... else or if then else if then else... and so on?
 
It's probably worth trying to play with the production rules in systematic and/or creative ways, to see what can be produced. Experimentation is often useful when faced with something new.

(Also, if you do wind up enumerating all short ways of applying the production rules a small number of times, you will stumble across an example of the ambiguity -- this particular ambiguity is relatively simple)
 
I don't understand what you mean, by if you do wind up enumerating all short ways of applying the production rules a small number of times, you will stumble across an example of the ambiguity.
I was never actually given an example fo ambiguity so I'm confusedm from the defination it seems that if i can come up with two different ways of coming up with the same sentence its ambiguous? But I don't see how I would do that
 
from the defination it seems that if i can come up with two different ways of coming up with the same sentence its ambiguous?
Right. If you go through and apply the production rules in different ways and get the same sentence, it's an example of ambiguity.

Technically you're going to produce the same intermediate in two different ways (because you don't have a production rule that turns intermediate symbols into final symbols), but that's good enough for this exercise.

But I don't see how I would do that
That's why I said "stumble" rather than "proceed knowing exactly how things are going to work out"!
 
<stmt> => if <expr> then <stmt>
=> if <expr> then if <expr> then <stmt> else <stmt>

and

<stmt> => if <expr> then <stmt> else <stmt>
=> if <expr> then if <expr> then <stmt> else <stmt>


I got those two, they go two different routes but end up the same.
Now I need to rewrite the grammar to remove the ambiguity.
I came up with:
<stmt> => if <expr> then <block>

<block> => <stmt>
| <stmt> else <stmt>
 
  • #10
When you say rewrite the grammar, do you mean that it should generate exactly the same sentences? Some programming languages deal with the ambiguity by changing things a bit; e.g.
<stmt> => if <expr> then <stmt> else <stmt> end if​

Anyways, have you tried generating strings from your new grammar in systematic and creative ways? I grant that it's obvious anything from the original grammar can be generated, but does your grammar allow new strings the original didn't?
 
  • #11
Hurkyl said:
When you say rewrite the grammar, do you mean that it should generate exactly the same sentences??

I'm not exactly sure, that's all I was told by the assignment.
Hurkyl said:
Some programming languages deal with the ambiguity by changing things a bit; e.g.
<stmt> => if <expr> then <stmt> else <stmt> end if​

Anyways, have you tried generating strings from your new grammar in systematic and creative ways? I grant that it's obvious anything from the original grammar can be generated, but does your grammar allow new strings the original didn't?

I don't see it allowing anything else
 
  • #12
SpiffyEh said:
I don't see it allowing anything else
I think you're right on this point. (I made an error earlier)

Now the real test... is your example free of ambiguity? It contains pretty much exactly the same ambiguity as the original one, doesn't it?
 
  • #13
hmm... I'm not sure what can be done to make it not have any
 

Similar threads

  • · Replies 6 ·
Replies
6
Views
2K
  • Poll Poll
  • · Replies 142 ·
5
Replies
142
Views
7K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 15 ·
Replies
15
Views
4K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 48 ·
2
Replies
48
Views
8K
Replies
6
Views
6K
Replies
20
Views
5K
Replies
2
Views
2K