CSE 109 Progam 5 Due 9 PM Monday 31 March 2008 (Late Collection dates: 9 PM 1, 2 April 2008) This programming assignment is the third step of five in which we will create a program to compile YAPL++ programs and generate the corresponding SML code. For this assignment I ask you to write a program that parses YAPL programs (a restricted set of YAPL++ programs). That is your program should determine whether a given file contains a syntactically correct YAPL program. Your program should read the command line and use the first two files listed on the command line, the first for input and the second for output. The input file should consist of what purports to be a a YAPL program. The output file should consist of the results of the parse, either indicating that the YAPL program is correct or diagnosing the first parse error. Part of the parse involves determining whether a reference to a line number by some jump instruction is legitimate. Therefore you should parse the program in two passes, building a table of line numbers and a table of variable names on the first pass and then, on the second pass, checking whether a line number referred to by a jump is legitimate. The keys in the tables should be Words containing the line numbers and variable names. In later assignments, the line numbers and variable names will correspond to addresses in SML memory. The address is (part of?) the information stored in the table entry referred to by a given key. Note that you may want to store entries for the numbers that appear in expressions, e.g., write 100, load 50, etc. (In code generation you will need to store the numbers in memory and then retrieve the contents for a given instruction.) This suggests that line numbers should be stored in their own table to distinguish them from numbers that appear in arithmetic expressions. To greatly simplify this assignment, assume that your program determines whether the input file satisfies the syntax of YAPL, whose diagrams I handed out in class. The diagrams can also be reached by a link on the course web page. Thus, the parser should only accept statements like read x goto 100 if x>2 100 write x y<-x write y Assignment 7 will deal with the problem of parsing the more complicated forms of these statements, e.g., x <- x*x+4*(yot-two) write 2*x*y+4 It is VERY IMPORTANT for future assignments that you create a class Parser to do the parsing. Its use should look something like: ifstream in; ofstream out; .... ..... Parser p(in,out); p.parse(). It is also VERY IMPORTANT that the methods in Parser be highly modularized. To submit your assignment, create a subdirectory of /cse109.081 called p5 (that is, from your root directory cse109.081/p5) for doing your work. Finally, indicate you wish the assignment to be collected by executing the unix command "touch DONE", which will create a file of 0 bytes named DONE. Then all files in the subdirectory p5 (but not in subdirectories of the subdirectory p5) will be collected. I will test your program using your Makefile. Note: This assignment depends on classes Lex, Word, HashTable, Link, and Scan developed in p1, p3 and p4. You can use the object files, lex.o, scan.o, and word.o, as well as lex.h, scan.h, link.h, hashtab.h, and word.h. These files are in /proj/csc109/p5. If you use them, it is VERY IMPORTANT that you use them as I direct. Remember, you don't want to aggravate me. First, copy the .h files into your directory, but do NOT COPY the .o files: your makefile will (should) erase them with the command "make clean". Second, all references to these .o files in your Makefile should be prefixed with /proj/csc109/p5. For example, the p5: command might be p5: p5.o parse.o /proj/csc109/p5/lex.o /proj/csc109/p5/word.o /proj/csc109/p5/scan.o g++ -o p5 parse.o p5.o /proj/csc109/p5/lex.o /proj/csc109/p5/word.o /proj/csc109/p5/scan.o where the files for a given command are all on one line, which emacs will allow, but for which I have insufficient room here. Thus, the above example from the Makefile has only two (long) lines. Note: I will store some test files in /proj/csc109/p5. I will use these files, and perhaps others, to test your program. Note: You should compare the behavior of your program to the behavior of YAPL, my solution to this assignment. NOTE: IT IS VERY IMPORTANT THAT YOU USE A CLASS, SAY Parser, FOR DOING THE ACTUAL PARSE. THIS WILL MAKE ASSIGNMENT #7 MUCH EASIER (IT WILL BE IMPLEMENTED BY MAKING A SUBCLASS OF Parser). Once again I remind you to avoid unfair collaboration. To understand what is meant by "unfair collaboration" read the syllabus again.