Ambiguity in Context-Free Grammar

Learn to identify and resolve ambiguity in grammars, and gain hands-on experience with parsing methods such as LL(1), LR(0), SLR(1), LR(1), and LALR(1)

17 Participants 30 Minutes Beginner

This lab provides hands-on experience in identifying and resolving ambiguity in grammars, a fundamental aspect of designing programming languages and compilers. You will also gain practical knowledge of various parsing methods, including LL(1), LR(0), SLR(1), LR(1), and LALR(1). Understanding these parsing techniques is crucial for building robust compilers and interpreters for programming languages.



1. Basic understanding of formal grammars and context-free grammars.

2. You can also checkout our Lab on “Construct a basic DFA” to get some basic understanding.



Key Concepts:

Ambiguity in Grammars:

Ambiguity in grammars occurs when a given language can be parsed in multiple ways, leading to uncertainty in the interpretation of expressions or statements.

Resolving ambiguity is essential to ensure that a grammar can be parsed deterministically.

Parsing Methods:

Parsing is the process of analyzing the syntax of a programming language to create a parse tree or abstract syntax tree.

Various parsing methods exist, each with its own strengths and weaknesses in handling different types of grammars and languages.


Parsing Methods Covered in this Lab

1.  LL(1) Parsing:

- LL(1) parsers are top-down parsers that use a predictive parsing table to make parsing decisions.

- They are suitable for a wide range of grammars but require an unambiguous and left-factored grammar.

2. LR(0) Parsing:

- LR(0) parsers are bottom-up parsers that use a state transition table to perform shift and reduce actions.

- They can handle a broader class of grammars compared to LL(1) parsers.

3. SLR(1) Parsing:

- SLR(1) parsers are a variation of LR(0) parsers with lookahead symbols to reduce conflicts.

- They strike a balance between generality and simplicity.

4. LR(1) Parsing:

- LR(1) parsers are more powerful than LR(0) and SLR(1) parsers, as they consider one lookahead symbol.

- They can handle a wider range of grammars but require a more extensive parsing table.

5. LALR(1) Parsing:

- LALR(1) parsers are a compromise between LR(1) and SLR(1) parsers, offering good parsing power with a compact parsing table.

- They are commonly used in practice.


Refer to these links for more info:

1. Ambiguity in Context free Grammar and Context free Languages - GeeksforGeeks



This lab offers a hands-on exploration of identifying and resolving ambiguity in grammars and the practical application of parsing methods such as LL(1), LR(0), SLR(1), LR(1), and LALR(1). By the end of this lab, you will have a deeper understanding of how different parsing techniques work and when to apply them in the context of compiler design and language processing.

Ambiguity in CFG


Have a doubt? Got stuck somewhere?



Related Labs


Construct Basic NFA

Theory of Computation

  • 30 m
  • Beginner
  • 106
Learn to construct a Non Deterministic Finite Automation (NFA) with an alphabet ∑ = {0, 1} that accepts all strings ending with 01


Theory of Computation

  • 30 m
  • Beginner
  • 45
Explore context-free grammars (CFG) and Chomsky Normal Form (CNF) using hands-on activity

Explore Pushdown Automata

Theory of Computation

  • 30 m
  • Beginner
  • 177
Explore PDA for recognition of context-free languages e.g. L = { 0^n 1^n | n≥0 }

Turing Machine Program

Theory of Computation

  • 30 m
  • Beginner
  • 92
Understand and run Palindrome validation program on Turing Machine Simulator