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

  • Thread starter SpiffyEh
  • Start date
In summary, the conversation discusses an If-Else grammar and the concept of ambiguity in grammar. The conversation explores the definition of ambiguity and how it applies to the given grammar. Two examples of ambiguous sentences are generated using the production rules, and the conversation also touches on ways to rewrite the grammar to remove ambiguity. However, no solution to completely eliminate ambiguity is found.
  • #1
SpiffyEh
194
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 grammer 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
  • #2
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)
 
  • #3
all i can generate is an if then and an if then else
 
  • #4
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.
 
  • #5
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?
 
  • #6
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)
 
  • #7
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
 
  • #8
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"!
 
  • #9
<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 differnt 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
 

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

1. What is ambiguity in grammars?

Ambiguity in grammars refers to the property of a language or a set of rules that allows for more than one possible interpretation or meaning. This can occur when a sentence or phrase can be parsed or understood in multiple ways.

2. Why is ambiguity a problem in grammars?

Ambiguity can lead to confusion and miscommunication, as different interpretations can result in different meanings. This can be particularly problematic in formal languages, such as programming languages, where ambiguity can result in errors or unexpected outcomes.

3. How can ambiguity in grammars be resolved?

One way to resolve ambiguity is by adding more rules or constraints to the grammar that clarify the intended meaning. Another approach is to use punctuation or other symbols to disambiguate phrases or sentences. In some cases, the context in which the language is used can also help to resolve ambiguity.

4. What are some examples of ambiguous sentences in grammars?

One example is the phrase "old men and women," which can be interpreted as either "men who are old and women" or "old men and old women." Another example is the sentence "I saw her duck," which can mean "I saw her pet duck" or "I saw her quickly move out of the way."

5. How does ambiguity in grammars affect natural language processing?

Ambiguity in grammars poses a challenge for natural language processing (NLP) systems, which rely on precise rules and interpretations to understand and generate language. Resolving ambiguity in NLP often requires the use of advanced algorithms and techniques, such as machine learning, to accurately interpret and process language.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
3
Views
979
  • Programming and Computer Science
Replies
1
Views
1K
Replies
14
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
3K
  • Sci-Fi Writing and World Building
2
Replies
48
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
20
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
950
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Programming and Computer Science
Replies
3
Views
1K
  • STEM Academic Advising
Replies
2
Views
763
Back
Top