next up previous
Next: countassg.cc Up: Overview Previous: What is C-Breeze not?

A Simple Example: Counting Assignments

Let's look at a simple application of C-Breeze. Suppose we want to count the number of assignments in a C program. That is, we want to know how many assignment expressions (e.g. a = b + c, a += 2) occur in the program. We'll write some code that will walk C-Breeze's AST for the program, counting assignments as it goes along.

To do this, we'll write a walker. Walker is a C++ class in C-Breeze that traverses an AST. Our walker will be a subclass of Walker. Walker has special virtual functions that are called at each different kind of node in the tree. These functions do nothing by default, but we can override them to do whatever we want. The function we are interested in is at_binary(); this function is called whenever a binary operator node is visited. We'll override this function with our own version that will check whether the binary operator is an assignment, and if so increment a counter. Then we'll write a Phase that can be invoked from the command line, to run our subclass of Walker on each translation unit of the input C program. Here is the C++ code; we'll call it countassg.cc:



 
next up previous
Next: countassg.cc Up: Overview Previous: What is C-Breeze not?
Calvin Lin
2002-01-31