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
    ");
      }
      next_token();
      Module_Ob = new Module("my compiler", Context);
      Driver();
      Module_Ob->dump();
          return 0;
    }

How it works…

The main function is responsible for calling the lexer and parser so that both can act over a piece of code that is being input to the compiler frontend. From the main function, driver function is invoked to start the process of parsing.

See also

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.189.178.237