450: LAB 07

CMPS 450 Lab 07 - Scope Checking

resources:
lab07_files
dragon ch 6

Scope checking is a semantic issue that must be handled by a context-sensitive SDT or attribute grammar in the parser.

The parser in lab07_files parses but does not do any scope or type checking. Integers and floats are passed across as NUMBER. Type checking is not the focus of this lab so you do not need to add that ability. Your job is to modify lab07.y to implement scope checking.

There are two types of scope problems:

 #1) multiple identifiers with the same name declared in the same scope

 #2) a reference to an identifier that has not been declared 
As an example of scope violation #1, this code passes syntax but should generate a semantic scope error.
       int i;
       int i;  
       {
         i = 7;
       }
As an example of scope violation #2, the code below is syntactically correct but it should generate a scope error since there is a reference to identifier 'f' that has not been declared.
       int i;
       {
         f = 7;
       }
The parser in lab07.y can use information in the symbol table to catch both types of scope errors. On the first encounter of an identifier the scanner lab07.l inserts the identifier into the symbol table and sets its type to -1. When the parser parses a declaration such as
     int myid; 
it checks if the type of myid is -1. If so, it sets the type. If not, the parser knows that the identifier has been declared twice and spits out an error msg.

Similarily, while parsing

    {  
       foo = 7;
    } 
the parser checks if identifier foo is -1. If so, the parser knows that the identifier has not been declared and spits out an error msg.

Modify lab07.y to catch these semantic errors. Generate an appropriate error message. You do not need to worry about type checking in this lab. The job of the parser is easy in this simple language because dec_seq must occur before a block. This means that all identifiers must be declared before entering the block. If you are careful you only need to add five or so lines of code to lab07.y. Test your code against bad.cf:

// there are 2 scope errors 
int iname;
int iname;
{ 
  fname = 4;
} 
Sample output:
iname declared twice, lineno 3
fname not declared, lineno 5
semantic error cnt: 2 	lines of code: 7