COMS W3261
Computer Science Theory
Lecture 10: October 14, 2009
Context-Free Grammars
1. Outline
- Review
- Definition of a context-free grammar
- Derivations
- Leftmost and rightmost derivations
- Parse trees
2. Review
- Decision properties
- Testing equivalence of states
- Testing equivalence of DFA's
- Minimizing the number of states in a DFA
3. Definition of a Context-Free Grammar (CFG)
- A CFG is a formalism for defining a language.
- A CFG has four components (V, T, P, S):
- V is a finite set of variables called nonterminals,
sometimes called syntactic categories.
- Each variable represents a language.
- T is a finite set of symbols called terminals.
- The set of terminals is the alphabet of the language
defined by the grammar.
- P is a finite set of productions, rewrite rules of the form
A → α
- where A is a nonterminal and α is a string (possibly empty)
of nonterminals and terminals.
- S is a nonterminal, called the start symbol.
- Example grammar G1:
- V = {
S }
- T = { ( , ) }
- P is the set with the two productions
S → S ( S )
S → ε
- S is the start symbol.
4. Derivations
- A grammar is used to define a language.
- Example of a derivation of
( )( ) from S in G1:
S ⇒ S ( S )
⇒ S ( S ) ( S )
⇒ ( S ) ( S )
⇒ ( ) ( S )
⇒ ( ) ( )
This derivation shows that ( )( ) is string in the
language defined by G1.
L(G), the set of all strings of terminals that can be derived
from the start symbol
of a grammar G, is the language defined by G.
We often call a string in L(G) a sentence of L(G).
A string of terminals and nonterminals that can be derived from
the start symbol of a grammar is called a sentential form.
5. Leftmost and Rightmost Derivations
- A derivation in which at each step we replace the leftmost nonterminal
by one of its production bodies is called a leftmost derivation.
- The derivation above is a leftmost derivation of
( )( )
from S in G1.
- A rightmost derivation is one in which at each step we replace the
rightmost nonterminal by one of its production bodies.
- Here is a rightmost derivation of
( )( ) from S
in G1:
S ⇒ S ( S )
⇒ S ( )
⇒ S ( S ) ( )
⇒ S ( ) ( )
⇒ ( ) ( )
6. Parse Trees
- A derivation can be represented by a parse tree.
- Let G = (V, T, P, S) be a CFG. A parse tree for G is a tree in which:
- Each interior node is labeled by a nonterminal in V.
- Each leaf is labeled by a nonterminal, or a terminal, or ε
- If an interior node is labeled by a nonterminal A and its children are
labeled X1, X2, ... , Xk, then
A → X1X2 ... Xk is a production in P.
- The yield of a parse tree is the string obtained by
concatenating the labels of the leaves from the left.
- Derivations, parse trees, leftmost derivations, rightmost derivations,
and recursive inference are equivalent.
- A parser for a grammar G is a program that takes as input a string
and produces as output a parse tree for the string or a message
saying that the string cannot be generated by G.
- A parser generator is a program that takes as input a grammar G
and produces as output a parser for G. YACC is a widely used
parser generator.
7. Practice Problems
- Construct a CFG that generates
{
anbn | n ≥ 0 }:
- Construct a CFG that generates {
wwR | w
is any string of a's and b's }:
- Construct a CFG for arithmetic expressions with the operators
+ and *, parentheses, and a constant c.
- Construct a CFG for regular expressions over the alphabet {0, 1}.
8. Reading Assignment
aho@cs.columbia.edu