All Exams  >   Computer Science Engineering (CSE)  >   Compiler Design  >   All Questions

All questions of Syntax Directed Translation for Computer Science Engineering (CSE) Exam

A translation scheme is shown as-

Output for 3 * 2 + 4 is ______.
    Correct answer is '18'. Can you explain this answer?

    Sudhir Patel answered
    Given grammar, is
     is an ambiguous grammar.
    Therefore two outputs are possible for single input i.e. 3 * 2 + 4.
    Like: output 1 = 10
    output 2 = 18
    This problem is resolved by YACC Tool. So, according to rule of YACC Tool:
    (1)  S-R conflict is resolved by ‘S’
    (2) Ri – Rj conflict is resolved by Ri where i < j.
    So, scanning input from left to right 

    Consider the following grammar (that admits a series of declarations, followed by expressions) and the associated syntax directed translation (SDT) actions, given as pseudo-code:
    P → D* E*
    D → int ID {record that ID.lexeme is of type int}
    D → bool ID { record that ID.lexeme is of type bool}
    E → E1 + E2 {check that E1.type = E2.type = int; set E.type := int}
    E → !E1 {check that E1.type = bool; set E.type := bool}
    E → ID {set E.type := int}
    With respect to the above grammar; which one of the following choices is correct?
    • a)
      The actions will lead to infinite loop.
    • b)
      The actions can be used to correctly type-check any syntactically correct program.
    • c)
      The actions can be used to type-check syntactically correct boolean variable declarations and boolean expressions.
    • d)
      The actions can be used to type-check syntactically correct integer variable declarations and integer expressions.
    Correct answer is option 'D'. Can you explain this answer?

    Ankita Bose answered
    Understanding the Grammar and Actions
    The provided grammar allows for a series of variable declarations followed by expressions, specifically for integer and boolean types. Each production rule is coupled with syntax-directed translation (SDT) actions which help in type-checking.
    Type Declarations
    - The rules for declarations are:
    - D → int ID: Records that ID is of type int.
    - D → bool ID: Records that ID is of type bool.
    These rules imply that the grammar can handle both integer and boolean variable declarations.
    Type Checking for Expressions
    - The expression rules are:
    - E → E1 + E2: Requires both operands to be of type int.
    - E → !E1: Requires E1 to be of type bool.
    - E → ID: For integers, it sets the type based on the ID's declaration.
    The actions effectively enforce type correctness for integer expressions but have no provisions for handling boolean expressions.
    Analysis of the Options
    - Option A: Infinite Loop
    - The grammar and actions are well-defined, and there is no recursion that could lead to an infinite loop.
    - Option B: Correct Type-Check for Any Program
    - The grammar does not support the type-checking of boolean expressions, so this option is incorrect.
    - Option C: Type-Check Boolean Declarations and Expressions
    - While boolean declarations are supported, boolean expressions are not adequately handled in the rules provided, making this option incorrect.
    - Option D: Type-Check Integer Declarations and Expressions
    - This is correct. The grammar and actions effectively handle integer declarations and ensure type consistency for integer expressions, making it the most suitable choice.
    Conclusion
    The correct answer is option D, as the grammar and SDT actions successfully manage integer type declarations and expressions, ensuring type safety in those contexts.

    _____ is done by attaching rules or algorithms or program fragments to productions in a grammar.
    • a)
      Syntax-directed translation
    • b)
      Lexical analysis
    • c)
      Execution
    • d)
      Loading
    Correct answer is option 'A'. Can you explain this answer?

    Sudhir Patel answered
    Syntax Directed Translation is done by attaching augmented rules to the grammar that facilitate semantic analysis. It involves passing information top bottom and/or bottom up the parse tree in form of attributes attached to the nodes. Syntax directed translation rules use the following
    1. lexical values of nodes
    2. constants
    3. attributes associated to the non-terminals in their definitions.

    Consider the following grammar along with translation rules.

    Here # and % are operators and id is a token that represents an integer and id•val represents the corresponding integer value. The set of non-terminals is {S, T, R, P} and a subscripted non-terminal indicates an instance of the non-terminal.
    Using this translation scheme, the computed value of S•val for root of the parse tree for the expression 20#10%5#8%2%2 is ______.
      Correct answer is '80'. Can you explain this answer?

      Sudhir Patel answered
      Concept:
      The given grammar along with translation rules is,

      The given string is=  20#10%5#8%2%2
      Rule 1:
      In the given SDT, % has more precedence than # and % because it is away from starting symbol.
      Rule 2:
      Both % and * are left-associative because the left non-terminal is present left most on the right side grammar.
      The given string is=  20#10%5#8%2%2
      The given string is=  20x10÷5 x 8÷2÷2
      The given string is= 20 x2x4÷2
      The given string is= 20 x2x2
      The given string is= 40 x2
      The given string is= 80
      Hence the correct answer is 80.

      The below mentioned picture shows the:­
      • a)
        Conceptual view of lexical analysis
      • b)
        Conceptual view of syntax analysis
      • c)
        Conceptual view of syntax directed translation
      • d)
        Conceptual view of top-­down parsing
      Correct answer is option 'C'. Can you explain this answer?

      Sudhir Patel answered
      Concept:
      Basic idea of syntax directed translation:
      Syntax-Directed Constructing a parse tree or syntax tree and computing the values of attributes at the nodes of the tree by visiting them in some order is the definition of translation. In many circumstances, translation can be done without creating an explicit tree during parsing.
      Evaluation Orders for syntax directed translation:
      • Dependency Graphs.
      • Ordering the Evaluation of Attributes.
      • S-Attributed Definitions.
      • L-Attributed Definitions.
      • Semantic Rules
      Note:
      • Evaluation of the semantic rules may generate code, save information in a symbol table, issue error messages, or perform any other activities.
      • Special cases of syntax-directed definitions can be implemented in a single pass by evaluating semantic rules during parsing, without explicitly constructing a parse tree or a graph showing dependencies between attributes.
      Hence the correct answer is the Conceptual view of syntax-directed translation.

      Consider the following grammar and the semantic actions to support the inherited type declaration attributes. Let X1, X2, X3, X4, X5 and X6 be the placeholders for the non-terminals D, T, L or L1 in the following table:
      Which one of the following are the appropriate choices for X1, X2, X3 and X4?
      • a)
        X1 = L, X2 = T, X3 = L1, X4 = L
      • b)
        X1 = T, X2 = L, X3 = L1, X4 = T
      • c)
        X1 = L, X2 = L, X3 = L1, X4 = T
      • d)
        X1 = T, X2 = L, X3 = T, X4 = L1
      Correct answer is option 'A'. Can you explain this answer?

      Sudhir Patel answered
      Concepts:
      Synthesized attributes:

      A Synthesized attribute is an attribute of the non-terminal on the left-hand side of a production. Synthesized attributes represent information that is being passed up the parse tree. The attribute can take value only from its children.
      S-attributed SDT
      If an SDT uses only synthesized attributes, it is called as S-attributed SDT.
      Inherited attributes:
      An attribute of a nonterminal on the right-hand side of a production is called an inherited attribute. The attribute can take value either from its parent or from its siblings.
      L-attributed SDT
      If an SDT uses both synthesized attributes and inherited attributes with a restriction that inherited attribute can inherit values from left siblings only, it is called as L-attributed SDT.
      In SDT:
      D → T L {X1.type = X2.type}
      L.type = T.type (Inherited attributed)
      ∴ X1 = L and X2 = T
      T can be int or float
      In SDT:
      L → L1, id {X3.type = X4.type}
      L1.type = L.type (Inherited attributed)
      ∴ X3 = L1 and X4 = L
      Hence option 1 is correct.

      Chapter doubts & questions for Syntax Directed Translation - Compiler Design 2025 is part of Computer Science Engineering (CSE) exam preparation. The chapters have been prepared according to the Computer Science Engineering (CSE) exam syllabus. The Chapter doubts & questions, notes, tests & MCQs are made for Computer Science Engineering (CSE) 2025 Exam. Find important definitions, questions, notes, meanings, examples, exercises, MCQs and online tests here.

      Chapter doubts & questions of Syntax Directed Translation - Compiler Design in English & Hindi are available as part of Computer Science Engineering (CSE) exam. Download more important topics, notes, lectures and mock test series for Computer Science Engineering (CSE) Exam by signing up for free.

      Compiler Design

      26 videos|67 docs|30 tests

      Signup to see your scores go up within 7 days!

      Study with 1000+ FREE Docs, Videos & Tests
      10M+ students study on EduRev