Chapter 04 - Context Free Language

Embed Size (px)

Citation preview

  • 8/9/2019 Chapter 04 - Context Free Language

    1/21

    Chapter – 04

    Context Free Language andGrammar

    Context Free Languages – Parsing and ambiguity –Sentential forms – leftmost and rightmost derivation– Derivation tree or Parse tree – Denition andrelationship between parse tree and derivation -

    Simpliation of ontext free grammar

    !

  • 8/9/2019 Chapter 04 - Context Free Language

    2/21

    Context Free Language(CFL)

    "

    #o overome the problem in regular language$ %eneeded the solution is alled ontext free language$

    Context free language is generated by the ontextfree grammar$ &egular grammar is simple butontext grammar is omplex ompared to theregular grammar$

    From the diagram' regular language is a Sub set ofthe CFL$ CFL is omplex one and it an solve theproblems( faed in regular language

  • 8/9/2019 Chapter 04 - Context Free Language

    3/21

    Context Free Grammar

    )

    Denition*

    + , .( #( P( S/

    . – Set of non terminals# – set of terminals

    P – Prodution rules* this rules are used to derive theertain string

    S – starting state

  • 8/9/2019 Chapter 04 - Context Free Language

    4/21

    Cont.,

    0

    Derivation and Languages:

    +eneration of language using spei rules is alled derivation$

    Exampe 4.!: Constru"t the CFG #or the anguage having an$num%er o# a&s over the set ' a *.

    &$1 , a2 , 3 ( a( aa( aaa( aaaa( 4$5

    Prodution rules for the same*

    S  aS rule !S   rule "

    .ow if you want to derive the string 6aaa7$ Start with the startsymbol$

    +

    a+ rule!

    aa+ rule!

    aaa+ rule!

    aaa rule"

    aaa

  • 8/9/2019 Chapter 04 - Context Free Language

    5/21

    Cont.,

    8

    Exampe 4.: Constru"t the CFG #or the givenanguage

    L an

     %n

    - n 0 *L , 3 ab( aabb( aaabbb( 4$5 , 3 e9ual number of a andb5

    Prodution rule for the same*

    S  aSb rule!

    S   rule"

    .ow if you want to derive the string 6aabb7$ :ou anstart the derivation from the start symbol

    +

    a+b rule!

    aa+bb rule!

    aabb rule!

    aabb rule"

  • 8/9/2019 Chapter 04 - Context Free Language

    6/21

    Le#tmost Derivation and /ightmost Derivation

    ;

    +eneration of the string using prodution rulesis alled derivation$

  • 8/9/2019 Chapter 04 - Context Free Language

    7/21

    Derivation ree

    =

    Derivation tree is a graphial representation for thederivation of the given prodution rules for the given

    CF+$

  • 8/9/2019 Chapter 04 - Context Free Language

    8/21

    Cont.,

    >

    Exampe 4.1: Let G %e the grammar

    + a2 - %3

    3 a - a+ - %332 % - %+ - a22

    For the string %aaa%%a%%a nd e#tmost, rightmostderivation and Derivation tree

    First write the prodution rules separately li?e below

    S  a@ rule!

    S  bA rule"

    A  a rule)

    A  aS rule0

    A  bAA rule8

    @  b rule;

    @  bS rule=

    @  a@@ rule>

  • 8/9/2019 Chapter 04 - Context Free Language

    9/21

    Cont.,

    B

    Le#tmost Derivation /ightmostDerivation

    + +b3 r! b3 r"

    ba+ r0 ba+ r0

    baa2 r! baa2 r!

    baaa2@ r> baaa@2 r>

    baaab+@ r= baaa@b+ r=

    baaabb3@ r" baaa@bb3 r"

    baaabba2 r) baaa2bba r)baaabbab+ r; baaab+bba r=

    baaabbabb3 r! baaabb3bba r"

    baaabbabba r) baaabbabba r)

  • 8/9/2019 Chapter 04 - Context Free Language

    10/21

    Cont.,

    !

  • 8/9/2019 Chapter 04 - Context Free Language

    11/21

    5arsing

    !!

    Parsing is the proess of analying a string ofsymbols( either in natural or omputer language$

  • 8/9/2019 Chapter 04 - Context Free Language

    12/21

    3m%iguit$

    !"

    +rammar an be derived in either leftmost orrightmost derivation$ %e an draw the derivationtree alled as parse tree or syntax tree$ Parse tree isa uni9ue one though the derivation is leftmost orrightmost$

  • 8/9/2019 Chapter 04 - Context Free Language

    13/21

    Cont.,

    !)

    Exampe 4.4: he given CFG is G (6, , 5, +)7here

    6 E * 5 E E 8 E id * E E 9 E

    + E * E id * s the given grammaram%iguous;

    .ow if the string is id 9 id 8 id then we an draw theparse tree

    So the above grammar is ambiguous grammar

  • 8/9/2019 Chapter 04 - Context Free Language

    14/21

    +impi"ation o# CFG

    !0

    All +rammars are not always optimied$ #hatmeans grammars are ontaining some unneessarysymbolsnon-terminals/ and this will inrease thelength of the grammar$ Simpliation of grammar

    means redution of grammar by removing uselesssymbols$

    i$&emoval of useless symbols

    ii$1limination of prodution

    iii$&emoval of unit prodution

  • 8/9/2019 Chapter 04 - Context Free Language

    15/21

    Cont.,

    !8

    /emova o# useess s$m%os:

    remove the useless non-terminals from the produtionrules$

  • 8/9/2019 Chapter 04 - Context Free Language

    16/21

    Cont.,

    !;

    P, 3 S  !!A E !!

      A   5

    Exampe 4.=: +impi#$ the given CFG

    + 32 - C3

    2 2C - 32

    3 a

    C a2 - % 

  • 8/9/2019 Chapter 04 - Context Free Language

    17/21

    Cont.,

    !=

    Eimination o# produ"tion rue:

    &emove the symbol from the grammar withoutaffeting the meaning of the grammar$ #hus is notneessary for the grammar$

    Exampe 4.>: /emove the produ"tion #rom the#oo7ing CFG

    + ?@A? 0? -

    @ !@ -

     #o remove the prodution( delete the prodution rule and

    to preserve the meaning of the CF+( plaing the value in the

    other prodution instead of the orresponding non-terminal$

    +tep!: remove the produ"tion

    &emove the rules   and :    

  • 8/9/2019 Chapter 04 - Context Free Language

    18/21

    Cont.,

    !>

    +tep: o preserve the meaning o# grammar:

    Plae the   to rule !* S  :    :

    S   :

       : S   :    :

    Plae the :   to rule !* S      

    Plae the   and :     to rule !* S        

    Plae the   to rule "*    

    Plae the :   to rule 0*  :  !  !

    Colletively an write the CF+*

    + ?@ - @? - ?? - ? -@ 

    ? 0

    @ !

  • 8/9/2019 Chapter 04 - Context Free Language

    19/21

    Cont.,

    !B

    /emoving the Bnit produ"tion:

    Gnit produtions are the produtions in whih one non-terminal gives another non-terminal only without terminal$

     : and :  H

     #hen you an minimie the rule li?e  H

    Exampe 4.: /emove the unit produ"tions #rom given CFG

    + 03 - !2 - C

    3 0+ - 002 ! - 3

    C 0!

    $S  A E !@ E 0!

    A  S E

    @  ! E A

    C  !

  • 8/9/2019 Chapter 04 - Context Free Language

    20/21

    Cont.,

    "

    Exampe 4.: ptimie the given CFG %$ redu"ingthe grammar

    + 3 - 0C!3 2 - 0! - !0

    C - CD

    2 and D are useess s$m%os %e"ause 2 and D is

    not avaia%e+ 3 is a unit produ"tion

    C   - prodution

    S  ! E !

    A  ! E !

    C  

  • 8/9/2019 Chapter 04 - Context Free Language

    21/21

    "!

    End o# Chapter 04