Theory of computation miscellaneous


Theory of computation miscellaneous

Theory of Computation

  1. Consider the following expression grammar G:
    E → E - T | T
    T → T + F | F
    F → (E) | id
    Which of the following grammars is not left recursive, but is equivalent to G?









  1. View Hint View Answer Discuss in Forum

    E → E - T |
    T → T + F | F
    F → ( E ) | id
    Using the rule for removal of left recursion is
    A → Aα / β
    A → βA '
    A ' → α A ' / ∈
    Then, the given grammar is written as :-
    E' → −TE' / ∈
    E → +TE '
    T ' → + FT ' / ∈
    T → FT '
    F → (E) | id
    Now by putting E ' as X and T ' as Y, then
    X → −TX / ∈
    E → TX
    Y → +FY / ∈
    T → FY
    F → (E) | id
    Hence option (c) is correct.

    Correct Option: C

    E → E - T |
    T → T + F | F
    F → ( E ) | id
    Using the rule for removal of left recursion is
    A → Aα / β
    A → βA '
    A ' → α A ' / ∈
    Then, the given grammar is written as :-
    E' → −TE' / ∈
    E → +TE '
    T ' → + FT ' / ∈
    T → FT '
    F → (E) | id
    Now by putting E ' as X and T ' as Y, then
    X → −TX / ∈
    E → TX
    Y → +FY / ∈
    T → FY
    F → (E) | id
    Hence option (c) is correct.


  1. Consider the following grammar:
    stmt - > if expr then expr else expr; stmt | o
    expr - > term relop term | term
    term - > id | number
    id - > a | b | c
    number - > [0-9]
    where relop is a relational operator (e.g., <, >, ...), o refers to the empty statement, and if, then, else are terminals. Consider a program P following the above grammar containing ten if terminals. The number of control flow paths in P is ________. For example, the program
    if e1 then e2 else e3
    has 2 control flow paths, e1 → e2 and e1 → e3.









  1. View Hint View Answer Discuss in Forum

    The number of control Paths is depends on number of if statement that is 2n, where, n is the number. of if statements For (2 “if statements”) 22 = 4 control flow Paths are possible :

    These four control flow are :
    (1) e1 → e2
    (2) e1 → e3
    (3) (e2 if e4 ) → e5
    (4) (e3 if e4 ) → e5
    So, for (10 “if statements”), 210 = 1024.
    Control flow path are possible. Hence, answer is 1024.

    Correct Option: A

    The number of control Paths is depends on number of if statement that is 2n, where, n is the number. of if statements For (2 “if statements”) 22 = 4 control flow Paths are possible :

    These four control flow are :
    (1) e1 → e2
    (2) e1 → e3
    (3) (e2 if e4 ) → e5
    (4) (e3 if e4 ) → e5
    So, for (10 “if statements”), 210 = 1024.
    Control flow path are possible. Hence, answer is 1024.



  1. Consider the context-free grammars over the alphabet {a, b, c} given below. S and T are non-terminals.
    G1 : S → aSb / T, T → CT | ∈
    G2 : S → bSa \ T, T → cT | ∈
    The language L(G1) ∩ L(G2) is









  1. View Hint View Answer Discuss in Forum

    The context free grammar given over alphabets
    ∑ = {a, b, c}, with S and T are nonterminals.
    Given, G1 : S → aSb | T , T → cT | ∈
    G2 : S → bSa | T , T → cT | ∈
    Lets L(G1) is the language for Grammar (G1) and L(G2) is the language for Grammar (G2).
    where L(G1) = { an cm bn | m , n ≥ 0 }
    L(G2) = { bn cm an | m , n ≥ 0 }
    then L(G1) ∩ L(G2) = { an cm bn } ∩ { bn cm an }
    = { cm | m ≥ 0 } = C*
    Since the only common strings will be those strings with only ‘C’, so the intersection is not finite but regular.

    Correct Option: B

    The context free grammar given over alphabets
    ∑ = {a, b, c}, with S and T are nonterminals.
    Given, G1 : S → aSb | T , T → cT | ∈
    G2 : S → bSa | T , T → cT | ∈
    Lets L(G1) is the language for Grammar (G1) and L(G2) is the language for Grammar (G2).
    where L(G1) = { an cm bn | m , n ≥ 0 }
    L(G2) = { bn cm an | m , n ≥ 0 }
    then L(G1) ∩ L(G2) = { an cm bn } ∩ { bn cm an }
    = { cm | m ≥ 0 } = C*
    Since the only common strings will be those strings with only ‘C’, so the intersection is not finite but regular.


  1. If G is a grammar with productions
    S → SaS | aSb | bSa | SS | ∈
    where S is the start variable, then which one of the following strings is not generated by G ?









  1. View Hint View Answer Discuss in Forum

    The given grammar with productions,
    S → SaS | aSb | bSa | SS | ∈
    Now consider,
    S → aSb | bSa | SS | ∈
    This grammar generates all strings with equal number of ‘a’ and ‘b’.
    Now, S → SaS can only generate strings where ‘a’ is more than ‘b’. Since on left and right of ‘a’ in SaS, S will have only strings with na = nb or na > nb.
    Now consider each options
    1. S → SS → aSbS → abS → abaSb → abab ,when, S → ∈
    2. S → aSb → aSaSb → aaaSb → aaab , when S → ∈
    3. S → SS → aSbS → abS → abbSa → abbSaSa → abbaa, when ( S → ∈ )
    4. “babba” which is a string with nb > na is not possible to generate by the given grammar.
    Hence, option (d) is correct.

    Correct Option: D

    The given grammar with productions,
    S → SaS | aSb | bSa | SS | ∈
    Now consider,
    S → aSb | bSa | SS | ∈
    This grammar generates all strings with equal number of ‘a’ and ‘b’.
    Now, S → SaS can only generate strings where ‘a’ is more than ‘b’. Since on left and right of ‘a’ in SaS, S will have only strings with na = nb or na > nb.
    Now consider each options
    1. S → SS → aSbS → abS → abaSb → abab ,when, S → ∈
    2. S → aSb → aSaSb → aaaSb → aaab , when S → ∈
    3. S → SS → aSbS → abS → abbSa → abbSaSa → abbaa, when ( S → ∈ )
    4. “babba” which is a string with nb > na is not possible to generate by the given grammar.
    Hence, option (d) is correct.



  1. Consider the following grammar:

    What is FOLLOW(Q) ?









  1. View Hint View Answer Discuss in Forum

    Follow (Q) ?
    Follow (Q) is First (R) Hence First (R) = {W, E}.
    We add ‘W’ in Follow (Q) and for ∑.
    We calculate :
    First (S) = {y}
    So, follow (Q) = First (R S)
    = {{W} – ∑} ∪ First (S)
    Follow (Q) = {W, y} ( ∴ First (S) = {y} )
    Hence option (c) is correct.

    Correct Option: C

    Follow (Q) ?
    Follow (Q) is First (R) Hence First (R) = {W, E}.
    We add ‘W’ in Follow (Q) and for ∑.
    We calculate :
    First (S) = {y}
    So, follow (Q) = First (R S)
    = {{W} – ∑} ∪ First (S)
    Follow (Q) = {W, y} ( ∴ First (S) = {y} )
    Hence option (c) is correct.