SlideShare a Scribd company logo
1
Context-Free Grammars
Formalism
Derivations
Backus-Naur Form
Left- and Rightmost Derivations
2
Informal Comments
 A 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.
3
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.
4
Example: CFG for { 0n
1n
| n > 1}
 Productions:
S -> 01
S -> 0S1
 Basis: 01 is in the language.
 Induction: if w is in the language,
then so is 0w1.
5
CFG Formalism
 Terminals = 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.
6
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.
 , , ,… are strings of terminals and/or
variables.
7
Example: Formal CFG
 Here is a formal CFG for { 0n
1n
| n > 1}.
 Terminals = {0, 1}.
 Variables = {S}.
 Start symbol = S.
 Productions =
S -> 01
S -> 0S1
8
Derivations – Intuition
 We 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 ->.
9
Derivations – Formalism
 We say A =>  if A ->  is a
production.
 Example: S -> 01; S -> 0S1.
 S => 0S1 => 00S11 => 000111.
10
Iterated Derivation
 =>* means “zero or more derivation
steps.”
 Basis:  =>*  for any string .
 Induction: if  =>*  and  => , then
 =>* .
11
Example: Iterated Derivation
 S -> 01; S -> 0S1.
 S => 0S1 => 00S11 => 000111.
 So S =>* S; S =>* 0S1; S =>* 00S11; S
=>* 000111.
12
Sentential Forms
 Any string of variables and/or
terminals derived from the start
symbol is called a sentential form.
 Formally,  is a sentential form iff
S =>* .
13
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) = {0n
1n
| n > 0}. Note: ε is a legitimate
right side.
14
Context-Free Languages
 A 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.
15
BNF Notation
 Grammars 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.
16
BNF Notation – (2)
 Symbol ::= 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.
17
BNF Notation – Kleene
Closure
 Symbol … 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 … with a new
variable A and productions A -> A | .
18
Example: Kleene Closure
 Grammar for unsigned integers can
be replaced by:
U -> UD | D
D -> 0|1|2|3|4|5|6|7|8|9
19
BNF Notation: Optional Elements
 Surround one or more symbols by […]
to make them optional.
 Example: <statement> ::= if
<condition> then <statement> [; else
<statement>]
 Translation: replace [] by a new
variable A with productions A ->  | ε.
20
Example: Optional Elements
 Grammar for if-then-else can be
replaced by:
S -> iCtSA
A -> ;eS | ε
21
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>}…]
22
Translation: Grouping
 You may, if you wish, create a new
variable A for {}.
 One production for A: A -> .
 Use A in place of {}.
23
Example: Grouping
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… .
24
Leftmost and Rightmost
Derivations
 Derivations 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.”
25
Leftmost Derivations
 Say wA =>lm w if w is a string of
terminals only and A ->  is a
production.
 Also,  =>*lm  if  becomes  by a
sequence of 0 or more =>lm steps.
26
Example: Leftmost
Derivations
 Balanced-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.
27
Rightmost Derivations
 Say Aw =>rm w if w is a string of
terminals only and A ->  is a
production.
 Also,  =>*rm  if  becomes  by a
sequence of 0 or more =>rm steps.
28
Example: Rightmost Derivations
 Balanced-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.

More Related Content

PDF
Context-Free Grammars.pdf
PPT
context-freelanguages and properties lecture note.ppt
PPT
Context free languages
PPT
Context free languages
PPT
Unit-2 Context free grammer. ppt CFG CFL
PPT
Context free grammer.ppt
PPTX
Conteext-free Grammer
PPTX
L11 context-free grammers 2.pptx parse tree
Context-Free Grammars.pdf
context-freelanguages and properties lecture note.ppt
Context free languages
Context free languages
Unit-2 Context free grammer. ppt CFG CFL
Context free grammer.ppt
Conteext-free Grammer
L11 context-free grammers 2.pptx parse tree

Similar to Context-Free Languages under theory of automata.ppt (20)

PPTX
Automata definitions
PDF
cfl2.pdf
PPTX
AUTOMATA AUTOMATA AUTOMATA Automata6Chapter5.pptx
PPTX
ContextFreeGrammars (1).pptx
PPTX
ContextFreeGrammars.pptx
PPT
CFG-properties.ppt all concepts are available into this
PPTX
introductory Discrete structure workshop.pptx
PDF
QB104541.pdf
PDF
Automata
PDF
Automata
PPTX
Chapter3pptx__2021_12_23_22_52_54.pptx
PDF
Chapter2CDpdf__2021_11_26_09_19_08.pdf
PDF
Context Free Grammar
PDF
Chapter 3 REGULAR EXPRESSION.pdf
PPT
Context free grammar
PPT
Regular expression with DFA
PDF
Regular expression
PPT
2. context free langauages
PPTX
Mod 2_RegularExpressions.pptx
PPTX
Ch2 automata.pptx
Automata definitions
cfl2.pdf
AUTOMATA AUTOMATA AUTOMATA Automata6Chapter5.pptx
ContextFreeGrammars (1).pptx
ContextFreeGrammars.pptx
CFG-properties.ppt all concepts are available into this
introductory Discrete structure workshop.pptx
QB104541.pdf
Automata
Automata
Chapter3pptx__2021_12_23_22_52_54.pptx
Chapter2CDpdf__2021_11_26_09_19_08.pdf
Context Free Grammar
Chapter 3 REGULAR EXPRESSION.pdf
Context free grammar
Regular expression with DFA
Regular expression
2. context free langauages
Mod 2_RegularExpressions.pptx
Ch2 automata.pptx
Ad

