next up previous
Next: Specification Files Up: A Generated Solution Previous: Implementing the Memory

   
Initialization and Execution

Tree construction and execution of the interpreter are closely bound to the generator that processed the specifications for the tree and the computations. That generator exports NODEPTR as the type of the tree node. LIGA_ATTREVAL is the operation that should be applied to the root to carry out the specified computations, but the generator does not export its prototype:

Make the prototype of the evaluator available[46] :

extern void LIGA_ATTREVAL(NODEPTR);
This macro is invoked in definition 52.

The generator also constructs a function for each rule. Its name is the rule name prefixed with Mk, and it has one more parameter than the number of right-hand side symbols (excluding literal terminals). The additional parameter is the first, and specifies the text coordinates (line and column) associated with the node. Here is an example of a rule and the prototype of the function generated from it:

RULE AssignStm: Stm ::= id ':=' Exp END;
NODEPTR MkAssignStm(CoordPtr, CharPtr, NODEPTR)

CoordPtr is a pointer to the data type representing a coordinate. Note that the type of a parameter corresponding to a nonliteral terminal symbol is the type declared as the value of that symbol, while the type of a parameter corresponding to a nonterminal symbol is always NODEPTR.

Finally, the generator exports an initialization function InitTree taking no parameters and delivering no result. InitTree must be invoked before invoking any of the factory methods.

Given this information, construction of the tree is tedious but straightforward:

Code to construct the tree[47] :

NODEPTR tree;

InitTree();

tree =
  MkAxiom(NoPosition,
    MkCompoundStm(NoPosition,
      MkAssignStm(NoPosition,
        "a",
        MkOpExp(NoPosition,
          MkNumExp(NoPosition,5),
          MkPlus(NoPosition),
          MkNumExp(NoPosition,3)
        )
      ),
      MkCompoundStm(NoPosition,
        MkAssignStm(NoPosition,
          "b",
          MkEseqExp(NoPosition,
            MkPrintStm(NoPosition,
              MkPairExpList(NoPosition,
                MkIdExp(NoPosition,"a"),
                MkLastExpList(NoPosition,
                  MkEseqExp(NoPosition,
                    MkPrintStm(NoPosition,
                      MkLastExpList(NoPosition,
                        MkOpExp(NoPosition,
                          MkIdExp(NoPosition,"a"),
                          MkDiv(NoPosition),
                          MkNumExp(NoPosition,4)
                        )
                      )
                    ),
                    MkOpExp(NoPosition,
                      MkIdExp(NoPosition,"a"),
                      MkMinus(NoPosition),
                      MkNumExp(NoPosition,1)
                    )
                  )
                )
              )
            ),
            MkOpExp(NoPosition,
              MkNumExp(NoPosition,10),
              MkTimes(NoPosition),
              MkIdExp(NoPosition,"a")
            )
          )
        ),
        MkPrintStm(NoPosition,MkLastExpList(NoPosition,MkIdExp(NoPosition,"b")))
      )
    )
  );
This macro is invoked in definition 52.

Once the tree has been constructed, LIGA_ATTREVAL can be invoked on it to carry out the specified computations. In this case, however, those computations depend on the memory ADT and that ADT must be initialized:

Code to carry out the computations over the tree[48] :

Initialize the memory module[45]
LIGA_ATTREVAL(tree);
This macro is invoked in definition 52.


next up previous
Next: Specification Files Up: A Generated Solution Previous: Implementing the Memory
William Waite
1998-08-30