CS75 Spring 2007
Project 1 Sample Output


Below is an example of C-- code that can be used to test your scanner. Note that your actual token names do not need to be the same as the ones shown in these examples.

C-- code

// this will be ignored
int main() {
  x = 0;
  _abc = 8;

  /* this too 
     will be
     ignored */

  if (x == 10) 
    write x;
  else
    ;

  return 0000042
}

Notice that there are a number of errors in this code, some can be caught by the scanner, some by the parser, and some can only be recognized in code generation. For example the last statement is missing a semi-colon. This error can be recognized by the parser. Also, variables must be declared at the top of a block in C--, but the variables x and _abc have not been declared. This error can be caught in code generation, but not by the scanner. However the scanner can recognize that identifiers must start with an alphabetic letter, so the id _abc is invalid. In addition, the scanner can recognize that an integer may not begin with leading zeros. In both of these cases, the scanner returns an err token with an associated message.

Scanner output

int
id main
lparen
rparen
lbrace
id x
assign
num 0
semi
err unrecognized character _
id abc
assign
num 8
semi
if
lparen
id x
eq
num 10
rparen
write
id x
semi
else
semi
return
err integers cannot have leading zeros 0000042
semi
rbrace