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

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...