Sie sind auf Seite 1von 20

c c


      

 

Consider the following declarations:


type complex = record re, im: integer end;

person = record name: alfa;


age: integer;
location: complex;
salary: real end;

var people = array [ 7..14, (austin, dallas, houston)]


of person;

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 how it was derived and give the aref form.

è    

a ‘how how an operator precedence parser would parse the string:


a A - (B / C - D) / E + F

‘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

     


 

Consider the following declarations:


type complex = record re, im: real end;

item = record name: alfa;


age: integer;
location: complex;
displayed: boolean;
color: array[(red,green,blue)] of integer;
link: ^item;
cost: real end;

var items = array [ 8..12, (low, med, hi), 7..11] of item;

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]

‘how how it was derived and give the aref form.

è    

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.

‘ome sample exam 2 questions:

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 
    

1. Regular expressions


a. Give a regular expression for an identifier in minijava
b. Give a regular expression for comments in minijava
2. Suppose we have token A matching regular expression "abc", token B matching regular
expression "abc*" in our grammar. On input abcc, which token is matched? Why?
3. What is an ambiguous grammar? Why are they bad when used to design or implement
programming languages?
4. Define Left-recursive and right-recursive as they pertain to grammars.
5. Consider this excerpt of our MiniJava grammar:
. E -> E op E
7. E -> E [E]
8. E -> id | integerLiteral
9. op -> + | - | *
a. Construct the First and Follow sets for this grammar
b. Can this grammar be parsed by an LL(0) parser? If not, rewrite the grammar so that it
can be parsed by an LL(0) parser.
c. Construct the State diagram and shift/reduce table for compiling the above grammar by
an LR(0) grammar.
d. Can this grammar be parsed by an LR(0) parser? Can it be parsed by an SLR parser? If
not, rewrite the grammar so that it can be parsed by an SLR parser.
10.A common syntax error is a mis-spelled reserved word. How would you modify your scanner
and/or parser to specifically catch misspellings of reserved words? As long as you catch them,
could you just go ahead an continue compiling (and in fact output an executable)? (If so, explain
how; if not, explain why not.)
11.Our compiler operates in several passes -- parser, symbol table construction, type checker, IRT
construction, code generation. This is convenient, but is it necessary? Explain any features of
MiniJava that require multiple passes.
12.The Token class generated by sablecc includes fields with the line number and position for each
token. Assuming you do not build an abstract syntax tree, but instead pass the entire parse tree
onto the later passes of the compiler, explain how you could build a Visitor class that goes
through the parse tree and assigns a line number to each expression and statement node in the
parse tree.
13.Explain the issues involved in typechecking
14. x = this.foo(1, 3, false);

(i.e. describe all the things you must check for correctness.)

15.Explain the use of the static link in the Activation Record.


16.Suppose you language could have functions as a return type -- i.e. a method could return a
function as a return value. Describe the issues involved in implmenting this in a compiler.
17.Translate: x = a+ 5 into an IR Tree.
18.Suppose you did not implement any operator precedence in your grammar. How will this impact
your implementation of IR Trees?
19.Data I use in my research is given to me in a form that is easy for humans to read and write, but
not very convenient for the processing I do on it. Therefore, these data files get translated into a
different format. The human readable format looks like the following:
20. SUBJECT ID : S10
21.
22. CRITERIA 2304 : operations
23. CATEGORY 4240 : does not apply
24. CARDS: 4  7 10 13 14 15 1 18 19 21 23 2
25. CATEGORY 4241 : objects in the operation
2. CARDS: 5 11 17 22 25
27. CATEGORY 4242 : coding features to change the object
28. CARDS: 8 9 12 20 24
29. CATEGORY 4243 : names of the operation
30. CARDS: 1 2 3
31.
32. CRITERIA 2305 : things i learned in computer science
33. CATEGORY 4244 : CS I
34. CARDS: 1 3 5 7 8 9 10 13 17 20
35. CATEGORY 4245 : CS II
3. CARDS: 2 11 12 14 1 18 19 21 24 25 2
37. CATEGORY 424 : Data Structures
38. CARDS: 4 22
39. CATEGORY 4247 : haven't learned
40. CARDS:  15 23
41.
42. CRITERIA 230 : places i usually define things, make things, create
43. CATEGORY 4248 : does not apply
44. CARDS: 4  7 10 15 23
45. CATEGORY 4249 : defined outside of method
4. CARDS: 2 5 11 14 17 18 19 25
47. CATEGORY 4250 : made or constructed inside method
48. CARDS: 1 3 8 9 12 13 1 20 21 22 24 2
49. *
50. SUBJECT ID : S09
51.
52. CRITERIA 2300 : things at same level in terms of abstraction when you
write a program
53. CATEGORY 4227 : synonyms for function
54. CARDS: 1 2 3
55. CATEGORY 4228 : broad terms looking at program from high level
5. CARDS: 5 7 15
57. CATEGORY 4229 : things you might see within a method
58. CARDS: 8 9 12 1 19 20 21 24
59. CATEGORY 4230 : things you would see in a class
0. CARDS: 11 17 18 25
1. CATEGORY 4231 : things that don't fit in the others
2. CARDS: 4  10 13 14 22 23 2

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.

The format we want to put the data in is:

4240,S10,2304,does not apply,00010110010011110110101001


