This is a written assignment, rather than a programming assignment. You should feel free to discuss the assignment and possible solutions with your colleagues, but each student should turn in his or her own set of solutions. If you discuss the assignment with other students, make sure to cite them in your assignment.
This is an optional assignment. Successful completion of the assignment will result in some number of units of extra credit.
You are not permitted to use ``global'' attributes for your solutions to any of these problems. All attributes must be associated with nonterminals in the grammar.
Problem One: Length of Sequences
Here is a simple grammar for nonempty comma-separated sequences of integers.
iseq ::= INT iseq ::= INT COMMA iseq
Extend this grammar with rules to compute a length attribute of each sequence.
Problem Two: Empty Sequences
Extend the attribute grammar of the previous problem so that it can accept empty sequences. Your new grammar should be unambiguous and should still permit computation of a length attribute.
Problem Three: Parity
Consider the following ambiguous grammar for binary numbers.
binary ::= empty binary ::= bit binary binary ::= binary bit bit ::= ZERO bit ::= ONE
Extend the grammar to compute a parity attribute which is 0 if the number of 1's in the binary number is even, and 0 if the number of 1's in the binary number is odd.
Problem Four: Evaluation
Consider the simple language defined by the following rules
program ::= statements statements ::= empty statements ::= statement SEMI statements statement ::= ID ASSIGN exp statement ::= PRINT exp exp ::= exp PLUS term exp ::= exp MINUS term exp ::= term term ::= term TIMES stuff term ::= term DIV stuff term ::= stuff stuff ::= MINUS factor stuff ::= factor factor ::= LPAREN exp RPAREN factor ::= id factor ::= num
Write an attribute grammar that computes an output attribute for programs. The output attribute should be a list containing the values given by the print statements, in order. If errors are encountered during processing (e.g., undeclared variables or division by zero), they should be inserted into the output list at the appropriate point and an appropriate ``guesstimate'' should be used.
It is likely that you will need other attributes, such as a symbol table.
Problem Five: ``Pure'' Evaluation
Extend the grammar from the previous problem so that it does not permit reassignment to variables. An attempt to reassign to a variable is considered an error.
Problem Six: Type Checking
Consider the following simplified grammar of expressions over strings and integers. The grammar is overloaded in that the plus sign is used to add integers and to concatenate strings. If a string and an integer are added, the results should be a string.
exp ::= exp PLUS factor exp ::= exp MINUS factor exp ::= factor factor ::= ( exp ) factor ::= NUM factor ::= STRING
Add rules for computing a type attribute of expressions. Note that valid types are string, integer, and error.
Problem Seven: Cyclic Type Definitions
Modified from a problem in chapter five of Appel.
The Tiger language definition states that every cycle of type definitions must go through a record or array. Why?
If the compiler forgets to check for this error, nothing ``terrible'' will happen. Why?
Disclaimer Often, these pages were created "on the fly" with little, if any, proofreading. Any or all of the information on the pages may be incorrect. Please contact me if you notice errors.
Source text last modified Thu Oct 8 10:40:53 1998.
This page generated on Thu Oct 8 10:41:08 1998 by SiteWeaver.
Contact our webmaster at email@example.com