Syntax-Directed Translation Guide
Syntax-Directed Translation Guide
   ✫                                                        ✪
Lect 9                                             Goutam Biswas
   ✬                                                                                  ✩
Compiler Design                              IIIT Kalyani, WB                          2
Translation
   ✫                                                                                  ✪
Lect 9                                                                      Goutam Biswas
   ✬                                                         ✩
Compiler Design                 IIIT Kalyani, WB               3
Translation
Example
   ✫                                                                                         ✪
                 have used subscript to differentiate between two instances of E.
Example: Calculator
Note
Note
         E → E1 + T
           {
               E.exp=(char*)malloc(strlen(E1.exp)+
                                   strlen(T.exp)+4);
               strcpy(E.exp, E1.exp); strcat(E.exp, " ");
               strcat(E.exp, T.exp);
               strcat(E.exp, " + ");
               free(E1.exp); free(T.exp);
           }
         Again there is no side-effect
   ✫                                                            ✪
Lect 9                                                 Goutam Biswas
   ✬                                                        ✩
Compiler Design                IIIT Kalyani, WB              10
   ✫                                                       ✪
Lect 9                                            Goutam Biswas
   ✬                                                                        ✩
Compiler Design                                 IIIT Kalyani, WB             12
   ✫                                                                        ✪
                  are also called virtual registers.
Note
   ✫                                                       ✪
Lect 9                                            Goutam Biswas
   ✬                                                            ✩
Compiler Design                 IIIT Kalyani, WB                 15
Associating Information
Definition
Definition
         • A syntax-directed translation is an
           executable specification of SDD. Fragments
           of programs are associated to different points
           in the production rules.
         • The order of execution of the code is
           important in this case.
   ✫                                                         ✪
Lect 9                                              Goutam Biswas
   ✬                                                           ✩
Compiler Design                 IIIT Kalyani, WB                18
Example
   ✫                                                           ✪
Lect 9                                                Goutam Biswas
   ✬                                                       ✩
Compiler Design               IIIT Kalyani, WB              19
Note
   ✫                                                       ✪
Lect 9                                            Goutam Biswas
   ✬                                                        ✩
Compiler Design              IIIT Kalyani, WB                20
Note
   ✫                                                        ✪
Lect 9                                             Goutam Biswas
   ✬                                                     ✩
Compiler Design              IIIT Kalyani, WB             21
Note
   ✫                                                     ✪
Lect 9                                          Goutam Biswas
   ✬                                                              ✩
Compiler Design                 IIIT Kalyani, WB                   22
A Simple Example
   ✫                                                            ✪
Lect 9                                                 Goutam Biswas
   ✬                                               ✩
Compiler Design        IIIT Kalyani, WB             24
                  0 : S′ → N $
                  1: N → SL
                  2: S → +
                  3: S → −
                  4: L → LB
                  5: L → B
                  6: B → 0
                  7: B → 1
   ✫                                               ✪
Lect 9                                    Goutam Biswas
   ✬                                                        ✩
Compiler Design                IIIT Kalyani, WB              25
Note
   ✫                                                        ✪
Lect 9                                             Goutam Biswas
   ✬                                                                      ✩
Compiler Design                      IIIT Kalyani, WB                      26
LR(0) Automaton
                  q0 : S ′ → •N $    N → •SL            S → •+
                       S → •−
                  q1 : S ′ → N • $
                  q2 : N → S • L L → •LB                L → •B
                       B → •0        B → •1
                  q3 : S → +•
                  q4 : S → −•
                  q5 : N → SL•       L → L • B B → •0
                       B → •1
   ✫                                                                      ✪
Lect 9                                                           Goutam Biswas
   ✬                                                 ✩
Compiler Design          IIIT Kalyani, WB             27
LR(0) Automaton
                  q6 : L → B•
                  q7 : B → 0•
                  q8 : B → 1•
                  q9 : L → LB•
   ✫                                                 ✪
Lect 9                                      Goutam Biswas
   ✬                                                                     ✩
