Assignment 1 with answers

advertisement
Princess Nora Bint Abdul Rahman University
College of Computer and Information Sciences
Department of Computer Science
1434 - 1435 (Second semester)
CS 444 - Programming Languages Concepts
Assignment 1 with Answers
Due date: Thursday 27/2/2014 (8 am - 1:30 pm)

Question 1. Using the grammar below:
<S>  a <A> | <B> g
<A>  <C> g | b <A> | a <A>
<B>  <C> b | <C> <B>
<C>  a
Show a parse tree for: abag
Answer:
CS 444

Assignment 1
Question 2. Consider the following grammar:
<Exp> → <Num> | <Exp> + <Exp> | <Exp> * <Exp>
<Num>→ 2 | 3 | 5
a) Prove that the grammar is ambiguous using the sentence: 2+3*5
b) Find a solution to avoid ambiguity.
Answer:
a) The following two distinct parse trees for the same string prove that the
grammar is ambiguous.
b) We can eliminate the ambiguity from the grammar by introducing a new
syntactic category <Term> that produces expressions of the form <Exp> *
<Exp>
<Exp> → <Exp> + <Exp> | <Term>
<Term> → <Term> * <Term> | <Num>
<Num>→ 2 | 3 | 5
This modification corresponds to assigning * a higher priority than +
Page 2 of 6
CS 444

Assignment 1
Question 3. Convert the following BNF to EBNF:
<program>  begin <stmt_list> end
<stmt_list>  <stmt>
| < stmt > ; <stmt_list>
<stmt>  <var> = <expression>
<var>  A | B | C
<expression>  <var> + <var>
| <var> - <var>
| <var>
Answer:
<program>  begin <stmt_list> end
<stmt_list>  <stmt> { ; <stmt> }
<stmt>  <var> = <expression>
<var>  A | B | C
<expression>  <var> [ (+ | -) <var> ]
Page 3 of 6
CS 444

Assignment 1
Question 4. Consider the following grammar:
<S>  a <S> c <B> | <A> | b
<A>  c <A> | c
<B>  d | <A>
Which of the following sentences are in the language defined by this grammar?




acccbd
aabcdcd
acd
accc
Answer:
acccbd
Clearly S can begin with a and end with b. But how can we get "bd"? The only way to
get a b is through the first S production but that requires c to appear immediately
before d so the first string is not generated by this grammar.
aabcdcd
This string is easily generated by this grammar.
S => aScB
=> aaScBcB // S => aScB
=> aabcBcB // S => b
=> aabcdcB // B => d
=> aabcdcd // B => d
acd
We can easily get from S to aScd but there is no "epsilon production" for S. We can't
make the S simply go away without it being replaced by some terminal so the string
acd is not generated by this grammar.
accc
This string is easily generated by this grammar.
S => aScB
// S => A
=> aAcB
// A => c
=> accB
// B => A
=> accA
// A => c
=> accc
Page 4 of 6
CS 444

Assignment 1
Question 5. Change the attribute grammar of Example 3.6 in the book (Page
136) whose BNF basis is:
<assign>  <var> = <expr>
<expr>  <var> + <var>
| <var>
<var>  A | B | C
Where the language rules change as follows:
Data types cannot be mixed in expressions, but assignment statements need not have
the same types on both sides of the assignment operator.
Answer:
1. Syntax rule: <assign>  <var> = <expr>
2. Syntax rule: <expr>  <var>[2] + <var>[3]
Semantic rule: <expr>.actual_type  <var>[2].actual_type
Predicate: <var>[2].actual_type = = <var>[3].actual_type
3. Syntax rule: <expr>  <var>
Semantic rule: <expr>.actual_type  <var>.actual_type
4. Syntax rule: <var>  A | B | C
Semantic rule: <var>.actual_type  look-up (<var>.string)
Page 5 of 6
CS 444

Assignment 1
Question 6. Prove the following program is correct:
{n > 0}
count = n;
sum = 0;
while count <> 0 do
sum = sum + count;
count = count - 1;
end
{sum = 1 + 2 + . . . + n}
Answer:
1. Suppose that the precondition is true, so that n = 3 (for example) as the
program begins.
2. Then count is assigned 3 and sum is assigned 0.
3. After the loop ends, the value of sum is 6.
4. The value of sum according to the postcondition = 1 + 2 + 3 = 6
5. The two values match, hence the program is correct.
Page 6 of 6
Download