In this recipe, you will learn how to call the parser function from the main function of our TOY parser.
To invoke a driver program to start parsing, define the driver function as shown in the following:
toy.cpp
file:$ vi toy.cpp
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; } } }
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; }
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.
18.189.178.237