Compiler Design                      IIIT Kalyani, WB                     28
                  S         Action                 Goto
                      + −    0   1      $     N S L B
                  0 s3 s4                     1    2
                  1                   Acc
                  2          s7 s8                      5   6
                  3          r6 r6
                  4          r7 r7
                  5          s7 s8     r1                   9
   ✫                                                                     ✪
Lect 9                                                          Goutam Biswas
   ✬                                                            ✩
Compiler Design                     IIIT Kalyani, WB             29
                  S     Action                  Goto
                      + −   0   1      $   N S L B
                  6         r5 r5 r5
                  7         r6 r6 r6
                  8         r7 r7 r7
   ✫                                                            ✪
Lect 9                                                 Goutam Biswas
   ✬                                                                ✩
Compiler Design                  IIIT Kalyani, WB                    30
Attributes of Non-Terminals
SDD
         0 : S′ → N     print N.val
         1 : N → SL     if (S.sign == ’-’) N.val= - L.val;
                        else N.val = L.val;
         2: S     → +   S.sign = ’+’;
         3: S     → −   S.sign = ’-’;
         4 : L → L1 B L.val = 2*L1.val+B.val;
         5: L → B       L.val = B.val;
         6: B → 0       B.val = 0;
         7: B → 1       B.val = 1;
   ✫                                                        ✪
Lect 9                                             Goutam Biswas
   ✬                                                                    ✩
Compiler Design                     IIIT Kalyani, WB                     32
   ✫                                                                    ✪
Lect 9                                                         Goutam Biswas
   ✬                                                                     ✩
Compiler Design                           IIIT Kalyani, WB                36
N N.val=L1.val
                           + L2               B2 B2.val=1
         L2.val=2*L3.val+B1.val
L3.val=B0.val L3 B1 1 B1.val=0
B0.val=1 B0 0
                                   1
   ✫                                                                     ✪
Lect 9                                                          Goutam Biswas
   ✬                                                                      ✩
Compiler Design                               IIIT Kalyani, WB             37
Synthesized Attribute
   ✫                                                                      ✪
Lect 9                                                           Goutam Biswas
   ✬                                                          ✩
Compiler Design                 IIIT Kalyani, WB               38
S-Attributed
   ✫                                                          ✪
Lect 9                                               Goutam Biswas
   ✬                                                                ✩
Compiler Design                   IIIT Kalyani, WB                   39
   ✫                                                                ✪
Lect 9                                                     Goutam Biswas
   ✬                                                        ✩
Compiler Design                IIIT Kalyani, WB              40
SDD
         0 : S′ → N     print N.val
         1 : N → SL     L.pos = 0
                        if (S.sign == ’-’) N.val= - L.val;
                        else N.val = L.val;
         2: S     → +   S.sign = ’+’;
         3: S     → −   S.sign = ’-’;
         4 : L → L1 B L1 .pos = L.pos+1;
                        B.pos = L.pos;
                        L.val = L1 .val+B.val;
   ✫                                                        ✪
Lect 9                                             Goutam Biswas
   ✬                                                      ✩
Compiler Design               IIIT Kalyani, WB             41
SDD
                  5 : L → B B.pos = L.pos;
                              L.val = B.val;
                  6: B → 0    B.val = 0;
                  7: B → 1    B.val = 2B.pos ;
   ✫                                                      ✪
Lect 9                                           Goutam Biswas
   ✬                                                        ✩
Compiler Design                IIIT Kalyani, WB              42
Exercise
         Draw the parse tree for −101 and show the flow
         of information.
   ✫                                                        ✪
Lect 9                                             Goutam Biswas
   ✬                                                       ✩
Compiler Design               IIIT Kalyani, WB              43
Note
   ✫                                                       ✪
Lect 9                                            Goutam Biswas
   ✬                                                           ✩
Compiler Design                 IIIT Kalyani, WB                44
Exercise
                         1: N → L
                         2: L → LB
                         3: L → B
                         4: B → 0
                         5: B → 1
          Associate appropriate attributes to the
         non-terminals and give rules of semantic
         actions. Write bison specification for the
         grammar.
   ✫                                                           ✪
Lect 9                                                Goutam Biswas
   ✬                                                        ✩
