Assignment 2, solutions
Part of
the homework for 22C:50, Spring 2003
|
A: ANOTHER: EXTRA: B #F0 ;EXTRA LABELS
a) Modified Extended BNF grammar from Figure 2.9 to allow this:
<statement> ::= { <identifier> : } [ ( B | W ) <operand> ]Note that the only change was the replacement of one set of square braces (meaning optional construct) with curly braces (meaingin zero or more repetitions of this construct).
b) Modify the procedure statement from 2.16 to allow this:
void statement () { /* parse optional label */ while (!strcmp(lex.next,":")) { lex_scan(); /* skip over identifier */ lex_scan(); /* skip over colon */ } /* parse opcode and operand */ --- this part is unchanged --- } /* statement */
The change was just as small, the replacement of the keyword if with while.
c) Modify your answer to handle errors gracefully:
void statement () { /* parse optional label */ while (!strcmp(lex.next,":")) { if (!is_identifier(lex.this)) error( "identifier expected" ); lex_scan(); /* skip over identifier */ lex_scan(); /* skip over colon */ } /* parse opcode and operand */ --- this part is unchanged --- } /* statement */
int first_byte ( int i ) /* first byte of a 16-bit integer on the 80x86 */ { return i & 0xFF; } int second_byte ( int i ) /* second byte of a 16-bit integer on the 80x86 */ { return (i >> 8) & 0xFF; }
The above solutions are legal C or C++, and the expressions at the heart of each routine are legal Java.
BOOLEAN ispunc( lexeme l, char p ) /* returns true if the lexeme l is the character p */ { if (l.typ != punctuation) { return FALSE; } else { return line[l.start] == p; } }