Recommend article: “精读《手写SQL编译器》”. I learned to write from this article.
Last article, I’ve wrote some simple grammar structure of SQL language. Because of time, and the Fronted Part is not the important part in the whole project, I choose to write it in most stupid way.
Build a C++ class for each Node in Syntax Tree.
This is has nothing to say because if it is the most straighted and simple way.
1 | |- SQLAssignStatement.h |
And for each non-leaf node, since you already know the function of the tree, you can ignore other fixed member such as reserved word.
Let’s take Create Table
for example:
1 |
|
I just analyze one by one violently.
And in class Parser
, create a new LexicalAnalyzer
Class and make it to get tokens.
If the first Token is ‘CREATE’ and the second Token is ‘TABLE’ and the third token is ‘{‘ and so on.
This is not a good way but it is very direct.
Maybe I’ll find a better way after finish all basic functions of SQL Database.