Checkpoint (10%): Parts 1-3, Due Tuesday Feb. 15 at the beginning of class
(you may not use a late day on the checkpoint)
Complete Project (90%): Parts 1-4, Due: Tuesday Feb. 22
BEFORE 9:30 am
Parts 1-3 may be submitted on-line with part 4 (if you do so, please
submit parts 1-3 in ACSII, postscript or pdf form), or you may submit
a hard copy of parts 1-3 separately in class on the same day that you
handin part 4 on-line (this may be a re-submission of your graded
checkpoint if you have made no changes to parts 1-3 since the checkpoint).
Problem Introduction
Getting Started
What to Hand in
A Note on Code Style and Grading
C-- Programming Language Specification
On-line Unix and C help
lexan
that
returns the next token read from a file stream called
source
.
'a'
should be
read as an integer token with value 97
, the ASCII code
of the character.
/* this is a comment that can go for several lines */ // this is a single line comment, where everything to right is ignored
I suggest you use the simplecompiler as a starting point for your
solution:
~newhall/public/cs75/simplecompiler/
This contains a Makefile and a reasonable modular design for your solution, both of which you should feel free to change. If you use this as a starting point, you will need to pretty much start over with the code in the lexer.c file, and you will need to change main to call just your lexan function and the emit function (main should not call parse). You can use the error and symbol table functions as they are implemented, or use them as a guide for implementing your own version of the symbol table. Also, you should change the emit function so that it outputs "TOKEN.attr" for every token. After your lexer returns the DONE token, your main function should printout the contents of the symbol table.
For example, if the input is: if (x1 >= 'a') The output of your program should look something like: IF LPAREN ID.x1 GE NUM.97 RPAREN DONE SYMBOL TABLE CONTENTS: # if you added keywords to your symbol table, ---------------------- # they would be printed here too ID x1Also, before submitting, you should change main so that it takes the C-- source code file as a command line argument:
% ./lexan foo.c-- # assuming lexan is the name of my LA executableTo do this use argc and argv parameters to main (if you don't know how to do this, look in some of the C references in the lab, or ask me or someone else in the class for help):
int main(int argc, char *argv[]) {Make sure to test your lexical analyzer on both valid and invalid C-- programs (think about the types of errors a lexical analyzer can and cannot detect).
script
command to capture all of a
shell's stdin, stdout, stderr to a file, and dos2unix
to clean-up the resulting typescript file)
/* * Function: Area(float w, float h) * Computes the area of a rectangle given its height and width * * param w: the witdth of the rectangle in inches * param h: the height of the rectangle in inches * returns: the area of the rectangle * or -1 if the area cannot be computed due to a bad input value */
In addition, you code should be easily extensible to allow for larger
problem sizes. For example, if you use a fixed-sized array for the
symbol table then use a constant when referring to its max size in your
program (e.g. x = SYMTABSIZE;
where SYMTABSIZE is defined
as a constant) rather than the numeric value (x = 512;
).
This way, your program can be easily modified to handle a larger symbol
table size by making just one change to the constant's definition.
See the links to Unix and C help at the bottom of the cs75 homepage if you need help using some unix commands or debugging your C program.
lpr grammar.ps): C-- Grammar
C-- Grammar:
Program ------> VarDeclList FunDeclList
VarDeclList --> epsilon
VarDecl VarDeclList
VarDecl ------> Type id ;
Type id [ num] ;
FunDeclList --> FunDecl
FunDecl FunDeclList
FunDecl ------> Type id ( ParamDecList ) Block
ParamDeclList --> epsilon
ParamDeclListTail
ParamDeclListTail --> ParamDecl
ParamDecl, ParamDeclListTail
ParamDecl ----> Type id
Type id[]
Block --------> { VarDeclList StmtList }
Type ---------> int
char
StmtList -----> Stmt
Stmt StmtList
Stmt ---------> ;
Expr ;
return Expr ;
read id ;
write Expr ;
writeln ;
break ;
if ( Expr ) Stmt else Stmt
while ( Expr ) Stmt
Block
Expr ---------> Primary
UnaryOp Expr
Expr BinOp Expr
id = Expr
id [ Expr ] = Expr
Primary ------> id
num
( Expr )
id ( ExprList )
id [ Expr ]
ExprList -----> epsilon
ExprListTail
ExprListTail --> Expr
Expr , ExprListTail
UnaryOp ------> - | !
BinOp --------> + | - | * | / | == | != | < | <= | > | >= | && | ||