Ambiguity Analysis of an Expression Grammar with +, *, Parentheses, and id

Ambiguity Analysis of an Expression Grammar with +, *, Parentheses, and id

Verified Sources
Nov 4, 2026

Consider the grammar:

E→E+T∣TE \rightarrow E + T \mid T T→T∗F∣FT \rightarrow T * F \mid F F→(E)∣idF \rightarrow (E) \mid id

To decide whether it is ambiguous, we must determine whether there exists at least one string of terminals (e.g., a token sequence like id+id∗idid+id*id) that has two distinct parse trees (equivalently, two distinct leftmost derivations / rightmost derivations). A grammar is ambiguous if some string has multiple parse trees; it is unambiguous if every string has exactly one parse tree.

Key keyword ambiguity} questions to answer here:

  • Where can ++ and ∗* appear in the derivations?
  • Does the grammar enforce operator precedence (∗* binds tighter than ++)?
  • Does the grammar enforce left associativity for both ++ and ∗*?

We’ll show that the grammar enforces:

  1. ∗* has higher precedence than ++, because EE expands into TT’s and only EE adds ++.
  2. Both ++ and ∗* are left-associative, because the recursion is of the form E→E+TE \to E + T and T→T∗FT \to T * F (left recursion).

As a result, the parse structure for any expression is forced: ++ “splits” the expression into a sequence of TT terms, and each TT “splits” further into a sequence of FF factors separated by ∗*, with parentheses handled by F→(E)F \to (E).

The grammar is therefore unambiguous.

type="tip" title="Pro Tip" content="To prove unambiguity, it’s often enough to argue uniqueness of the operator-level decomposition: show that every string has exactly one way to split into top-level ++ terms and then into ∗* factors."

Operator decomposition idea (why the grammar structure is forced)

Observe the nonterminals’ roles:

  • EE represents expressions built from ++ connecting one or more TT’s.
  • TT represents terms built from ∗* connecting one or more FF’s.
  • FF represents the smallest units: either (E)(E) or idid.

So any derivation for a string of the form:

  • EE will produce some sequence containing ++ at the “top level”.
  • TT will produce some sequence containing ∗* at the “next level”.
  • FF will never introduce ++ or ∗* by itself—those operators can appear only via expanding EE or TT.

This immediately yields precedence: a ∗* cannot “escape” to become a ++ at the EE level, because EE only creates ++ by using the production E→E+TE \to E + T.

keyword precedence] and keyword associativity] here follow from the grammar’s stratification:

  • ∗* happens inside TT
  • ++ happens inside EE

Unambiguity justification by forced grouping at each operator level

  1. 1
    Step 1

    Let ww be a string derived from EE (e.g., id+id∗idid+id*id, (id+id)∗id(id+id)*id, etc.).

  2. 2
    Step 2

    At the top level, the only way to introduce a ++ token in a derivation from EE is via E→E+TE \to E + T. Therefore any derived string that contains ++ must have a unique decomposition into a left part and a right TT at the outermost ++ that is not inside parentheses.

  3. 3
    Step 3

    When ++ occurs inside parentheses, it is generated only inside F→(E)F \to (E). This prevents ++ from interfering with the outer EE-level decomposition because the parentheses create a boundary for the substring derived by that EE.

  4. 4
    Step 4

    Once the outer split produces a left EE and a right TT, each TT substring can contain ∗* only through T→T∗FT \to T * F. Thus each TT substring has a forced decomposition into left and right parts around ∗* at the corresponding TT level.

  5. 5
    Step 5

    Similarly, if a TT substring contains ∗*, it must correspond to repeated use of T→T∗FT \to T * F with FF units as the leaves. Parentheses again isolate F→(E)F \to (E) occurrences uniquely.

  6. 6
    Step 6

    Because the recursion is E→E+TE \to E + T (not E→T+EE \to T + E) and T→T∗FT \to T * F (not T→F∗TT \to F * T), the grouping of a chain like id+id+idid+id+id is forced to left association: ((id+id)+id)((id+id)+id). Likewise, id∗id∗idid*id*id groups as ((id∗id)∗id)((id*id)*id).

  7. 7
    Step 7

    Since the outermost ++ decomposition is unique (thanks to operator placement rules and parentheses boundaries), and each produced TT substring has a unique ∗* decomposition, and each FF leaf is either (E)(E) or idid, the entire parse tree for ww is unique. Hence the grammar is unambiguous.

type="warning" title="Common Pitfall" content="A grammar with left recursion can still be ambiguous in some cases (e.g., if both parse structures can introduce the same operators at the same syntactic level). Here, the separation into E (only +) and T (only *) is what prevents competing parse trees."

Worked micro-examples (showing uniqueness)

Example 1: id+id∗idid+id*id

  • The ∗* must be inside TT because EE only adds with E→E+TE \to E+T.
  • There is only one plausible hierarchy:
    • E⇒E+TE \Rightarrow E + T
    • left E⇒T⇒F⇒idE \Rightarrow T \Rightarrow F \Rightarrow id
    • right T⇒T∗FT \Rightarrow T * F
    • T⇒F⇒idT \Rightarrow F \Rightarrow id for the left factor
    • F⇒idF \Rightarrow id for the right factor

So the structure corresponds to:

  • id+(id∗id)id + (id * id)

Example 2: (id+id)∗id(id+id)*id

Parentheses force the id+idid+id part to be parsed as an EE inside a single FF:

  • T⇒T∗FT \Rightarrow T * F
  • the FF on the left is (E)(E), and inside it the EE must be E⇒E+TE \Rightarrow E + T

So it becomes:

  • (id+id)∗id(id+id) * id

These examples illustrate precedence: without parentheses, ∗* groups tighter; with parentheses, the EE inside FF groups first.

How the grammar enforces structure from tokens to parse tree

Leaves

1) Token level

Only idid or (E)(E) appear as FF."

Build products

2) Factor level

T→T∗FT \to T * F constructs sequences of FF connected by ∗*."

Build sums

3) Term level

E→E+TE \to E + T constructs sequences of TT connected by ++. "

Recursive isolation

4) Parentheses boundary

F→(E)F \to (E) creates a boundary so outer operators cannot regroup inside."

Knowledge Check

Question 1 of 3
Q1Single choice

The grammar E→E+T∣TE \to E + T \mid T, T→T∗F∣FT \to T * F \mid F, F→(E)∣idF \to (E) \mid id is best classified as: