You have 70 minutes to answer these questions. Please read all questions before you start. This is a closed-book exam. Each question is worth 25 points. Please remember that you are bound by the CU honor code.
Consider the following syntax for "if" statement:
<ifstmt> → if (<condition>)
<statement>
| if (<condition>) <statement> else <statement>
<statement> → <ifstmt> | <assignstmt>
You can assume C++ syntax for <condition> and <assignstmt>.
[9 points] What does it mean for a grammar to be ambiguous? Show that the <ifstmt> grammar above is ambiguous.
[9 points] What is the implication of the ambiguity exposed in (i)?
[7 points] Describe the difference between syntax and semantics
Scope and lifetime are two attributes of variables in programming languages.
[6 points] Explain the concept of scope
[6 points] Explain the concept of lifetime
[13 points] Explain the difference between scope and lifetime. Also, give an example that illustrates the difference.
Answer the following questions about types and type checking.
[9 points] Write a program that would behave differently in a strongly-typed language and a weakly-typed (i.e., not strongly-typed) language. Restrict your program to use only the types in MYSTERY (i.e., integer, subrange, procedure, or array). Explain why your program has this behavior.
[8 points] Does strongly-typed mean that a language does all type checking at compile time? Explain.
[8 points] Give and explain one advantage of structural equivalence over name equivalence. Give and explain one advantage of name equivalence over structural equivalence.
For each of the following types, explain what they are and describe a realistic situation where they would be useful:
[8 points] Enumeration types
[8 points] Subrange types
[9 points] Union types (You can talk about either discriminated or non-discriminated union. Be sure to indicate which one you are talking about).