CMPU-331 Compilers (Spring 2019)


In this phase, you will implement eight basic semantic actions responsible for processing variable declarations in the code.
(Actions 1, 2, 3, 4, 6, 7, 9 and 13.)


Augmented Grammar

Semantic Actions Phase I

Sample Output

Invocation of semantic actions should appear in your parse traces similarly to match or push as they are part of the function of your parse.

Here is some sample output including semantic actions.


1. SemanticAction Class

Create a SemanticAction class, including declarations for an Execute() function and a semantic stack.

2. Private Data Members

Add the following private data members to the class and initialise them as indicated. They are all booleans:

Name Initialised to Purpose
Insert/Search Insert Flag for Insert or Search mode when accessing symbol table.
Global/Local Global Flag indicating whether currently in global or local environment mode.
Array/Simple Simple Indicates if the next set of variables being declared are arrays or not.
GlobalMemory 0 The next free location in memory assigned for allocation to global variables.
LocalMemory 0 The next free location in memory assigned for allocation to local variables.

Your variable names do not have to match exactly. :-)

3. The Execute() Function

Implement and test the Execute() function. This function is the entry point from the parser and takes a semantic action number and a token as input. It simply determines the action number and calls the appropriate semantic action routine.

4. Semantic Action Routines

Implement the actions listed above. It is easiest to do them in the order below:

  • Actions 1, 2 & 6.
  • Actions 4, 7 & 13. (These require items to be pushed to the semantic stack).
  • Action 9.
  • Action 3.

Action 3 is the most involved. Before you start on action 3, test your code: make sure the others work, your semantic stack behaves as it should, and so forth.

5. Semantic Stack Dump

Implement a “semantic stack dump” routine that prints the semantic stack contents.