CSCI 439: Lab 2 (Fall 2022)

The lab2 repo is now available to fork/clone, following the same processes as lab1, e.g.
ssh csci fork csci439/lab2 csci439/$USER/lab2

In this lab we'll start building and using a symbol table, still with lex and yacc, to check context sensitive information as a program is being parsed. Our checks will essentially address the bonus material from lab 1: identifying variables that are used before being declared or are declared multiple times.

A sample language, .lex, and .yacc file are provided with the lab, where the .lex and .yacc contain partial implementations for a suitable symbol table. The language provided currently supports a sequence of variable declarations (variables declared of type int or float), followed by a sequence of assignment statements (variable = value;). A valid program might thus look like

x int ;
foo float ;
pi float ;
something int ;
something = 3 ;
pi = 3.14 ;
foo = pi ;
x = something ;

The core objective of the lab exercise is to complete the implementation so it correctly flags errors involving use-before-declaration, errors involving multiple-declaration, and type-mismatch errors (while still accepting valid programs of course).

The secondary objective is to add support a sequence of one or more blocks, each of the form { declarations assignments } with scope recognition: so that variables are accessible within the block in which they are declared but are not accessible outside that block. This means a variable can be declared multiple times, as long as they are in different blocks.

What's provided so far:


Building and using a symbol table with lex/yacc


The lab exercise

As stated earlier, the primary part of the lab exercise (worth 70% of the lab mark) is to complete the lex/yacc code so it correctly identifies when variables are used without having been declared, when variables are declared multiple times, and when type mismatches occur (ints and floats are to be treated as incompatible types).

To do so:
(i) in lab2.lex you'll need to complete the insert and lookup functions,
(ii) in lab2.yacc you'll need to complete the C components of the "value: VARNAME" and "action" grammar rules.

The secondary objective of the lab exercise (worth 30% of the lab mark) is to add blocks and basic scope handling, so that variables are only valid in the block in which they are declared, but variables of the same name can be declared as long as they are in different blocks. Valid programs might thus look like:

{
   abc float ;
   i int ;
   j int ;
   abc = 1.1 ;
   j = 10 ;
   i = j ;
}
{
   x int ;
   i int ;
   x =  6 ;
   i = x ;
}
etc

To do so you will need to

One possible approach would be to give each block a unique integer id using a seperate global variable (just increments at the start of each new scope) and including that with the variable information in the symbol table.