Defining a TOY language
Before implementing a lexer and parser, the syntax and grammar of the language need to be determined first. In this chapter, a TOY language is used to demonstrate how a lexer and a parser can be implemented. The purpose of this recipe is to show how a language is skimmed through. For this purpose, the TOY language to be used is simple but meaningful.
A language typically has some variables, some function calls, some constants, and so on. To keep things simple, our TOY language in consideration has only numeric constants of 32-bit Integer type A, a variable that need not declare its type (like Python, in contrast to C/C++/Java, which require a type declaration) in the TOY language.
How to do it…
The grammar can be defined as follows (the production rules are defined below, with non-terminals on Left Hand Side (LHS) and a combination of terminals and non-terminals on Right Hand Side (RHS); when LHS is encountered, it yields appropriate RHS defined in the production...