CS25 Lecture Presentation 5

download CS25 Lecture Presentation 5

of 12

Transcript of CS25 Lecture Presentation 5

  • 8/20/2019 CS25 Lecture Presentation 5

    1/26

    1

    CS 25Automata Theory & Formal Languages

     Lecture 5 

  • 8/20/2019 CS25 Lecture Presentation 5

    2/26

     

    CONTEXT-FREE GRAMMARS(CFGs)

     ― a notation for describing languages

     ― !ore "o#erful t$an finite auto!ata or RE%s& but still cannot defineall "ossible languages

     ― can describe features #it$ recursi'e structure

     ―

    First used in t$e stud of $u!an language ― !"ortant a""lication in s"ecification and co!"ilation of

    "rogra!!ing languages

  • 8/20/2019 CS25 Lecture Presentation 5

    3/26

     

    CONTEXT-FREE GRAMMARS(CFGs)

    E*a!"les+ CFG for , n.n / n 0 .1G1:  

    S -> 01

      S -> 0S1

     ― Consists of 2RO34CTONS (collection of substitution rules)

     ― Rules $as 5ARA67E and STRNG ('ariables and ot$er s!bolscalled ter!inals) se"arated b arro#s

  • 8/20/2019 CS25 Lecture Presentation 5

    4/26

     

    FORMA7 3EFNTON OFCONTEXT-FREE GRAMMAR A conte*t-free gra!!ar is a 8-tu"le (V,

    Σ

    , R, S)& #$ere

    1. V is a finite set called VARIABLES

      Σ is a finite set& dis9oint fro! 5& called TERMINALS

    3. R is a finite set of RULES/PRODUCTIONS

    4. S is t$e START SYMBOL

  • 8/20/2019 CS25 Lecture Presentation 5

    5/26

    5

    CFGs+ 2roductions

    ●  A production  $as t$e for! 'ariable -0 string of 'ariables and

    ter!inals

    Con'ention+ –  A& 6& C&: are 'ariables

     – a& b& c&: are ter!inals – α, β, γ ,…… are strings of ter!inals and;or 'ariables

     – For convenience, we abbreviate rules with the same left-hand variable into a single line

    ● E*+ A→ A. / 6

  • 8/20/2019 CS25 Lecture Presentation 5

    6/26

    6

    FORMA7 3EFNTON OF

    CONTEXT-FREE GRAMMARExample !F" G for # 0n1n $ n > 1%&

     

    G= (V, Σ, R, S)& #$ere

    1. V < ,S1

      Σ < ,& .1

    3. R  < ,S -0 .&

    S -0 S.14. S is t$e start s!bol

  • 8/20/2019 CS25 Lecture Presentation 5

    7/26

    7

    3ER5ATON fro! CFG=s

    ● 3ER5ATON is t$e se>uence of substitutions to obtain a string

    ● ?e derive  strings in t$e language of a CFG b starting #it$ t$e

    start s!bol& and re"eatedl re"lacing so!e 'ariable A b t$e

    rig$t side of one of its "roductions – T$at is& t$e @"roductions for A are t$ose t$at $a'e A on t$e left

    side of t$e -0

    ●  All strings generated b t$e gra!!ar constitute t$e

    7ANG4AGE OF TBE GRAMMAR –  7(G.) -language of gra!!ar G.

  • 8/20/2019 CS25 Lecture Presentation 5

    8/26

    8

    3ER5ATON fro! CFG=s

    ● For!all& #e sa α Aβ 

  • 8/20/2019 CS25 Lecture Presentation 5

    9/26

    9

    3ER5ATON fro! CFG=s

    7EFTMOST 3ER5ATON at e'er ste"& t$e left!ost

    re!aining 'ariable is t$e one re"laced

    RGBTMOST 3ER5ATON - at e'er ste"& t$e rig$t!ost

    re!aining 'ariable is t$e one re"laced

  • 8/20/2019 CS25 Lecture Presentation 5

    10/26

    10

    3ER5ATON fro! CFG=sG < (5& Σ& R& HEX2R0) #$ere

      5 is ,HEX2R0& HTERM0& HFACTOR01&

      Σ S ,a& I& X& (& )1&

      R is HEX2R0 → HEX2R0 I HTERM0 / HTERM0

      HTERM0 → HTERM0 X HFACTOR0 / HFACTOR0

      HFACTOR0 → (HEX2R0) / a

    7eft!ost 3eri'ation of a a ! a +

    HEX2R0 ⇒ HEX2R0 I HTERM0

    ⇒HTERM0 I HTERM0

    ⇒HFACTOR0 I HTERM0

    ⇒a I HTERM0

    ⇒a I HTERM0 X HFACTOR0

    ⇒a I HFACTOR0 X HFACTOR0

    ⇒a I a X HFACTOR0

    ⇒a I a X a

  • 8/20/2019 CS25 Lecture Presentation 5

    11/26

    11

    3ER5ATON fro! CFG=sG < (5& Σ& R& HEX2R0) #$ere

      5 is ,HEX2R0& HTERM0& HFACTOR01&

      Σ S ,a& I& X& (& )1&

      R is HEX2R0 → HEX2R0 I HTERM0 / HTERM0

      HTERM0 → HTERM0 X HFACTOR0 / HFACTOR0

      HFACTOR0 → (HEX2R0) / a

    Rig$t!ost 3eri'ation of aIaXa +

    HEX2R0 ⇒ HEX2R0 I HTERM0

    ⇒HEX2R0 I HTERM0 X HFACTOR0

    ⇒HEX2R0 I HTERM0 X a

    ⇒HEX2R0 I HFACTOR0 X a

    ⇒HEX2R0 I a X a

    ⇒HTERM0 I a X a

    ⇒HFACTOR0 I a X a

    ⇒a I a X a

  • 8/20/2019 CS25 Lecture Presentation 5

    12/26

    12

    SENTENTA7 FORMS

    ● 'n( string of variables and)or terminals

    derived from the start s!bol is called a

    sentential form&● Formall(, α is a sentential form iff S *>+α&

  • 8/20/2019 CS25 Lecture Presentation 5

    13/26

    13

    7ANG4AGE OF A GRAMMAR

    ● f " is a !F", then ."/, the language of G,

    is #w $ S *>+ w%&

     – ote w must be a terminal string, S is the starts(mbol&

    ● Example " has productions S -> ε andS -> 0S1&

    ● ."/ * #0n1n $ n > 0%&Note: ε is a legitimateright side.

  • 8/20/2019 CS25 Lecture Presentation 5

    14/26

    14

    CONTEXT-FREE 7ANG4AGES

    ● ' language that is defined b( some !F" is

    called a context-free language&

    here are !F2s that are not regular languages,such as the example 3ust given&

    ● 4ut not all languages are !F2s&

    ● ntuitivel( !F2s can count two things, not three&

    ● 24SB3O?N A4TOMATA (23A) is a class of

    !ac$ines recogniDing conte*t-free languages

    (CF7s)

  • 8/20/2019 CS25 Lecture Presentation 5

    15/26

     

    EXERCSES+

    . 4sing t$e gra!!ar G defined in t$e "re'ious slide& s$o# t$e left!ost and rig$t!ostderi'ation #it$ t$eir corres"onding "arse trees for t$e string (a + a) X a

    Gi'en t$e follo#ing gra!!ar+

    HSENTENCE0→ HNO4N-2BRASE0 H5ER6-2BRASE0

    HNO4N-2BRASE0 → HCOM27X-NO4N0 / HCOM27X-NO4N0 H2RE2-2BRASE0

    H5ER6-2BRASE0→ HCOM27X-5ER60 / HCOM27X-5ER60 H2RE2-2BRASE0

    H2RE2-2BRASE0→ H2RE20 HCOM27X-NO4N0

    HCOM27X-NO4N0 → HARTC7E0 HNO4N0

    HCOM27X-5ER60→ H5ER60 / H5ER60 HNO4N-2BRASE0

    HARTC7E0→ a / t$e

    HNO4N0→ bo / girl / flo#er 

    H5ER60 → touc$es / liJes / sees

    H2RE20 → #it$

    a Gi'e t$e for!al definition

    b S$o# t$e left!ost deri'ation for+

    t"e #ir$ %ee% a &$o'er 

    a (o) 'it" a &$o'er $i*e% t"e #ir$ 

  • 8/20/2019 CS25 Lecture Presentation 5

    16/26

    16

    2ARSE TREES

     ―

    re"resents t$e (sntactic) structure of w ― an alternati'e re"resentation to deri'ations and

    recursi'e inferences

     ― t$ere can be se'eral "arse trees for t$e sa!e

    string ― ideall t$ere s$ould be onl one "arse tree (t$e

    @true structure) for eac$ string& ie t$e language

    s$ould be una!biguous

  • 8/20/2019 CS25 Lecture Presentation 5

    17/26

    17

     AM6G4TK

    So!eti!es& a gra!!ar generates a string in se'eral different#as (se'eral "arse trees and $ence& different !eanings) – E*+ HEX2R0 → HEX2R0 I HEX2R0 / HEX2R0 * HEX2R0 / (HEX2R0) / a

    ● 5ou should be able to prove that a a ! a is generated

    ambiguousl(&

    ' grammar generates a string ambiguousl(, if the string has twodifferent parse trees, 6 two different derivations&

    ● ' string is derived ambiguously in CFG if it has two or more

    different leftmost derivations&

    ● he grammar is ambiguous if it generates some string

    ambiguousl(&

    ● INHERENTLY AMBIGUOUS - languages that can onl( be

    generated b( ambiguous grammars&

    E*+ , i.

     9

    J / i

  • 8/20/2019 CS25 Lecture Presentation 5

    18/26

     

    EXERCSES+

    Gi'en t$e follo#ing CFG&

     E →  E E + E , E + -E. + id 

    a. S$o# t$at t$e gi'en gra!!ar is a!biguous b s$o#ing t$eleft!ost deri'ations and "arse trees for t$e string id I id id

    b) Construct an e>ui'alent una!biguous gra!!ar 

  • 8/20/2019 CS25 Lecture Presentation 5

    19/26

     

    3ESGNNG CONTEXT-FREEGRAMMARS

    ● Many CFGs are union of simpler CFGs. Construt CFGs

    for simpler CFGs an! t"en om#ine S → S1 $ S% $ & $ S'

     ― E*+ , n.n / n ≥  1 4 , .nn / n ≥  1

    ● f reular, onstrut *F+ first an! t"en onert to CFG

    . MaJe 'ariable Ri → aR 9 if δ(>i& a) < > 9

    Add Ri→ L if >i is t$e acce"t state

    MaJe R t$e start 'ariable #$ere > is t$e start state

    ● -o lin'e! su#strins. /se R→

    uRv.

     ― E*+ , n.n / n≥ 1

  • 8/20/2019 CS25 Lecture Presentation 5

    20/26

     

    3ESGNNG CONTEXT-FREEGRAMMARS

    ● Strins may ontain ertain strutures t"at appearreursiely as part of t"e ot"er struture.

     ― 2lace 'ariable s!bol generating t$e structure in t$e location ofrules corres"onding to #$ere t$at structure !a recursi'el a""ear 

     – E*a!"le+ G < (5& Σ& R& HEX2R0) #$ere

     – 5 is ,HEX2R0& HTERM0& HFACTOR01&

     – Σ S ,a& I& X& (& )1&

     – R is HEX2R0 → HEX2R0 I HTERM0 / HTERM0

     –   HTERM0 → HTERM0 X HFACTOR0 / HFACTOR0

     –   HFACTOR0 → (HEX2R0) / a

  • 8/20/2019 CS25 Lecture Presentation 5

    21/26

     

    CBOMSK NORMA7 FORM (CNF)

    ● Simplifie! form

    ● Form: A → BC

      A → a

      S → L

    #$ere A, B and C are an 'ariables e*ce"t t$at B and C !anot be start 'ariables a is an ter!inal S is t$e start 'ariable

  • 8/20/2019 CS25 Lecture Presentation 5

    22/26

     

    CBOMSK NORMA7 FORM (CNF)

    -"eorem:

     An conte*t-free language is generated b a conte*t-free gra!!ar in C$o!sJ nor!al for!

  • 8/20/2019 CS25 Lecture Presentation 5

    23/26

     

    CON5ERSON OF ANK GRAMMARTO CNF

    1. +!! ne start sym#ol. (S → S)

    %. 0liminate all rules of form +→ .● (f R → uA' and A → L& R → uA' / u'●  f R → uA'A# and A → L& R → uA'A# / u'A# / uA'# /

    u'#●  f R → A and A → L& do t$e sa!e for R)

    3. 0liminate all unit rules of form + → 2.● (f 6 → u and A → 6& A → u)

    4. Conert remainin rules into t"e proper form.● (Re"lace A →  u0u1 2u*   #$ere J ≥  and eac$ ui   is a

    'ariable or a ter!inal s!bol #it$ A → u0 A03 A0 →  u1  A1 3

     A1 →   u4 A4323 A*51  →   u*50u*  f J ≥  & re"lace ui  #it$ U i  

    and add U i  →  ui )

  • 8/20/2019 CS25 Lecture Presentation 5

    24/26

     

    CON5ERSON OF ANK GRAMMARTO CNF

    0ample:

    S → ASA / a6

     A→ 6 / S

    6 → b / L

  • 8/20/2019 CS25 Lecture Presentation 5

    25/26

     

    EXERCSES

    . Find CFGs t$at generate t$ese regular languages o'er t$e

    al"$abet ∑ < ,a& b1+

    a) T$e language defined b (aaa I b)

    b) All strings #it$ an e'en nu!ber of a%sc) All strings #it$ an odd nu!ber of a%s or an e'en nu!ber of

    b%s

    Gi'e a CFG t$at generates t$e non-regular language2A7N3ROME (strings t$at read t$e sa!e for#ard andbacJ#ard) o'er al"$abet ,a& b1

  • 8/20/2019 CS25 Lecture Presentation 5

    26/26

     

    EXERCSES

    Gi'en t$e follo#ing CFG&G + S → aM

      S → bS

      M → aF

      M → bS

      F → bF

      F → L

    ?$at is 7(G) P

    8 Find an e>ui'alent gra!!ar in CNF for t$e follo#ing CFGs+i S → SS / A

     A → SS / AS / a

    ii S → aA / b6

     A → aAA / bS / b

    6 → b66 / aS / a