Midterm I

Part of materials for 22C:50, Fall 2003
by Douglas W. Jones
THE UNIVERSITY OF IOWA Department of Computer Science

 

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.

  1. Hand assemble this EAL code using two passes to resolve forward references, and show the final contents of the symbol table. There are extra blanks! Do not fill in anything in unused symbol table entries or next to lines that assemble nothing into memory. It is OK to cross out and replace values where they are changed. (2 points)
     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
    

  2. Complete the header comments in this fragment of a parser. (2 points)
    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: ________________________________________________

  3. Given a string s, the substr(s,i,j) operator returns a new string that is the substring of s running from character i to character j (counting from character 1). Given two strings s1 and s2, the operation index(s1,s2) returns the index of the first character in s2 that is found in s1. The operation length(s) returns the length of s in characters.

    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)

    _________________________________________________________________
    
    _________________________________________________________________
    
    _________________________________________________________________
    
  4. Write EAL code equivalent to that on the left, but without using any macro or conditional directives. (2.0 points)
    MACRO T,I	                           ____________________
      IF I > 0
        B I                                    ____________________
        T (I >> 1)
      ENDIF                                    ____________________
      B I
    ENDMAC                                     ____________________
    
    X 2                                        ____________________
    

     

    Name: ________________________________________________

     

     

     

  5. Consider the following fragment from the EBNF grammar for C:

    <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" );
    		
    	}
    }