Midterm I
Part of
materials for 22C:50, Fall 2003
|
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 has 5 problems; spend about 10 minutes on each.
line hexadecimal source number location value text 1 ________ _______ W B symbol table symbol value 2 ________ _______ B #12 ________ ________ 3 ________ _______ B = 12 ________ ________ 4 ________ _______ ; etaion shrdlu ________ ________ 5 ________ _______ A: W B ________ ________ 6 ________ _______ B A 7 ________ _______ W A+B 8 ________ _______ B = #10
void parse_transfix() /* parse one transfix following this EBNF grammar: _____________________________________________ _____________________________________________ */ { if (lex_ispunc( &lex_this, '(' )) { lex_scan(); while (!lex_ispunc( &lex_this, ')' ) parse_transfix(); parse_punc( ')', "end paren expected" ); } else { parse_womble(); } }
Name: ________________________________________________
The constant string values nonblank, alphabetic, nonalphanumeric, numeric, and nonnumeric contain one each of every character of the corresponding type.
Here is a proposal for the design of a lexical analyzer:
lex_scan() { int i; lex_this = lex_next; i = index( line, nonblank ); if (i > 1) line = substr( line, i, length( line ) ); i = index( line, alphabetic ); if (i == 1) { /* found an identifier */ i = index( line, nonalphanumeric ); lex_next = substr( line, 1, i-1 ); line = substr( line, i, length( line ) ); return; } i = index( line, numeric ); if (i == 1) { /* found a number */ i = index( line, nonnumeric ); lex_next = substr( line, 1, i-1 ); line = substr( line, i, length( line ) ); return; } /* found punctuation */ lex_next = substr( line, 1, 1 ); line = substr( line, 2, length( line ) ); return; }
What is seriously wrong with this incomplete but workable proposal? (2 points)
_________________________________________________________________ _________________________________________________________________ _________________________________________________________________
MACRO T,I ____________________ IF I > 0 B I ____________________ T (I >> 1) ENDIF ____________________ B I ENDMAC ____________________ X 2 ____________________
Name: ________________________________________________
<variable> ::= <identifier> { <qualifier> }
| * <variable>
<qualifier> ::= . <identifier>
| [ <expression> ]
This describes syntactic details, and we will assume a lexical analyzer such as the one used with the EAL assembler in order to write some code for a parser. In the fragment of parsing analysis code below, all details that don't relate to the above grammar fragment been left out. The blanks relate to key issues that connect the code with the grammar! Fill in the blanks! (2 points)
void parse_variable() { if (!lex_ispunc( &lex_this, '*' )) { if (lex_this.typ != identifier) error( "identifier expected" ); _______________________________________ while (lex_ispunc( &lex_this, '.' ) || lex_ispunc( &lex_this, '[' )) { _______________________________________ } } else { _______________________________________ parse_variable(); } } void parse_qualifier() { if (lex_ispunc( &lex_this, '.' )) { lex_scan(); _______________________________________ lex_scan(); } else if (lex_ispunc( &lex_this, '[' )) { lex_scan(); _______________________________________ parse_punc( ']', "end bracket expected" ); } }