CMPS 350 HW 03 - Formal Grammars & BNF Notation

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

01. Which of the following Java language components are lexemes?
    A. *       B. identifier         C. while       D. integer 
A B C D
02. Which of the following Java constructs is a token?
    A. for     B. literal      C. both A & B
A B C
03. Programming languages are primarily defined by what type of grammar?
    A. context-sensitive    B. context-free    C. regular   D. unrestricted
A B C D
BNF GRAMMAR 1 <expr> ::= <expr> + <term> | <term> - <term> <term> ::= <var> | const <var> ::= a | b | c | d
04. The rule <expr> ::= <expr> + <term> from GRAMMAR 1 makes addition A. left associative. B. right associative.
A B
05. Which statements are true in the language defined by GRAMMAR 1?
    A. subtraction will always be evaluated before addition
    B. a '+' terminal will be lower in the parse tree than a '-' terminal
    C. the evaluation order of '+' and '-' will vary depending on the context 
    D. there are 5 terminals in the language 
A B C D
06. The following is a valid expression in GRAMMAR 1. 
         a + b - c  
T F
07. Following GRAMMAR 1, the first sentential form in the derivation of the
     expression below is what?
          a - b + c
   A. <expr> + <term>    B. <term> - <term>  
A B
08. Which of the following conditions are necessary and sufficient to claim that
    a context-free grammar is ambiguous? 
    A. at least one rule contains the epsilon symbol (the empty list).
    B. the LHS appears twice in the RHS the rule (double-recursion).
    C. parsing a sentence in the grammar results in two distinct parse trees.
    D. a choice of two production rules can be successfully applied at one step.
A B C D
09. Select the true statements. In the model of grammars developed by Chomsky
    A. the language of a^nb^nc^n; i.e., {abc,aabbcc,aaabbbccc,...}, is regular.
    B. context-free grammars and BNF grammars are theoretically equivalent.
    C. a regular grammar is Chomsky's most restrictive form of grammar. 
    D. high-level programming languages are primarily context-free languages.  
A B C D
10. A statement list in Perl is one or more statements separated by semi-colons
    and enclosed in curly braces. Which is a correct BNF grammar for a Perl 
    statement list? (The curly braces and semicolon are terminal symbols.)

    A. <stmt_list> ::= { <stmts> }
       <stmts> ::=  <stmt> | <stmt>; <stmts>
    B. <stmt_list> ::= { <stmt> } | { <stmt>; <stmt_list> }  
A B
BNF GRAMMAR 2 <assign> ::= <id> = <expr> <expr> ::= <expr> + <term> | <term> <term> ::= <term> * <factor> | <factor> <factor> ::= (<expr>) | <id> <id> ::= a | b | c
11. In Grammar 2 you know * has precedence over + because A. <term> -> <term> * <factor> before <factor> -> <id>. B. in the parse tree for 5 + 3 * 4, * is below +. C. Both A & B.
A B C
12. Select the true statements concerning Grammer 2. 
    A. If you swap + and *, + will take precedence over *.
    B. + is defined in terms of *.
    C. + and * associate to the left.
    D. ((a + b) * (c * a)) is a valid expression in the language.
A B C D
BNF GRAMMAR 3 <assign> ::= <id> = <expr> <expr> ::= <id> + <expr> | <id> * <expr> | (<expr>) | <id> <id> -> a | b | c
13. Which of the following are valid sentences in the language defined by GRAMMAR 3? A. a = a * (b + (c * a)) B. a = a + b * c C. b = (a + b) * c D. a = (a + b) * (c + d)
A B C D
14. In GRAMMAR 3 one outcome of rule <expr> ::= (<expr>) will be to allow 
    addition to be evaluated before multiplication if desired.
T F
GRAMMAR 4: <assign> ::= <id> = <expr> <expr> ::= <expr> + <term> | <term> <term> ::= <term> * <factor> | <factor> <factor> ::= (<expr>) | <id> <id> ::= a | b | c
15. Is the partial leftmost derivation using Grammar 4 shown below correct? a = (a + b) * c <assign> => <id> = <expr> => a = <expr> => a = <term> => a = <term> * <factor> => a = <factor> * <factor>
T F
16. Is the parse tree below correct for the given statement following GRAMMAR 4?

                a = (a + b) * c

                    <assign> 
                   /   |   \ 
                 <id>  =   <expr>
                  |          |
                  a       <term>   
                         /   |  \
                      <term> * <factor>
                        |          |
                     <factor>     <id>
                        |          |
                    (<expr>)       c 
                     /  |  \       
                <expr>  + <term>   
                  |         |
                <term>    <factor>
                  |         |
                <factor>   <id>
                  |         |
                 <id>       b
                  |
                  a 
T F
17. Is the following grammar ambiguous?
     <S> -> <A>
     <A> -> <A> + <A>  | <id>
     <id> -> a | b | c 
YES NO
ERROR IS QUESTION - A. was 'abbbcc' which could not be generated
18. Which string can be derived by this grammar?
    <S> -> <A> <B> <C>
    <A> -> a<A> | a 
    <B> -> b<B>c | b 
    <C> -> c<C> | c  
    A. abbbccc    B. abbc   C. both A & B 
A B C
 
ERROR IN ANSWER - FIXED NOW
19. Consider the grammar:
    <S> -> <A> a <B> b
    <A> -> <A> b | b 
    <B> -> a <B> | a

    Which strings are in the language defined by this grammar?
    A. bbbab       B. bbaaaaa        C. baab         D. bbaabb     
A B C D
20. Consider the grammar
    S -> AB | AD
    A -> Aa | b
    B -> Bb | a
    D -> cDc | d 
   Which of the following is not a valid string in the language?
   A. bccdcc        B. bbbbab        C. aaaacccc  
A B C
21. The grammar below defines the set of strings of 1 or more "b"s followed by 
    2 or more "a"s and ending with one "b". 

    <S> -> <A> a <B> b
    <A> -> <A> b | b
    <B> -> a <B> | a
T F
22. Java is a procedural, object-oriented, and imperative language.
T F
23. Which of the following is true in Java?
    A. garbage collection for memory that is dynamically allocated is automatic
    B. you do not need to call a destructor
    C. both statements are true.  
A B C
24. The Java character set is what?
    A. 16-bit extended ASCII (Text-16)
    B. 16-bit Unicode (UTF-16)
    C. 32-bit Unicode (UTF-32)
    D. 64-bit Hex  (Hex-64) 
A B C D
25. Java does not support direct access to memory through pointers. How does
    this impact the Java runtime environment?
    A. it reduces the risk of segmentation faults
    B. it reduces the risk of buffer overflows
    C. it is more difficult to write low-level drivers for the operating system
    D. it eliminates dynamic memory leaks
A B C D
26. The length of the bit strings produced by this grammar will always be odd.

           A -> 0 | 01A  | 1AA 
T F

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

Errors:

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