Nolife Compiler (Part 1):
A Context-Sensitive Analyzer
CS4131
Spring 2005
Due Date: Friday, March 18, 2005 at 5pm
Purpose
This project is intended to give you experience building a context-sensitive analyzer. You will use the flex scanner generator and the bison parser generator system. You will get experience manipulating a programming language grammar and building an abstract syntax tree and performing context-sensitive analysis. Read this document and the accompanying Nolife documents completely before embarking on this adventure.
Project Summary
Your task is to construct a context-sensitive analyzer that accepts the Nolife programming language. A parser will obtain input by calling a lexical analyzer (scanner) on a token-by-token basis and build an abstract syntax tree of the program. Your parser will construct an abstract syntax tree (AST) to represent the program structure. After the AST is constructed, another pass should be made over the AST to perform type checking. The output of your program will be a listing of the abstract syntax tree for the input program or a list of error messages.
The Scanner and Parser
You are to construct a scanner using the flex scanner generator system and a parser using the bison parser generator system with the grammar provided. You should proceed in three distinct steps.
The form of the abstract syntax tree is specified in a document titled "An Abstract Syntax Tree for Nolife". That document also specifies the format for listing the tree.
Context-Sensitive Analysis
To compile a Nolife program requires a large amount of knowledge that cannot be detected in a context-free parse. This suggests a compiler design that includes a separate context-sensitive analyzer. The analyzer should perform the following tasks:
These tasks can be performed in two separate passes over the tree, or they can be compressed into a single tree-walking pass. Of course, since each pass requires use of an accurate block-structured symbol table, a single-pass implementation will be more efficient.
The next two subsections describe these tasks in more detail.
Mixed Type Expressions
Nolife supports three basic data types: integer, floating point, and character. Each expression and subexpression has a type that can be determined at compile time. Your lab should determine (1) the type of each subexpression, (2) where coercions must be inserted, and (3) where invalid type combinations exist.
Figure 1 gives type conversion tables for several of the Nolife operators.
Your lab will need to insert the appropriate conversions and report any expressions that would require an illegal coercion.
Context-Sensitive Errors
Your lab should detect the following context-sensitive errors and report them to the user on stderr.
Nolife Specification
The syntax of Nolife is specified in a document titled "Nolife: A Language For Practice Implementation". The grammar given in that document will need to be massaged to create one that is acceptable for input to bison. The following additional information may be of use.
The scope defined by the main program is called the global scope. Names declared in the global scope are accessible from the point of declaration to the end of the program, including the body of the main program and any subprograms that do not redeclare the same name. All variables declared in the main program are in the global scope.
Requirements
Write all of your code in C or C++. It will be tested on wopr and MUST work there. You will receive no special consideration for programs which "work" elsewhere, but not on wopr.
Your code should be well-documented. You will need to submit a brief
report of what works, main difficulties you encountered, and any other
relevant information that you consider will help in grading your work.
You will submit all of your files, INCLUDING a makefile to build
your compiler. I will just type "make" and expect everything to "come
out right."
(Come out right in this context means I can type "nc
foo.nl"
and I will get the AST of foo.nl dumped to stdout.)
Your program will be graded on whether it produces a correct AST for
all correct programs and whether it catches the required
context-sensitive errors. Extra credit will be given for mnemonic
parse error messages. If you modify the AST definition given in the
document "An Abstract Syntax Tree for Nolife", you must document your
changes and submit that document in a file called "ASTChanges".