Midterm I
Part of
materials for 22C:50, Summer 2004
|
Name: ________________________________________________ ID Number: ___________________
Please answer in the space provided! Your name must be legible and in the form used on your University ID card! Illegible and verbose answers will be penalized! This exam is open-book, open-notes, closed neighbor! This exam 10 points; allocate 4-5 minutes per point.
line hexadecimal source number location value text 1 ________ _______ . = #10 symbol table symbol value 2 ________ _______ W A ________ ________ 3 ________ _______ B = 10 ________ ________ 4 ________ _______ ; commentary ________ ________ 5 ________ _______ A: ________ ________ 6 ________ _______ B B 7 ________ _______ B = . 8 ________ _______ W B
void parse_oddity() /* parse one oddity following this EBNF grammar: _____________________________________________ _____________________________________________ */ { if (lex_ispunc( &lex_this, '(' )) { lex_scan(); while (!lex_ispunc( &lex_this, ')' )) { parse_oddity; } lex_scan(); } else { parse_thingus(); } }
Name: ________________________________________________
If the input text has balanced parentheses, it works just fine. If the input text has unbalanced parentheses, it sometimes goes into a loop.void parse_oddity() /*A*/ { /*B*/ if (lex_ispunc( &lex_this, '(' )) { /*C*/ lex_scan(); /*D*/ while (!lex_ispunc( &lex_this, ')' )) { /*E*/ parse_oddity; /*F*/ } /*G*/ lex_scan(); /*H*/ } else { /*I*/ parse_thingus(); /*J*/ } /*K*/ } /*L*/
a) Exactly what kind of unbalanced parens cause this? (1 point)
________________________________________________________ ________________________________________________________ ________________________________________________________
b) Which line should have been augmented with a check for end-of-file to prevent this? (0.5 points)
________________________________________________________
c) Which line should read parse_punc( ')', "end paren expected");? (0.5 points)
________________________________________________________
#define two(a,b) a b a ___________________________________ two( i, = ) + 1; ___________________________________ j = two( 2, + ); ___________________________________ k = two( two( 2, + ), * ); ___________________________________ ___________________________________
Name: ________________________________________________
parse_if_statement() /* <if statement> ::= IF <expression> THEN <block> [ ELSE <block> ] ENDIF */ { int if_number = unique_integer(); int else_number = unique_integer(); lex_scan(); parse_expression(); printf( " JFALSE . \n" ); printf( "FIX%d = .-2 \n", if_number ); parse_keyword( then_handle ); parse_block(); if (is_keyword( else_handle )) { printf( " JUMP . \n" ); printf( "FIX%d = .-2 \n", else_number ); printf( "HERE = . \n" ); printf( ". = FIX%d \n", if_number ); printf( " W HERE \n" ); printf( ". = HERE \n" ); if_number = else_number lex_scan(); parse_block(); } parse_keyword( endif_handle ); printf( "HERE = . \n" ); printf( ". = FIX%d \n", if_number ); printf( " W HERE \n" ); printf( ". = HERE \n" ); }
a) Make an educated guess about the format of the operand field of the JUMP and JFALSE instructions. (1 point)
________________________________________________________ ________________________________________________________
a) The output of this compiler is an EAL-like assembly language. How is the forward reference problem solved by the combination of assembler and compiler. (1 point)
________________________________________________________ ________________________________________________________ ________________________________________________________ ________________________________________________________ ________________________________________________________