CSC347 Homework 0

CSC347 Sep13

slide version

single file version

For Tuesday, Sep 13

These problems will be discussed in class. Consider them as practice for the "real" homework assignment. Be sure to ask any questions in class that you might need in order to do these problems.

Reading material is the chapter "Describing Syntax and Semantics" from the text. Also see the documents page for links to downloading and installing Perl.

Contents

  1. Question 1
  2. Question 1a answer
  3. Question 1b answer
  4. Question 1c answer
  5. Question 2
  6. Question 2 answer.
  7. Question 3
  8. Question 3 answer
  9. Question 4
  10. Question 4 answer

Question 1[2]

Using the following grammar, show a parse tree and a leftmost derivation for each of the following statements:

<assign> -> <id> '=' <expr>
<id> -> 'A' | 'B' | 'C'
<expr> -> <id> '+' <expr>
                   | <id> '*' <expr>
                   | '(' <expr> ')'
                   | <id>
    1. A = A * (B + (C * A))
    2. B = C * (A * C + B)
    3. A = A * (B + (C))

Question 1a answer[3]


Give a left-most derivation of

A = A * (B + (C * A))

for the grammar:
Rule Number       Rule
1                 <assign> -> <id> '=' <expr>
2,3,4             <id> -> 'A' | 'B' | 'C'
5                 <expr> -> <id> '+' <expr>
6                         | <id> '*' <expr>
7                         | '(' <expr> ')'
8                         | <id>



Left most derivation:
                                                             Rule used
    1   <assign> => id = <expr>                     1
    2            => A = <expr>                      2
    3            => A = id * <expr>                 6
    4            => A = A * <expr>                  2
    5            => A = A * ( <expr> )              7
    6            => A = A * (id + <expr>)           5
    7            => A = A * (B + <expr>)            3
    8            => A = A * (B + (<expr>))          7
    9            => A = A * (B + (id * <expr>))     6
   10            => A = A * (B + (C * <expr>))      4
   11            => A = A * (B + (C * id))          8
   12            => A = A * (B + (C * A))           2

Question 1b answer[4]


Give a left-most derivation of

B = C *(A * C + B)

for the grammar:
Rule Number       Rule
1                 <assign> -> <id> '=' <expr>
2,3,4             <id> -> 'A' | 'B' | 'C'
5                 <expr> -> <id> '+' <expr>
6                         | <id> '*' <expr>
7                         | '(' <expr> ')'
8                         | <id>



Left most derivation:
                                            Rule used

    1   <assign> => id = <expr>                     1
    2            => B = <expr>                      3
    3            => B = id * <expr>                 6
    4            => B = C * <expr>                  4
    5            => B = C * ( <expr> )              7
    6            => B = C * (id * <expr>)           6
    7            => B = C * (A * <expr>)            2
    8            => B = C * (A * id + <expr>)       5
    9            => B = C * (A * C + <expr>)        4
   10            => B = C * (A * C + id)            8
   11            => B = C * (A * C + B)             3

Question 1c answer[5]


Give a left-most derivation of

A = A * (B + (C))

for the grammar:
Rule Number       Rule
1                 <assign> -> <id> '=' <expr>
2,3,4             <id> -> 'A' | 'B' | 'C'
5                 <expr> -> <id> '+' <expr>
6                         | <id> '*' <expr>
7                         | '(' <expr> ')'
8                         | <id>



Left most derivation:
                                            Rule used
    1   <assign> => id = <expr>                     1
    2            => A = <expr>                      2
    3            => A = id * <expr>                 6
    4            => A = A * <expr>                  2
    5            => A = A * ( <expr> )              7
    6            => A = A * (id + <expr>)           5
    7            => A = A * (B + <expr>)            3
    8            => A = A * (B + (<expr>))          7
    9            => A = A * (B + (id))              8
   10            => A = A * (B + (C))               4

Question 2[6]

Define: Ambiguous grammar.

Question 2 answer.[7]

Ambiguous grammar

An ambiguous grammar is a grammar for which there exists some sentence in the language of the grammar that has two different parse trees.

Equivalently, an ambiguous grammar is a grammar for which there exists some sentence in the language of the grammar that has two different left-most derivations.

Question 3[8]

Show that the following grammar is ambiguous:


   <S> -> <A>
   <A> -> <A> '+' <A> | <id>
  <id> -> 'a' | 'b' | 'c'

Question 3 answer[9]



Show that this grammar is ambiguous:

1            <S> -> <A>
2,3          <A> -> <A> '+' <A> | <id>
4,5,6        <id> -> 'a' | 'b' | 'c'



The following string is a sentence for this grammar and it has two
different left-most derivations: 

   a + b + c

Left-most derivation 1:
                               Rule Used 
    1   <S> => <A>                     1         
    2       => <A> + <A>               2         
    3       => id + <A>                3         
    4       => a + <A>                 4         
    5       => a + <A> + <A>           2
    6       => a + id + <A>            3
    7       => a + b + <A>             5
    8       => a + b + id              3
    9       => a + b + c               6

Left-most derivation 2:
                               Rule Used 
    1   <S> => <A>                     1         
    2       => <A> + <A>               2         
    3       => <A> + <A> + <A>         2
    4       => id + <A> + <A>          3
    5       => a + <A> + <A>           4
    6       => a + id + <A>            3
    7       => a + b + <A>             5
    8       => a + b + id              3
    9       => a + b + c               6

Question 4[10]

What is the output of the following Perl program?


$p1 = "prog1.java";

$p1 =~ s/(.*)\.java/$1.cpp/;

print "$p1\n";

Question 4 answer[11]

What is the output of the following Perl program?


    1   $p1 = "prog1.java";
    2   $p1 =~ s/(.*)\.java/$1.cpp/;
    3   print "$p1\n";



prog1.cpp