SlideShare a Scribd company logo
Grammars




           1
Grammars
  Grammars express languages

  Example:   the English language
sentence      noun _ phrase     predicate


noun _ phrase       article   noun


 predicate      verb                 2
article    a
article    the


noun      boy
noun      dog


verb      runs
verb      walks   3
A derivation of “the boy walks”:



sentence      noun _ phrase         predicate
              noun _ phrase         verb
              article    noun       verb
             the noun       verb
             the boy verb
             the boy walks
                                       4
A derivation of “a dog runs”:


sentence      noun _ phrase      predicate
              noun _ phrase      verb
              article    noun    verb
             a noun       verb
             a dog verb
             a dog runs
                                    5
Language of the grammar:



       L = { “a boy runs”,
             “a boy walks”,
             “the boy runs”,
             “the boy walks”,
             “a dog runs”,
             “a dog walks”,
             “the dog runs”,    6
Notation
               noun    boy
               noun    dog


 Variable                     Terminal
                 Production
    or
                 rule
Non-terminal

                                7
Another Example

 Grammar:         S     aSb
                  S

 Derivation of sentence
                        ab :

            S     aSb       ab

      S     aSb         S
                                 8
S     aSb
Grammar:S



Derivation of sentence
                       aabb :
    S       aSb       aaSbb       aabb


        S       aSb           S          9
S        aSb     aaSbb   aaaSbbb        aaabbb
    Other derivations:




S      aSb aaSbb aaaSbbb
       aaaaSbbbb aaaabbbb

                                   10
Language of the grammar:

       L = { “a boy runs”,
             “a boy walks”,
             “the boy runs”,
             “the boy walks”,
             “a dog runs”,
             “a dog walks”,
             “the dog runs”,
             “the dog walks” }
                                 11
Language of the grammar


           S     aSb
           S

               n n
       L {a b : n 0}


                          12
More Notation

                              G      V , T , P, S
    Grammar
          V : A finiteSet of variables

          T : A finiteSet of terminal symbols
           S : Start variable

          P:       Set of Production rules
                                                    13

V and T are assumed to be disjoint
Example
        G        S      aSb
 Grammar     :   S

    G       V , T , P, S

V   {S }     T       {a, b}
                       P {S   aSb, S
                                  14
                                       }
More Notation

    Sentential Form:
              A sentence that contains
              variables and terminals

    Example:
S      aSb     aaSbb      aaaSbbb        aaabbb

         Sentential Forms           sentence
                                         15
*
                   S       aaabbb
    We write:




    Instead of:
S      aSb        aaSbb     aaaSbbb   aaabbb
                                      16
*
                        w1       wn
In general we write:




       w1     w2       w3            wn
If:

                                       17
*
              w       w
By default:




                          18
Example
Grammar   Derivations
S   aSb      *
          S
S
              *
          S       ab
              *
          S       aabb
              *
          S       aaabbb
                         19
Example
Grammar         Derivations
S   aSb
           S   aaSbb
S

           aaSbb    aaaaaSbbbbb


                          20
Another Grammar Example
            G   S  Ab
     Grammar        : A   aAb
                     A
    Derivations:


S          Ab        aAbb       abb
S       Ab         aAbb     aaAbbb         aabbb
                                      21
Example
Grammar         Derivations
S   aSb
           S   aaSbb
S

           aaSbb    aaaaaSbbbbb


                          22
More Derivations
S   Ab aAbb aaAbbb          aaaAbbbb
    aaaaAbbbbb      aaaabbbbb


S   aaaabbbbb

S   aaaaaabbbbbbb
     n n
S   a b b                     23
Language of a Grammar


 For a grammar G
 with start variable S :

          L(G ) {w : S       w}


             String of terminals
                                   24
Example
              G      S     Ab
 For grammar       : A     aAb
                    A

                         n n
          L(G ) {a b b : n 0}

                     n n
     Since:    S    a b b
                                 25
A Convenient Notation
     A     aAb
                         A    aAb |
     A



 article    a
                        article        a | the
 article    the
                                  26
Linear Grammars




                  27
Linear Grammars

 Grammars with
 at most one variable at the right side
 of a production

             S    aSb           S     Ab
 Examples:   S                  A     aAb
                                 A
                                          28
A Non-Linear Grammar
  Grammar G :   S SS
                             S
Why S           SS
                             S    aSb
What will happen if
                             S    bSa
S       S instead



            L(G) {w : na ( w) nb ( w)}

    Number of         a in string w      29
Another Linear Grammar
       G       S    A
 Grammar   :   A    aB |
               B    Ab


                   n n
        L(G ) {a b : n 0}
                            30
Right-Linear Grammars

 All productions have form:   A    xB
                              A    x

                                  string of
 Example:                         terminals
                S    abS
                S    a
                                     31
Left-Linear Grammars
                              A        Bx
 All productions have form:       or
                              A        x

                                  string of
              S    Aab            terminals
 Example:
              A    Aab | B
              B    a                       32
Regular Grammars




                   33
Regular Grammars

 A regular grammar is any
 right-linear or left-linear grammar

 Examples:       G1                    G2
       S     abS                 S      Aab
       S     a                   A      Aab | B
                                 B      a   34
Observation

 Regular grammars generate regular
 languages               G2
     G1
 Examples:             S     Aab
 S    abS               A    Aab | B
 S    a                 B    a

 L(G1) (ab) * a        L(G2 ) aab(ab) *
                                     35
Regular Grammars
Generate
Regular Languages



                    36
Theorem
Languages
                   Regular
Generated by
                   Languages
Regular Grammars




                      37
Theorem - Part 1

Languages
                             Regular
Generated by
                             Languages
Regular Grammars


    Any regular grammar generates
    a regular language


                                38
Theorem - Part 2

Languages
                             Regular
Generated by
                             Languages
Regular Grammars


     Any regular language is generated
     by a regular grammar


                                 39
Proof – Part 1
 Languages
                             Regular
 Generated by
                             Languages
 Regular Grammars


  The language L (G ) generated by
  any regular grammar G is regular


                                 40
The case of Right-Linear
Grammars

  Let   G be a right-linear grammar

  We will prove:   L(G ) is regular

                       L( M )   L(G )

  Proof idea:      We will construct NFA     M
                                        41

                   with L(M)=L(G)
Grammar    G is right-linear
Example:
             S     aA | B
             A     aa B
             B     b B|a


                               42
Construct NFA M such that
    every state is a grammar variable:



                       A                 special
           S                    VF
                                         final state
                      B
S     aA | B
A     aa B
B     b B|a                               43
Add edges for each production:
             a    A
         S                  VF

                  B

S   aA
                                 44
a   A
        S            VF

                 B

S   aA | B
                          45
A
             a       a


        S            a   VF

                 B
S   aA | B
A   aa B
                              46
A
             a       a


         S           a       VF

                 B
S   aA | B
                         b
A   aa B
B   bB                            47
A
             a       a


        S            a           VF
                         a
                 B
S   aA | B
                             b
A   aa B
B   bB | a                            48
A
         a       a


    S            a            VF
                      a
             B

                          b

S   aA   aaaB        aaabB         aaaba
                                      49
NFA   M                      Grammar
          A                              G
              a                      S        aA | B
    a
                                     A        aa B
S             a                      B        bB | a
                          VF
                  a
          B
                               L( M ) L(G )
                      b
                               aaab * a b * a
                                             50
In General

 A right-linear grammar    G

 has variables:   V0 ,V1,V2 ,


 and productions:    Vi   a1a2 amV j
                          or
                    Vi    a1a2 am
                                        51
We construct the NFA    M such that:

each variable      Vicorresponds to a
node:         V1          V3
    V0
                                    VF
            V2                    special
                         V4
                                  final state
                                        52
Vi   a1a2 amV j
For each production:

we add transitions and intermediate
nodes
Vi  a1       a2         ………
                                    am   Vj



                                    53
For each production:
                       Vi   a1a2 am
we add transitions and intermediate
nodes
Vi  a1       a2         ………
                                  am
                                        VF



                                   54
Resulting NFA        M looks like this:
                              a9

                      a2           a4
             a1    V1                   V3
                      a3                      a5
        V0
             a3               a4
                                                   VF
                                   a8        a9
                  V2     a5
                                    V4


It holds that:    L(G )        L( M )              55
The case of Left-Linear
Grammars

 Let   G be a left-linear grammar

 We will prove: L (G ) is regular
 Proof idea:
    We will construct a right-linear
    grammar G with                          R
                          L(G )     L(G )
                                       56
Since G is left-linear grammar
the productions look like:


           A    Ba1a2 ak

           A    a1a2 ak


                                 57
Left G
         A Ba1a2 ak
linear
 Construct right-linear grammar   G

                     A       Bv

Right
         G       A    ak a2a1B
linear
                         R
                 A    v B             58
Construct right-linear grammar   G
Left
         G        A    a1a2 ak
linear
                  A    v


Right
         G       A    ak a2a1
linear
                          R
                 A    v              59
R
                           L(G )   L(G )
 It is easy to see that:
        G
 Since       is right-linear, we have:
                           R             L(G )
L(G )              L(G )
Regular           Regular                Regular
Language          Language               Language

                                          60
Proof - Part 2
 Languages
                             Regular
 Generated by
                             Languages
 Regular Grammars


  Any regular language L is generated
  by some regular grammar G


                                 61
Any regular language L is generated
by some regular grammar G



Proof idea:
  Let   M be the NFA with L   L(M ).

  Construct from M a regular grammar   G
  such that L ( M ) L (G )
                                 62
Since L is regular
  there is an NFA M such that   L         L(M )
                          b
Example:
            M                   a
                      a
                q0        q1              q2
                                          b
L ab * ab(b * ab) *             q3
L L(M )                              63
Convert   M to a right-linear grammar
                          b

            M                 a
                     a
                q0       q1              q2

                                     b
q0   aq1
                               q3


                                    64
b

           M                 a
                    a
               q0       q1        q2
q0   aq1                          b
q1   bq1                     q3
q1   aq2

                             65
b

           M                 a
                    a
               q0       q1        q2
q0   aq1
q1   bq1                          b

q1   aq2                     q3

q2   bq3
                             66
L(G )       L( M )   L
     G                                  b

q0       aq1           M                     a
                                a
                           q0           q1        q2
q1       bq1
q1       aq2                                      b

q2       bq3                                 q3

q3       q1
q3                                           67
In General
                                a
For any transition:      q              p




 Add production:         q      ap


              variable       terminal       variable
                                            68
For any final state:        qf




Add production:        qf



                                 69
Since   G is right-linear grammar


        G is also a regular grammar


        with   L(G )   L( M )   L
                                      70
For any regular language one can
construct left linear as well as right
linear grammar.




                                         71
A
                 a       a


                         a           VF
S                            a            F → Ba

                     B                    B → Bb      here F is
                                                    start symbol
    S   aA | B                            B → Aaa
                                 b        B→S
    A   aa B                              A → Sa

    B   bB | a                            S→       this is new rule
                                                   72

                                          as S is now a final state
A → aB   D → Cb
B → bB   D→B
         C→B a
B → aC
         B →B b     Now D is the
C→b D             start variable

D→ B     B → Aa
         A→
D→


                              73
Language generated by both

aaab* a +b*a




                             74
75
76
77
Context-Free Languages




                     78
Context-Free Languages

  n n              R
{a b }         {ww }


    Regular Languages


                          79
Context-Free Languages




Context-Free            Pushdown
Grammars                Automata

                                      stack

                       automaton
                                 80
Context-Free Grammars




                        81
Example
A context-free grammar   G:   S      aSb
                              S



A derivation:

        S       aSb   aaSbb   aabb
                                  82
S     aSb
        S


L(G )         n n
            {a b : n 0}


        (((( ))))
                          83
A context-free grammar   G:   S   aSa
                              S   bSb
                              S


Another derivation:

S    aSa     abSba    abaSaba      abaaba
                                  84
S   aSa
        S   bSb
        S



L(G )       R
        {ww : w {a, b}*}

                     85
Example

A context-free grammar   G:   S   aSb
                              S   SS
                              S


A derivation:

      S     SS   aSbS      abS    ab
                                  86
A context-free grammar   G:    S      aSb
                               S      SS
                               S


A derivation:

S    SS     aSbS   abS        abaSb        abab
                                      87
S     aSb
            S     SS
            S

L(G )    {w : na ( w) nb ( w),
                and na (v) nb (v)
                in any prefix v}

        () ((( ))) (( ))         88
Definition: Context-Free
Grammars
    Grammar
                     G       (V , T , P, S )

Variables     Terminal       Start
              symbols        variable

        Productions of the form:
                 A       x
   x is string of variables and terminals      89
Definition: Context-Free
Languages

 A language       L is context-free

 if and only if

there is a C.F.grammar      Gwith

                             L    L(G )
                                          90
Derivation Order
1. S   AB      2. A                 aaA            4. B       Bb
                      3. A                         5. B
Leftmost derivation:
        1        2             3            4          5
    S       AB       aaAB          aaB          aaBb        aab
Rightmost derivation:
        1        4         5            2          3
    S       AB       ABb           Ab       aaAb       aab
                                                       91
S     aAB
                  A     bBb
                  B     A|
Leftmost derivation:
S    aAB abBbB abAbB             abbBbbB
     abbbbB abbbb
Rightmost derivation:
 S    aAB aA abBb             abAb
      abbBbb abbbb                   92
Derivation Trees




                   93
Def:
 G =(V,T,P,S)
An ordered tree is a derivation tree for G iff
1 The root is labeled S
2 every leaf has a label from T { }
3 Every interior vertex has a label from V
4 If a vertex has label A(variable) and its children
    are labeled from (L to R)
    a1,a2,…an then P must contain a production
    A       a1 a2 …an
5 A leaf labeled      has no sibling.
                                            94
Yield:

The string of terminals obtained by
reading the leaves of the tree from left
to right omitting any ’s encountered
is called yield of the tree.



                                       95
Partial derivation tree

A tree that has properties 3,4,5 but 1
need not
And 2 is replaced by
V    T { }


                                         96
S   AB       A       aaA |        B        Bb |

                 S       AB
                     S

         A                    B




                                      97
S   AB       A       aaA |       B        Bb |

         S       AB      aaAB
                     S

         A                   B

    a    a       A

                                     98
S   AB          A       aaA |       B        Bb |

        S   AB      aaAB        aaABb
                        S

            A                   B

    a       a       A       B       b

                                        99
S   AB        A       aaA |        B          Bb |

S   AB       aaAB         aaABb        aaBb
                      S

         A                     B

    a    a        A        B       b

                                        100
S   AB       A       aaA |        B         Bb |

S       AB   aaAB        aaABb       aaBb         aab
Derivation Tree          S

             A                   B

         a   a       A       B       b

                                          101
S   AB       A       aaA |        B         Bb |

S       AB   aaAB        aaABb       aaBb            aab
Derivation Tree          S

             A                   B
                                                yield

         a   a       A       B       b          aa     b
                                                 aab
                                          102
Partial Derivation Trees
S    AB       A aaA |              B         Bb |


                          S       AB
Partial derivation tree       S


                    A                  B
                                       103
S     AB      aaAB


Partial derivation tree        S

                    A              B

             a      a      A

                                   104
sentential
            S     AB      aaAB
                                   form


Partial derivation tree        S

                    A                 B

                                            yield
             a      a      A
                                            aaAB
                                      105
Sometimes, derivation order doesn’t matter
Leftmost:
S    AB      aaAB      aaB           aaBb           aab
Rightmost:
S    AB      ABb       Ab    aaAb             aab
                                 S
Same derivation tree
                        A                 B

                   a    a    A        B       b

                                              106
Ambiguity




            107
E E E | E E | (E) | a
    a a a

     E    E   E E     a E     a E E
                    a a E     a a*a
E         E
                    leftmost derivation

a    E         E

      a        a                108
E E E | E E | (E) | a
    a a a

E    E E     E E E a E E E
            a a E a a a
                              E             E
    leftmost derivation

                          E             E   a

                          a       109   a
E       E E | E E | (E) | a
                 a a a
              Two derivation trees
        E                              E

E            E                   E             E

a       E          E       E               E   a

        a           a      a         110   a
The grammarE          E E | E E | (E) | a
is ambiguous:

 string   a a a has two derivation trees

          E                        E

    E         E                E             E


    a     E       E        E       E         a

          a       a        a       a
                                       111
The grammarE       E E | E E | (E) | a
is ambiguous:

string   a a a has two leftmost derivations

  E      E E     a E      a E E
               a a E      a a*a

  E      E E    E E E a E E
               a a E a a a         112
Definition:
  A context-free grammar    G is ambiguous

  if some string   w L(G ) has:

      two or more derivation trees




                                     113
In other words:

  A context-free grammar     G is ambiguous

  if some string   w L(G ) has:

       two or more leftmost derivations
                   (or rightmost)



                                    114
Why do we care about ambiguity?

               a a a
               take   a   2
    E                               E

E         E                   E             E

a   E           E         E             E   a

    a           a         a       115   a
2 2 2

    E                     E

E       E           E             E

2   E       E   E             E   2

    2       2   2       116   2
2 2 2 6              2 2 2 8
       6                       8
      E                        E
 2         4             4              2
E          E             E              E
      2         2   2               2
2     E        E     E             E    2

      2        2     2       117   2
Correct result:   2 2 2 6

                    6
                   E
             2          4
            E           E
                   2         2
             2     E        E

                   2        2    118
• Ambiguity is bad for programming languages




• We want to remove ambiguity




                                   119
We fix the ambiguous grammar:
     E    E E | E E | (E) | a

New non-ambiguous grammar:   E   E T
                             E   T
                             T   T F
                             T   F
                             F   (E)
                                 120


                             F   a
E   E T   T T   F T    a T       a T F
    a F F   a a F     a a a
                       E             a a a
E    E T
                 E               T
E   T
T   T F          T           T             F
T   F
                 F           F             a
F   (E)
F    a           a           a
                                     121
Unique derivation tree

          E           a a a
    E             T

     T        T          F

     F        F          a

     a        a
                              122
The grammar   G:    E   E T
                    E   T
                    T   T F
                    T   F
                    F   (E)
                    F   a
is non-ambiguous:
     Every string w L (G ) has
     a unique derivation tree    123
Inherent Ambiguity


     Some context free languages
     have only ambiguous grammars
    Example:         n n m        n m m
                 L {a b c }       {a b c }


S      S1 | S2     S1   S1c | A    S2         aS2 | B
                   A    aAb |      B     bBc |
                                        124
n n n
The string     a b c
has two derivation trees

          S                    S

          S1                   S2

     S1         c          a              S2

                                    125
Compiler

           Lexical
                        parser
           analyzer



input                                    output

                                 machine
 program
                                 code
                                   126
A parser knows the grammar
of the programming language




                              127
The parser finds the derivation
of a particular input

                                  derivation
               Parser
 input                        E => E + E
             E -> E + E
                                => E + E * E
10 + 2 * 5      |E*E
                                => 10 + E*E
                | INT
                                => 10 + 2 * E
                                => 10 + 2 * 5
                                     128
derivation tree
 derivation
                       E

E => E + E        E     +       E
  => E + E * E
  => 10 + E*E     10
                           E         *   E
  => 10 + 2 * E
  => 10 + 2 * 5            2             5
                               129

More Related Content

What's hot

Nondeterministic Finite Automata
Nondeterministic Finite AutomataNondeterministic Finite Automata
Nondeterministic Finite AutomataAdel Al-Ofairi
 
Context free languages
Context free languagesContext free languages
Context free languages
Jahurul Islam
 
language , grammar and automata
language , grammar and automatalanguage , grammar and automata
language , grammar and automata
ElakkiyaS11
 
Regular expressions-Theory of computation
Regular expressions-Theory of computationRegular expressions-Theory of computation
Regular expressions-Theory of computation
Bipul Roy Bpl
 
Futher pure mathematics 3 hyperbolic functions
Futher pure mathematics 3 hyperbolic functionsFuther pure mathematics 3 hyperbolic functions
Futher pure mathematics 3 hyperbolic functions
Dennis Almeida
 
Regular Expression to Finite Automata
Regular Expression to Finite AutomataRegular Expression to Finite Automata
Regular Expression to Finite Automata
Archana Gopinath
 
Theory of Automata
Theory of AutomataTheory of Automata
Theory of Automata
Farooq Mian
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
Ratnakar Mikkili
 
Propositional logic & inference
Propositional logic & inferencePropositional logic & inference
Propositional logic & inferenceSlideshare
 
Formal Languages and Automata Theory unit 3
Formal Languages and Automata Theory unit 3Formal Languages and Automata Theory unit 3
Formal Languages and Automata Theory unit 3
Srimatre K
 
lattice
 lattice lattice
Formal Languages and Automata Theory unit 2
Formal Languages and Automata Theory unit 2Formal Languages and Automata Theory unit 2
Formal Languages and Automata Theory unit 2
Srimatre K
 
Regular Expression in Compiler design
Regular Expression in Compiler designRegular Expression in Compiler design
Regular Expression in Compiler design
Riazul Islam
 
Chomsky Normal Form
Chomsky Normal FormChomsky Normal Form
Chomsky Normal Form
Jasmine Peniel
 
2. context free langauages
2. context free langauages2. context free langauages
2. context free langauagesdanhumble
 
Cauchy riemann equations
Cauchy riemann equationsCauchy riemann equations
Cauchy riemann equationssajidpk92
 
Introduction to Approximation Algorithms
Introduction to Approximation AlgorithmsIntroduction to Approximation Algorithms
Introduction to Approximation Algorithms
Jhoirene Clemente
 
Context free grammars
Context free grammarsContext free grammars
Context free grammarsRonak Thakkar
 
Formal Languages and Automata Theory Unit 1
Formal Languages and Automata Theory Unit 1Formal Languages and Automata Theory Unit 1
Formal Languages and Automata Theory Unit 1
Srimatre K
 
Chapter 4. logic function and boolean algebra
Chapter 4. logic function and boolean algebraChapter 4. logic function and boolean algebra
Chapter 4. logic function and boolean algebra
Ashish KC
 

What's hot (20)

Nondeterministic Finite Automata
Nondeterministic Finite AutomataNondeterministic Finite Automata
Nondeterministic Finite Automata
 
Context free languages
Context free languagesContext free languages
Context free languages
 
language , grammar and automata
language , grammar and automatalanguage , grammar and automata
language , grammar and automata
 
Regular expressions-Theory of computation
Regular expressions-Theory of computationRegular expressions-Theory of computation
Regular expressions-Theory of computation
 
Futher pure mathematics 3 hyperbolic functions
Futher pure mathematics 3 hyperbolic functionsFuther pure mathematics 3 hyperbolic functions
Futher pure mathematics 3 hyperbolic functions
 
Regular Expression to Finite Automata
Regular Expression to Finite AutomataRegular Expression to Finite Automata
Regular Expression to Finite Automata
 
Theory of Automata
Theory of AutomataTheory of Automata
Theory of Automata
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 
Propositional logic & inference
Propositional logic & inferencePropositional logic & inference
Propositional logic & inference
 
Formal Languages and Automata Theory unit 3
Formal Languages and Automata Theory unit 3Formal Languages and Automata Theory unit 3
Formal Languages and Automata Theory unit 3
 
lattice
 lattice lattice
lattice
 
Formal Languages and Automata Theory unit 2
Formal Languages and Automata Theory unit 2Formal Languages and Automata Theory unit 2
Formal Languages and Automata Theory unit 2
 
Regular Expression in Compiler design
Regular Expression in Compiler designRegular Expression in Compiler design
Regular Expression in Compiler design
 
Chomsky Normal Form
Chomsky Normal FormChomsky Normal Form
Chomsky Normal Form
 
2. context free langauages
2. context free langauages2. context free langauages
2. context free langauages
 
Cauchy riemann equations
Cauchy riemann equationsCauchy riemann equations
Cauchy riemann equations
 
Introduction to Approximation Algorithms
Introduction to Approximation AlgorithmsIntroduction to Approximation Algorithms
Introduction to Approximation Algorithms
 
Context free grammars
Context free grammarsContext free grammars
Context free grammars
 
Formal Languages and Automata Theory Unit 1
Formal Languages and Automata Theory Unit 1Formal Languages and Automata Theory Unit 1
Formal Languages and Automata Theory Unit 1
 
Chapter 4. logic function and boolean algebra
Chapter 4. logic function and boolean algebraChapter 4. logic function and boolean algebra
Chapter 4. logic function and boolean algebra
 

Viewers also liked

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
 
Lecture: Context-Free Grammars
Lecture: Context-Free GrammarsLecture: Context-Free Grammars
Lecture: Context-Free Grammars
Marina Santini
 
Compiler Design - Ambiguous grammar, LMD & RMD, Infix & Postfix, Implementati...
Compiler Design - Ambiguous grammar, LMD & RMD, Infix & Postfix, Implementati...Compiler Design - Ambiguous grammar, LMD & RMD, Infix & Postfix, Implementati...
Compiler Design - Ambiguous grammar, LMD & RMD, Infix & Postfix, Implementati...
Saikrishna Tanguturu
 
On Inherent Complexity of Computation, by Attila Szegedi
On Inherent Complexity of Computation, by Attila SzegediOn Inherent Complexity of Computation, by Attila Szegedi
On Inherent Complexity of Computation, by Attila Szegedi
ZeroTurnaround
 
Ambiguty
AmbigutyAmbiguty
Ambiguty
Rajendran
 
Argon ion lasers
Argon ion lasersArgon ion lasers
Argon ion lasers
ajay singh
 
Spanish grammer book
Spanish grammer bookSpanish grammer book
Spanish grammer bookDiegoTspanish
 
L3 cfg
L3 cfgL3 cfg
Transformational-Generative Grammar
Transformational-Generative GrammarTransformational-Generative Grammar
Transformational-Generative GrammarRuth Ann Llego
 
Transformational Grammar by: Noam Chomsky
Transformational Grammar by: Noam ChomskyTransformational Grammar by: Noam Chomsky
Transformational Grammar by: Noam ChomskyShiela May Claro
 
What is Universal Grammar Theory and its Criticism
What is Universal Grammar Theory and its Criticism What is Universal Grammar Theory and its Criticism
What is Universal Grammar Theory and its Criticism
Farhad Mohammad
 
Grammar Powerpoint
Grammar PowerpointGrammar Powerpoint
Grammar Powerpoint
Aaron Liebo
 

Viewers also liked (15)

Deterministic context free grammars &non-deterministic
Deterministic context free grammars &non-deterministicDeterministic context free grammars &non-deterministic
Deterministic context free grammars &non-deterministic
 
Lecture: Context-Free Grammars
Lecture: Context-Free GrammarsLecture: Context-Free Grammars
Lecture: Context-Free Grammars
 
Compiler Design - Ambiguous grammar, LMD & RMD, Infix & Postfix, Implementati...
Compiler Design - Ambiguous grammar, LMD & RMD, Infix & Postfix, Implementati...Compiler Design - Ambiguous grammar, LMD & RMD, Infix & Postfix, Implementati...
Compiler Design - Ambiguous grammar, LMD & RMD, Infix & Postfix, Implementati...
 
On Inherent Complexity of Computation, by Attila Szegedi
On Inherent Complexity of Computation, by Attila SzegediOn Inherent Complexity of Computation, by Attila Szegedi
On Inherent Complexity of Computation, by Attila Szegedi
 
Ambiguty
AmbigutyAmbiguty
Ambiguty
 
Argon ion lasers
Argon ion lasersArgon ion lasers
Argon ion lasers
 
Spanish grammer book
Spanish grammer bookSpanish grammer book
Spanish grammer book
 
L3 cfg
L3 cfgL3 cfg
L3 cfg
 
Syntax course
Syntax courseSyntax course
Syntax course
 
Narrative
NarrativeNarrative
Narrative
 
Grammar
GrammarGrammar
Grammar
 
Transformational-Generative Grammar
Transformational-Generative GrammarTransformational-Generative Grammar
Transformational-Generative Grammar
 
Transformational Grammar by: Noam Chomsky
Transformational Grammar by: Noam ChomskyTransformational Grammar by: Noam Chomsky
Transformational Grammar by: Noam Chomsky
 
What is Universal Grammar Theory and its Criticism
What is Universal Grammar Theory and its Criticism What is Universal Grammar Theory and its Criticism
What is Universal Grammar Theory and its Criticism
 
Grammar Powerpoint
Grammar PowerpointGrammar Powerpoint
Grammar Powerpoint
 

Similar to Grammar

Class7
 Class7 Class7
Class7issbp
 
Class5
 Class5 Class5
Class5issbp
 
Class5
Class5Class5
Class5
kpcs2010
 
Context free grammar
Context free grammarContext free grammar
Context free grammar
Radhakrishnan Chinnusamy
 
Alphabets , strings, languages and grammars
Alphabets , strings, languages  and grammarsAlphabets , strings, languages  and grammars
Alphabets , strings, languages and grammars
hele987
 
Pumping lemma for cfl
Pumping lemma for cflPumping lemma for cfl
Pumping lemma for cfl
Muhammad Zohaib Chaudhary
 

Similar to Grammar (7)

Class7
 Class7 Class7
Class7
 
Class5
 Class5 Class5
Class5
 
Properties of cfg
Properties of cfgProperties of cfg
Properties of cfg
 
Class5
Class5Class5
Class5
 
Context free grammar
Context free grammarContext free grammar
Context free grammar
 
Alphabets , strings, languages and grammars
Alphabets , strings, languages  and grammarsAlphabets , strings, languages  and grammars
Alphabets , strings, languages and grammars
 
Pumping lemma for cfl
Pumping lemma for cflPumping lemma for cfl
Pumping lemma for cfl
 

More from lavishka_anuj

Simplifiaction of grammar
Simplifiaction of grammarSimplifiaction of grammar
Simplifiaction of grammarlavishka_anuj
 
Non regular languages
Non regular languagesNon regular languages
Non regular languageslavishka_anuj
 
Moore and mealy machines
Moore and mealy machinesMoore and mealy machines
Moore and mealy machineslavishka_anuj
 
Finite automata intro
Finite automata introFinite automata intro
Finite automata introlavishka_anuj
 

More from lavishka_anuj (9)

Simplifiaction of grammar
Simplifiaction of grammarSimplifiaction of grammar
Simplifiaction of grammar
 
One ques
One quesOne ques
One ques
 
Non regular languages
Non regular languagesNon regular languages
Non regular languages
 
Moore and mealy machines
Moore and mealy machinesMoore and mealy machines
Moore and mealy machines
 
Flat
FlatFlat
Flat
 
Finite automata intro
Finite automata introFinite automata intro
Finite automata intro
 
Finite automata
Finite automataFinite automata
Finite automata
 
Pushdown automata
Pushdown automataPushdown automata
Pushdown automata
 
Turing machines
Turing machinesTuring machines
Turing machines
 

Recently uploaded

Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
Anna Sz.
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
Jisc
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
GeoBlogs
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
Celine George
 
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
AzmatAli747758
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
Col Mukteshwar Prasad
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
bennyroshan06
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
Atul Kumar Singh
 
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdfESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
Fundacja Rozwoju Społeczeństwa Przedsiębiorczego
 
Introduction to Quality Improvement Essentials
Introduction to Quality Improvement EssentialsIntroduction to Quality Improvement Essentials
Introduction to Quality Improvement Essentials
Excellence Foundation for South Sudan
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
Vivekanand Anglo Vedic Academy
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
EduSkills OECD
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
Tamralipta Mahavidyalaya
 
Sectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdfSectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdf
Vivekanand Anglo Vedic Academy
 

Recently uploaded (20)

Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
 
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
 
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdfESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
 
Introduction to Quality Improvement Essentials
Introduction to Quality Improvement EssentialsIntroduction to Quality Improvement Essentials
Introduction to Quality Improvement Essentials
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
Sectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdfSectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdf
 

Grammar

  • 2. Grammars Grammars express languages Example: the English language sentence noun _ phrase predicate noun _ phrase article noun predicate verb 2
  • 3. article a article the noun boy noun dog verb runs verb walks 3
  • 4. A derivation of “the boy walks”: sentence noun _ phrase predicate noun _ phrase verb article noun verb the noun verb the boy verb the boy walks 4
  • 5. A derivation of “a dog runs”: sentence noun _ phrase predicate noun _ phrase verb article noun verb a noun verb a dog verb a dog runs 5
  • 6. Language of the grammar: L = { “a boy runs”, “a boy walks”, “the boy runs”, “the boy walks”, “a dog runs”, “a dog walks”, “the dog runs”, 6
  • 7. Notation noun boy noun dog Variable Terminal Production or rule Non-terminal 7
  • 8. Another Example Grammar: S aSb S Derivation of sentence ab : S aSb ab S aSb S 8
  • 9. S aSb Grammar:S Derivation of sentence aabb : S aSb aaSbb aabb S aSb S 9
  • 10. S aSb aaSbb aaaSbbb aaabbb Other derivations: S aSb aaSbb aaaSbbb aaaaSbbbb aaaabbbb 10
  • 11. Language of the grammar: L = { “a boy runs”, “a boy walks”, “the boy runs”, “the boy walks”, “a dog runs”, “a dog walks”, “the dog runs”, “the dog walks” } 11
  • 12. Language of the grammar S aSb S n n L {a b : n 0} 12
  • 13. More Notation G V , T , P, S Grammar V : A finiteSet of variables T : A finiteSet of terminal symbols S : Start variable P: Set of Production rules 13 V and T are assumed to be disjoint
  • 14. Example G S aSb Grammar : S G V , T , P, S V {S } T {a, b} P {S aSb, S 14 }
  • 15. More Notation Sentential Form: A sentence that contains variables and terminals Example: S aSb aaSbb aaaSbbb aaabbb Sentential Forms sentence 15
  • 16. * S aaabbb We write: Instead of: S aSb aaSbb aaaSbbb aaabbb 16
  • 17. * w1 wn In general we write: w1 w2 w3  wn If: 17
  • 18. * w w By default: 18
  • 19. Example Grammar Derivations S aSb * S S * S ab * S aabb * S aaabbb 19
  • 20. Example Grammar Derivations S aSb S aaSbb S aaSbb aaaaaSbbbbb 20
  • 21. Another Grammar Example G S Ab Grammar : A aAb A Derivations: S Ab aAbb abb S Ab aAbb aaAbbb aabbb 21
  • 22. Example Grammar Derivations S aSb S aaSbb S aaSbb aaaaaSbbbbb 22
  • 23. More Derivations S Ab aAbb aaAbbb aaaAbbbb aaaaAbbbbb aaaabbbbb S aaaabbbbb S aaaaaabbbbbbb n n S a b b 23
  • 24. Language of a Grammar For a grammar G with start variable S : L(G ) {w : S w} String of terminals 24
  • 25. Example G S Ab For grammar : A aAb A n n L(G ) {a b b : n 0} n n Since: S a b b 25
  • 26. A Convenient Notation A aAb A aAb | A article a article a | the article the 26
  • 28. Linear Grammars Grammars with at most one variable at the right side of a production S aSb S Ab Examples: S A aAb A 28
  • 29. A Non-Linear Grammar Grammar G : S SS S Why S SS S aSb What will happen if S bSa S S instead L(G) {w : na ( w) nb ( w)} Number of a in string w 29
  • 30. Another Linear Grammar G S A Grammar : A aB | B Ab n n L(G ) {a b : n 0} 30
  • 31. Right-Linear Grammars All productions have form: A xB A x string of Example: terminals S abS S a 31
  • 32. Left-Linear Grammars A Bx All productions have form: or A x string of S Aab terminals Example: A Aab | B B a 32
  • 34. Regular Grammars A regular grammar is any right-linear or left-linear grammar Examples: G1 G2 S abS S Aab S a A Aab | B B a 34
  • 35. Observation Regular grammars generate regular languages G2 G1 Examples: S Aab S abS A Aab | B S a B a L(G1) (ab) * a L(G2 ) aab(ab) * 35
  • 37. Theorem Languages Regular Generated by Languages Regular Grammars 37
  • 38. Theorem - Part 1 Languages Regular Generated by Languages Regular Grammars Any regular grammar generates a regular language 38
  • 39. Theorem - Part 2 Languages Regular Generated by Languages Regular Grammars Any regular language is generated by a regular grammar 39
  • 40. Proof – Part 1 Languages Regular Generated by Languages Regular Grammars The language L (G ) generated by any regular grammar G is regular 40
  • 41. The case of Right-Linear Grammars Let G be a right-linear grammar We will prove: L(G ) is regular L( M ) L(G ) Proof idea: We will construct NFA M 41 with L(M)=L(G)
  • 42. Grammar G is right-linear Example: S aA | B A aa B B b B|a 42
  • 43. Construct NFA M such that every state is a grammar variable: A special S VF final state B S aA | B A aa B B b B|a 43
  • 44. Add edges for each production: a A S VF B S aA 44
  • 45. a A S VF B S aA | B 45
  • 46. A a a S a VF B S aA | B A aa B 46
  • 47. A a a S a VF B S aA | B b A aa B B bB 47
  • 48. A a a S a VF a B S aA | B b A aa B B bB | a 48
  • 49. A a a S a VF a B b S aA aaaB aaabB aaaba 49
  • 50. NFA M Grammar A G a S aA | B a A aa B S a B bB | a VF a B L( M ) L(G ) b aaab * a b * a 50
  • 51. In General A right-linear grammar G has variables: V0 ,V1,V2 , and productions: Vi a1a2 amV j or Vi a1a2 am 51
  • 52. We construct the NFA M such that: each variable Vicorresponds to a node: V1 V3 V0 VF V2 special V4 final state 52
  • 53. Vi a1a2 amV j For each production: we add transitions and intermediate nodes Vi a1 a2 ……… am Vj 53
  • 54. For each production: Vi a1a2 am we add transitions and intermediate nodes Vi a1 a2 ……… am VF 54
  • 55. Resulting NFA M looks like this: a9 a2 a4 a1 V1 V3 a3 a5 V0 a3 a4 VF a8 a9 V2 a5 V4 It holds that: L(G ) L( M ) 55
  • 56. The case of Left-Linear Grammars Let G be a left-linear grammar We will prove: L (G ) is regular Proof idea: We will construct a right-linear grammar G with R L(G ) L(G ) 56
  • 57. Since G is left-linear grammar the productions look like: A Ba1a2 ak A a1a2 ak 57
  • 58. Left G A Ba1a2 ak linear Construct right-linear grammar G A Bv Right G A ak a2a1B linear R A v B 58
  • 59. Construct right-linear grammar G Left G A a1a2 ak linear A v Right G A ak a2a1 linear R A v 59
  • 60. R L(G ) L(G ) It is easy to see that: G Since is right-linear, we have: R L(G ) L(G ) L(G ) Regular Regular Regular Language Language Language 60
  • 61. Proof - Part 2 Languages Regular Generated by Languages Regular Grammars Any regular language L is generated by some regular grammar G 61
  • 62. Any regular language L is generated by some regular grammar G Proof idea: Let M be the NFA with L L(M ). Construct from M a regular grammar G such that L ( M ) L (G ) 62
  • 63. Since L is regular there is an NFA M such that L L(M ) b Example: M a a q0 q1 q2 b L ab * ab(b * ab) * q3 L L(M ) 63
  • 64. Convert M to a right-linear grammar b M a a q0 q1 q2 b q0 aq1 q3 64
  • 65. b M a a q0 q1 q2 q0 aq1 b q1 bq1 q3 q1 aq2 65
  • 66. b M a a q0 q1 q2 q0 aq1 q1 bq1 b q1 aq2 q3 q2 bq3 66
  • 67. L(G ) L( M ) L G b q0 aq1 M a a q0 q1 q2 q1 bq1 q1 aq2 b q2 bq3 q3 q3 q1 q3 67
  • 68. In General a For any transition: q p Add production: q ap variable terminal variable 68
  • 69. For any final state: qf Add production: qf 69
  • 70. Since G is right-linear grammar G is also a regular grammar with L(G ) L( M ) L 70
  • 71. For any regular language one can construct left linear as well as right linear grammar. 71
  • 72. A a a a VF S a F → Ba B B → Bb here F is start symbol S aA | B B → Aaa b B→S A aa B A → Sa B bB | a S→ this is new rule 72 as S is now a final state
  • 73. A → aB D → Cb B → bB D→B C→B a B → aC B →B b Now D is the C→b D start variable D→ B B → Aa A→ D→ 73
  • 74. Language generated by both aaab* a +b*a 74
  • 75. 75
  • 76. 76
  • 77. 77
  • 79. Context-Free Languages n n R {a b } {ww } Regular Languages 79
  • 80. Context-Free Languages Context-Free Pushdown Grammars Automata stack automaton 80
  • 82. Example A context-free grammar G: S aSb S A derivation: S aSb aaSbb aabb 82
  • 83. S aSb S L(G ) n n {a b : n 0} (((( )))) 83
  • 84. A context-free grammar G: S aSa S bSb S Another derivation: S aSa abSba abaSaba abaaba 84
  • 85. S aSa S bSb S L(G ) R {ww : w {a, b}*} 85
  • 86. Example A context-free grammar G: S aSb S SS S A derivation: S SS aSbS abS ab 86
  • 87. A context-free grammar G: S aSb S SS S A derivation: S SS aSbS abS abaSb abab 87
  • 88. S aSb S SS S L(G ) {w : na ( w) nb ( w), and na (v) nb (v) in any prefix v} () ((( ))) (( )) 88
  • 89. Definition: Context-Free Grammars Grammar G (V , T , P, S ) Variables Terminal Start symbols variable Productions of the form: A x x is string of variables and terminals 89
  • 90. Definition: Context-Free Languages A language L is context-free if and only if there is a C.F.grammar Gwith L L(G ) 90
  • 91. Derivation Order 1. S AB 2. A aaA 4. B Bb 3. A 5. B Leftmost derivation: 1 2 3 4 5 S AB aaAB aaB aaBb aab Rightmost derivation: 1 4 5 2 3 S AB ABb Ab aaAb aab 91
  • 92. S aAB A bBb B A| Leftmost derivation: S aAB abBbB abAbB abbBbbB abbbbB abbbb Rightmost derivation: S aAB aA abBb abAb abbBbb abbbb 92
  • 94. Def: G =(V,T,P,S) An ordered tree is a derivation tree for G iff 1 The root is labeled S 2 every leaf has a label from T { } 3 Every interior vertex has a label from V 4 If a vertex has label A(variable) and its children are labeled from (L to R) a1,a2,…an then P must contain a production A a1 a2 …an 5 A leaf labeled has no sibling. 94
  • 95. Yield: The string of terminals obtained by reading the leaves of the tree from left to right omitting any ’s encountered is called yield of the tree. 95
  • 96. Partial derivation tree A tree that has properties 3,4,5 but 1 need not And 2 is replaced by V T { } 96
  • 97. S AB A aaA | B Bb | S AB S A B 97
  • 98. S AB A aaA | B Bb | S AB aaAB S A B a a A 98
  • 99. S AB A aaA | B Bb | S AB aaAB aaABb S A B a a A B b 99
  • 100. S AB A aaA | B Bb | S AB aaAB aaABb aaBb S A B a a A B b 100
  • 101. S AB A aaA | B Bb | S AB aaAB aaABb aaBb aab Derivation Tree S A B a a A B b 101
  • 102. S AB A aaA | B Bb | S AB aaAB aaABb aaBb aab Derivation Tree S A B yield a a A B b aa b aab 102
  • 103. Partial Derivation Trees S AB A aaA | B Bb | S AB Partial derivation tree S A B 103
  • 104. S AB aaAB Partial derivation tree S A B a a A 104
  • 105. sentential S AB aaAB form Partial derivation tree S A B yield a a A aaAB 105
  • 106. Sometimes, derivation order doesn’t matter Leftmost: S AB aaAB aaB aaBb aab Rightmost: S AB ABb Ab aaAb aab S Same derivation tree A B a a A B b 106
  • 107. Ambiguity 107
  • 108. E E E | E E | (E) | a a a a E E E E a E a E E a a E a a*a E E leftmost derivation a E E a a 108
  • 109. E E E | E E | (E) | a a a a E E E E E E a E E E a a E a a a E E leftmost derivation E E a a 109 a
  • 110. E E E | E E | (E) | a a a a Two derivation trees E E E E E E a E E E E a a a a 110 a
  • 111. The grammarE E E | E E | (E) | a is ambiguous: string a a a has two derivation trees E E E E E E a E E E E a a a a a 111
  • 112. The grammarE E E | E E | (E) | a is ambiguous: string a a a has two leftmost derivations E E E a E a E E a a E a a*a E E E E E E a E E a a E a a a 112
  • 113. Definition: A context-free grammar G is ambiguous if some string w L(G ) has: two or more derivation trees 113
  • 114. In other words: A context-free grammar G is ambiguous if some string w L(G ) has: two or more leftmost derivations (or rightmost) 114
  • 115. Why do we care about ambiguity? a a a take a 2 E E E E E E a E E E E a a a a 115 a
  • 116. 2 2 2 E E E E E E 2 E E E E 2 2 2 2 116 2
  • 117. 2 2 2 6 2 2 2 8 6 8 E E 2 4 4 2 E E E E 2 2 2 2 2 E E E E 2 2 2 2 117 2
  • 118. Correct result: 2 2 2 6 6 E 2 4 E E 2 2 2 E E 2 2 118
  • 119. • Ambiguity is bad for programming languages • We want to remove ambiguity 119
  • 120. We fix the ambiguous grammar: E E E | E E | (E) | a New non-ambiguous grammar: E E T E T T T F T F F (E) 120 F a
  • 121. E E T T T F T a T a T F a F F a a F a a a E a a a E E T E T E T T T F T T F T F F F a F (E) F a a a 121
  • 122. Unique derivation tree E a a a E T T T F F F a a a 122
  • 123. The grammar G: E E T E T T T F T F F (E) F a is non-ambiguous: Every string w L (G ) has a unique derivation tree 123
  • 124. Inherent Ambiguity Some context free languages have only ambiguous grammars Example: n n m n m m L {a b c } {a b c } S S1 | S2 S1 S1c | A S2 aS2 | B A aAb | B bBc | 124
  • 125. n n n The string a b c has two derivation trees S S S1 S2 S1 c a S2 125
  • 126. Compiler Lexical parser analyzer input output machine program code 126
  • 127. A parser knows the grammar of the programming language 127
  • 128. The parser finds the derivation of a particular input derivation Parser input E => E + E E -> E + E => E + E * E 10 + 2 * 5 |E*E => 10 + E*E | INT => 10 + 2 * E => 10 + 2 * 5 128
  • 129. derivation tree derivation E E => E + E E + E => E + E * E => 10 + E*E 10 E * E => 10 + 2 * E => 10 + 2 * 5 2 5 129