Context-free Grammars Example : S Shortened notation : S aSaS | aSa | bSb S bSb Which strings...
-
Upload
aileen-caldwell -
Category
Documents
-
view
220 -
download
2
Transcript of Context-free Grammars Example : S Shortened notation : S aSaS | aSa | bSb S bSb Which strings...
Context-free Grammars
Example :
S Shortened notation :
S aSa S | aSa | bSb
S bSb
Which strings can be generated from S ?
[Section 6.1]
Context-free Grammars
Def: A context-free grammar (CFG) is a 4-tuple G = (V,,S,P) where
- V is a finite set of variables (nonterminal symbols)
- is a finite set of terminal symbols (terminals)
- S 2 V is the start symbol
- P is the finite set of grammar rules (productions) of the form A , where A 2 V and 2 (V [ )*
(V and are disjoint.)
For example from previous slide :
[Section 6.1]
Context-free Grammars
A derivation of the string “abaaba” :
Def :
Let G=(V,,S,P) be a CFG. For , 2 (V [ )* we write )G if there is a production C in P such that = 1C2 and = 12. We say that is derived from .
If 1, …, n 2 (V [ )* and k ) k+1 for k=1,…,n-1, we write 1 )G
* n.
Language generated by G, denoted L(G), is { x 2 * | ______ }
[Section 6.1]
Context-free Grammars
Examples :
{ akbk | k ¸ 0 }
{ x 2 {a,b}* | x has at least 2 b’s }
{ x 2 {a,b}* | |x| is even }
[Section 6.1]
Context-free Grammars
Example :
S | abS | baS | aSb | bSa | Sab | Sba
What is the language generated by this grammar ?
(We’ve seen this before…)
[Section 6.1]
Context-free Grammars
Our G : S | abS | baS | aSb | bSa | Sab | Sba | SS
Recall that we proved (using structural induction) that every string generated by G contains the same number of a’s and b’s.
Moreover, we can generate every string with equal number of a’s and b’s by the rules of G.
[Section 6.1]
Closure Properties of CFL’s
Given are two CFG’s G1 = (V1,,S1,P1) and G2 = (V2,,S2,P2).
- Give a CFG G such that L(G) = L(G1) [ L(G2).
- Give a CFG G such that L(G) = L(G1)L(G2).
- Give a CFG G such that L(G) = L(G1)*.
Thus, context-free languages (CFL’s) are closed under union, concatenation, and Kleene’s star.
[Section 6.2]
Derivation Trees & Ambiguity
Consider G: S S + S | S * S | (S) | x
Describe the language generated by G:
Give all derivations of x + x * x :
[Section 6.4]
Derivation Trees
We will draw derivation trees of x + x * x :
A leftmost derivation is a derivation in which each derivation step uses a rule for the leftmost nonterminal symbol.
Derivation trees are in 1-1 correspondence with leftmost derivations.
[Section 6.4]
Ambiguity
A CFG G is ambiguous iff there exists a string x 2 L(G) such that x has more than one distinct derivation trees (or, equivalently, more than one leftmost derivation).
Is S S + S | S * S | (S) | x ambiguous ?
Can you give an unambiguous grammar for mathematical expressions ?
[Section 6.4]
Ambiguity in Programming Languages
Consider the following production rule :
<statement>
if ( <expression> ) <statement> |
if ( <expression> ) <statement> else <statement> |
<otherstatement>
What are the terminals and nonterminals in the above ?
How to fix the problem ?
[Section 6.4]
CFL’s vs. Regular Languages
Is every context-free language regular ?
Is every regular language context-free ?
(Recall the definition of regular languages.)
[Section 6.5]
Parsing & Normal Forms
How to determine if a string is in a language generated by a CFG ? (Algorithms that do this are called parsers.)
It helps to have the grammar in a normal form (more restrictions than the original definition of CFG’s).
Chomsky normal form :
Every rule is of the form A or A A1A2 where A,A1,A2 2 V and 2 .
Example : S S + S | S * S | (S) | x
Convert this grammar into Chomsky normal form.
[Section 6.6]
Parsing & Normal Forms
How to determine if a string is in a language generated by a CFG ? (Algorithms that do this are called parsers.)
It helps to have the grammar in a normal form (more restrictions than the original definition of CFG’s).
Chomsky normal form :
Every rule is of the form A or A A1A2 where A,A1,A2 2 V and 2 .
Example : S S + S | S * S | (S) | x
Convert this grammar into Chomsky normal form.
[Section 6.6]
Parsing & Normal Forms
Greibach normal form :
Every rule is of the form A where 2 and 2 V*.
Example : S S + S | S * S | (S) | x
Convert this grammar into Greibach normal form.
[Section 6.6]