4 Context Free Grammar

Theory of Computation · Unit 4 · 9 hrs

Context Free Grammar

Exam-focused notes for Context Free Grammar (Theory of Computation, CSC262): what the TU syllabus asks and how it has actually been tested, with 13 solved past questions from this unit.

What this unit covers

  • Introduction to Context Free Grammar (CFG)
  • Components of CFG
  • Use of CFG
  • Context Free Language (CFL)
  • Types of derivations: Bottomup and Topdown approach
  • Leftmost and Rightmost
  • Language of a grammar
  • Parse tree and its construction
  • Ambiguous grammar
  • Use of parse tree to show ambiguity in grammar
  • Regular Grammars: Right Linear and Left Linear
  • Equivalence of regular grammar and finite automata
  • Simplification of CFG: Removal of Useless symbols, Nullable Symbols, and Unit Productions
  • Chomsky Normal Form (CNF)
  • Greibach Normal Form (GNF)
  • Backus-Naur Form (BNF)
  • Context Sensitive Grammar
  • Chomsky Hierarchy
  • Pumping Lemma for CFL
  • Application of Pumping Lemma
  • Closure Properties of CFL

Leftmost and Rightmost

20815 marks

Define the language of a grammar. For the grammar , show the leftmost derivation for the string 00100 with its parse tree. S→0S0∣1∣εS \rightarrow 0S0 \mid 1 \mid \varepsilonS→0S0∣1∣ε[5]

- Grammar $G$ with productions: $$S \rightarrow 0S0 \mid 1 \mid \varepsilon$$ - Start symbol: $S$ - Terminals: $\{0, 1\}$ - Non-terminal: $\{S\}$ - Target string: $00100$ All data is present and readable. --- For a grammar $G = (V, T, P, S)$ where $V$ is th...

Full solved answer →

Equivalence of regular grammar and finite automata

20815 marks

Represent the following regular grammar to finite automata. S→aA∣aB∣εS \rightarrow aA \mid aB \mid \varepsilonS→aA∣aB∣εA→aA∣aSA \rightarrow aA \mid aSA→aA∣aSB→bB∣εB \rightarrow bB \mid \varepsilonB→bB∣ε[5]

Production Rules ------ S → aA \ aB \ ε A → aA \ aS B → bB \ ε --- Each non-terminal in the grammar becomes a state in the FA. We also need a dead/final state for terminals. - States: S, A, B, F (where F is the final/accepting state) - S is the start state ...

Full solved answer →

Chomsky Normal Form

20815 marks

Convert the following grammar to CNF. S→AAB,A→aA∣ε,B→ab∣aS \rightarrow AAB, \quad A \rightarrow aA \mid \varepsilon, \quad B \rightarrow ab \mid aS→AAB,A→aA∣ε,B→ab∣a[5]