Recently uploaded (20)

PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PDF
composite construction of structures.pdf
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PPTX
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
PPTX
bas. eng. economics group 4 presentation 1.pptx
PPT
Mechanical Engineering MATERIALS Selection
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
DOCX
573137875-Attendance-Management-System-original
PDF
R24 SURVEYING LAB MANUAL for civil enggi
PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PPTX
Sustainable Sites - Green Building Construction
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
Embodied AI: Ushering in the Next Era of Intelligent Systems
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
composite construction of structures.pdf
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
bas. eng. economics group 4 presentation 1.pptx
Mechanical Engineering MATERIALS Selection
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
573137875-Attendance-Management-System-original
R24 SURVEYING LAB MANUAL for civil enggi
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
Sustainable Sites - Green Building Construction
UNIT-1 - COAL BASED THERMAL POWER PLANTS
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
Model Code of Practice - Construction Work - 21102022 .pdf
Ad

Context-Free Languages under theory of automata.ppt

  • 2. 2 Informal Comments  A 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.
  • 3. 3 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.
  • 4. 4 Example: CFG for { 0n 1n | n > 1}  Productions: S -> 01 S -> 0S1  Basis: 01 is in the language.  Induction: if w is in the language, then so is 0w1.
  • 5. 5 CFG Formalism  Terminals = 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.
  • 6. 6 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.  , , ,… are strings of terminals and/or variables.
  • 7. 7 Example: Formal CFG  Here is a formal CFG for { 0n 1n | n > 1}.  Terminals = {0, 1}.  Variables = {S}.  Start symbol = S.  Productions = S -> 01 S -> 0S1
  • 8. 8 Derivations – Intuition  We 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 ->.
  • 9. 9 Derivations – Formalism  We say A =>  if A ->  is a production.  Example: S -> 01; S -> 0S1.  S => 0S1 => 00S11 => 000111.
  • 10. 10 Iterated Derivation  =>* means “zero or more derivation steps.”  Basis:  =>*  for any string .  Induction: if  =>*  and  => , then  =>* .
  • 11. 11 Example: Iterated Derivation  S -> 01; S -> 0S1.  S => 0S1 => 00S11 => 000111.  So S =>* S; S =>* 0S1; S =>* 00S11; S =>* 000111.
  • 12. 12 Sentential Forms  Any string of variables and/or terminals derived from the start symbol is called a sentential form.  Formally,  is a sentential form iff S =>* .
  • 13. 13 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) = {0n 1n | n > 0}. Note: ε is a legitimate right side.
  • 14. 14 Context-Free Languages  A 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.
  • 15. 15 BNF Notation  Grammars 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.
  • 16. 16 BNF Notation – (2)  Symbol ::= 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.
  • 17. 17 BNF Notation – Kleene Closure  Symbol … 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 … with a new variable A and productions A -> A | .
  • 18. 18 Example: Kleene Closure  Grammar for unsigned integers can be replaced by: U -> UD | D D -> 0|1|2|3|4|5|6|7|8|9
  • 19. 19 BNF Notation: Optional Elements  Surround one or more symbols by […] to make them optional.  Example: <statement> ::= if <condition> then <statement> [; else <statement>]  Translation: replace [] by a new variable A with productions A ->  | ε.
  • 20. 20 Example: Optional Elements  Grammar for if-then-else can be replaced by: S -> iCtSA A -> ;eS | ε
  • 21. 21 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>}…]
  • 22. 22 Translation: Grouping  You may, if you wish, create a new variable A for {}.  One production for A: A -> .  Use A in place of {}.
  • 23. 23 Example: Grouping 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… .
  • 24. 24 Leftmost and Rightmost Derivations  Derivations 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.”
  • 25. 25 Leftmost Derivations  Say wA =>lm w if w is a string of terminals only and A ->  is a production.  Also,  =>*lm  if  becomes  by a sequence of 0 or more =>lm steps.
  • 26. 26 Example: Leftmost Derivations  Balanced-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.
  • 27. 27 Rightmost Derivations  Say Aw =>rm w if w is a string of terminals only and A ->  is a production.  Also,  =>*rm  if  becomes  by a sequence of 0 or more =>rm steps.
  • 28. 28 Example: Rightmost Derivations  Balanced-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.