COMS W4115
Programming Languages and Translators
Lecture 6: Context-Free Grammars
February 11, 2013

Lecture Outline

1. Context-Free Grammars (CFG's)

2. Derivations and Parse Trees

3. Ambiguity

4. Examples of Context-Free Grammars

5. Yacc: a Language for Specifying Syntax-Directed Translators

6. Practice Problems

  1. Let G be the grammar S → a S b S | b S a S | ε.
    1. What language is generated by this grammar?
    2. Draw all parse trees for the sentence abab.
    3. Is this grammar ambiguous?
  2. Let G be the grammar S → a S b | ε. Prove that L(G) = { anbn | n ≥ 0 }.
  3. Consider a sentence of the form id + id + ... + id where there are n plus signs. Let G be the grammar in section (3) above. How many parse trees are there in G for this sentence when n equals
    1. 1
    2. 2
    3. 3
    4. 4
    5. m?
  4. Write down a CFG for regular expressions over the alphabet {a, b}. Show a parse tree for the regular expression a | b*a.

7. Reading



aho@cs.columbia.edu