SlideShare a Scribd company logo
1 of 29
Context-Free Grammars

           Formalism
          Derivations
       Backus-Naur Form
Left- and Rightmost Derivations
                          1
BEGUM ROKEYA UNIVERSITY
         ,RANGPUR

MD.JAHURUL ISLAM
ID:00805034
Email:jahurulbrur@gmail.com




                              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 { 0n1n | 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 { 0n1n | 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) = {0n1n | 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 grammar:
    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 grammar:
    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.
                                   29

More Related Content

What's hot

2. context free langauages
2. context free langauages2. context free langauages
2. context free langauages
danhumble
 

What's hot (20)

language , grammar and automata
language , grammar and automatalanguage , grammar and automata
language , grammar and automata
 
2. context free langauages
2. context free langauages2. context free langauages
2. context free langauages
 
Lecture: Automata
Lecture: AutomataLecture: Automata
Lecture: Automata
 
Theory of Automata
Theory of AutomataTheory of Automata
Theory of Automata
 
Context free grammar
Context free grammar Context free grammar
Context free grammar
 
Context Free Grammar
Context Free GrammarContext Free Grammar
Context Free Grammar
 
Lecture 1,2
Lecture 1,2Lecture 1,2
Lecture 1,2
 
Formal language
Formal languageFormal language
Formal language
 
Deterministic Finite Automata (DFA)
Deterministic Finite Automata (DFA)Deterministic Finite Automata (DFA)
Deterministic Finite Automata (DFA)
 
L3 cfg
L3 cfgL3 cfg
L3 cfg
 
CONTEXT FREE GRAMMAR
CONTEXT FREE GRAMMAR CONTEXT FREE GRAMMAR
CONTEXT FREE GRAMMAR
 
Ch3 4 regular expression and grammar
Ch3 4 regular expression and grammarCh3 4 regular expression and grammar
Ch3 4 regular expression and grammar
 
Context free grammar
Context free grammarContext free grammar
Context free grammar
 
Lecture 5
Lecture 5Lecture 5
Lecture 5
 
Chomsky & Greibach Normal Forms
Chomsky & Greibach Normal FormsChomsky & Greibach Normal Forms
Chomsky & Greibach Normal Forms
 
Chomsky Normal Form
Chomsky Normal FormChomsky Normal Form
Chomsky Normal Form
 
Strings Functions in C Programming
Strings Functions in C ProgrammingStrings Functions in C Programming
Strings Functions in C Programming
 
Lecture 8
Lecture 8Lecture 8
Lecture 8
 
NFA & DFA
NFA & DFANFA & DFA
NFA & DFA
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 

Viewers also liked

Class7
 Class7 Class7
Class7
issbp
 
Deterministic context free grammars &non-deterministic
Deterministic context free grammars &non-deterministicDeterministic context free grammars &non-deterministic
Deterministic context free grammars &non-deterministic
Leyo Stephen
 

Viewers also liked (20)

Context free langauges
Context free langaugesContext free langauges
Context free langauges
 
Properties of cfg
Properties of cfgProperties of cfg
Properties of cfg
 
Class7
 Class7 Class7
Class7
 
Normal forms cfg
Normal forms   cfgNormal forms   cfg
Normal forms cfg
 
CNF & Leftmost Derivation - Theory of Computation
CNF & Leftmost Derivation - Theory of ComputationCNF & Leftmost Derivation - Theory of Computation
CNF & Leftmost Derivation - Theory of Computation
 
Deterministic context free grammars &non-deterministic
Deterministic context free grammars &non-deterministicDeterministic context free grammars &non-deterministic
Deterministic context free grammars &non-deterministic
 
Grammar
GrammarGrammar
Grammar
 
Top Down Parsing, Predictive Parsing
Top Down Parsing, Predictive ParsingTop Down Parsing, Predictive Parsing
Top Down Parsing, Predictive Parsing
 
Theory of Automata and formal languages Unit 3
Theory of Automata and formal languages Unit 3Theory of Automata and formal languages Unit 3
Theory of Automata and formal languages Unit 3
 
Theory of Automata and formal languages unit 2
Theory of Automata and formal languages unit 2Theory of Automata and formal languages unit 2
Theory of Automata and formal languages unit 2
 
Turing machines
Turing machinesTuring machines
Turing machines
 
NFA or Non deterministic finite automata
NFA or Non deterministic finite automataNFA or Non deterministic finite automata
NFA or Non deterministic finite automata
 
Lecture: Regular Expressions and Regular Languages
Lecture: Regular Expressions and Regular LanguagesLecture: Regular Expressions and Regular Languages
Lecture: Regular Expressions and Regular Languages
 
Lexical analyzer
Lexical analyzerLexical analyzer
Lexical analyzer
 
Grammar
GrammarGrammar
Grammar
 
Turing machine by_deep
Turing machine by_deepTuring machine by_deep
Turing machine by_deep
 
Reintroducing Web Technology
Reintroducing Web TechnologyReintroducing Web Technology
Reintroducing Web Technology
 
push down automata
push down automatapush down automata
push down automata
 
Turing Machine
Turing MachineTuring Machine
Turing Machine
 
Pushdown automata
Pushdown automataPushdown automata
Pushdown automata
 

Similar to Context free languages

L11 context-free grammers 2.pptx parse tree
L11 context-free grammers 2.pptx parse treeL11 context-free grammers 2.pptx parse tree
L11 context-free grammers 2.pptx parse tree
sunitachalageri1
 
ContextFreeGrammars.pptx
ContextFreeGrammars.pptxContextFreeGrammars.pptx
ContextFreeGrammars.pptx
PEzhumalai
 
contextfreegrammars-120925004035-phpapp02.pdf
contextfreegrammars-120925004035-phpapp02.pdfcontextfreegrammars-120925004035-phpapp02.pdf
contextfreegrammars-120925004035-phpapp02.pdf
ry54321288
 

Similar to Context free languages (20)

Context-Free Grammars.pdf
Context-Free Grammars.pdfContext-Free Grammars.pdf
Context-Free Grammars.pdf
 
Context free grammer.ppt
Context free grammer.pptContext free grammer.ppt
Context free grammer.ppt
 
Conteext-free Grammer
Conteext-free GrammerConteext-free Grammer
Conteext-free Grammer
 
L11 context-free grammers 2.pptx parse tree
L11 context-free grammers 2.pptx parse treeL11 context-free grammers 2.pptx parse tree
L11 context-free grammers 2.pptx parse tree
 
ContextFreeGrammars.pptx
ContextFreeGrammars.pptxContextFreeGrammars.pptx
ContextFreeGrammars.pptx
 
ContextFreeGrammars (1).pptx
ContextFreeGrammars (1).pptxContextFreeGrammars (1).pptx
ContextFreeGrammars (1).pptx
 
Context free grammar
Context free grammarContext free grammar
Context free grammar
 
Chapter 3 REGULAR EXPRESSION.pdf
Chapter 3 REGULAR EXPRESSION.pdfChapter 3 REGULAR EXPRESSION.pdf
Chapter 3 REGULAR EXPRESSION.pdf
 
QB104541.pdf
QB104541.pdfQB104541.pdf
QB104541.pdf
 
Automata
AutomataAutomata
Automata
 
Automata
AutomataAutomata
Automata
 
Ch02
Ch02Ch02
Ch02
 
Mod 2_RegularExpressions.pptx
Mod 2_RegularExpressions.pptxMod 2_RegularExpressions.pptx
Mod 2_RegularExpressions.pptx
 
Unit iii
Unit iiiUnit iii
Unit iii
 
Context Free Grammar
Context Free GrammarContext Free Grammar
Context Free Grammar
 
cfl2.pdf
cfl2.pdfcfl2.pdf
cfl2.pdf
 
Automata definitions
Automata definitionsAutomata definitions
Automata definitions
 
Regular expression with DFA
Regular expression with DFARegular expression with DFA
Regular expression with DFA
 
Bound
BoundBound
Bound
 
contextfreegrammars-120925004035-phpapp02.pdf
contextfreegrammars-120925004035-phpapp02.pdfcontextfreegrammars-120925004035-phpapp02.pdf
contextfreegrammars-120925004035-phpapp02.pdf
 

Recently uploaded

Top profile Call Girls In Meerut [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Meerut [ 7014168258 ] Call Me For Genuine Models We...Top profile Call Girls In Meerut [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Meerut [ 7014168258 ] Call Me For Genuine Models We...
gajnagarg
 
Top profile Call Girls In eluru [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In eluru [ 7014168258 ] Call Me For Genuine Models We ...Top profile Call Girls In eluru [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In eluru [ 7014168258 ] Call Me For Genuine Models We ...
gajnagarg
 
Jual Obat Aborsi Bandung ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan ...
Jual Obat Aborsi Bandung ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan ...Jual Obat Aborsi Bandung ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan ...
Jual Obat Aborsi Bandung ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan ...
ZurliaSoop
 
Call Girls In Ratnagiri Escorts ☎️8617370543 🔝 💃 Enjoy 24/7 Escort Service En...
Call Girls In Ratnagiri Escorts ☎️8617370543 🔝 💃 Enjoy 24/7 Escort Service En...Call Girls In Ratnagiri Escorts ☎️8617370543 🔝 💃 Enjoy 24/7 Escort Service En...
Call Girls In Ratnagiri Escorts ☎️8617370543 🔝 💃 Enjoy 24/7 Escort Service En...
Nitya salvi
 
Abortion pill for sale in Muscat (+918761049707)) Get Cytotec Cash on deliver...
Abortion pill for sale in Muscat (+918761049707)) Get Cytotec Cash on deliver...Abortion pill for sale in Muscat (+918761049707)) Get Cytotec Cash on deliver...
Abortion pill for sale in Muscat (+918761049707)) Get Cytotec Cash on deliver...
instagramfab782445
 
一比一定(购)西悉尼大学毕业证(WSU毕业证)成绩单学位证
一比一定(购)西悉尼大学毕业证(WSU毕业证)成绩单学位证一比一定(购)西悉尼大学毕业证(WSU毕业证)成绩单学位证
一比一定(购)西悉尼大学毕业证(WSU毕业证)成绩单学位证
eqaqen
 
Q4-Trends-Networks-Module-3.pdfqquater days sheets123456789
Q4-Trends-Networks-Module-3.pdfqquater days sheets123456789Q4-Trends-Networks-Module-3.pdfqquater days sheets123456789
Q4-Trends-Networks-Module-3.pdfqquater days sheets123456789
CristineGraceAcuyan
 
怎样办理巴斯大学毕业证(Bath毕业证书)成绩单留信认证
怎样办理巴斯大学毕业证(Bath毕业证书)成绩单留信认证怎样办理巴斯大学毕业证(Bath毕业证书)成绩单留信认证
怎样办理巴斯大学毕业证(Bath毕业证书)成绩单留信认证
eeanqy
 
Minimalist Orange Portfolio by Slidesgo.pptx
Minimalist Orange Portfolio by Slidesgo.pptxMinimalist Orange Portfolio by Slidesgo.pptx
Minimalist Orange Portfolio by Slidesgo.pptx
balqisyamutia
 
ab-initio-training basics and architecture
ab-initio-training basics and architectureab-initio-training basics and architecture
ab-initio-training basics and architecture
saipriyacoool
 
一比一定(购)卡尔顿大学毕业证(CU毕业证)成绩单学位证
一比一定(购)卡尔顿大学毕业证(CU毕业证)成绩单学位证一比一定(购)卡尔顿大学毕业证(CU毕业证)成绩单学位证
一比一定(购)卡尔顿大学毕业证(CU毕业证)成绩单学位证
wpkuukw
 
Resume all my skills and educations and achievement
Resume all my skills and educations and  achievement Resume all my skills and educations and  achievement
Resume all my skills and educations and achievement
210303105569
 
怎样办理伯明翰大学学院毕业证(Birmingham毕业证书)成绩单留信认证
怎样办理伯明翰大学学院毕业证(Birmingham毕业证书)成绩单留信认证怎样办理伯明翰大学学院毕业证(Birmingham毕业证书)成绩单留信认证
怎样办理伯明翰大学学院毕业证(Birmingham毕业证书)成绩单留信认证
eeanqy
 

Recently uploaded (20)

Top profile Call Girls In Meerut [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Meerut [ 7014168258 ] Call Me For Genuine Models We...Top profile Call Girls In Meerut [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Meerut [ 7014168258 ] Call Me For Genuine Models We...
 
Top profile Call Girls In eluru [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In eluru [ 7014168258 ] Call Me For Genuine Models We ...Top profile Call Girls In eluru [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In eluru [ 7014168258 ] Call Me For Genuine Models We ...
 
Jual Obat Aborsi Bandung ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan ...
Jual Obat Aborsi Bandung ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan ...Jual Obat Aborsi Bandung ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan ...
Jual Obat Aborsi Bandung ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan ...
 
Call Girls In Ratnagiri Escorts ☎️8617370543 🔝 💃 Enjoy 24/7 Escort Service En...
Call Girls In Ratnagiri Escorts ☎️8617370543 🔝 💃 Enjoy 24/7 Escort Service En...Call Girls In Ratnagiri Escorts ☎️8617370543 🔝 💃 Enjoy 24/7 Escort Service En...
Call Girls In Ratnagiri Escorts ☎️8617370543 🔝 💃 Enjoy 24/7 Escort Service En...
 
Abortion pill for sale in Muscat (+918761049707)) Get Cytotec Cash on deliver...
Abortion pill for sale in Muscat (+918761049707)) Get Cytotec Cash on deliver...Abortion pill for sale in Muscat (+918761049707)) Get Cytotec Cash on deliver...
Abortion pill for sale in Muscat (+918761049707)) Get Cytotec Cash on deliver...
 
一比一定(购)西悉尼大学毕业证(WSU毕业证)成绩单学位证
一比一定(购)西悉尼大学毕业证(WSU毕业证)成绩单学位证一比一定(购)西悉尼大学毕业证(WSU毕业证)成绩单学位证
一比一定(购)西悉尼大学毕业证(WSU毕业证)成绩单学位证
 
Just Call Vip call girls Kasganj Escorts ☎️8617370543 Two shot with one girl ...
Just Call Vip call girls Kasganj Escorts ☎️8617370543 Two shot with one girl ...Just Call Vip call girls Kasganj Escorts ☎️8617370543 Two shot with one girl ...
Just Call Vip call girls Kasganj Escorts ☎️8617370543 Two shot with one girl ...
 
Call Girls Jalaun Just Call 8617370543 Top Class Call Girl Service Available
Call Girls Jalaun Just Call 8617370543 Top Class Call Girl Service AvailableCall Girls Jalaun Just Call 8617370543 Top Class Call Girl Service Available
Call Girls Jalaun Just Call 8617370543 Top Class Call Girl Service Available
 
Q4-Trends-Networks-Module-3.pdfqquater days sheets123456789
Q4-Trends-Networks-Module-3.pdfqquater days sheets123456789Q4-Trends-Networks-Module-3.pdfqquater days sheets123456789
Q4-Trends-Networks-Module-3.pdfqquater days sheets123456789
 
怎样办理巴斯大学毕业证(Bath毕业证书)成绩单留信认证
怎样办理巴斯大学毕业证(Bath毕业证书)成绩单留信认证怎样办理巴斯大学毕业证(Bath毕业证书)成绩单留信认证
怎样办理巴斯大学毕业证(Bath毕业证书)成绩单留信认证
 
Minimalist Orange Portfolio by Slidesgo.pptx
Minimalist Orange Portfolio by Slidesgo.pptxMinimalist Orange Portfolio by Slidesgo.pptx
Minimalist Orange Portfolio by Slidesgo.pptx
 
ab-initio-training basics and architecture
ab-initio-training basics and architectureab-initio-training basics and architecture
ab-initio-training basics and architecture
 
LANDSCAPE ARCHITECTURE PORTFOLIO - MAREK MITACEK
LANDSCAPE ARCHITECTURE PORTFOLIO - MAREK MITACEKLANDSCAPE ARCHITECTURE PORTFOLIO - MAREK MITACEK
LANDSCAPE ARCHITECTURE PORTFOLIO - MAREK MITACEK
 
一比一定(购)卡尔顿大学毕业证(CU毕业证)成绩单学位证
一比一定(购)卡尔顿大学毕业证(CU毕业证)成绩单学位证一比一定(购)卡尔顿大学毕业证(CU毕业证)成绩单学位证
一比一定(购)卡尔顿大学毕业证(CU毕业证)成绩单学位证
 
TRose UXPA Experience Design Concord .pptx
TRose UXPA Experience Design Concord .pptxTRose UXPA Experience Design Concord .pptx
TRose UXPA Experience Design Concord .pptx
 
Essential UI/UX Design Principles: A Comprehensive Guide
Essential UI/UX Design Principles: A Comprehensive GuideEssential UI/UX Design Principles: A Comprehensive Guide
Essential UI/UX Design Principles: A Comprehensive Guide
 
Resume all my skills and educations and achievement
Resume all my skills and educations and  achievement Resume all my skills and educations and  achievement
Resume all my skills and educations and achievement
 
怎样办理伯明翰大学学院毕业证(Birmingham毕业证书)成绩单留信认证
怎样办理伯明翰大学学院毕业证(Birmingham毕业证书)成绩单留信认证怎样办理伯明翰大学学院毕业证(Birmingham毕业证书)成绩单留信认证
怎样办理伯明翰大学学院毕业证(Birmingham毕业证书)成绩单留信认证
 
Abortion pills in Riyadh +966572737505 <> buy cytotec <> unwanted kit Saudi A...
Abortion pills in Riyadh +966572737505 <> buy cytotec <> unwanted kit Saudi A...Abortion pills in Riyadh +966572737505 <> buy cytotec <> unwanted kit Saudi A...
Abortion pills in Riyadh +966572737505 <> buy cytotec <> unwanted kit Saudi A...
 
Gamestore case study UI UX by Amgad Ibrahim
Gamestore case study UI UX by Amgad IbrahimGamestore case study UI UX by Amgad Ibrahim
Gamestore case study UI UX by Amgad Ibrahim
 

Context free languages

  • 1. Context-Free Grammars Formalism Derivations Backus-Naur Form Left- and Rightmost Derivations 1
  • 2. BEGUM ROKEYA UNIVERSITY ,RANGPUR MD.JAHURUL ISLAM ID:00805034 Email:jahurulbrur@gmail.com 2
  • 3. 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
  • 4. 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
  • 5. Example: CFG for { 0n1n | 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
  • 6. 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
  • 7. 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
  • 8. Example: Formal CFG Here is a formal CFG for { 0n1n | n > 1}. Terminals = {0, 1}. Variables = {S}. Start symbol = S. Productions = S -> 01 S -> 0S1 8
  • 9. 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
  • 10. Derivations – Formalism We say αAβ => αγβ if A -> γ is a production. Example: S -> 01; S -> 0S1. S => 0S1 => 00S11 => 000111. 10
  • 11. Iterated Derivation =>* means “zero or more derivation steps.” Basis: α =>* α for any string α. Induction: if α =>* β and β => γ, then α =>* γ. 11
  • 12. Example: Iterated Derivation S -> 01; S -> 0S1. S => 0S1 => 00S11 => 000111. So S =>* S; S =>* 0S1; S =>* 00S11; S =>* 000111. 12
  • 13. 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
  • 14. 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}. Note: ε is a legitimate right side. 14
  • 15. 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
  • 16. 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
  • 17. 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
  • 18. 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
  • 19. 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
  • 20. 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
  • 21. Example: Optional Elements Grammar for if-then-else can be replaced by: S -> iCtSA A -> ;eS | ε 21
  • 22. 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
  • 23. Translation: Grouping You may, if you wish, create a new variable A for {α}. One production for A: A -> α. Use A in place of {α}. 23
  • 24. 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
  • 25. 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
  • 26. 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
  • 27. Example: Leftmost Derivations Balanced-parentheses grammar: 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
  • 28. 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
  • 29. Example: Rightmost Derivations Balanced-parentheses grammar: 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. 29