Docsity

Scanner and Parser, Semantic Analysis and Code Generation – Project Assignment | CS 3240, Study Guides, Projects, Research of Computer Science

Toggle sidebar
bg1
CS 3240 Project Assignment
Part I: Scanner and Parser, due: 11:59pm on WebCT, Oct. 26, 2006
Part II: Semantic Analysis and Code Generation, due: 11:59pm on WebCT, Nov. 30, 2006
November 20, 2006
Total: 100 points plus 25 bonus points for each part
Guidelines
1. You are advised to do the project in a group of two to three students. Collaboration
helps learning. Working in groups means actually sitting together and code/discuss.
While you are allowed to inspire yourself from previous programs, under no cir-
cumstance may you copy literally the solutions from any source.
2. Your project will be graded based on the correctness of your code and documentation.
But remember that clean structured code is easier to debug. Plan substantial amount
of time in advance for debugging your code.
3. You can use C, C++, or Java for the programming assignment. It should compile and
run on CoC UNIX machines. You will need to give a demo of each part of the project
near the due date, and you are expected to explain what your code does during your
demo sessions.
1 Introduction
The goal of this project is to construct a simplified C compiler. It will not be a full-fledged
C compiler, as a substantial portion of the language has been omitted to make the project
feasible in one semester. Even with this simplification, the project should be done as a
team effort with two to three students, and it is no small feat.
You will generate a program that will take a subset of C, from now on called C∗, and
generate instructions in the assembly language for a stack processor machine. You are
provided with a virtual machine that emulates the stack processor and allows you to run
1
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Scanner and Parser, Semantic Analysis and Code Generation – Project Assignment | CS 3240 and more Study Guides, Projects, Research Computer Science in PDF only on Docsity!

CS 3240 Project Assignment

Part I: Scanner and Parser, due: 11:59pm on WebCT, Oct. 26, 2006

Part II: Semantic Analysis and Code Generation, due: 11:59pm on WebCT, Nov. 30, 2006

November 20, 2006

Total: 100 points plus 25 bonus points for each part

Guidelines

  1. You are advised to do the project in a group of two to three students. Collaboration helps learning. Working in groups means actually sitting together and code/discuss. While you are allowed to inspire yourself from previous programs, under no cir- cumstance may you copy literally the solutions from any source.
  2. Your project will be graded based on the correctness of your code and documentation. But remember that clean structured code is easier to debug. Plan substantial amount of time in advance for debugging your code.
  3. You can use C, C++, or Java for the programming assignment. It should compile and run on CoC UNIX machines. You will need to give a demo of each part of the project near the due date, and you are expected to explain what your code does during your demo sessions.

1 Introduction

The goal of this project is to construct a simplified C compiler. It will not be a full-fledged C compiler, as a substantial portion of the language has been omitted to make the project feasible in one semester. Even with this simplification, the project should be done as a team effort with two to three students, and it is no small feat. You will generate a program that will take a subset of C, from now on called C∗, and generate instructions in the assembly language for a stack processor machine. You are provided with a virtual machine that emulates the stack processor and allows you to run

your compiled code. Full description of the machine is included in this document. The machine is very simple to allow you to focus on the compiler itself, rather than learning computer architectures. To complete this project you’ll use all sort of knowledge covered in class, including regular languages, grammar, parsing and syntax trees, semantics analysis, etc..

2 The language C∗

The language C∗^ is a subset of C. For detailed explanation of the functionality of C∗, please refer to ANSI C for interpretation. A C∗^ program is defined in a single file, without support for preprocessing. The language is case-sensitive. The basic types in C∗^ are int, float, and void, where void is used for function return values and for pointers. The int and float types work in the same way as their C counter- parts, and they can be mixed in expressions by automatic type conversion or type casting. From these basic variable types, it is possible to make more complex variables like struct, enum or arrays. A variable can be one of the simple or complex types, or it could be a pointer to one of those types. Our C∗^ has no memory allocation, so pointers can only be declared, manipulated, casted, assigned and operated on. C∗^ has two conditional code structures: the if statement with an optional else part, and the switch statement with one or more case parts. The ANSI C convention applies here. The conditions in the if statement must be an integer or the result of a comparison, which evaluates to an integer. On the switch cases, only integer or enum constants can be used the case statements. C∗^ supports two types of loops, including the while and the do-while statements. These structures follow the C syntax. It is possible to break the loop using the special instructions break and continue. We allow comparisons between simple types, including integer with integer, float with float, and integer with float. Comparison between structures or arrays are not allowed. Enums are actually integers, so it is OK to compare them with an integer or float. We allow comparisons using one of the following operators: {==,! =, <, ≤, >, ≥}. C∗^ has functions similar to those in C. A function returns a value, which may be a void type. Any type (a basic type or a struct) or its pointer can be a parameter to a function. All parameters are passed by value, i.e. they are copied into the function. The only way to have more than one return value is to use pointers. The scopes of variables in C∗^ work in the same way as in C. There are global variables, function variables, and then block variables (variables declared inside a block). The more nested variables have precedence. The struct type also works in the same way as in C, and it can have many members and be of basic or derived types. Their access follows the same convention as in C. An enum defines a new type to be used as variable type, which is translated to an integer. It also defines an ordered list of keywords that belong to the

  • ppc D[SP] = PC; SP++;
  • rpc PC = D[SP-1]; SP–;
  • halt stops the VM. Always put in the end.

