A context-free grammar is considered ambiguous if there exists at least one string in the language generated by the grammar that has more than one distinct leftmost derivation, rightmost derivation, or parse tree.
This grammar generates the language $L = \{a^n b^n \mid n \ge 0\}$. For any string generated by this grammar, there is only one possible sequence of production rules to follow. For example, to generate $a^2b^2$, the derivation must be $S \Rightarrow aSb \Rightarrow aaSbb \Rightarrow aabb$. This grammar is unambiguous.
This grammar is commonly used for arithmetic expressions. It is ambiguous because it does not inherently define the precedence or associativity of operators '+' and '*'. For instance, the string $id + id * id$ can be parsed in multiple ways:
The existence of multiple parse trees for the same string demonstrates ambiguity.
This grammar generates strings containing any number of 'a's. Consider the string $aa$. It can be derived in multiple ways:
Since there are multiple distinct leftmost derivations (and thus parse trees) for the string $aa$, this grammar is ambiguous.
This grammar generates the language $L = \{a^n \mid n \ge 0\}$. To generate any string $a^n$, the only possible derivation involves applying the rule $S \rightarrow aS$ exactly $n$ times, followed by $S \rightarrow \epsilon$. For example, $S \Rightarrow aS \Rightarrow aaS \Rightarrow aaaS \Rightarrow aaaa$. There is only one sequence of choices. This grammar is unambiguous.
Based on the analysis, grammars 2 ($E \rightarrow E + E \mid E * E \mid id$) and 3 ($S \rightarrow aS \mid Sa \mid \epsilon$) are ambiguous.
| A | S $\rightarrow$ Ab aS$\rightarrow$ aA A$\rightarrow$ a | B | S $\rightarrow$ Ab A $\rightarrow$ Sa A $\rightarrow$ a |
| C | S $\rightarrow$ AS S $\rightarrow$ aA A$\rightarrow$ a | D | S $\rightarrow$ Ab S $\rightarrow$ aA A$\rightarrow$ a |
| E | S $\rightarrow$ Sb A $\rightarrow$ Aa A $\rightarrow$ $\varepsilon$ |
Which of the following grammars is (are)ambiguous ?
(1) S → SS | aSb | bSa |e
(2) S → aSbS | bSaS |e
(3) S → aAB
A → bBb
B → A Ι e
Consider the grammar,
G = S → abScB | e,
B→ bB | b.
What is the language generated by the given grammar ?
Consider the given grammar
S → Abc | ad
A → eS | Cr| ξ
C → f/p/ξ
Compute FIRST (A)
Select the sentence which is meaningful and grammatically correct.