Docsity

The Language C - Languages and Computation - Notes | CS 3240, Assignments 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 Analyzer and Code Generator, due: 11:59pm on WebCT, Nov. 30, 2006
September 28, 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 simplied C compiler. It will not be a full edged
C compiler, as a substantial portion of the language has been omitted to make the project
feasible in one semester. Even with this simplication, the project should be done as a
team eort 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 The Language C - Languages and Computation - Notes | CS 3240 and more Assignments 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 Analyzer and Code Generator, due: 11:59pm on WebCT, Nov. 30, 2006

September 28, 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 simplied C compiler. It will not be a full edged C compiler, as a substantial portion of the language has been omitted to make the project feasible in one semester. Even with this simplication, the project should be done as a team eort 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 sorts 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 dened in a single le, without support for preprocessing. The language is case-sensitive. The basic types in C∗^ are int, oat, and void, where void is used for function return values and for pointers. The int and oat 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 for the case constants. 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, oat with oat, and integer with oat. Comparison between structures or arrays are not allowed. Enums are actually integers, so it is OK to compare them with an integer or oat. 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 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 accesses follow the same convention as in C. An enum denes a new type to be used as variable type, which is translated to an integer. It also denes an ordered list of integer constants that belong to

3.1 CODE-RELATED

  • jmp num PC = PC+num [NUM can be negative]
  • jnz num will do a JMP num if the zero ag is not
  • jne num will do a JMP num if the neg ag is not set
  • 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 oat from input SP++;
  • pri print D[SP-1] as integer
  • prf print D[SP-1] as oat
  • 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 oats 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++;

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;

4 Tasks

4.1 Part I: Scanner and Parser

During the rst 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, which should be organized internally for later semantics analysis and also be printed out in a readable form for correctness verication and grading. 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 on 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 team 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 eorts on checking and simple recovery of syntax errors with useful error messages, including 15 points on the implementation and 10 points for its documentation.

4.2 Part II: Semantic Analyzer and Code Generator

In this part, your need to perform semantic analysis (including manipulation of symbol table, type inference and checking, etc.) and code generation. We will provide the code for a Virtual Machine that interprets the stack machine assembly code. Your compiler's output should run in that virtual machine. For the basic requirement of this part, you need not handle the switch statements and do-while loops (but you should support the if statements and the while loops, along with other requirements). Support for switch and do-while will account for bonus points.

4.2.1 Deliverables

As for part I, you must submit your code and documentation on 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 inference;
  2. Explain the translation from the syntax tree to the assembly language for the stack processor.
  3. Document the contributions of each team 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.

A Sample Code

To give you an idea how C∗^ works, here is a short example:

int g_int; float g_float;

int Factorial(int i) { if (i < 2) return i; return i*Factorial(i-1); }

void main() { int n, f; Print("Enter a number: "); n = ReadInt(); f = Factorial(n); Print("The factorial is :"); PrintInt(f); }

push 17 rpc ; back from ret call muli psp push - addi store rpc ; end of if pop psp push - addi store rpc