7193282 Greibach Normal Form

download 7193282 Greibach Normal Form

of 16

Transcript of 7193282 Greibach Normal Form

  • 8/8/2019 7193282 Greibach Normal Form

    1/16

    GREIBACH NORMALGREIBACH NORMAL

    FORMFORMPresented by:-Presented by:-

    NEHA JAINNEHA JAIN

    II-CSEII-CSE399/05399/05

  • 8/8/2019 7193282 Greibach Normal Form

    2/16

    GREIBACH NORMAL FORMGREIBACH NORMAL FORM

    IN CNF we put restrictions on the length ofIN CNF we put restrictions on the length ofthe right sides of a production.the right sides of a production.

    AABCBC

    AAaaWhere A,B,C are in V, and a is in T.Where A,B,C are in V, and a is in T.

    In GNF restrictions are placed on positionsIn GNF restrictions are placed on positionsin which terminals and variables canin which terminals and variables canappear.appear.

  • 8/8/2019 7193282 Greibach Normal Form

    3/16

    DEFINITIONDEFINITION

    A context-free grammar is said to be inA context-free grammar is said to be inGreibach Normal Form if all productionsGreibach Normal Form if all productionshave the formhave the form

    AAaxax

    Where aWhere a TT

    And xAnd xV*V*

  • 8/8/2019 7193282 Greibach Normal Form

    4/16

    THEOREMTHEOREM

    For every context-free grammar GFor every context-free grammar G withoutwithout L(G),L(G),

    There exists an equivalent grammarThere exists an equivalent grammarInIn

    Greibach Normal FormGreibach Normal Form

  • 8/8/2019 7193282 Greibach Normal Form

    5/16

    CONVERSION OF CFG TO GNFCONVERSION OF CFG TO GNF

    Starting with a grammar: G = ( V, T, P, S)Starting with a grammar: G = ( V, T, P, S) 1a) Eliminate useless variables that can not1a) Eliminate useless variables that can not

    become terminalsbecome terminals 1b) Eliminate useless variables that can not be1b) Eliminate useless variables that can not be

    reachedreached 2) Eliminate epsilon productions2) Eliminate epsilon productions 3) Eliminate unit productions3) Eliminate unit productions 4) Convert productions to Chomsky Normal4) Convert productions to Chomsky Normal

    FormForm 5) Convert productions to Greibach Normal5) Convert productions to Greibach NormalForm using algorithmForm using algorithm

  • 8/8/2019 7193282 Greibach Normal Form

    6/16

    STEP 1STEP 1

    Every CFG must be in CNF form, if notEvery CFG must be in CNF form, if notthen convert it into CNF.then convert it into CNF.

    Now rename all variables (V) by A1, A2,.Now rename all variables (V) by A1, A2,.

    An where A1 is the start symbol.An where A1 is the start symbol.

  • 8/8/2019 7193282 Greibach Normal Form

    7/16

    STEP 2STEP 2

    Now modify the grammar so that everyNow modify the grammar so that everyproduction are of the following form:production are of the following form:

    AA

    aa

    or Aor AAjAj

    where j>I andwhere j>I and V*V*

  • 8/8/2019 7193282 Greibach Normal Form

    8/16

    STEP 2 (..contd)STEP 2 (..contd) To get a CNF in the above discussed format, we should follow theTo get a CNF in the above discussed format, we should follow the

    method:method: Start with AStart with A11 and proceed to Aand proceed to Ann. Suppose. Suppose

    upto Aupto Am-1m-1 the above condition 1

  • 8/8/2019 7193282 Greibach Normal Form

    9/16

    STEP 3STEP 3

    Since ASince Ann is the highest numbered variable thenis the highest numbered variable thenproductions are of the form Aproductions are of the form Ann aa . .

    ,, the leftmost symbol of right side ofthe leftmost symbol of right side ofany production forany production for AA

    nnmust be eithermust be either

    terminal orterminal or AAmm.. ReplaceReplace AAmm on right side of productionon right side of production AAm-1m-1

    by replacement rule.by replacement rule.

    Now leftmost symbol of the right hand sideNow leftmost symbol of the right hand sideof productions forof productions for AAm-1m-1 is terminal. Repeat thisis terminal. Repeat thisprocess for Aprocess for Am-2m-2 AAm-3m-3.. A.. A1.1. now R.H.S. of eachnow R.H.S. of eachproduction for an Aproduction for an Aii starts with a terminalstarts with a terminalsymbol.symbol.

  • 8/8/2019 7193282 Greibach Normal Form

    10/16

    STEP 4STEP 4

    The new variablesThe new variables BBii introduced tointroduced to

    eliminate left recursion in step 2 have toeliminate left recursion in step 2 have tobe simplified such that all productions arebe simplified such that all productions areof the form in GNF.of the form in GNF.

    This is done by substitution rules.This is done by substitution rules.

    NoNo BBii production can start with anotherproduction can start with anotherBBjj Finally combining all productions we getFinally combining all productions we get

    the required grammar in GNF.the required grammar in GNF.

  • 8/8/2019 7193282 Greibach Normal Form

    11/16

    FinallyFinally

    The net conclusion is simple.The net conclusion is simple.

    3.3. apply substitution ruleapply substitution rule4.4. eliminate left recursion.eliminate left recursion.

    5.5. check if all productions are in GNF.check if all productions are in GNF.

  • 8/8/2019 7193282 Greibach Normal Form

    12/16

    EXAMPLE 1EXAMPLE 1

    CONVERT THE GRAMMARCONVERT THE GRAMMARSSAB | BCAB | BC

    AA aB | bA | aaB | bA | a

    BBbB | cC | bbB | cC | b

    CCc into GNF.c into GNF. Here the production SHere the production SAB | BC is not in GNF.AB | BC is not in GNF.

    On applying the substitution rule we immediatelyOn applying the substitution rule we immediatelyget equivalent grammar in GNF.get equivalent grammar in GNF.

    SSaBB | bAB | aB | bBC | cCC | bCaBB | bAB | aB | bBC | cCC | bC

  • 8/8/2019 7193282 Greibach Normal Form

    13/16

    EXAMPLE 2EXAMPLE 2

    CONVERT THE GRAMMARCONVERT THE GRAMMARSS abaSa | aba Into GNF.abaSa | aba Into GNF.

    If we introduce new variables A and B andIf we introduce new variables A and B and

    productions asproductions as AAa , Ba , Bb and substitute into the givenb and substitute into the given

    grammar asgrammar as SS aBASA | ABAaBASA | ABA

    AAa , Ba , Bbb

    Which is in GNF.Which is in GNF.

  • 8/8/2019 7193282 Greibach Normal Form

    14/16

    EXAMPLE 3EXAMPLE 3

    Convert the grammarConvert the grammar SSABAB

    AA

    BS | aBS | a BBSA | bSA | b into GNF.into GNF.

    SOLUTION:SOLUTION:

    http://convert%20the%20grammar.doc/#Slide%2054http://convert%20the%20grammar.doc/#Slide%2054http://convert%20the%20grammar.doc/#Slide%2054
  • 8/8/2019 7193282 Greibach Normal Form

    15/16

    APPLICATIONSAPPLICATIONS

  • 8/8/2019 7193282 Greibach Normal Form

    16/16

    THANK YOUTHANK YOU