CMPS 350 HW 04 - Attribute Grammars

You must click 'check' on each question or your score will not be recorded.

01. Under the C language, which issues below require an attribute grammar?  
    A. Preventing the dangling else problem. 
    B. Preventing duplicate identifier names in the same block.
    C. Controlling naming so that a variable name does not begin with a digit. 
    D. Enforcing type checking in parameter passing. 
A B C D
02. An attribute grammar
    A. adds context-sensitive information to a context-free grammar. 
    B. describes runtime semantics; i.e., the behavior of a running program.
    C. Both statements are true.  
A B C
03. In the partial grammar below, assume <string>.Size is an attribute whose
    value is returned by function Size. What type of attribute is Size in
    the context of this production rule? Note that '<-' denotes assignment 
    from right to left.

       <string> ::= <string>[2] x 
       Size(<string>) <- Size(<string>[2]) + 1  

    A. synthesized attribute        
    B. inherited attribute  
    C. both A & B
A B C
04. Select the true statement.
    A. Synthesized attributes pass information from child to parent up the 
       parse tree.
    B. The parent node is the head (left-hand side) of a production rule.
    C. both A & B
A B C
05. Select the true statements concerning attribute grammars.
    A. The values of intrinsic attributes are assigned before parsing begins.
    B. Inherited attributes are decorated in a bottom-up fashion.
    C. Synthesized attributes carry information up the parse tree.
    D. Intrinic attributes are a type of synthesized attributes.  
A B C D

  ATTRIBUTE GRAMMAR 1, with semantic rules enclosed in {}.

 1. <assign> -> <var> = <expr>
    Predicate: <var>.type == <expr>.type
 2. <expr> -> <var>[1] + <var>[2]   
    {<expr>.type <- <var[1].type}
    Predicate: <var>[1].type == <var>[2].type
 3. <expr> -> <var>
    {<expr>.type <- <var.type}
 4. <var> -> A | B | C
    {<var>.type <- lookupType(<var>.name)} // lookupType() returns int or real 

06. The predicate in Rule 2 of Attribute Grammar 1 ensures what?
    A. Data types cannot be mixed in expressions.
    B. The type of the left-hand side (lvalue) in an assignment statement must 
       match the type of the right-hand side (rvalue).
    C. both A & B
A B C
07. Grammar 1 Rule 2 uses inheritance to pass information from <var>[1].type
    to <expr>.type.
T F
08. The attribute .type in Grammar 1 Rule 4 is assigned a value before parsing 
    begins. 
T F
09. What is true concerning the decorated parse tree for this assignment 
    statement?  A = B + C

                          <assign>
                         /          \
                     <var>   =     <expr>
                    .type=int        .type=?
                      |           /         \ 
                      A      <var>     +    <var>
                            .type=int        .type=?
                               |                |
                               B                C

A. If C is type int the predicate in Grammar 1 Rule 1 will return true. 
B. <expr>.type will be assigned type int by Rule 2.
C. If C is type real the predicate at Rule 2 will return true.
D. This grammar allows compiler coercions. 
A B C D

ATTRIBUTE GRAMMAR 2 (see text ch 3)
o actual_type: attribute for <var> and <expr> - holds int or real 
o expected_type: attribute for <expr>

Rule 1. <assign> -> <var> =  <expr>
      Semantic rule: <expr>.expected_type <- <var>.actual_type 
Rule 2. <expr> -> <var>[2] + <var>[3]
      Semantic rule: <expr>.actual_type <- 
                   if (<var>[2].actual_type == int) and 
                   (<var>[3].actual_type == int) then int else real      
      Predicate: <expr>.actual_type == <expr>.expected_type
Rule 3. <expr> -> <var>
      Semantic rule: <expr>.actual_type <- <var>.actual_type
      Predicate: <expr>.actual_type == <expr>.expected_type
Rule 4. <var> -> A | B | C
      Semantic rule: <var>.actual_type <- lookup (<var>.string)
Assume these values in the symbol table: int A; real B, real C

10. What is true if you parse B = A with Attribute Grammar 2?
    A. Information is carried from <var> to <expr>.
    B. Information is carried from <expr> to <var>.
    C. Both A & B
A B C
11. If you parse C = A with Attribute Grammar 2, what does this semantic rule do?
 
        <expr>.actual_type <- <var>.actual_type  (Rule #3)

    A. <expr>.actual_type is synthesized from <var>.actual_type
    B. <var>.actual_type is synthesized from <expr>.actual_type
    C. <expr>.actual_type is inherited from <var>.actual_type  
A B C
12. What is true if you parse the statement below using the symbol table values?
                      C = A

    A. This statement violates the predicate rule that applies to
       <expr> -> <var>[2] + <var>[3]
    B. This statement violates the predicate at Rule 3.
    C. This statement does not violate a predicate rule.
A B C
13. What is true about the statement below using the values in the symbol table?
                  B = A + C

    A. This statement violates the predicate at Rule 2.
    B. This statement violates the predicate at Rule 3.
    C. The compiler coerces B to an int in the assignment statement.
    D. The compiler coerces A to a real in the expression A + C.
A B C D
14. What is true when this predicate is evaluated in Attribute Grammar 2?
      Predicate: <expr>.actual_type == <expr>.expected_type

   A. The value of expected_type has been inherited from the left-hand side of 
      the assignment statement.
   B. The value of expected_type has been synthesized from the right-hand side 
      of the assignment statement.   
A B
15. Prolog is based on a closed world assumption. This means that 
    A. anything you do not know and cannot deduce is false.
    B. unless explicitly known as false you can assume true. 
A B
16. What does this prolog rule mean?
            loves(jack,X) :- rich(X).
    A. jack loves everyone who is rich.
    B. If jack loves you then you are rich.
    C. If you are not rich then Jack does not love you. 
A B C
 
17. Which of the following is the correct form of a definite clause? 
    Notation: '^' is AND;   'v' is OR;  '~' is NOT

    A. p ^ q ^ r ^ ... w ^ ~z
    B. ~p v ~q v ~r v ... v ~w v z
    C. ~p ^ ~q ^ ~r ^ ... ^ ~w ^ z  
A B C
18. A definite clause without a body is a fact in prolog.
T F
 
19. What does this Prolog rule mean if the universe of discourse is all widgets?

          alsoRelated(X,Y) :- related(Z,X), related(Z,Y).

    A. If every widget that is related to X is also related to Y, then widgets 
       X and Y are alsoRelated.
    B. If there is a widget Z that is related to widget X and also related to
       widget Y, then X and Y are alsoRelated.
A B
20. Which is the correct recursive rule after this rule to define ancestors?
            ancestor(X,Y) :- parent(X,Y).

    A. ancestor(X,Y) :- ancestor(Z,Y), parent(X,Z).
    B. ancestor(X,Y) :- parent(X,Z), ancestor(Z,Y).
A B
21. This BNF grammar
  <cmdlist> ::= <cmd> | <cmdlist>; <cmd>
     is equivalent to this EBNF grammar, where { } denotes 0 or more times.
  <cmdlist> ::= <cmd> | {;<cmdlist>}
T F

  Right: Wrong: Percent: (Must be 100% for credit)

Errors:

Name (first name only): Sleipnir Username: 3-digit ID: