programming assignment
project 1/Assignment 1.pdf
Assignment 1 CS323 Due on 6/14/2015 (Sunday 10PM, softcopy) 6/15/2015 (Monday 5Pm, hardcopy) The first assignment is to write a lexer(lexical analyzer). You can build your entire lexer using a FSM, Or build using at least FSMs for identifier, integer and real (the rest can be written ad-hoc) Note: In your documentation (design section), YOU MUST write the REs for Identifiers, Real and Integer, and also show the NFSM using Thompson. The Lexer A major component of your assignment will be to write a procedure (Function) that returns a token when it is needed. Your lexer should return a record, one field for the token and another field the actual "value" of the token (lexeme), i.e. the instance of a token. Your main program should test the lexer i.e., your program should read a file containing the source code of Rat13SU generate tokens and print out the results to a file. Make sure that you print both, the tokens and lexemes. Basically, your main program should work as follows while not finished (i.e. not end of the source file) do call the lexer for a token print the token and lexeme endwhile Do at least 3 test cases and make sure that you turn in proper documentation. (See the handout). Example while (fahr < upper) a = 23.00; token lexeme keyword while separator ( identifier fahr operator < identifier upper separator ) identifier a operator = real 23.00 separator ;
project 1/Compiler Project.pdf
1
Compiler Project for CPSC323 The programming assignments are based on a language called "Rat15su" which is described as follows. The Rat15su language is designed to be easy to understand. It has a short grammar and relatively clean semantics.
RAT15SU 1) Lexical Conventions: The lexical units of a program are identifiers, keywords, integers, reals, operators and other separators. Blanks, tabs and newlines (collectively, "white space") as described below are ignored except as they serve to separate tokens. Some white space is required to separate otherwise adjacent identifiers, keywords, reals and integers. <Identifier> is a sequence of letters and digits The first character must be a letter and the last character CANNOT be a digit Upper and lower cases are same. <Integer> is an unsigned decimal integer i.e., a sequence of decimal digits. <Real> is integer followed by “.”and integer, e.g., 123.00 Some identifiers are reserved for use as keywords, and may not be used otherwise: e.g., int, if, else, fi, return, read etc 2) Syntax rules The following BNF describes the Rat15SU. <Rat15su> ::= $$ <Opt Function Definitions> $$ <Opt Declaration List> <Statement List> $$ <Opt Function Definitions> ::= <Function Definitions> | <Empty> <Function Definitions> ::= <Function> | <Function> <Function Definitions> <Function> ::= function <Identifier> ( <Opt Parameters>) <Opt Declaration List> <Body> <Opt Parameters> ::= <Parameters> | <Empty> <Parameters> ::= <Parameter> | <Parameter> , <Parameters> <Parameter> ::= <Identifier> <Qualifier> <Opt Declaration List> ::= <Declaration List> | <Empty> <Declaration List> := <Declaration> ; | <Declaration> ; <Declaration List> <Declaration> ::= <Qualifier > <IDs> <Qualifier> ::= integer | boolean | real <IDs> ::= <Identifier> | <Identifier>, <IDs> <Body> ::= { < Statement List> } <Statement List> ::= <Statement> | <Statement> <Statement List> <Statement> ::= <Compound> | <Assign> | <If> |<Return> | <Write> | <Read> | <While> <Compound> ::= { <Statement List> } <Assign> ::= <Identifier> = <Expression> ; <If> ::= if ( <Condition> ) <Statement> fi | if ( <Condition> ) <Statement> else <Statement> fi <Return> ::= return <Expression> ; <Write> ::= write ( <Expression>); <Read> ::= read ( <IDs> ); <While> ::= while ( <Condition> ) <Statement> <Condition> ::= <Expression> <Relop> <Expression>
2
<Relop> ::= == | != | > | < <Expression> ::= <Expression> + <Term> | <Expression> - <Term> | <Term> <Term> ::= <Term> * <Factor> | <Term> / <Factor> | <Factor> <Factor> ::= - <Primary> | <Primary> <Primary> ::= <Identifier> | <Integer> | <Identifier> (<IDs> ) | ( <Expression> ) | <Real> | true | false <Empty> ::= <Identifier>, <Integer>, <Real> are token defined above 3) Some Semantics Rat15su is a conventional imperative programming language. A Rat15su program
consists of a sequence of functions followed by the "main body" where the program executes.
All variables and functions must be declared before use. There is an implied expressionless return at the end of all functions; the value returned by
expressionless return statement is undefined. Arithmetic expressions have their conventional meanings. Integer division ignores any remainder. Type casting is not allowed (e.g., assigning an integer to a real variable) No arithmetic operations are allowed with booleans (e.g., true + false) Others as we will define during the session
4) A sample Rat15su Program $$ function convert (fahr integer) { return 5*(fahr -32)/9; } $$ integer low, high, step; read(low, high, step); while (low < high) { write (low); write (convert (low)); low = low + step; } $$
3
VERY, VERY IMPORTANT !!! For each programming assignment, you should turn in the following: 1) A hardcopy of a) About 2 pages of documentation including i) your name and your partner’s name ii) Problem Statement iii) How to use your program (very important) iv) Discuss the design of your project and choice of the algorithms v) Describe the limitations of your program (e.g. maximum # of lines etc.) and any shortcomings. b) Source code listing with proper comments for each procedures, sections if necessary. c) Test cases. Find at least 3 test cases (< 5, < 10, > 10) and the results of the testing 2) You will also to submit a softcopy of all mentioned in (1) and executable file
(.exe) under DOS/WINDOW using the “submit” feature on Titanium. Before submission, Zip your files and submit the “zipped” file.
Note: 1. I must be able to run your program in order to give you a grade. 2. I will accept late project however, there will be some deductions: 1 point deduction for the first day and 0.2 each day you are late (from max of 10). For example, if you are late for one week, then the maximum point you will get is 10 – ( 1 + 1.2 ) = 7.8 3. If you turn in a program that cannot be run, there will be an automatic 2 points deduction. 4. If you don’t turn in the documentation, there will be an automatic 2 points deduction. Final Notes: - You will most likely not pass this course without doing the projects
- The projects are built on each other, so make sure that you do well the first project