Book Image

LLVM Cookbook

Book Image

LLVM Cookbook

Overview of this book

Table of Contents (16 chapters)
LLVM Cookbook
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Invoking a driver for parsing


In this recipe, you will learn how to call the parser function from the main function of our TOY parser.

How to do it…

To invoke a driver program to start parsing, define the driver function as shown in the following:

  1. Open the toy.cpp file:

    $ vi toy.cpp
  2. A Driver function called from the main function, and a parser can now be defined as follows:

    static void Driver() {
      while(1) {
        switch(Current_token) {
        case EOF_TOKEN : return;
        case ';' : next_token(); break;
        case DEF_TOKEN : HandleDefn(); break;
        default : HandleTopExpression(); break;
      }
      }
    }
  3. The main() function to run the whole program can be defined as follows:

    int main(int argc, char* argv[]) {
      LLVMContext &Context = getGlobalContext();
      init_precedence();
      file = fopen(argv[1], "r");
      if(file == 0) {
        printf("Could not open file\n");
      }
      next_token();
      Module_Ob = new Module("my compiler", Context);
      Driver();
      Module_Ob->dump();
          return 0;
    }

How it works…

The main function...