Compiler Design                IIIT Kalyani, WB              45
Example
   ✫                                                        ✪
Lect 9                                             Goutam Biswas
   ✬                                               ✩
Compiler Design        IIIT Kalyani, WB             46
                  0 : S′ → N
                  1: N → SL
                  2: S → +
                  3: S → −
                  4: L → BL
                  5: L → B
                  6: B → 0
                  7: B → 1
   ✫                                               ✪
Lect 9                                    Goutam Biswas
   ✬                                                                 ✩
Compiler Design                    IIIT Kalyani, WB                   47
Attributes of Non-Terminals
   ✫                                                                 ✪
Lect 9                                                      Goutam Biswas
   ✬                                                        ✩
Compiler Design                IIIT Kalyani, WB              48
         0 : S′ → N     print N.val
         1 : N → SL     if (S.sign == ’-’) N.val=- L.val;
                        else N.val = L.val;
         2: S     → +   S.sign = ’+’;
         3: S     → −   S.sign = ’-’;
         4 : L → BL1 if(B.val)
                          L.val=pow(2,L1.pos)+L1.val;
                        else L.val=L1.val;
                        L.pos=L1.pos+1;
   ✫                                                        ✪
Lect 9                                             Goutam Biswas
   ✬                                                        ✩
Compiler Design                 IIIT Kalyani, WB             49
   ✫                                                        ✪
Lect 9                                             Goutam Biswas
   ✬                                                        ✩
Compiler Design                IIIT Kalyani, WB              50
Example
Parse Tree
   ✫                                                        ✪
Lect 9                                             Goutam Biswas
   ✬                                                       ✩
Compiler Design           IIIT Kalyani, WB                  52
T L ;
double L , id
                          id
   ✫                                                       ✪
Lect 9                                            Goutam Biswas
   ✬                                                                                        ✩
Compiler Design                                IIIT Kalyani, WB                             53
Note
   ✫                                                                                        ✪
Lect 9                                                                            Goutam Biswas
   ✬                                                        ✩
Compiler Design                IIIT Kalyani, WB              54
SDDefinition
           1 : D → T L;      L.type = T.type
           2: T   → int      T.type = INT
           3: T   → double T.type = DOUBLE
           4 : L → L1 , id   L1.type = L.type
                             addSym(id.name, L.type)
           5 : L → id        addSym(id.name, L.type)
   ✫                                                        ✪
Lect 9                                             Goutam Biswas
   ✬                                                        ✩
Compiler Design                 IIIT Kalyani, WB             55
Inherited Attribute
   ✫                                                        ✪
Lect 9                                             Goutam Biswas
   ✬                                                         ✩
Compiler Design                  IIIT Kalyani, WB             56
S-Attributed Definitions
   ✫                                                         ✪
Lect 9                                              Goutam Biswas
   ✬                                                           ✩
Compiler Design                  IIIT Kalyani, WB               57
L-Attributed Definitions
L-Attributed Grammar
   ✫                                                                     ✪
Lect 9                                                          Goutam Biswas
   ✬                                                                   ✩
Compiler Design                      IIIT Kalyani, WB                   59
Rules
             1 : D → T L;          L.type = T.type
             2: T    → int         T.type = INT
             3: T    → double T.type = DOUBLE
             4 : L → L1 , id       L1.type = L.type
                                   addSym(id.name, L.type)
             5 : L → id            addSym(id.name, L.type)
   ✫                                                                   ✪
Lect 9                                                        Goutam Biswas
   ✬                                                        ✩
Compiler Design                IIIT Kalyani, WB              60
Note
   ✫                                                        ✪
Lect 9                                             Goutam Biswas
   ✬                                                          ✩
Compiler Design                 IIIT Kalyani, WB               61
Solution I
   ✫                                                          ✪
Lect 9                                               Goutam Biswas
   ✬                                                            ✩
Compiler Design                  IIIT Kalyani, WB                62
Solution II
   ✫                                                            ✪
Lect 9                                                 Goutam Biswas
   ✬                                                        ✩
