Let's write a compiler, part 5: A code generator
Joker_vD 2021-08-19 15:08:41 +0000 UTC [ - ]
That simplifies adding symbols: you keep checking for duplicates until the depth of the symbols you see drops below your depth, and actual insertion doesn't need the pointer to the list's last element, you insert at the head.
That simplifies destroying symbols: you keep popping the head until you meet a TOK_PROCEDURE at which point you stop.
That simplifies looking symbols up: you search until the first name match and return it immediately, instead of remembering the last seen matching symbol while traversing the full list.
I am not even talking about efficiency (although of course this implementation is also more efficient in all 3 use cases), it's about code simplicity: the less context you need to maintain during a list traversal, the easier it is to understand what this traversal looks for.
p4bl0 2021-08-19 09:04:34 +0000 UTC [ - ]
chrisseaton 2021-08-19 10:36:10 +0000 UTC [ - ]
This is my effort - trying to show genuine data structures and processes.
junon 2021-08-19 09:12:11 +0000 UTC [ - ]
im3w1l 2021-08-19 13:14:36 +0000 UTC [ - ]
while expression is not a numeral
replace all "\((NUMERAL)\)" with first group
if find first "(NUMERAL)([*/])(NUMERAL)"
replace with result
else if find first "(NUMERAL)([+-])(NUMERAL)"
replace with result
ashton314 2021-08-19 15:06:21 +0000 UTC [ - ]
im3w1l 2021-08-19 16:51:05 +0000 UTC [ - ]
p4bl0 2021-08-19 09:37:04 +0000 UTC [ - ]
But nonetheless even if regexps would not be able to validate syntax, it does not mean that the source-to-source transformation could not be (mostly) achieved using them, because the source and target language are quite similar.
Imagine a new language exactly like C but every semicolon is replaced by a duck "\_o<". You could not parse it using regexps, but regexp could mostly "compile" it to C as it only requires to look for all "\_o<" to replace them with ";".
beecafe 2021-08-19 10:07:48 +0000 UTC [ - ]
*A real Regular Expression. The extended versions with e.g backrefs can. But they're also Turing complete anyway
p4bl0 2021-08-19 10:13:48 +0000 UTC [ - ]
fjfaase 2021-08-19 11:00:35 +0000 UTC [ - ]
ModernMech 2021-08-19 12:18:49 +0000 UTC [ - ]
https://news.ycombinator.com/item?id=28184187
https://news.ycombinator.com/item?id=28199971
https://news.ycombinator.com/item?id=28209950
Alternatively, congrats to the author, for managing to get his blog on the front page 4 out of the last 5 days in a row. Has that ever been done before on HN, I wonder? It's gotta be some sort of record.
ingve 2021-08-19 12:48:49 +0000 UTC [ - ]
[0] https://craftinginterpreters.com/
[1] https://news.ycombinator.com/item?id=27997167
[2] https://github.com/langjam/langjam
chrisseaton 2021-08-19 12:45:34 +0000 UTC [ - ]
People submit it and upvote it.
ofiryanai 2021-08-19 10:23:14 +0000 UTC [ - ]
I'm trying to write sort of a SQL compiler. The current goal is to analyze queries and find similarities, later maybe to translate between sql dialects. I found Uber's QueryParser[1] but it's in haskell, so I started wrapping the python sqlparse[2] library and implement a Visitor to traverse their weird AST. 1. How close is it to implementing a compiler? 2. Is there theory you can suggest further reading for that matter? 3. Would you use a different language/library then I picked?
Thanks :)
[1] https://github.com/uber/queryparser [2] https://github.com/andialbrecht/sqlparse
refneb 2021-08-19 12:28:10 +0000 UTC [ - ]
[1] https://www.antlr.org/
[2] https://github.com/antlr/grammars-v4
codr7 2021-08-19 11:23:27 +0000 UTC [ - ]
https://github.com/codr7/swifties
ofiryanai 2021-08-19 12:39:44 +0000 UTC [ - ]