Beruflich Dokumente
Kultur Dokumente
a ssuming alfa is 8 bytes, integer is 4 bytes, and real is 8 bytes, how much storage is
occupied by the array people?
a Calculate the effective address of the expression:
a people[10,dallas].location.im
è
how the contents of the stacks at each step; produce a tree as output.
a ive one advantage and one disadvantage of hashing as a method of symbol table
organization.
a Consider the regular expression (a | b)*b+b* . What is the simplest regular expression
that denotes the same language?
a ive the allowable form of productions for a Regular grammar.
a Consider the following grammar:
a S --> a S
a S --> S b
a S --> b
o What kind of grammar is this?
o What kind of language does it denote?
o As there a simpler kind of grammar that denotes the same language? Af so, give the
grammar; if not, explain why not.
a riefly and clearly define the following terms: ... 8 terms chosen from the vocabulary list
on the study guide.
c c
a ssuming alfa is 8 bytes, integer is 4 bytes, and real is 8 bytes, how much storage is
occupied by the array items?
a how the intermediate code for the expression:
a items[10, hi, 8].link^.color[blue]
è
a robot moves on a square grid. The robot can go forward (f), turn left (l), or turn right
(r). ive a grammar to describe the language of all sequences of moves that leave the
robot pointing in the same direction as when it started.
a What kind of grammar is the above (in the Chomsky hierarchy)?
a Describe a kind of local optimization.
a Describe what it means for a subexpression to be (a) available, (b) busy, (c) killed.
a Describe sources of extra run-time overhead in (a) time and (b) space in an object-
oriented language.
a Draw boxes around the following code to show the basic blocks:
a n := k*2;
a if n < j then write('less')
a else begin k := k - 1; write('more') end;
a writeln(k);
ùumber the blocks and draw a flow graph; give the matrix form of the flow graph and its
transitive closure.
a ive an advantage and a disadvantage for (a) call by reference (b) call by value.
a What are the most important things to optimize in a scientific program? Why?
a ive three examples of computer architecture innovations that require compiler
innovations to be effective.
a riefly and clearly define the following terms: ... 20 terms chosen from the vocabulary
list on the study guide.
1. RA define the following terms and give an example of how each term is used: (4
each, no more than six on the midterm)
o R Parsing
o Canonical R(1) Table
o R(0) Atems
o R Parsing
o Closure of a et of R(0) Atems
o yntax-directed translation
o bstract syntax tree
o tatic scoping
o Dynamic scoping
o Declaration of a name
o lse of a name
o lndeclared name
o áultiply declared name
o ymbol table
o èverloading
o Parameter passing
o Call by value
o Call by reference
o Call by value-result
o ase/immutable type
o Type constructor
o Type coercion
o èverloading
o Type polymorphism
o ùame equivalence for types
o tructural equivalence for types
o Anterpreter
o ctivation/call of a function
o ifetime of a function
o ctivation tree
o ctivation record
o lignment (to words)
2. Describe the relationship between a V and an in an R(0) grammar. How
does this relate to the notion of the
in an R(0) grammar? ive an algorithm for
constructing the
of a set of items R(0) with respect to a particular grammar. (10)
Ê. or the following grammar, construct the set of R(0) states to recognize viable prefixes
of this language. Then fill out an R parse table for this grammar and indicate whether
the grammar is ambiguous. (15)
-> $
-> -- | ++ | id
-> -- | ++ | .
4. What is the difference between R and R(1) parsing? What changes when
constructing an R table versus an R table? How does an R parser differ from
R and R(1) parsers? (10)
5. An bison, how do you (1) resolve conflicts related to operator precedence, (2) resolve
conflicts due to operator associativity, and (Ê) attach a syntax-directed translation action
to a production? (10)
6. elow is a grammar for understanding simple arithmetic expressions:
-> *
-> +
-> ( )
-> int ssuming that precedence and associativity have been handled, what translation
actions would you add to the grammar to get it to print out the input expression in a
postfix notation (e.g., (Ê + 5) * 4 would print out as Ê 5 + 4 *). ùow add actions to print
out the expression in a prefix notation (* + Ê 5 4). (15)
7. Discuss how the semantic stack works for an parser and how an T can emerge
from that stack. how the actions you would attach to the grammar in the previous
question to build up an T. ènce you have added the actions, show any further
adjustments you would need to make to the grammar to make it (1). (ssume * has
higher precedence than + and both are left associative.) (15)
8. Describe static and dynamic scoping and how they differ. ive an example of a program
that has different output assuming static versus dynamic scoping. (10)
9. áost programming languages have scoping rules with respect to variables. Why? ive an
example of a situation that requires scoping rules to resolve (and where scoping is
useful). Would it be possible to avoid having scoping rules (and if so, what are the
advantages and disadvantages of your approach)? (15)
10.Define parameter passing and the parameter passing methods call by value and call by
reference. ive an example of a program that works differently under the two different
mechanisms. (15)
11.What are the two main jobs of a symbol table? What types of errors does it catch in
performing these jobs? (10)
12.What are the main operations for a symbol table? Discuss the data structures associated
with a symbol table maintained as a list of hashtables and how the operations of a symbol
table are implemented in that case. ive an example of what your symbol table would
look like for a sample program. (15)
1Ê.nswer the previous question but where the symbol table is maintained as a hash table of
lists. (15)
14.What is a type system? Who defines it and how is it used in creating a compiler? (10)
15. ive three examples of tasks that are typically performed in a type checker. What types
of errors are recognized in a type checker? (10)
16.What are three of the constructors available for constructing types? What operations are
typically associated with these constructors and how are they used in type checking? (10)
17.What is the difference between static and dynamic type checking? What is the difference
between name and structural equivalence? What is a cyclic type? ive examples to
demonstrate your answers. (15)
18.how an outline of how a piece of code might work to type check a divide operator that
can apply to two operands of type int or float but does not apply to values of type bool.
How would your code change if coercion from bools to ints and ints to floats is allowed
(but not bools to floats)? (15)
19.How would you type check a for statement from C++? What symbol table issues are
introduced by a for statement? (15)
20.What does it mean for a name to be overloaded? What sections of your compiler must be
adapted to allow for overloading? (15)
21.What are the advantages and disadvantages of an interpreter? Describe two different
types of interpreters (as discussed in class). (15)
22.What are some of the key questions that must be answered in order to implement an
interpreter that works by first parsing an entire file and then executing the resulting T
(you may indicate other steps of compilation you think should be included before
intepretation occurs)? (15)
2Ê.How is memory generally laid out for a program? What are the various parts of the
memory associated with the program used for? (10)
24.What are the components of an activation record? How is an R used? (10)
25.Data can be allocated statically, on the stack, or in the heap. What types of variables are
the different sections of the program used for? (10)
c c
(i.e. describe all the things you must check for correctness.)
Each set of data for a subject begins with the keyword SUBJECTID a ':' and then some string that
starts with S or E and is followed by integers. The data is then grouped by CRITERIA, with several
CATEGORYs in the criteria, each having a set of CARDS. The data for a particular subject ends
when either we hit an asterisk, in which case it will be followed by another subject, or if we hit
the end-of-file, in which case there is no more data.
In this comma-separated format, the line holds the category number, the subject id, the
category name, and a 26-length vector of 0's and 1's indicating which cards are in the category
described by this line.
c
! !!
1.
a. Construct a nondeterministic finite automaton (ù) for each of the following
regular expressions
i. (a|b)*
ii. (a*|b*)*
iii. ((A|a)b*)*
b. Construct a deterministic finite automaton (D) from each ù constructed
above.
2.
a. Construct the set of R(0) items for the following grammar (for regular
expressions):
ùote that the first bar, '|', is a symbol in the alphabet, not the separator meta-
symbol. The quote symbol is not in the alphabet, though.
b. Construct the R parse table for the above grammar. Resolve any shift-reduce
conflicts according to the following precedence rules:
* has the highest precedence and is left-associative
concatenation has the second highest precedence and is left associative.
'|' has the lowest precedence and is left associative.
ssume that the code for generating expressions and statements has already been written.
Construct a register interference graph for the program shown in figure 9.1Ê on page 544
of the dragon book.
What is the minimum number of physical registers needed in order to avoid spilling?
draw the stack, including all the elements (local variables, parameters, static link,
dynamic link) of each activation record after Q is called in the body of C.
1. ive a regular expression using flex notation that descibes the set of strings that start with
, end with and have zero or more digits between them.
a[0-9]*c
2. Write a program using flex that copies the standard input to the standard output, except
that it replaces each sequence of consecutive blanks and tabs by a single blank.
3. %{
4. #include <stdio.h>
5. %}
. %%
7. [ \t]+ {printf(" ");}
8. "\n" {printf("\n");}
9. . {printf("%s", yytext);}
10. %%
11. int main()
12. {
13. yylex();
14. return 0;
15. }
16.how that the following grammar is ambiguous.
S -> S c S
S -> d
S -> c S d
S -> c
S => cSd
=> ccSdd
=> cccdd
29.
30. S
31. /|\
32. / | \
33. c S d
34. /|\
35. / | \
3. c S d
37. |
38. c
Ê9.What are the irst and ollow sets for the following grammar? The start nonterminal is .
E -> A
E -> L
A -> n
A -> i
L -> ( S )
S -> E , S
S -> E
First(L) = {'(' }
Follow(S) = {')' }
41.eft factor the grammar of the preceding question and then write a recursive descent
parser for it. The parser should only read a string and tell whether it is in this language.
The tokens of this language are characters. Presume a lexical analyzer is available, and
that statement match(c) will check the current lookahead token to see whether it is
character c. Af so, it will put the next token into variable lookahead. Af not, it will print
"no" and stop the program. tatement AùAT_ R initializes the lexer, setting
lookahead to the first token.
E -> A
E -> L
A -> n
A -> i
L -> ( S )
S -> E S'
S'-> , S
S'-> (empty)
void E()
{
if(lookahead == '(') L();
else A();
}
void A()
{
if(lookahead == 'n') match('n');
else match('i');
}
void L()
{
match('(');
S();
match(')');
}
void S()
{
E();
Sprime();
}
void Sprime()
{
if(lookahead == ',') {
match(',');
S();
}
}
int main()
{
INIT_LEXER;
E();
printf("yes");
return 0;
}
42.When a (top-down) LL(1) parser needs to decide whether to use a production, how much of the
sequence of tokens generated by the right-hand side of the production can the parser see?
When a (bottom-up) SLR(1) parser needs to make the same decision, how much does it see?
The (1) parser sees just the first token of the sequence of tokens generated by the
right-hand side of the production. The R(1) parser sees the entire right hand side plus
one more token after that.
This page includes questions by the instructor (John oyland) and will include questions that
students in Compci 654 wrote as part of Homework #4 (edited and made anonymous). t least
one of the (appropriate) questions will appear on the first midterm. The actual midterm will have
four questions, although some may consist of a set of short-answer/review questions.
" #
ny of the review questions from Chapters 1 and 2 may appear on the midterm.
$
#
a If one is designing and implementing a new language, what steps are needed?
a What is the difference between a phase of a compiler and a pass of a compiler?
a Briefly describe why one may wish to split a compile into modules corresponding to the phases.
a Give advantages and disadvantages to using tools such as @ and to implement parts of a
compiler.
a Describe what the ``front-end'' and ``back-end'' of a compiler are. Why are they distinguished?
a What is a checked dynamic semantic error ?
a What is maximal munch? When does it come into play? What should you do about it?
a Scanners need to return a series of tokens, whereas a DFA just returns Y or N. How does one
implement a scanner using a DFA? What problems can you foresee in the implementation?
a What is the difference between a deterministic and a nondeterministic automaton ?
a Why is it important that the scanner not frequently look at characters multiple times ?
a List four ways in which a lexical error can be handled. What method does the Cool compiler use
?
a What advantages do context-free grammars have over regular expressions for describing
programming languages.
a What is a derivation ?
a What is a sentential form ?
a What is the yield of a parse tree ?
a Compare and contrast top-down and bottom-up parsing in terms of complexity, class of
grammars accepted, contents of the parse stack, handling of tokens and type of derivation
obtained.
a What are the advantages of bottom-up parsing over top-down parsing ?
a What are FIRST and FOLLOW sets used for in SLL parsing? Which set is used for SLR parsing?
a The simplest way to handle an error is to print a message and then stop. Why should one do
more? Describe at least threee different ways to handle errors, pointing out advantages and
disadvantages.
a What is a shift/reduce conflict? a reduce/reduce conflict?
a What is an ambiguous grammar? Give an example.
a What connection is there between ambiguous grammars, inherently ambiguous languages and
parsing conflicts?
a Why don't we ever get shift/shift conflicts in LR parsing ?
a What is the difference between a teminator and a separator in a list of items?
Construct a regular expression that matches the language of all strings of ones and zeroes which
contain exactly Ê zeroes.
ive the ù construction for the following regular expression. Then find a D for the same
language: (0*1+)*0*
Construct an automaton for the following regular expression for da integers.
nondeterministic automaton is è .
[0-9](_?[0-9])*(#[0-9A-Fa-f](_?[0-9A-Fa-f])*#)?
ive a regular expression for the following automata where the set of states is {A,O}; A is the
start state and O is the only final state. The transitions are written as triples:
(@ , ,)
The automaton can go from from to to when the input is any character in the set given ([ ] denotes a
space character):
a -> t + t
t -> f * f | f
f -> ID | INT_CONST
construct a parse tree for 3*5+7. (What additions would be necessary to this grammar to let it parse
3*5*7? Is the grammar ambiguous now ?)
'
&
iven the following grammar, give the R(0) parse tables (the characteristic finite-state machine
(Cá)):
S -> E
E -> E '+' E
E -> 'a'
ist all the R items associated with the following grammar. Then construct the Cá.
S -> expr
expr -> expr '*' primary | primary
primary -> '7'
'
&
At has been proposed to add floating point numbers to Cool. The proposed form for floating point
constants is that a single dot occurs anywhere within a non-empty string of digits (including at
beginning or end) and that immediately following appear an exponent of the following
form: an ``e'' either upper or lower case, then an optional plus or minus sign and then a nonempty
string of digits. Write a regular expression suitable for flex that matches exactly this set of
floating-point constants. ou may use flex definitions.
c
(( ))
At has been proposed to making the else part of a conditional optional for Cool. Write the
modified context-free grammar production(s). Do we get the ``dangling else'' problem? xplain!
'
c
Write the extra productions to the syntax that would be required to use a nicer syntax for arrays
in Cool. áake sure your syntax can handle expressions such as
a[1] := a[i] + 1
What new tree node constructors would we need? Give their C prototypes, and give the bison actions
for your new syntactic rules.
'
/* Here Y=2*x+1; */
and it ate up this entire line as a single comment:
xplain why the first comment isn't matched and the second ``comment'' was matched. Then fix
the problem.
ome languages (such as Cool) have line comments; others (such as C) use bracketed comments
(see above); C++ has both kinds. Compare and contrast the benefits and problems of each kind.
An your answer touch on the following issues:
Does there exist a regular expression which describe non-empty strings containing the same
number of a's and b's and no other characters? What about a context-free grammar ?
Describe the following D in both plain (short!) nglish and as a regular expression. There are
four states {,,C,D}; is the start state and D is the only final state:
(A,1,A)
(A,0,B)
(B,1,C)
(B,0,B)
(C,0,D)
(C,1,A)
(D,0,B)
(D,1,C)
% " '
& '
An language Drool, we have two operators: the dispatch operator ., and the assignment operator
=, which is right-associative. The former has a higher precedence than the latter. ive a non-
ambiguous C so that expressions such as the following are parsed correctly:
z = x = obj1.f1().f2()
S -> z S | Z
Z -> | Zz
'',
',
expr : ...
| IF '( expr ')' expr ELSE expr
| expr '+' expr
| ...
;
When the Cool grammar is processed by bison we get a shift-reduce conflict for '+':
state 14
expr -> IF '(' expr ')' expr ELSE expr . (rule 35)
expr -> expr . '+' expr
ive a sample Cool expression that will lead the parser into such a state when parsing. An this
case, what is the correct action for the parser to take?
Describe the ``dangling else'' problem of Pascal and C/C++. As the problem we see here an
instance of the ``dangling else'' problem?
Af we don't correct the problem, how will bison resolve the conflict? Will this achieve the
correct result for all inputs?
How can the problem be fixed if we do not rely on bison's default conflict resolution?
P -> {D ; C}
D -> d; D| d
C -> c; C | c
s for my answers A actually got no for them all... so A'm thinking A did something wrong
Here is my explanation.
D-> d . ; D
D-> d .
P-> {D.;C} , $
D-> .d;D , ;
D-> .d , ;
item 2
D-> d.; D , ;
D-> d. , ;
item Ê
D-> d; . D , ;
D-> .d;D , ;
D-> .d , ;
ince item 2 goes to item Ê with ;, ùD "D-> d."'s (in item 2) look ahead token is also ;. this
causes a reduce to shift conflict, therefore this grammar is not R(1)