diff options
| Commit | db518ce607d467e2ec89531b8ae5ba73a81e6f57 (patch) | |
|---|---|---|
| Author | 2023-04-17 16:29:53 +0300 | |
| Committer | 2023-04-17 16:29:53 +0300 | |
| Files | 211ba19ae8828afa979f42750ae793772a09e445 | |
| syndamia.com-db518ce607d467e2ec89531b8ae5ba73a81e6f57.tar syndamia.com-db518ce607d467e2ec89531b8ae5ba73a81e6f57.tar.gz syndamia.com-db518ce607d467e2ec89531b8ae5ba73a81e6f57.zip | ||
| Parent | 752b4fb998d8b5e9943f098e23585061dbbaa7dc (diff) | |
[blog/seoc] Added information in together section
1 files changed, 13 insertions, 2 deletions
| -rw-r--r-- | src/blog/sane-explanation-of-compilers/part-1.md | 15 |
diff --git a/src/blog/sane-explanation-of-compilers/part-1.md b/src/blog/sane-explanation-of-compilers/part-1.md index 712bebe..e949e39 100644 --- a/src/blog/sane-explanation-of-compilers/part-1.md +++ b/src/blog/sane-explanation-of-compilers/part-1.md @@ -1011,8 +1011,19 @@ Now when you run `./a.out`, if you enter a valid string, you'll see a blank line The way we created that lexer and parser worked, but it wasn't very useful. Ideally, we want the lexer to also return the exact value the regex matched and we need to be creating our AST in the parser. -Regarding the first problem, we've already returned a value, we could return some sort of structure, but that wouldn't work with the way our code is generated. -What we can do is set a variable +Regarding the first problem, we've already returned a value, we could return some sort of `struct`, but that wouldn't work with the way our code is generated. +Since everything is compiled into one program, we could use a global variable, which we set in the lexer and use in the parser code. +That variable is [already defined](https://www.gnu.org/software/bison/manual/html_node/Token-Values.html) and is called `yylval`. + +As with everything else, we define it's properties inside our grammar (yacc/bison) file. +By default it's type is `int` (in Bison), but usually you would want it to be a [union](https://en.wikipedia.org/wiki/Union_type#C/C++). +For now let's simplify our lives and make it a union of int and char[^yylval-type]. + +Second, we need to make a tree. +I'll not go into details, but we'll use a normal C `struct`ure for a node, and every node has it's value (a character) and two pointers, one to the left and one to the right. +After we've matched a string, I'll print the tree in a nice to view format. + +All in all, we'll have: ## Appendix |