Hi all, I have a bit off topic question but seems related. 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. I…
Let's write a compiler, part 5: A code generator
11–20 of 32 posts
Re: Let's write a compiler, part 5: A code generator
#12https://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.
Re: Let's write a compiler, part 5: A code generator
#13Re: Let's write a compiler, part 5: A code generator
#14Hi all, I have a bit off topic question but seems related. 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. I…
Re: Let's write a compiler, part 5: A code generator
#15Hi all, I have a bit off topic question but seems related. 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. I…
Depends on the complexity of your queries, but if you have a narrow subset that you're interested in, implementing a recursive descent parser for just those parts of the syntax that fits your problem like a glove could be a better solution. https://github.com/codr7/swifties
Re: Let's write a compiler, part 5: A code generator
#16The ontology of compilation seems to be the only thing people are engaging with regarding this blog post series here on HN, so I don't see why this series is on the front page every day when the discussion is so stale. If you want to talk about how compiling to C is or isn't compiling, save yourself the trouble, everything has been said already in the past week (including this thread): https://news.ycombinator.com/it…
People submit it and upvote it.
Re: Let's write a compiler, part 5: A code generator
#17The ontology of compilation seems to be the only thing people are engaging with regarding this blog post series here on HN, so I don't see why this series is on the front page every day when the discussion is so stale. If you want to talk about how compiling to C is or isn't compiling, save yourself the trouble, everything has been said already in the past week (including this thread): https://news.ycombinator.com/it…
[0] https://craftinginterpreters.com/
[1] https://news.ycombinator.com/item?id=27997167
[2] https://github.com/langjam/langjam
Re: Let's write a compiler, part 5: A code generator
#18This series of blogposts is amusing but it is kind of frustrating too: the end result is merely a compiler. It is a source to source translator from a source language which is very very similar to a subset of the target language. Well I guess you can call that a compiler, but it really doesn't teach the readers what an actual compiler looks like. Exaggerating a little, I would say that it feels like a few calls to se…
It could not. Unless your language is regular class, then regular expressions cannot parse the language accurately.
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 resultRe: Let's write a compiler, part 5: A code generator
#19Earlier quoted context omitted.
It could not. Unless your language is regular class, then regular expressions cannot parse the language accurately.
You can build a parser around regexes though, where most of the code is regexes and then you have a little bit of code to deal with the irregularity. For instance consider arithmethic expressions consisting of constants, -, +, *, /, and parentheses. You could evaluate that using something like (expression to parse a numeral is left as exercise to reader). while expression is not a numeral replace all "\((NUMERAL)\)"…
Re: Let's write a compiler, part 5: A code generator
#20That 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.