Running lexer and parser on our TOY language

Now that a full-fledged lexer and parser for our TOY language grammar are defined, it's time to run it on example TOY language.

Getting ready

To do this, you should have understanding of TOY language grammar and all the previous recipes of this chapter.

How to do it…

Run and test the Lexer and Parser on TOY Language, as shown in the following:

  1. First step is to compile the toy.cpp program into an executable:
    $ clang++ toy.cpp  -O3 -o toy
  2. The toy executable is our TOY compiler frontend. The toy language to be parsed is in a file called example:
    $ cat example
    def foo(x , y)
    x + y * 16
  3. This file is passed as argument to be processed by the toy compiler:
    $ ./toy example

How it works…

The TOY compiler will open the example file in read mode. Then, it will tokenize the stream of words. It will come across the def keyword and return DEF_TOKEN. Then, the HandleDefn() function will be called, which will store the function name and the argument. It will recursively check for the type of token and then call the specific token handler functions to store them into respective ASTs.

See also

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

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