Compiler Design                IIIT Kalyani, WB              63
         1 : D → T L;        initType(L.list, T.type)
         2 : T → int         T.type = INT
         3 : T → double T.type = DOUBLE
         4 : L → L1 , id     L.list = L 1.list +
                             mklist(addSym(id.name))
         5 : L → id          L.list =
                             mklist(addSym(id.name))
         Read ‘+’ as append in the list.
   ✫                                                        ✪
Lect 9                                             Goutam Biswas
   ✬                                                         ✩
Compiler Design                 IIIT Kalyani, WB              64
Solution III
   ✫                                                         ✪
Lect 9                                              Goutam Biswas
   ✬                                                                    ✩
Compiler Design                  IIIT Kalyani, WB                        65
LR(0) Automaton
              q0 : S → •D        D → •T L;          T → •int
                  T → •double
              q1 : S → D•
              q2 : D → T • L     L → •L, id L → •id
              q3 : T → int•
              q4 : S → double•
              q5 : D → T L•;     L → L•, id
              q6 : L → id•
   ✫                                                                    ✪
Lect 9                                                         Goutam Biswas
   ✬                                                  ✩
Compiler Design           IIIT Kalyani, WB             66
LR(0) Automaton
                  q7 : L → L, •id
                  q8 : L → L, id•
                  q9 : D → T L; •
   ✫                                                  ✪
Lect 9                                       Goutam Biswas
   ✬                                                                  ✩
Compiler Design                  IIIT Kalyani, WB                      67
   ✫                                                                  ✪
Lect 9                                                       Goutam Biswas
   ✬                                                        ✩
Compiler Design                IIIT Kalyani, WB              70
Note
   ✫                                                        ✪
Lect 9                                             Goutam Biswas
   ✬                                                         ✩
Compiler Design                IIIT Kalyani, WB               71
Note
   ✫                                                         ✪
Lect 9                                              Goutam Biswas
   ✬                                                              ✩
Compiler Design                  IIIT Kalyani, WB                  72
             S → E$          { print E.val }
            E → E1 + T { E.val = E1.val + T.val}
            E → T            { E.val = T.val}
            T     → T1 ∗ F   { T.val = T1.val * F.val}
            T     → F        { T.val = F.val}
            F → (F )         { F.val = E.val}
            F → ic           { F.val = ic.val}
   ✫                                                              ✪
Lect 9                                                   Goutam Biswas
   ✬                                                                                     ✩
Compiler Design                              IIIT Kalyani, WB                             73
                  E0 .s = E1 .s + T1 .s          E0
              E1 .s = E2 .s + T2 .s
                                           E1                   T1       T1 .s = 4
                                                   +
                                                 T2 .s = 3
                                                      T2        F1       F1 .s = 4
                      E2 .s = 2   E2           +
                                          F2 .s = 3
                                                      F2
                     T3 .s = 2    T3                             ic ic.val=4
                     F3 .s = 2    F3         ic.val=3 ic
                                                                     4
ic.val=2 ic 3
   ✫                                                                                     ✪
Lect 9                                                                          Goutam Biswas
   ✬                                                     ✩
Compiler Design              IIIT Kalyani, WB             74
                        S   → E$
                        E   → T E′
                        E ′ → +T E ′
                        E′ → ε
                        T   → FT′
                        T ′ → ∗F T ′
                        T′ → ε
                        F   → (E)
                        F   → ic
   ✫                                                     ✪
Lect 9                                          Goutam Biswas
   ✬                                                                              ✩
Compiler Design                        IIIT Kalyani, WB                            75
E0
T3 E1′
                       T3′        +                 E2′
                  F3                      T2
                       ε                        +              E3′
                                                      T1
                  ic              F2      T2′
                                                                     ε
                                                      F1       T1′
                                  ic       ε
                  2
                                                          ic     ε
                                  3
   ✫                                                                              ✪
Lect 9                                                                   Goutam Biswas
   ✬                                                                                        ✩
