Midterm
Exam Practice Questions
After
you have given all of the problems a good try, see the answers below.
1) Two
features of high-level programming languages that enhance readability are
_________________ and ________________________.
2) Subprogram
calls and recursion are the focus of ____________________ languages.
3) A language for which all grammars are ambiguous is ____________________ ambiguous.
4) The lifetime of _______________________ variables is dependent upon when the programmer specifically allocates and deallocates them.
6) LL(1) stands for __________________________________________________________.
1) If dynamic type binding is used in a language, type checking of assignments cannot be done.
2) Pascal uses NAME EQUIVALENCE for its type checking.
1)
Explain the idea of orthogonality,
and give an example that demonstrates it using a language that you know.
2)
Write a regular grammar that generates all
binary strings containing 3 1's (they can contain any number of 0's, and the
NULL character is a valid terminal).
3)
Consider
the context-free grammar and string below.
Give the parse-tree for the string.
S ®
0S0 | 1S1 | 0 | 1
001101100
4)
Explain why the C++ union is not type-safe,
and why
5)
State whether or not the following grammar is
ambiguous. Justify your answer. Assume upper case letters are non-terminals and
that l is the
null string.
A à 0A |
1B | l
B à 1B | 0A
| l
ANSWERS:
1) Two
features of high-level programming languages that enhance readability are
___comments______ and ___long_id_ names___.
2) Subprogram
calls and recursion are the focus of ___functional_______ languages.
3) A language for which all grammars are ambiguous is ____inherently_____ ambiguous.
4) The lifetime of ___explicit heap-dynamic___ variables is dependent upon when the programmer specifically allocates and deallocates them.
5) The lowest level syntactic elements in a program are ____lexemes_________ (usually made up of characters).
6) LL(1) stands for ___left-to-right parsing, leftmost derivation, 1 character lookahead____
1) If dynamic type binding is used in a language, type checking of assignments cannot be done.
TRUE – any assignment is legal
2) Pascal uses NAME EQUIVALENCE for its type checking.
FALSE – although Pascal largely uses name equivalence, there are some exceptions, such as two anonymous array variables declared in the same declaration statement.
1)
Explain the idea of orthogonality,
and give an example that demonstrates it using a language that you know.
See text and notes for
definition. One example is array and
record types in a language such as Ada. These features can be combined to form
arbitrarily complex data structures, although each in itself is fairly simple.
2)
Write a regular grammar that generates all
binary strings containing 3 1's (they can contain any number of 0's, and the
NULL character is a valid terminal).
N = { A,
B, C, D } T = { 0, 1, NULL } S = A
P =
A ® 0A | 1B
B ® 0B | 1C
C ® 0C |
1D
D ® 0D |
NULL
3)
Consider
the context-free grammar and string below.
Give the parse-tree for the string.
S ®
0S0 | 1S1 | 0 | 1
001101100
See notes and text for parse trees.
4)
Explain why the C++ union is not type-safe,
and why Ada record-variants are (for the most part)
type safe.
Once a C++ union variable is
created, it can at any time be accessed through any of the identifiers within
the union, each of which may be of different types. Thus there is no way to check if the data in
the variable is consistent with the type through which it is being
accessed. Ada,
on the other hand, has a discriminant in its
record-variants that sets which identifier within the union is the current one
being used. Only the identifier
indicated by the discriminant can be used to access
the union. Furthermore, the discriminant cannot be changed unless the entire record is
changed as well, thereby disallowing inconsistent data.
5)
State whether or not the following grammar is
ambiguous. Justify your answer. Assume upper case letters are non-terminals and
that l is the
null string.
A à 0A |
1B | l
B à 1B | 0A
| l
This grammar is ambiguous. Consider the string 0110. It can be derived through both of the parse
trees shown below.
S
| \
A B
| \ | \
| A | B
| | | | \
| l |
| B
| | |
| \
| | |
| A
| | |
| \
0 1 1 0
l
S
| \
A B
| \ | \
| A | A
| | \ | |
| | B | l
| | | \ |
| | |
B |
| | |
| |
0 1 1 l 0