Unit 2 of 4 · M.Sc IT Sem 4

Unit 2: Context-free languages

Theory of Computation notes · PTU syllabus (PGCA1927)

3 min read6 topics10 exam questions
On this page
  1. Unit summary
  2. Chomsky classification of grammars
  3. Context-free grammars
  4. Properties of context-free languages
  5. Simplification of grammars
  6. Chomsky normal form
  7. Greibach normal form
  8. Key terms
  9. Quick revision
  10. Important questions

Unit summary

Context-free grammars describe nested structures such as programming-language syntax. This unit covers the properties of context-free languages, the Chomsky classification, context-free grammars, simplification of grammars, Chomsky normal form and Greibach normal form.

After this unit you can

  • Classify grammars using the Chomsky hierarchy
  • Write context-free grammars and derivations
  • Simplify grammars
  • Convert grammars to Chomsky and Greibach normal forms

PTU syllabus topics

  • Properties of context-free languages
  • Chomsky classification
  • context-free grammars
  • grammar simplification
  • Chomsky Normal Form
  • Greibach Normal Form
HierarchyChomsky hierarchy
  1. Type 0: recursively enumerable

    Turing machine

  2. Type 1: context-sensitive

    Linear bounded automaton

  3. Type 2: context-free

    Pushdown automaton

  4. Type 3: regular

    Finite automaton

1

Topic 1

Chomsky classification of grammars

HierarchyChomsky hierarchy
  1. Type 3: regular grammars

    A → aB or A → a; recognised by finite automata

  2. Type 2: context-free grammars

    A → α (one variable on the left); pushdown automata

  3. Type 1: context-sensitive grammars

    αAβ → αγβ, length non-decreasing; linear bounded automata

  4. Type 0: unrestricted grammars

    α → β; Turing machines

  • Each class contains the ones above it in the list of restrictions: regular ⊂ context-free ⊂ context-sensitive ⊂ recursively enumerable.
2

Topic 2

Context-free grammars

  • A CFG is a 4-tuple (V, T, P, S): variables, terminals, productions of the form A → α, and the start symbol.

Example

Balanced parentheses: S → (S) S or S → ε. Palindromes over {a, b}: S → aSa, bSb, a, b or ε. {aⁿbⁿ}: S → aSb or ε.

  • Derivations: leftmost (expand the leftmost variable first) and rightmost; a parse tree shows the structure. Expression grammar: E → E + T or T; T → T × F or F; F → (E) or id.
3

Topic 3

Properties of context-free languages

ComparisonClosure properties of CFLs
Closed?
Note

Union, concatenation, Kleene star

Yes

Combine grammars with a new start symbol

Intersection

No

{aⁿbⁿcᵐ} ∩ {aᵐbⁿcⁿ} = {aⁿbⁿcⁿ}, not context-free

Complement

No

Follows from non-closure under intersection

Intersection with a regular language

Yes

Product of PDA and DFA

  • Pumping lemma for CFLs: long strings can be written uvwxy with v and x pumped together; proves {aⁿbⁿcⁿ} is not context-free. Decidable: emptiness, finiteness and membership (CYK algorithm).
4

Topic 4

Simplification of grammars

ProcessSimplifying a CFG
  1. 1Remove ε-productions (except S → ε if needed)
  2. 2Remove unit productions (A → B)
  3. 3Remove useless symbols — first non-generating, then unreachable

Example

S → AB or a; A → a; B → C; C → b; D → d. Unit removal: B → b, C → b. D is unreachable and C becomes unreachable, so the result is S → AB or a, A → a, B → b.

5

Topic 5

Chomsky normal form

  • CNF: every production is A → BC or A → a (plus S → ε if ε is in the language). Every CFG without ε can be converted; parse trees become binary, which the CYK algorithm uses.
ProcessConverting to CNF
  1. 1Simplify the grammar
  2. 2Replace terminals in long right sides by new variables (Xa → a)
  3. 3Break right sides longer than two into chains of new variables

Example

S → aSb or ab becomes S → XaY or XaXb; Y → SXb; Xa → a; Xb → b.

6

Topic 6

Greibach normal form

  • GNF: every production is A → aα, where a is a terminal and α is a (possibly empty) string of variables. Each derivation step generates one terminal, so a string of length n needs exactly n steps — useful for constructing PDAs and parsers.
  • Conversion: start from CNF, order the variables, substitute to make productions begin with a higher-numbered variable or terminal, and remove left recursion (A → Aα or β becomes A → β or βZ; Z → α or αZ).

Key terms

Context-free grammar
Grammar with a single variable on the left of each production
Derivation
Sequence of production applications
Parse tree
Tree showing how a string is derived
Chomsky normal form
Productions A → BC or A → a
Greibach normal form
Productions starting with a terminal followed by variables

Quick revision

  • Type 0 to 3 grammars and their machines.
  • CFG 4-tuple; examples; leftmost and rightmost derivations; parse trees.
  • Closure: union, concatenation, star yes; intersection, complement no; CFL pumping lemma.
  • Remove ε, unit and useless productions.
  • CNF conversion; GNF and left-recursion removal.

Important exam questions

Practice questions written to the PTU exam pattern for this unit's syllabus: short answers (Section A style) and long answers (Sections B and C style).

Short-answer questions

  1. Q1.State the four types of the Chomsky hierarchy.
  2. Q2.Write a CFG for {aⁿbⁿ : n ≥ 1}.
  3. Q3.Are CFLs closed under intersection?
  4. Q4.What is a unit production?
  5. Q5.Define Chomsky normal form.
  6. Q6.Why is GNF useful?

Long-answer questions

  1. Q1.Explain the Chomsky classification of grammars.
  2. Q2.Explain context-free grammars with derivations and parse trees.
  3. Q3.Simplify a given context-free grammar.
  4. Q4.Convert a grammar to CNF and GNF.

Stuck on this unit?

Message SBS on WhatsApp for help with Theory of Computation, or to ask about studying M.Sc IT at Synetic.

WhatsApp us