$$S \rightarrow AAB, \quad A \rightarrow aA \mid \varepsilon, \quad B \rightarrow ab \mid a$$ --- CNF requires: Every production is either of the form A → BC (two non-terminals) or A → a (single terminal). No null productions (except possibly S → ε if ε ∈ L...

Full solved answer →
208010 marks

Define context free grammar with an example. Explain with example, how context free grammar is converted to Chomsky Normal Form.[10]

This is a theory and derivation question. The only structural data needed is the grammar chosen for the worked example, which is: $$ \begin{aligned} S &\to ASB \mid \varepsilon\\ A &\to aAS \mid a\\ B &\to SbS \mid A \mid bb \end{aligned} $$ No data is miss...

Full solved answer →
207910 marks

Given the following expression grammar for simple arithmetic expression with operator + and *. Remove the left recursion from this grammar then simplify and convert to CNF.E→E+T∣TE \rightarrow E+T \mid TE→E+T∣TT→T+F∣FT \rightarrow T+F \mid FT→T+F∣FF→(E)∣aF \rightarrow (E) \mid aF→(E)∣a[10]

The grammar as literally written in the question: $$E \to E+T \mid T$$ $$T \to T+F \mid F$$ $$F \to (E) \mid a$$ Note on interpretation: The problem statement says "operators + and ", but the middle rule is written with + again. The standard expression gram...

Full solved answer →
20785 marks

Construct the following grammar into Chomsky Normal Form. S→abSb∣a∣aAbS \rightarrow abSb \mid a \mid aAbS→abSb∣a∣aAbA→bS∣aAAb∣εA \rightarrow bS \mid aAAb \mid \varepsilonA→bS∣aAAb∣ε[5]

$$S \rightarrow abSb \mid a \mid aAbS$$ $$A \rightarrow bS \mid aAAb \mid \varepsilon$$ --- CNF requires every production to be of the form: - $A \rightarrow BC$ (two non-terminals), or - $A \rightarrow a$ (single terminal) --- Identify nullable variables: ...

Full solved answer →
20765 marks

Define Chomsky Normal Form and Greibach Normal Form in reference to CFG. Give a suitable example of each. [5]

--- A Context-Free Grammar (CFG) is said to be in Chomsky Normal Form if every production rule is of exactly one of the following two forms: A → BC (a non-terminal produces exactly two non-terminals) A → a (a non-terminal produces exactly one terminal) Wher...

Full solved answer →

Introduction to Context Free Grammar

20805 marks

What is the meaning of the term 'Context Free' in context free grammar? Justify with a suitable example. What is the need of a parse tree? [5]

--- The term "Context Free" means that the production rules can be applied to a non-terminal symbol regardless of the context (surrounding symbols) in which that non-terminal appears. Formally, a Context Free Grammar is defined as: G = (V, T, P, S) where th...

Full solved answer →
2080.110 marks

Define CFG. Construct a CFG that generates the language of all palindromes over {a,b} that do not contain the substring aa. Show the leftmost derivation and construct the equivalent parse tree for string babbbab.[10]

- Alphabet: $\Sigma = \{a, b\}$ - Language: all palindromes over $\{a,b\}$ that do NOT contain substring $aa$ - Target string for derivation and parse tree: babbbab - Marks: 10 All required data is present. --- A Context-Free Grammar is a 4-tuple $G = (V, T...

Full solved answer →

Parse tree and its construction

20795 marks

Define the term: Parse Tree, left-most and right-most derivation, sentential form and ambiguity with example. [5]

--- A parse tree is a graphical (tree) representation of how a string is derived from a Context Free Grammar (CFG). Formally, given a CFG G = (V, T, P, S), a parse tree has the following properties: - The root is labeled by the start symbol. - Each interior...

Full solved answer →

Regular Grammars

20795 marks

Define regular grammar. Also explain the method of converting right linear grammar into equivalent finite automata. [5]

--- A regular grammar is a grammar that represents a language which can also be represented by regular expressions. The language generated by a regular grammar is called a regular language, and it is accepted by both NFA and DFA. A regular grammar is a subs...

Full solved answer →

Chomsky Hierarchy

20785 marks

Explain about the Chomsky's Hierarchy about the language and programs. [5]

Chomsky's Hierarchy (proposed by Noam Chomsky) classifies formal languages and grammars into four levels based on their generative power and the computational model required to recognize them. Each level is a strict subset of the level above it. --- - Gramm...

Full solved answer →

Pumping Lemma for CFL

2080.15 marks

Prove that th language is not a context free grammar. $L = {a^n b^n c^n \mid n \geq 0}$ [5]

--- If A is a Context Free Language, then A has a pumping length P such that any string s where s ≥ P may be divided into 5 pieces s = uvxyz satisfying: 1. uv^i xy^i z ∈ A for every i ≥ 0 2. vy 0 (v and y are not both empty) 3. vxy ≤ P --- Assume L is Conte...

Full solved answer →