4241,S10,2304,objects in the operation,00001000001000001000010010
4242,S10,2304,coding features to change the
object,00000001100100000001000100
4243,S10,2304,names of the operation,11100000000000000000000000
4244,S10,2305,CS I,10101011110010001001000000
4245,S10,2305,CS II,01000000001101010110100111
424,S10,2305,Data Structures,00010000000000000000010000
4247,S10,2305,haven't learned,00000100000000100000001000
4248,S10,230,does not apply,00010110010000100000001000
4249,S10,230,defined outside of method,01001000001001001110000010
4250,S10,230,made or constructed inside
method,10100001100110010001110101

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.

1. Give a grammar to describe the human-readable format.


2. Assuming you have a parser that can handle your grammar and produces code and a
visitor structure like sablecc, how would you write a visitor to convert the parsed data
into the comma-separated format?
 Rewrite the trees in this file and this file to remove ESEQs.

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):

R -> R '|' R | RR | R* | (R) | a | b

ù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.

 Describe the advantages and disadvantages of generating intermediate code (such as


quads) vs. generating machine code directly from an ˜‘T.
 Write the code for generating three-address intermediate code for a for-loop in C of the
form

for(e1; e2; e3) stmt

˜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?

  iven the following Pascal program,


 Program Bar;
 Procedure Top;
 var z: integer;
 Procedure A(x: integer);
 begin
 writeln(x+z);
 end;
 Procedure B(y: integer);
 Procedure C(Procedure Q(z:integer))
 begin
 Q(y);
 end;
 begin
 C(A);
 end
 begin
 z := 7;
 B();
 end
 begin
 Top;
 end

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.

 Define the terms £


£  ,   V,  V  ,  £ V  ,
 V  V  , and VV V  
c 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

17.At suffices to show two different parser trees for dcdcd.


18. S S
19. /|\ /|\
20. / | \ / | \
21. S c S S c S
22. /|\ | | /|\
23. / | \ d d / | \
24. S c S S c S
25. | | | |
2. d d d d
27.‘how a parse tree and leftmost derivation for string  with respect to grammar

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(E) = {'n', 'i', '(' }

First(A) = {'n', 'i' }

First(L) = {'(' }

First(S) = {'n', 'i', '(' }

Follow(E) = {',', ')', $ }

Follow(A) = {',', ')', $ }

Follow(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(), A(), L(), S(), Sprime();

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 Comp‘ci 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*

‘ame as above for (aa|bb)*.

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):

1. First Automaton


2. (A,0,A)
3. (A,1,O)
4. Second Automaton (where "e" means epsilon)
5. (A,0,A)
. (A,e,O)
7. (O,e,A)
8. (O,1,O)

c  %   

 iven the following grammar

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


?    

 '  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.


' 
 
   

The following regular expression was proposed for C comments:

"/*" [*]* [^*]* [*]* "*/"


but it didn't match the following comment:

/* Here Y=2*x+1; */
and it ate up this entire line as a single comment:

/**/ y = "Hello, world\n"; /**/

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:

a The various purposes of comments.


a Whether it is clear what things are comments and what things are not.
a Whether usage is likely to lead to errors.
a How easy they are to implement.

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)

 %  " '

 iven the following grammar

expr -> term | expr '+' term


term -> factor | term '*' factor
factor -> INT | '(' expr ')'
is + left or right associative? What about *? Does * have higher or lower precedence than + ? Rewrite
the grammar so that it accepts the smae language, but the associativity of + is reversed.

&  '

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()

 iven the following context-free grammar

stmt : if_then | if_then_else | other_stmts


if_then : IF condition THEN stmt
if_then_else : IF condition THEN stmt ELSE stmt
You may assume that nonterminals other_stmts and condition have already been defined.

1. Show that this context-free grammar is ambiguous.


2. Rewrite the grammar so that it is not ambiguous, but still accepts the same language
Prove that the following grammar is ambiguous. Write a grammar that accepts the same
language that is not ambiguous. As the language inherently ambiguous? As it regular?

S -> z S | Z
Z -> | Zz

'', 

Given the following grammar:

stmtlist -> stmtlist stmt | stmt


stmt -> noun iverb adv | noun tverb noun
noun -> COMPILERS | I | YOU
iverb -> SLEEP | WORK
adv -> HARD | WELL
tverb -> LOVE | HATE | SEE
Is it LL(1) ? LL(k) for any k ? If so, give predict sets. If not, how can it be corrected to be LL(k)? LL(1)?

', 

 iven the context-free gramar

expr1 : expr2 ':'


| expr1 ';'
;
expr2 : expr2 '%' ID
| ID
;
Is this grammar LL(1), LR(1).

 iven the following grammar fragment for Cool:

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

'+' shift, and go to state 92

'+' [reduce using rule 35 (expr)]


$default reduce using rule 35 (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?

-1 down vote favorite

P -> {D ; C}

D -> d; D| d

C -> c; C | c

a) As the grammar (1)? xplain your answer.

b) As the grammar ‘R(1)? xplain your answer.

c) As the grammar R(1)? xplain your answer.

d) As the grammar ˜R? xplain your answer.

˜s for my answers A actually got no for them all... so A'm thinking A did something wrong

Here is my explanation.

a) At is not (1) because it is not left factored.

b) At is not ‘R, because of the transition diagram

item 2 ( which is... )

D-> d . ; D

D-> d .

We need to consult the follow set, ollow(D) = ;

Therefore this is not ‘R

c) At is not R(1) because of...


item 1

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)

d) This grammar is not ˜R because it is not R(1)

Thanks for your help!

Das könnte Ihnen auch gefallen