Syntactical analysis
The syntactical analysis is done by the parser, which we will implement next. The base of this is the grammar and the lexer from the previous sections. The result of the parsing process is a dynamic data structure called an abstract syntax tree (AST). The AST is a very condensed representation of the input and is well-suited for semantic analysis.
First, we will implement the parser, and after that, we will have a look at the parsing process within the AST.
A hand-written parser
The interface of the parser is defined in the header file, Parser.h
. It begins with some include
declarations:
#ifndef PARSER_H #define PARSER_H #include "AST.h" #include "Lexer.h" #include "llvm/Support/raw_ostream.h"
The AST.h
header file declares the interface for the AST and is shown later. The coding guidelines from LLVM forbid the use of the <iostream>
library, therefore, the header of the equivalent LLVM functionality is included...