3.2 I/O-RELATED

  • ini D[SP] ← read integer from input SP++;
  • inf D[SP] ← read float from input SP++;
  • pri print D[SP-1] as integer
  • prf print D[SP-1] as float
  • prs "string" print the string on the screen

3.3 STACK-RELATED

  • spi num SP = SP+num; [NUM can be negative]
  • push num D[SP] = num; SP++;
  • pushf num like push for floats D[SP] = num; SP++;
  • pop SP–;
  • load D[SP-1] = D[D[SP-1]];
  • store D[D[SP-1]] = D[SP-2]; SP = SP-2;
  • dup D[SP] = D[SP-1]; SP++;
  • psp D[SP] = SP; SP++;
  • lsp SP = D[SP-1];

3.4 DATA-OPERATIONS INTEGERS

  • addi D[SP-2] = D[SP-1] + D[SP-2]; SP = SP-1;
  • subi D[SP-2] = D[SP-1] - D[SP-2]; SP = SP-1;
  • muli D[SP-2] = D[SP-1] * D[SP-2]; SP = SP-1;
  • divi D[SP-2] = D[SP-1] / D[SP-2]; SP = SP-1;
  • negi D[SP-1] = −D[SP-1] ;
  • gti D[SP-2] = D[SP-1] > D[SP-2]; SP = SP-1;

3.6 BITWISE OPERATIONS

  • and D[SP-2] = D[SP-1] & D[SP-2]; SP=SP-1;
  • or D[SP-2]=D[SP-1] | D[SP-2]; SP=SP-1;
  • xor D[SP − 2] = D[SP − 1]ˆD[SP − 2]; SP=SP-1;
  • not D[SP-1] = ¬D[SP-1] ;

3.7 CONVERSION

  • itf Converts D[SP-1] from integer to float
  • fti Converts D[SP-1] from float to integer

4 Tasks

4.1 Part I: Scanner and Parser

During the first stage of the project, you are expected to implement the scanner and the parser for C∗. You may choose to implement them using Lex and Yacc, or implement them manually. The former approach is preferred over the latter. The key output of this stage should be the syntax trees. In addition, the parser would also need to interact with the symbol table, but you may choose to implement the symbol table during Part II of the project.

4.1.1 Deliverables

For each part, you must submit your code and documentation to WebCT by the due date shown on the front of this document. The code will cover 60% of the points, and will be graded based on its correctness on generating the correct syntax trees for a number of test programs. The documentation will cover 40% of points, and it must explain the following:

  1. What regular expressions did you use for the scanner.
  2. Explain your grammar. Is your grammar ambiguous? If so, how did you resolve them?
  3. Explain the design and the construction of the syntax tree.
  4. Document the contributions of each group member.

4.1.2 Bonus Points

Error checking and recovery is not required for this project, but they are highly desirable for real compilers. An extra 25 points will be honored for systematic efforts on error checking and simple error recovery with useful error messages, including 15 points on the implementation and 10 points for its documentation.

4.2 Part II: Semantic Analysis and Code Generation

In this part, your need to perform semantic analysis (including manipulation of symbol table, type checking and inference) and code generation.

4.2.1 Deliverables

As for part I, you must submit your code and documentation to WebCT by the due date. Your code will cover 60% of points, and will be graded based on its correctness on generating the correct and runnable code for a number of test programs. The documentation will cover 40% of points, and it must explain the following:

  1. The attribute grammars for symbol-table manipulation and type checking;
  2. Explain the translation from the syntax tree to the assembly language for the stack processor.
  3. Document the contributions of each group member.

4.2.2 Bonus Points

Extra 25 points will be honored for correct implementation for code generation of the switch statements and do-while loops, including 15 points on the implementation and 10 points for its documentation.

addi push 0 store ; DECLARE LOCAL VARIABLES spi 2 prs Enter a number ini push 0 load store ; FACTORIAL CALL spi 1 ppc push 10 ; OFFSET TO END OF CALL addi push 0 load push 0 load load push 38 ;ADDRESS OF FACTORIAL rpc ; END OF CALL prs The factorial is pri push 0 load lsp push 0 store rpc ; START OF THE FACTORIAL FUNCTION push - psp addi push 0 store push 0 load load push 2

gti jnz 15 ;OFFSET TO END OF IF push 0 load load push 0 load push - addi store push 0 load lsp push 0 store rpc ; END OF IF push 0 load load ; FACTORIAL RECURSIVE CALL spi 1 ppc push 12 ;END OF RECURSIVE CALL addi push 0 load push 0 load load push - addi push 38 ;ADDRESS OF FACTORIAL rpc ; BACK FROM RECURSIVE CALL muli push 0 load push - addi store