CS 315-01 Quizzes

  1. Date: Feb. 2, 2026
    Question: Given the following grammar,
    <program> ::= <stmt_list>
    <stmt_list> ::= <stmt> | <stmt> <stmt_list>
    <stmt> ::= <declaration_stmt> | <assign_stmt> 
    <declaration_stmt> ::= var <ident_list> ;
    <ident_list> ::= <var_id> | <var_id> , <ident_list>
    <var_id> ::=  x | y | z
    <assign_stmt> ::= <var_id> = <expression> ;
    <expression> ::= <expression> + <expression>
                   | <constant> | <var_id>
    <constant> ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
    
    Drive the string "var x, y, z; z = 3 + x + y;", using the rightmost derivation to show that it is in the language.

    Answer:

    The rightmost derivation:

    <program> ⇒ <stmt_list>
              ⇒ <stmt> <stmt_list>
              ⇒ <stmt> <stmt>
              ⇒ <stmt> <assign_stmt>
              ⇒ <stmt> <var_id> = <expression> ;
              ⇒ <stmt> <var_id> = <expression> + <expression> ;
              ⇒ <stmt> <var_id> = <expression> + <expression> + <expression> ;
              ⇒ <stmt> <var_id> = <expression> + <expression> + <var_id> ;
              ⇒ <stmt> <var_id> = <expression> + <expression> + y ;
              ⇒ <stmt> <var_id> = <expression> + <var_id> + y ;
              ⇒ <stmt> <var_id> = <expression> + x + y ;
              ⇒ <stmt> <var_id> = <constant> + x + y ;
              ⇒ <stmt> <var_id> = 3 + x + y ;
              ⇒ <stmt> z = 3 + x + y ;
              ⇒ <declaration_stmt> z = 3 + x + y ;
              ⇒ var <ident_list> ; z = 3 + x + y ;
              ⇒ var <var_id> , <ident_list> ; z = 3 + x + y ;
              ⇒ var <var_id> , <var_id> , <ident_list> ; z = 3 + x + y ;
              ⇒ var <var_id> , <var_id> , <var_id> ; z = 3 + x + y ;
              ⇒ var <var_id> , <var_id> , z ; z = 3 + x + y ;
              ⇒ var <var_id> , y , z ; z = 3 + x + y ;
              ⇒ var x , y , z ; z = 3 + x + y ;  ✓
    
    23 sentential forms.

  2. Date: Feb. 5, 2026
    Question: Given the following grammar,
    <program> ::= <stmt_list>
    <stmt_list> ::= <stmt> | <stmt> <stmt_list>
    <stmt> ::= <declaration_stmt> | <assign_stmt> 
    <declaration_stmt> ::= var <ident_list> ;
    <ident_list> ::= <var_id> | <var_id> , <ident_list>
    <var_id> ::=  x | y | z
    <assign_stmt> ::= <var_id> = <expression> ;
    <expression> ::= <expression> + <expression>
                   | <constant> | <var_id>
    <constant> ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
    

    a) Give a parse tree for the string "z = 3 - x - y;".

    b) Is the grammar ambiguous or not? Explain.

    Answer:

    a) The following is a parse tree for the string "z = 3 - x - y;".

    b) The following is also a parse tree for the same string. Since there are at least two different parse trees for the same string in the language of the greammar, the grammar is ambiguus.


  3. Date: Feb. 23, 2026
    Question: What are the strings in the language specified by the following grammar? Is it ambiguous? Justify you answer. Does it have any conflicts? If so, what type of conflicts, and what token causes the conflict?
    %token A B C D
    %%
    s: A x C A
     | A y A
    x: B ;
    y: B C ;
    
    What is the output of this scanner for the following input?

    Answer: Thle language is L={ABCA}. It is ambiguous since the only string in the language has two parse trees, as shown below.

    It has shift/reduce conflict on token C.


  4. Date: Mar. 5, 2026
    Question: What is written to the console by the following javascript code?
    <script>
      a = 3;
      console.log ("global, a="+a);
      function foo (arg) {
          var a = arg * 2;
          console.log("foo, a="+a+" b="+b);
          var b= 7;
          {
              console.log ("block, a="+a+" b="+b);
              var b= b * 2;
              console.log ("block, a="+a+" b="+b);
              let c = b * 3;
              console.log ("block, a="+a+" b="+b+" c="+c);
          }
          console.log("foo, a="+a+" b="+b);
      } // foo()
      foo(a+2)
      console.log ("global, a="+a);
    </script>
    

    Answer:

    global, a=3
    foo, a=10 b=undefined
    block, a=10 b=7
    block, a=10 b=14
    block, a=10 b=14 c=42
    foo, a=10 b=14
    global, a=3
    

  5. Date: Mar. 9, 2026
    Question: What is the output of the following Perl program?
    sub fun1 {
        local $a = 11;
        local $b = 22;
        sub fun1_1 {
            local $b = 33;
            print "Point1_1: a=$a, b=$b\n";
            fun1_2();
        }
        sub fun1_2 {
            local $a = 40;
            print "Point1_2: a=$a, b=$b\n";
            fun2();
        }
        fun1_1();
        print "Point1: a=$a, b=$b\n";
    }
    
    sub fun2 {
        $a = 55;
        print "Point2: a=$a, b=$b\n";
    }
    
    fun1();
    print "Point3: a=$a, b=$b\n";
    

    Answer:

    Point1_1: a=11, b=33
    Point1_2: a=44, b=33
    Point2: a=55, b=33
    Point1: a=11, b=22
    Point3: a=, b=