Parsing binary expressions

In this recipe, you will learn how to parse a binary expression.

Getting ready

We must have the custom-defined language—that is, the toy language in this case—and also stream of tokens generated by lexer. The binary expression parser requires precedence of binary operators for determining LHS and RHS in order. An STL map can be used to define precedence of binary operators.

How to do it…

To parse a binary expression, proceed with the following code flow:

  1. Open the toy.cpp file as follows:
    $ vi toy.cpp
  2. Declare a map for operator precedence to store the precedence at global scope in the toy.cpp file as follows:
    static std::map<char, int>Operator_Precedence;

    The TOY language for demonstration has 4 operators where precedence of operators ...

Get LLVM Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.