3 ms·
Thanks for sharing that. I've actually been learning some Forth recently and had been wondering how a parser or a compiler might actually look using Forth, and
by trealira 1y ago
Thanks for sharing that. I've actually been learning some Forth recently and had been wondering how a parser or a compiler might actually look using Forth, and you actually already made one, which is pretty cool. I was wondering this because deterministic parsers are often already stack based (LL and LR parsers can both be implemented as imperative programs that manipulate a stack based on the input tokens, or just recursive descent).
I still can't really understand everything just from reading the code, but I had assumed it would be some kind of explicitly stack-based state machine precedence parser, like this article describes: https://h14s.p5r.org/2014/10/shiftreduce-expression-parsing-by-douglas-gregor.html https://h14s.p5r.org/2014/10/shiftreduce-expression-parsing-...
Kind of looks like you did that in expr.fs? Not completely sure, though.
- vdupras 1y agoI'm not of the academic type and this type of vocabulary quickly loses me. Lots of words to describe something that, at its core, is pretty simple. Mind twisting, but simple. Infix notation parsing happens in ast.fs[1]. It parses tokens left to right and builds a tree (the structure is Lisp-like cons) describe the expression in such a way that expr.fs[2] then just has to recursively process the top tree node to have proper codegen. In other words, operator precedence, prefix/postfix operators, etc. are all considered in ast.fs. Once in expr.fs is reached, all those things have been processed. AST tests[3] give a good example of what ast.fs does. I'm not sure I'd say that Forth being stack-based particularly help there. This logic is more "recursive-based" than anything. Lots of recursion all around. [1]: https://git.sr.ht/~vdupras/duskos/tree/master/item/fs/comp/oberon/ast.fs https://git.sr.ht/~vdupras/duskos/tree/master/item/fs/comp/o... [2]: https://git.sr.ht/~vdupras/duskos/tree/master/item/fs/comp/oberon/expr.fs https://git.sr.ht/~vdupras/duskos/tree/master/item/fs/comp/o... [3]: https://git.sr.ht/~vdupras/duskos/tree/master/item/fs/tests/comp/oberon/ast.fs https://git.sr.ht/~vdupras/duskos/tree/master/item/fs/tests/...
- trealira 1y agoThanks for explaining and giving me an overview of your code. I guess I was overly focused on Forth being stack based, when it's also perfectly capable of processing trees recursively. Yeah, I just think parsers are really cool, and have read enough about different parsing algorithms that I'm used to vocab like that.