|
CMSC 330, Summer 2016Organization of Programming LanguagesProject 4 Part A - Small C ParserIntroductionIn this project, you will write a parser for SmallC, a tiny subset of the C programming language. As part of the project, you will write a parser that translates a plain text C program into an abstract syntax tree (AST), and a pretty print function that formats the code in a specific style. If you want to leran C, you can revisit the textbook from CMSC216. For purposes of this project, we will only test your parser with valid input. Thus your code may do whatever you want on a bad input. We do, however, recommend adding reasonable error handling code to your project for cases of malformed or otherwise incorrect C input, because it will make developing your project easier. As you are testing your program, you may inadvertently create incorrect input data; substantial time may be lost in trying to debug the program, only to find a few mistyped characters in your input data are the source of the problem. In Project 4 Part B, you will write an interpreter for SmallC. The interpreter executes the code represented as an AST, which is generated by the parser in this project. Imperative programming is not allowed. You can use semicolon seperated print statements. If you do not finish this project, you will not be able to do Project 4 Part B. If you find any error in the description or in the test files, report it to the instructor. Make sure you check the piazza announcements and errata section of the project periodically. Getting StartedDownload the following archive file p4a.zip and extract its contents.Along with files used to make direct submissions to the submit server (submit.jar, .submit, submit.rb), you will find the following project files:
Part 1: Parsing SmallCPut your solution to this part in the to do section of file smallc.ml. Your next task is to write a parser for C program, which in this case will be a function that turns a string into a C abstract syntax tree (AST). Your parser will take as input a sequence of tokens, produced by a scanner, which are the terminals of the C grammar, and output the C AST. We've supplied you with a function tokenize : string -> token list that acts as a scanner/lexer, converting the string input into a list of tokens, represented by the following data type: type token = For example, int main(){
int a;
a = 10;
int b;
b = 1;
int c;
c = a + b;
printf(c);
}
becomes a string "int main(){int a; a= 10; int b; b = 1; int c; c = a+b; printf(c);}". when called as tokenize "int main(){int a; a= 10; int b; b = 1; int c; c = a+b; printf(c);}", the return value is [Tok_Int; Tok_Main; Tok_LParen; Tok_RParen; Tok_LBrace; Tok_Int;Tok_Id "a"; Tok_Semi; Tok_Id "a"; Tok_Assign; Tok_Num 10; Tok_Semi; Tok_Int; Tok_Id "b"; Tok_Semi; Tok_Id "b"; Tok_Assign; Tok_Num 1; Tok_Semi; Tok_Int; Tok_Id "c"; Tok_Semi; Tok_Id "c"; Tok_Assign; Tok_Id "a"; Tok_Sum; Tok_Id "b"; Tok_Semi; Tok_Print; Tok_LParen; Tok_Id "c"; Tok_RParen; Tok_Semi; Tok_RBrace; Tok_END] What to do: You must write a function parse : token list -> ast that takes as input a list of tokens (returned from tokenize) and returns an AST. In this project, parse_Function : token list -> ast * token list is the parse function. Once you have done this, you can run it using the code in the file parser.ml. You are not allowed to use parser generator tools. You should use the idea of a recursive descent parser, as we discussed in class. Thus we suggest you write a function: parse_Function, which parses the non-terminal Function representing a single C function. parse_Function calls other fucntions such as parse_methodBody, parse_Statement to parse the body of the function. The context free grammar for smallC is given next, followed by the OCaml type definition ast of SmallC abstract syntax trees. SmallC GrammarThe grammar for C you need to support is as follows
basicType-> 'int'
mainMethod-> basicType 'main' '(' ')' '{' methodBody '}'
methodBody->(localDeclaration | statement)*
localDeclaration->basicType ID ';'
statement->
whileStatement
|ifStatement
|assignStatement
|printStatement
assignStatement->ID '=' exp ';'
ifStatement -> 'if' '(' exp ')' '{' ( statement)* '}' ( 'else' '{'( statement)* '}')?
whileStatement -> 'while''(' exp ')' '{'(statement )*'}'
printStatement->'printf' '(' exp ')' ';'
exp -> additiveExp (('>' | '<' | '==' ) additiveExp )*
additiveExp -> multiplicativeExp ('+' multiplicativeExp)*
multiplicativeExp-> powerExp ( '*' powerExp )*
powerExp->primaryExp ( '^' primaryExp) *
primaryExp->'(' exp ')' | ID | INITLIT
ID->( 'a'..'z' | 'A'..'Z') ( 'a'..'z' | 'A'..'Z' | '0'..'9')*
INTLIT-> ('0' | ('1'..'9') ('0'..'9')* )
where
SmallC ASTFor this project, a SmallC is represented using an AST (abstract syntax tree) defined using the following OCaml data type: type ast = Part 2: SmallC Pretty PrintIn this section, you will impelment pretty_print, a function that prints the SmallC source code in indented format. Pretty print uses following style:
int
main(){int x;int y;
while(x == y) {x=x+1;
a = 100;}
if(a == b){
printf(20);
}else{
printf(10);
}
}
pretty print output of above code is:
int main(){
[4 spaces indented]int x;
int y;
[4 spaces indented]while(x == y){
[4 spaces]x = x + 1;
a = 100;
}
if(a == b){
[4 spaces]printf(20);
}else{
[4 spaces]printf(10);
}
} [print a single "\n" here.]
Testing and SubmissionWe will test your project by calling your parsing and evaluation functions directly, so be sure to give those functions the types we expect, as given above. You can work on the interpreter and parser in any order, we will test each part independently.You may assume that all input test cases are syntactically correct. If the input SmallC code is not legal you may perform any action (e.g., exit, throw an exception). All your code should be in one fils, smallc.ml. You can submit your project in two ways:
Academic IntegrityThe Campus Senate has adopted a policy asking students to include the following statement on each assignment in every course: "I pledge on my honor that I have not given or received any unauthorized assistance on this assignment." Consequently your program is requested to contain this pledge in a comment near the top. Please carefully read the academic honesty section of the course syllabus. Any evidence of impermissible cooperation on projects, use of disallowed materials or resources, or unauthorized use of computer accounts, will be submitted to the Student Honor Council, which could result in an XF for the course, or suspension or expulsion from the University. Be sure you understand what you are and what you are not permitted to do in regards to academic integrity when it comes to project assignments. These policies apply to all students, and the Student Honor Council does not consider lack of knowledge of the policies to be a defense for violating them. Full information is found in the course syllabus---please review it at this time. [an error occurred while processing this directive] Copyright NoticeThis course project is copyright of Dr. Anwar Mamat. ©Anwar Mamat [2016]. All rights reserved. Any redistribution or reproduction of part or all of the contents in any form is prohibited without the express consent of the author. |