Semantic Action Class

Public

Method

Execute ( ActionNumber, LastToken )
This method is used to invoke the semantic actions themselves from the parser. ActionNumber provides the number of the action to execute, and LastToken is the token just handled by the parser. The Execute method consists of a switch statement that passes control to the appropriate action on the basis of the value of ActionNumber.

Protected

Data Members

SemanticStack
The semantic stack is used internally by the semantic actions. It needs to be capable of holding information of various types, so the items on the stack must be defined in a flexible way.

In Java, the easiest way to do this is to define the semantic stack as a stack of Objects so that any type can be pushed on. You will of course then have to cast things that you remove from the stack.

In C++, things are more complicated. One solution is to use the union type, as follows:

struct StackItem
{
  ItemType type;         // a discriminator so you can check what kind
                         // of thing the item actually is, if necessary
  union
  {
    TokenType* token;    // pointer to a token
    TableEntry* entry;   // pointer to entry in symbol table
    bool isArithmetic;   // indicates arithmetic or relational, for expressions
    int quadIndex;       // hangs on to integers indexing quadruples
    IntList* listPtr;    // pointer variable for ParmCount, E.True and E.False
  } ;
};
ItemType is an enumerated type for the different things in the union, e.g., TokenItem, EntryItem, ArithItem, QuadindexItem, ListptrItem. To create an item to put on the stack, declare it with an initialization. For example, if the item is a token and Current_Token contains the token to be pushed, use

        StackItem item;
        item.type = TokenItem;
        item.token = Current_Token;

To set up the semantic stack itself you use

        stack<StackItem> SemStack;
 
To push items on the stack, use the usual operators, e.g.,

        SemStack.push(item);


Quadruple array

You need to store your intermediate code ("TVI code") as it is being generated with the GEN function, in an array with integer indexes. Each element of the array is an array representing a quadruple, with four fields containing strings. Quadruples can be a separate class, in which you define the array and the methods to add to it, retrieve the contents of a given quadruple, retrieve the value of nextquad, etc.