SlideShare une entreprise Scribd logo
1  sur  129
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

Contenu connexe

Tendances

Semantics analysis
Semantics analysisSemantics analysis
Semantics analysisBilalzafar22
 
Regular expressions-Theory of computation
Regular expressions-Theory of computationRegular expressions-Theory of computation
Regular expressions-Theory of computationBipul Roy Bpl
 
Lecture 3,4
Lecture 3,4Lecture 3,4
Lecture 3,4shah zeb
 
Regular language and Regular expression
Regular language and Regular expressionRegular language and Regular expression
Regular language and Regular expressionAnimesh Chaturvedi
 
Relational algebra.pptx
Relational algebra.pptxRelational algebra.pptx
Relational algebra.pptxRUpaliLohar
 
Deterministic Finite Automata (DFA)
Deterministic Finite Automata (DFA)Deterministic Finite Automata (DFA)
Deterministic Finite Automata (DFA)Animesh Chaturvedi
 
Chapter1 Formal Language and Automata Theory
Chapter1 Formal Language and Automata TheoryChapter1 Formal Language and Automata Theory
Chapter1 Formal Language and Automata TheoryTsegazeab Asgedom
 
Theory of automata and formal language
Theory of automata and formal languageTheory of automata and formal language
Theory of automata and formal languageRabia Khalid
 
Chomsky classification of Language
Chomsky classification of LanguageChomsky classification of Language
Chomsky classification of LanguageDipankar Boruah
 
Regular Expression to Finite Automata
Regular Expression to Finite AutomataRegular Expression to Finite Automata
Regular Expression to Finite AutomataArchana Gopinath
 
Intro automata theory
Intro automata theory Intro automata theory
Intro automata theory Rajendran
 
Turing Machine
Turing MachineTuring Machine
Turing MachineAyAn KhAn
 
NFA or Non deterministic finite automata
NFA or Non deterministic finite automataNFA or Non deterministic finite automata
NFA or Non deterministic finite automatadeepinderbedi
 

Tendances (20)

Semantics analysis
Semantics analysisSemantics analysis
Semantics analysis
 
Regular expressions-Theory of computation
Regular expressions-Theory of computationRegular expressions-Theory of computation
Regular expressions-Theory of computation
 
Lecture 3,4
Lecture 3,4Lecture 3,4
Lecture 3,4
 
Regular language and Regular expression
Regular language and Regular expressionRegular language and Regular expression
Regular language and Regular expression
 
Relational algebra.pptx
Relational algebra.pptxRelational algebra.pptx
Relational algebra.pptx
 
Deterministic Finite Automata (DFA)
Deterministic Finite Automata (DFA)Deterministic Finite Automata (DFA)
Deterministic Finite Automata (DFA)
 
Chapter1 Formal Language and Automata Theory
Chapter1 Formal Language and Automata TheoryChapter1 Formal Language and Automata Theory
Chapter1 Formal Language and Automata Theory
 
Theory of automata and formal language
Theory of automata and formal languageTheory of automata and formal language
Theory of automata and formal language
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 
Finite automata
Finite automataFinite automata
Finite automata
 
Finite Automata
Finite AutomataFinite Automata
Finite Automata
 
Chomsky classification of Language
Chomsky classification of LanguageChomsky classification of Language
Chomsky classification of Language
 
Regular Expression to Finite Automata
Regular Expression to Finite AutomataRegular Expression to Finite Automata
Regular Expression to Finite Automata
 
Intro automata theory
Intro automata theory Intro automata theory
Intro automata theory
 
Turing Machine
Turing MachineTuring Machine
Turing Machine
 
Regular Grammar
Regular GrammarRegular Grammar
Regular Grammar
 
Push down automata
Push down automataPush down automata
Push down automata
 
Context free grammar
Context free grammarContext free grammar
Context free grammar
 
NFA or Non deterministic finite automata
NFA or Non deterministic finite automataNFA or Non deterministic finite automata
NFA or Non deterministic finite automata
 
Context free grammar
Context free grammar Context free grammar
Context free grammar
 

En vedette

Deterministic context free grammars &non-deterministic
Deterministic context free grammars &non-deterministicDeterministic context free grammars &non-deterministic
Deterministic context free grammars &non-deterministicLeyo Stephen
 
Context free languages
Context free languagesContext free languages
Context free languagesJahurul Islam
 
Lecture: Context-Free Grammars
Lecture: Context-Free GrammarsLecture: Context-Free Grammars
Lecture: Context-Free GrammarsMarina Santini
 
Context free grammars
Context free grammarsContext free grammars
Context free grammarsRonak Thakkar
 
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 SzegediZeroTurnaround
 
Argon ion lasers
Argon ion lasersArgon ion lasers
Argon ion lasersajay singh
 
Spanish grammer book
Spanish grammer bookSpanish grammer book
Spanish grammer bookDiegoTspanish
 
Context free grammars
Context free grammarsContext free grammars
Context free grammarsShiraz316
 
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 PowerpointAaron Liebo
 

En vedette (18)

Deterministic context free grammars &non-deterministic
Deterministic context free grammars &non-deterministicDeterministic context free grammars &non-deterministic
Deterministic context free grammars &non-deterministic
 
Context free languages
Context free languagesContext free languages
Context free languages
 
Lecture: Context-Free Grammars
Lecture: Context-Free GrammarsLecture: Context-Free Grammars
Lecture: Context-Free Grammars
 
Context free grammars
Context free grammarsContext free grammars
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
 
Context free grammars
Context free grammarsContext free grammars
Context free grammars
 
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
 

Similaire à Grammar

Similaire à 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
 

Plus de lavishka_anuj

Plus de 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
 

Dernier

Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajanpragatimahajan3
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
The byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxThe byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxShobhayan Kirtania
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...fonyou31
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...Sapna Thakur
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room servicediscovermytutordmt
 

Dernier (20)

Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
The byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxThe byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptx
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 

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