www.angelfire.com/dragon/letstry
cwave04 at yahoo dot com
Free Guestbook
My Guestbook

Back to the virtual laboratory

To understand how we use actions to store information in the intermediate form, let us go back to our virtual laboratory. Last time the lab had three windows. Now it has four. The new window is called the action window, and it shows the action to be taken everytime a reduce step is performed.

The main idea is this. When we use a rule in a reduce step we must collect the attributes in the right hand side of the rule, since once the step is over the righthand side will be replaced by the lefthand side. In our case, only the tokens have attributes, so we need actions for only the rules with tokens in their righthand sides.

Program WindowAction Window
Compiler Window
BNF window
Here we have used the standard C function memcpy to copy the exits. The function is declared in string.h.

The next question is how to insert the above things inside the bison input file. In particular, we have three kinds of things to insert:
  1. the data structure and variables
  2. the actions
  3. the functions arrcpy and find
The data structure and variables all go in the first part of the bison input file. The actions, as we have already mentioned, are to be writen after the rules they are related to. The functions are to be writen in the fourth part.

After inserting all these, the bison input file becomes adv6.y, which is the final version.

Putting everything together

Recall that a compiler consists of two ends: the front end and the back end. We had already implemented the back end earlier. Now we have finished making the front end. The front end is invoked by calling the function yyparse(). The back end is invoked similarly by calling the function emitCode(), which we wrote earlier. So now the main function of the compiler takes the simple form:

main() {
  yyparse();

  emitCode();
}

The complete system is given here.

And that's the end of this tutorial. Hope you enjoyed it!

In this tutorial we have introduced the basics of language design and compiler construction using a carefully crafted example language called AdvLang. The idea is to encourage the reader to try to design similar languages on his or her own. However, this tutorial has deliberately disregarded certain pitfalls that the reader is bound to encounter when creating a new language. In the second part of this tutorial we shall take up those issues.

Prev
© Arnab Chakraborty (2007)

free html visitor counters
hit counter