Compiler design miscellaneous


Compiler design miscellaneous

  1. Consider the syntax directed translation scheme (SDTS) given in the following. Assume attribute evaluation with bottom-up parsing, i.e., attributes are evaluated immediately after a reduction.
    E → E1 * T {E.val = E1 .val * T. val}
    E → T {E.val = T. val}
    T → F – T1 {T.val = F.val – T1. val}
    T → F {T.val = F. val}
    F → 2 {F.val = 2}
    F → 4 {F.val = 4}
    (a) Using this SDTS, construct a parse tree for the expression 4 – 2 – 4 * 2 and also compute its E.val.
    (b) It is required to compute the total number of reductions performed to parse a given input. Using synthesized attributes only, modify the SDTS given, without changing the grammar, to find E.red, the number of reductions performed while reducing an input to E.









  1. View Hint View Answer Discuss in Forum

    (a) E.val = 12
    (b) E → E1 * T {E.red = E1 .red + T. red + 1}
    E → T {E.red = T.red + 1}
    T → F – T1 {T.red = F.red + T1 .red + 1}
    T → F {T.red = F.red + 1}
    F → 2 {F.red = 1}
    F → 4 {F.red = 1}

    Correct Option: A

    (a) E.val = 12
    (b) E → E1 * T {E.red = E1 .red + T. red + 1}
    E → T {E.red = T.red + 1}
    T → F – T1 {T.red = F.red + T1 .red + 1}
    T → F {T.red = F.red + 1}
    F → 2 {F.red = 1}
    F → 4 {F.red = 1}


  1. Which of the following derivations does a top-down parser use while parsing an input string? The input is assumed to be scanned in left to right order.









  1. View Hint View Answer Discuss in Forum

    In top down parsing, we just start with the start symbol and compare the RHS of the different productions against the first piece of input to see which of the productions should be used. A topdown parser is called LL parser because it parses the input from Left to right. eg:
    S → Ax
    A → a
    For an input ax Top down parser will do parsing as

    Correct Option: A

    In top down parsing, we just start with the start symbol and compare the RHS of the different productions against the first piece of input to see which of the productions should be used. A topdown parser is called LL parser because it parses the input from Left to right. eg:
    S → Ax
    A → a
    For an input ax Top down parser will do parsing as



  1. Given the following expression grammar
    E → E * F | F + E | F
    E → F – | id
    Which of the following is true?









  1. View Hint View Answer Discuss in Forum

    ‘–’ has higher precedence than ‘*’ as ‘–’ is a leaf node in the tree formed using this whereas ‘*’ is not.

    Correct Option: B

    ‘–’ has higher precedence than ‘*’ as ‘–’ is a leaf node in the tree formed using this whereas ‘*’ is not.


  1. Consider the following C code segment :
    for (i = 0, i < n; i ++) {
    for (j = 0, j < n; j ++) {
    if (i % 2){
    x + = (4 * j + 5 * i);
    Y + = (7 + 4 * j);}}}
    Which one of the following is false?









  1. View Hint View Answer Discuss in Forum

    The expression (4*j) is used at two places- so common sub-expression elimination is possible i% 2 is loop invariant for the inner loop
    5*i is also loop invariant for inner loop x + = 5*i can be replaced by x + = p;
    p + = 5; (p must be initialized to 0 before the loop).
    Thus replacing * with + gives strength reduction. So, only (d) is false here.

    Correct Option: D

    The expression (4*j) is used at two places- so common sub-expression elimination is possible i% 2 is loop invariant for the inner loop
    5*i is also loop invariant for inner loop x + = 5*i can be replaced by x + = p;
    p + = 5; (p must be initialized to 0 before the loop).
    Thus replacing * with + gives strength reduction. So, only (d) is false here.



  1. Consider the grammar shonws below :
    S → CC
    C → cc/ d
    This grammar is









  1. View Hint View Answer Discuss in Forum

    First (S) = First (C) = {c,d} there are no multiple in single row of parsing table hence grammar is LL1
    Note : if we have A → B/C for grammar to be LL(1) first(B) intersection First(C) should be null otherwise grammar is not LL1. If First(B) contains epsilon then Follow(A) intersection First(C) should be null. Using this we can say grammar is LL(1) or not without constructing parsing table.
    An ∈ ∈ free LL(1) grammar is also SLR(1) and hence LALR(1) and LR(1) too.
    Since there is no conflict, the grammar is LL(1). We can construct a predictive parse table with no conflicts. This grammar also LR(0), SLR(1), CLR(1) and LALR(1).

    Correct Option: A

    First (S) = First (C) = {c,d} there are no multiple in single row of parsing table hence grammar is LL1
    Note : if we have A → B/C for grammar to be LL(1) first(B) intersection First(C) should be null otherwise grammar is not LL1. If First(B) contains epsilon then Follow(A) intersection First(C) should be null. Using this we can say grammar is LL(1) or not without constructing parsing table.
    An ∈ ∈ free LL(1) grammar is also SLR(1) and hence LALR(1) and LR(1) too.
    Since there is no conflict, the grammar is LL(1). We can construct a predictive parse table with no conflicts. This grammar also LR(0), SLR(1), CLR(1) and LALR(1).