> G(
H/0|DTimes New Romanp5D,0DWo
0DDTahomaew Romanp5D,0DWo
0D" DMonotype Sortsp5D,0DWo
00DSymbole Sortsp5D,0DWo
0D@DLucida Sans Unicode,0DWo
0D"a.
@n?" dd@ @@``xp?:
! "#$%&'()+*,-./0123456789:c$pf3f33f3@g4JdJd08ppp@uʚ;2Nʚ;<4!d!ddЁ
0P<4dddddЁ
0P<4BdBddЁ
0P:2___PPT9/0?O=<Context-Free GrammarsFFormalism
Derivations
Backus-Naur Form
Left- and Rightmost Derivations%Informal CommentsA context-free grammar is a notation for describing languages.
It is more powerful than finite automata or RE s, but still cannot define all possible languages.
Useful for nested structures, e.g., parentheses in programming languages.(f
.Informal Comments (2)Basic idea is to use variables to stand for sets of strings (i.e., languages).
These variables are defined recursively, in terms of one another.
Recursive rules ( productions ) involve only concatenation.
Alternative rules for a variable allow union.;!Example: CFG for { 0n1n | n > 1} T"33
oProductions:
S -> 01
S -> 0S1
Basis: 01 is in the language.
Induction: if w is in the language, then so is 0w1.X
R
3f 3f+
CFG FormalismTerminals = symbols of the alphabet of the language being defined.
Variables = nonterminals = a finite set of other symbols, each of which represents a language.
Start symbol = the variable whose language is the one being defined.b f; ffHf:Productions.A production has the form variable -> string of variables and terminals.
Convention:
A, B, C,& are variables.
a, b, c,& are terminals.
& , X, Y, Z are either terminals or variables.
& , w, x, y, z are strings of terminals only.
a, b, g,& are strings of terminals and/or variables.V
f-3
3f.Example: Formal CFG33
Here is a formal CFG for { 0n1n | n > 1}.
Terminals = {0, 1}.
Variables = {S}.
Start symbol = S.
Productions =
S -> 01
S -> 0S1^oJ.Derivations IntuitionWe derive strings in the language of a CFG by starting with the start symbol, and repeatedly replacing some variable A by the right side of one of its productions.
That is, the productions for A are those that have A on the left side of the ->.<SfS.Derivations FormalismWe say aAb => agb if A -> g is a production.
Example: S -> 01; S -> 0S1.
S => 0S1 => 00S11 => 000111.nf 332Iterated Derivation=>* means zero or more derivation steps.
Basis: a =>* a for any string a.
Induction: if a =>* b and b => g, then a =>* g.|+3f 3f Example: Iterated Derivation33aS -> 01; S -> 0S1.
S => 0S1 => 00S11 => 000111.
So S =>* S; S =>* 0S1; S =>* 00S11; S =>* 000111.=Sentential Forms.Any string of variables and/or terminals derived from the start symbol is called a sentential form.
Formally, a is a sentential form iff S =>* a.LSf&$
Language of a Grammar~If G is a CFG, then L(G), the language of G, is {w | S =>* w}.
Note: w must be a terminal string, S is the start symbol.
Example: G has productions S -> and S -> 0S1.
L(G) = {0n1n | n > 0}.?:G
f3f6339Context-Free LanguagesA language that is defined by some CFG is called a context-free language.
There are CFL s that are not regular languages, such as the example just given.
But not all languages are CFL s.
Intuitively: CFL s can count two things, not three.<3fs3f)>TZ!BNF NotationGrammars for programming languages are often written in BNF (Backus-Naur Form ).
Variables are words in <& >; Example: <statement>.
Terminals are often multicharacter strings indicated by boldface or underline; Example: while or WHILE.t=f 33^33,DNE$BNF Notation (2)pSymbol ::= is often used for ->.
Symbol | is used for or.
A shorthand for a list of productions with the same left side.
Example: S -> 0S1 | 01 is shorthand for S -> 0S1 and S -> 01.B<?><?337!
:BNF Notation Kleene ClosureSymbol & is used for one or more.
Example: <digit> ::= 0|1|2|3|4|5|6|7|8|9
<unsigned integer> ::= <digit>&
Note: that s not exactly the * of RE s.
Translation: Replace a& with a new variable A and productions A -> Aa | a.M!(K$33"!(3
."Example: Kleene Closure33 ZGrammar for unsigned integers can be replaced by:
U -> UD | D
D -> 0|1|2|3|4|5|6|7|8|92)[#BNF Notation: Optional ElementsSurround one or more symbols by [& ] to make them optional.
Example: <statement> ::= if <condition> then <statement> [; else <statement>]
Translation: replace [a] by a new variable A with productions A -> a | .;33
3,&Example: Optional Elements33Grammar for if-then-else can be replaced by:
S -> iCtSA
A -> ;eS | (-C,2).BNF Notation Grouping~Use {& } to surround a sequence of symbols that need to be treated as a unit.
Typically, they are followed by a & for one or more.
Example: <statement list> ::= <statement> [{;<statement>}& ]BM7<M7335+Translation: Grouping3You may, if you wish, create a new variable A for {a}.
One production for A: A -> a.
Use A in place of {a}.Hl3,Example: Grouping33 S -> S [{;S}& ]
Replace by S -> S [A& ] A -> ;S
A stands for {;S}.
Then by S -> SB B -> A& | A -> ;S
B stands for [A& ] (zero or more A s).
Finally by S -> SB B -> C | C -> AC | A A -> ;S
C stands for A& .$)&I3$&!'b*;C
/"Leftmost and Rightmost DerivationsDerivations allow us to replace any of the variables in a string.
Leads to many different derivations of the same string.
By forcing the leftmost variable (or alternatively, the rightmost variable) to be replaced, we avoid these distinctions without a difference. 0Leftmost Derivations*Say wAa =>lm wba if w is a string of terminals only and A -> b is a production.
Also, a =>*lm b if a becomes b by a sequence of 0 or more =>lm steps.- 2Example: Leftmost Derivations33Balanced-parentheses grammmar:
S -> SS | (S) | ()
S =>lm SS =>lm (S)S =>lm (())S =>lm (())()
Thus, S =>*lm (())()
S => SS => S() => (S)() => (())() is a derivation, but not a leftmost derivation.!3 Z4Rightmost Derivations*Say aAw =>rm abw if w is a string of terminals only and A -> b is a production.
Also, a =>*rm b if a becomes b by a sequence of 0 or more =>rm steps.- >
O/7Example: Rightmost Derivations33Balanced-parentheses grammmar:
S -> SS | (S) | ()
S =>rm SS =>rm S() =>rm (S)() =>rm (())()
Thus, S =>*rm (())()
S => SS => SSS => S()S => ()()S => ()()() is neither a rightmost nor a leftmost derivation.!3 dt d/
$
%'(*-.13568:<> ` ̙33` ` ff3333f` 333MMM` f` f` 3>?" dd@,?udd@ w " @ ` n?" dd@ @@``PR @ ` `p>>f(
6m P
T Click to edit Master title style!
!
0xp
RClick to edit Master text styles
Second level
Third level
Fourth level
Fifth level!
S
0Tu ``
>*
0z `
@*
0 `
@*Z
Byh@ ? ̙33 Default Design
0zrP
(
0G P
P*
0G G
R*
d
c$ ?
G
0 G
@G
RClick to edit Master text styles
Second level
Third level
Fourth level
Fifth level!
S
6G `P G
P*
6G ` G
R*
H
0h ? ̙330$(
r
S
r
S`
H
0h ? ̙33
`$$(
$r
$ S2GP
G
r
$ S\3GG
H
$0h ? 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-$($(6-$6D-$DR"$"R`0-$0`n>$>n|L-$L|Z$Zh$hv-$v$$$&-
--&&&y&/?w`Bw
@A&?w`Bw
f- @Times New Roman\?w`Bw
f- . 2
f1 .--IyH-- @"Tahomaw
@A&\?w`Bw
f- .2
#Context# !. . 2
#r-. .2
#
Free Grammars'22.--11x-- @"Tahomaw
A&\?w`Bw
f- .2
Formalism$
$. .2
wDerivationsr
. .2
"2Backus. . 2
"-. .2
"Naur
. .
2
"-Form$. .
2
_Left. . 2
_-. .-2
_and Rightmost Derivations
$
.--"System
f
G-&TNPP &Root EntrydO)OK<Current UserPSummaryInformation(e$2PowerPoint Document(h
!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdfghijklmnopqrstuvwxyz{|}~
ion: GroupingExample: Grouping#Leftmost and Rightmost DerivationsLeftmost DerivationsExample: Leftmost DerivationsRightmost DerivationsExample: Rightmost DerivationsFonts UsedDesign Template
Slide Titles(_D'Michael GoodrichMichael Goodrich_JeffJeff
!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdfghijklmnopqrstuvwxyz{|}~Root EntrydO)rCurrent User,SummaryInformation(e$2PowerPoint Document(hDocumentSummaryInformation8$.
@n?" dd@ @@``xp?:
!"#$&%'()*+,-./0123456789:
c$pf3f33f3@g4BdBd08Xppp@uʚ;2Nʚ;<4!d!ddЁ
0P<4dddddЁ
0P<4BdBddЁ
0P:2___PPT9/0?O=<Context-Free GrammarsFFormalism
Derivations
Backus-Naur Form
Left- and Rightmost Derivations%Informal CommentsA context-free grammar is a notation for describing languages.
It is more powerful than finite automata or RE s, but still cannot define all possible languages.
Useful for nested structures, e.g., parentheses in programming languages.(f
.Informal Comments (2)Basic idea is to use variables to stand for sets of strings (i.e., languages).
These variables are defined recursively, in terms of one another.
Recursive rules ( productions ) involve only concatenation.
Alternative rules for a variable allow union.;!Example: CFG for { 0n1n | n > 1} T"33
oProductions:
S -> 01
S -> 0S1
Basis: 01 is in the language.
Induction: if w is in the language, then so is 0w1.X
R
3f 3f+
CFG FormalismTerminals = symbols of the alphabet of the language being defined.
Variables = nonterminals = a finite set of other symbols, each of which represents a language.
Start symbol = the variable whose language is the one being defined.b f; ffHf:Productions.A production has the form variable -> string of variables and terminals.
Convention:
A, B, C,& are variables.
a, b, c,& are terminals.
& , X, Y, Z are either terminals or variables.
& , w, x, y, z are strings of terminals only.
a, b, g,& are strings of terminals and/or variables.V
f-3
3f.Example: Formal CFG33
Here is a formal CFG for { 0n1n | n > 1}.
Terminals = {0, 1}.
Variables = {S}.
Start symbol = S.
Productions =
S -> 01
S -> 0S1^oJ.Derivations IntuitionWe derive strings in the language of a CFG by starting with the start symbol, and repeatedly replacing some variable A by the right side of one of its productions.
That is, the productions for A are those that have A on the left side of the ->.<SfS.Derivations FormalismWe say aAb => agb if A -> g is a production.
Example: S -> 01; S -> 0S1.
S => 0S1 => 00S11 => 000111.nf 332Iterated Derivation=>* means zero or more derivation steps.
Basis: a =>* a for any string a.
Induction: if a =>* b and b => g, then a =>* g.|+3f 3f Example: Iterated Derivation33aS -> 01; S -> 0S1.
S => 0S1 => 00S11 => 000111.
So S =>* S; S =>* 0S1; S =>* 00S11; S =>* 000111.=Sentential Forms.Any string of variables and/or terminals derived from the start symbol is called a sentential form.
Formally, a is a sentential form iff S =>* a.LSf&
Language of a Grammar~If G is a CFG, then L(G), the language of G, is {w | S =>* w}.
Note: w must be a terminal string, S is the start symbol.
Example: G has productions S -> and S -> 0S1.
L(G) = {0n1n | n > 0}.?:G
f3f6339Context-Free LanguagesA language that is defined by some CFG is called a context-free language.
There are CFL s that are not regular languages, such as the example just given.
But not all languages are CFL s.
Intuitively: CFL s can count two things, not three.<3fs3f)>TZ!BNF NotationGrammars for programming languages are often written in BNF (Backus-Naur Form ).
Variables are words in <& >; Example: <statement>.
Terminals are often multicharacter strings indicated by boldface or underline; Example: while or WHILE.t=f 33^33,DNE$BNF Notation (2)pSymbol ::= is often used for ->.
Symbol | is used for or.
A shorthand for a list of productions with the same left side.
Example: S -> 0S1 | 01 is shorthand for S -> 0S1 and S -> 01.B<?><?337!
:BNF Notation Kleene ClosureSymbol & is used for one or more.
Example: <digit> ::= 0|1|2|3|4|5|6|7|8|9
<unsigned integer> ::= <digit>&
Note: that s not exactly the * of RE s.
Translation: Replace a& with a new variable A and productions A -> Aa | a.M!(K$33"!(3
."Example: Kleene Closure33 ZGrammar for unsigned integers can be replaced by:
U -> UD | D
D -> 0|1|2|3|4|5|6|7|8|92)[#BNF Notation: Optional ElementsSurround one or more symbols by [& ] to make them optional.
Example: <statement> ::= if <condition> then <statement> [; else <statement>]
Translation: replace [a] by a new variable A with productions A -> a | .;33
3,&Example: Optional Elements33Grammar for if-then-else can be replaced by:
S -> iCtSA
A -> ;eS | (-C,2).BNF Notation Grouping~Use {& } to surround a sequence of symbols that need to be treated as a unit.
Typically, they are followed by a & for one or more.
Example: <statement list> ::= <statement> [{;<statement>}& ]BM7<M7335+Translation: Grouping3You may, if you wish, create a new variable A for {a}.
One production for A: A -> a.
Use A in place of {a}.Hl3,Example: Grouping33 L -> S [{;S}& ]
Replace by L -> S [A& ] A -> ;S
A stands for {;S}.
Then by L -> SB B -> A& | A -> ;S
B stands for [A& ] (zero or more A s).
Finally by L -> SB B -> C | C -> AC | A A -> ;S
C stands for A& .$)&I3$&!'b*;C
/"Leftmost and Rightmost DerivationsDerivations allow us to replace any of the variables in a string.
Leads to many different derivations of the same string.
By forcing the leftmost variable (or alternatively, the rightmost variable) to be replaced, we avoid these distinctions without a difference. 0Leftmost Derivations*Say wAa =>lm wba if w is a string of terminals only and A -> b is a production.
Also, a =>*lm b if a becomes b by a sequence of 0 or more =>lm steps.- 2Example: Leftmost Derivations33Balanced-parentheses grammmar:
S -> SS | (S) | ()
S =>lm SS =>lm (S)S =>lm (())S =>lm (())()
Thus, S =>*lm (())()
S => SS => S() => (S)() => (())() is a derivation, but not a leftmost derivation.!3 Z4Rightmost Derivations*Say aAw =>rm abw if w is a string of terminals only and A -> b is a production.
Also, a =>*rm b if a becomes b by a sequence of 0 or more =>rm steps.- >
O/7Example: Rightmost Derivations33Balanced-parentheses grammmar:
S -> SS | (S) | ()
S =>rm SS =>rm S() =>rm (S)() =>rm (())()
Thus, S =>*rm (())()
S => SS => SSS => S()S => ()()S => ()()() is neither a rightmost nor a leftmost derivation.!3 dt d/
$
%'(*-.13568:<>
P(
r
SYP
Y
StY0<$
0Y
H
0h ? ̙33r,,>
՜.+,0
(On-screen ShowStanford University, CS Dept.Dh "Times New RomanTahomaMonotype SortsSymbolLucida Sans UnicodeDefault DesignContext-Free GrammarsInformal CommentsInformal Comments (2)"Example: CFG for { 0n1n | n > 1} CFG FormalismProductionsExample: Formal CFGDerivations IntuitionDerivations FormalismIterated DerivationExample: Iterated DerivationSentential FormsLanguage of a GrammarContext-Free Languages
BNF NotationBNF Notation (2)BNF Notation Kleene ClosureExample: Kleene Closure BNF Notation: Optional ElementsExample: Optional ElementsBNF Notation GroupingTranslat