Homework Assignment 1


Submission Instructions

Answer all of the following questions using either a word processor or text editor and submit your homework as either a pdf file or an ascii text file. Use the 461_submit script to submit your assignment.


Questions

  1. Explain the distinction between compilation and interpretation. What are the comparative advantages and disadvantages of the two approaches? (hint: you may need to read the textbook to find the advantages and disadvantages of the two approaches, as I did not cover them all in class).

  2. List the principal phases of compilation, and describe the work performed by each phase.

  3. Write regular expressions to describe the following set of tokens. Please use the notation used in class, although if you have used Perl, it is acceptable to use Perl notation. Do not worry about whitespace in your patterns unless specifically told to worry about it (i.e., your regular expressions do not have to check for white space).
    1. Dates of the form Jan dd, 20yy where dd is either a 1 or 2 digit number that may not start with a leading 0 (e.g., 03 is not valid) and yy is a two digit number that may start with a leading zero(s) (e.g., both 03 and 00 are valid). Do not worry about potentially invalid dates, such as 33, 45, etc. or future years, such as 15 or 34.
    2. Modify your regular expression so that it can handle any of the month abbreviations of Jan, Feb, Mar, Apr, May, or Jun. Note that rather than writing out the entire pattern 6 times, you might want to write out the pattern once and write a second pattern to express the 6 different months.
    3. Street addresses of the form 1678 Cardiff Rd. Your pattern should be as follows:

      • The street number is a 1-5 digit number that starts with a leading 1-9 (no leading 0 is permitted)
      • The street name is one or more whitespace separated names. Each name starts with a capital letter (A-Z) and is followed by zero or more lowercase, alphabetic characters (a-z). Use \s to denote a whitespace character.
      • The street suffix is one of Rd, Ln, Ave, St for the street suffix
      • There should be one space between the street number and street name and one space between the street name and the street suffix
    4. A number with the following characteristics:

      • It may have an optional, leading + or - sign
      • If it is an integer, then it starts with a non-zero digit and may have one or more additional digits following the start digit. An integer may not end with a decimal point (e.g., 57 is legal and 57. is not legal).
      • If it is a fractional number, then it has an integer part, a decimal point, and a fractional part that has one or more digits. The integer part is mandatory and may either be 0 or an integer as described in the previous rule. As examples, 1.03, 1.5, 1.000, and 0.35 are all legal, and .35 is illegal.

  4. Write a context free grammer to describe the following language constructs. Please use the notation used in class.

    1. Write a grammar that allows a user to type the following commands:
      	  move direction number    (e.g., move left 10, move up 20)
      	  rotate number angle-type (e.g., rotate 10 degrees, rotate .25 radians)
      	  zoom number   (e.g., zoom 2, zoom 0.5)
      	  scale number number number   (scale 2 3 5)
      	
      Here are additional specifications:

      • direction may be one of (up, down, left, right).
      • angle-type may be one of (degrees, radians).
      • number is a terminal so you don't have to specify it
    2. Write a grammar for relational expressions whose operators are <, <=, >, >=, ==, != and whose operands are either numbers or ids. You may assume that numbers and ids are both terminals. Example expressions might be:
      	   a < 10
      	   b > 50
                 c == d
      	   
      Use something like relExp as your start non-terminal because you will need to use exp later in this assignment.
    3. Expand your previous grammar so that it handles boolean expressions with the operators not, and, and or and also permits parenthesized expressions. The and/or operators should be infix operators and the not operator should be a prefix operator. A sample expression might be:
      	   a and not (b or (c and not d))
      	   
      Use something like boolExp as your start non-terminal because you will need to use exp later in this assignment.
    4. Expand your previous grammar so that it handles while loops and assignment expressions within your while loop. The form of a while loop is:
      	  while (boolean expression) do
      	     assignment statements and/or nested while loops
      	  end
              
      For example:
      	  while (a < 10 or b > 20) do
      	    a = b;
                  b = c * 20;
                  i = 0;
                  while (i < 10) do
      	       i = i + 1;
                  end
                end
      	 
      Your grammar should meet the following specifications:

      • while loops can be nested
      • the boolean expressions can be any boolean expression generated by your grammar from the previous problem. Do not repeat that grammar--just use your start terminal, which I hope is boolExp.
      • your assignment statements are of the form:
        id = exp;
        where exp is the non-terminal for the arithmetic expression grammar I used in class. Please do not copy the expression grammar I used in class, just use the exp non-terminal.

  5. In question 4a, is the context free grammar you wrote also a regular expression (hint: think about what is the key element of context free grammars that is missing from regular expressions).

  6. Construct a finite state machine that accepts all strings which match the following regular expression:
    letter (letter | digit)*