Compiler Design                              IIIT Kalyani, WB                                76
E0
T3 E1′
                           T3′               +                     E2′
          F3 .s = 2   F3                             T2
                           ε                               +                 E3′
                                                                       T1
                                             F2      T2′
             ic.val=2 ic
                                 F2 .s = 3                                          ε
                                                                       F1    T1′
                                   ic.val=3 ic        ε
                      2                                    F1 .s = 4
                                                               ic.val=4 ic     ε
                                              3
   ✫                                                                                        ✪
Lect 9                                                                             Goutam Biswas
   ✬                                                           ✩
Compiler Design                 IIIT Kalyani, WB                77
Note
Note
   ✫                                                           ✪
Lect 9                                                Goutam Biswas
   ✬                                                               ✩
Compiler Design                    IIIT Kalyani, WB                 79
         E    → T { E’.ival = T.sval } E ′
                    { E.sval = E’.sval }
         E ′ → +T { E1’.ival = E’.ival + T.sval } E1′
                    { E’.sval = E1’.sval }
         E ′ → ε { E’.sval = E’.ival }
   ✫                                                               ✪
Lect 9                                                    Goutam Biswas
   ✬                                                               ✩
Compiler Design                    IIIT Kalyani, WB                 80
         T        → F { T’.ival = F.sval } T ′
                     { T.sval = T’.sval }
         T ′ → ∗F { T1’.ival = T’.ival * F.sval } T1′
                     { T’.sval = T1’.sval }
         T ′ → ε { T’.sval = T’.ival }
   ✫                                                               ✪
Lect 9                                                    Goutam Biswas
   ✬                                                               ✩
Compiler Design                    IIIT Kalyani, WB                 81
   ✫                                                               ✪
Lect 9                                                    Goutam Biswas
   ✬                                                                                                  ✩
Compiler Design                                      IIIT Kalyani, WB                                   82
                                      E0       E0′ .s = E1′ .s = 9
                                                        E1′ .i = T3 .s = 2
         T3 .s = T3′ .s = 2   T3                        E1′        E1′ .s = E2′ .s = 9
                              T3′ .i = F3 .s = 2                     E2′ .i = E1′ .i + T2 .s = 2 + 3 = 5
                              T3′               +                   E2′       E2′ .s = E3′ .s = 9
         F3 .s = 2   F3                                 T2
                              T3′ .s = T3′ .i = 2                               E3′′.i = E2′ .i + T1 .s = 5 + 4 = 9
                              ε                              +                   E3
                                                           ′           T1
                                                F2      T2                              E3′ .s = E3′ .i = 9
            ic.val=2 ic
                                     F2 .s = 3                                            ε
                                                                       F1        T1 ′
                                      ic.val=3 ic        ε
                     2                                       F1 .s = 4
                                                               ic.val=4 ic          ε
                                                3
   ✫                                                                                                  ✪
Lect 9                                                                                     Goutam Biswas
   ✬                                                                               ✩
Compiler Design                            IIIT Kalyani, WB                         83
         Non                                      Terminal
         Term.         +               ∗            (           )         ic          $
          E                                      E → T E′              E → T E′
          T                                      T → FT′               T → FT′
          F                                      F → (E)                F → ic
          E′      E ′ → +T E ′                                E′ → ε               E′ → ε
          T′        T′ → ε        T ′ → ∗F T ′                T′ → ε               T′ → ε
   ✫                                                                               ✪
Lect 9                                                                    Goutam Biswas
   ✬                                                         ✩
Compiler Design                IIIT Kalyani, WB               84
Example
   ✫                                                         ✪
Lect 9                                              Goutam Biswas
   ✬                                                                  ✩
Compiler Design                    IIIT Kalyani, WB                    85
Left-most Derivation
         1        E                 10 → ic + ic ∗ ic T ′ E ′
         2 → T E′                   11 → ic + ic ∗ ic ε E ′
         3 → F T ′E ′               12 →          ic + ic ∗ ic
         4 → ic T ′ E ′
         5 → ic ε E ′
         6 → ic + T E ′
         7 → ic + F T ′ E ′
         8 → ic + ic T ′ E ′
         9 → ic + ic ∗ F T ′ E ′
   ✫                                                                  ✪
Lect 9                                                       Goutam Biswas
   ✬                                                                                ✩
Compiler Design                              IIIT Kalyani, WB                       86
         Start
                 Parsing Stack   Input                  Parsing Stack   Input
                 $E              ic + ic * ic $         $ E’ T’ F       ic * ic $
$ E’ T ic + ic * ic $ $ E’ T’ ic * ic $
$ E’ T’ F ic + ic * ic $ $ E’ T’ * ic $
$ E’ T’ ic ic + ic * ic $ $ E’ T’ F * ic $
$ E’ T’ + ic * ic $ $ E’ T’ F ic $
$ E’ + ic * ic $ $ E’ T’ ic ic $
$ E’ T + + ic * ic $ $ E’ T’ $
$ E’ T ic * ic $ $ E’ $
End $ $
   ✫                                                                                ✪
Lect 9                                                                   Goutam Biswas
   ✬                                                                ✩
Compiler Design                   IIIT Kalyani, WB                   87
   ✫                                                                ✪
Lect 9                                                     Goutam Biswas
   ✬                                                                ✩
Compiler Design                   IIIT Kalyani, WB                   88
   ✫                                                                ✪
Lect 9                                                     Goutam Biswas
   ✬                                                                ✩
Compiler Design                   IIIT Kalyani, WB                   89
Another Example
IS → if BE then S1 else S2 .
   ✫                                                       ✪
Lect 9                                            Goutam Biswas
   ✬                                                         ✩
Compiler Design                IIIT Kalyani, WB               91
Attributes of Statement
         IS → if BE         l1=newLabel(), l2=newLabel()
                  then S1   BE.true = l1, BE.false=l2
                  else S2 . S1 .next = S2 .next = IS.next
                            IS.code = BE.code + l1’:’ +
                            S1 .code + l2’:’ + S2 .code
   ✫                                                               ✪
Lect 9                                                    Goutam Biswas
   ✬                                                              ✩
Compiler Design                   IIIT Kalyani, WB                 94
         IS → if              {l1=newLabel(),l2=newLabel()
                              BE.true = l1, BE.false=l2}
                  BE          {S1 .next = IS.next }
                  then S1     {S2 .next = IS.next }
                  else S2 .
                              {IS.code = BE.code + l1’:’ +
                              S1 .code + l2’:’ + S2 .code}
   ✫                                                              ✪
Lect 9                                                   Goutam Biswas
   ✬                                                        ✩
Compiler Design                IIIT Kalyani, WB              95
Note
   ✫                                                        ✪
Lect 9                                             Goutam Biswas
   ✬                                                          ✩
Compiler Design                 IIIT Kalyani, WB               96
   ✫                                                          ✪
Lect 9                                               Goutam Biswas
   ✬                                                               ✩
Compiler Design                 IIIT Kalyani, WB                    97
   ✫                                                               ✪
Lect 9                                                    Goutam Biswas
   ✬                                                           ✩
Compiler Design                IIIT Kalyani, WB                 98
         BE →               { BE1 .true=l=newLabel()
                            BE1 .false = BE.false }
                  BE1 and
                            { BE2 .true = BE.true
                            BE2 .false = BE.false }
                  BE2
                            { BE.code = BE1 .code +
                            l’:’ + BE2 .code }
   ✫                                                           ✪
Lect 9                                                Goutam Biswas
   ✬                                                         ✩
Compiler Design                 IIIT Kalyani, WB              99
Note
   ✫                                                         ✪
Lect 9                                              Goutam Biswas
   ✬                                                          ✩
Compiler Design                IIIT Kalyani, WB               100
Rules
             1 : E → { putchar(’+’); } E1 + T
             2: E → T
             3: T   → { putchar(’*’); } T1 ∗ F
             4: T   → F
             5 : F → (E)
             6 : F → ic {printf(" \%d ", ic.val);}
   ✫                                                          ✪
Lect 9                                               Goutam Biswas
   ✬                                                                                            ✩
Compiler Design                                  IIIT Kalyani, WB                               101
Note
   ✫                                                                                            ✪
Lect 9                                                                                 Goutam Biswas