|
|
|
|
|
by cassepipe
1314 days ago
|
|
I have personally tried to build one in C but the parsing was the real pain, I managed to have a tokenizer, barely found how to make an AST and never figured out what to do with. All parsing tutorials are about parsing mathematical expressions, I found it hard to adapt to shell grammar. |
|
If you want POSIX shell you'll have at least 5K lines of parsing code; if you want bash it's at least 10K lines. It's closer to 20K lines of C in bash itself.
There's really no way around that, and IMO the best answer is to use a different language -- which is ALSO hard, because many language runtimes don't support fork() or signals in the way that a shell needs.
(e.g. CPython is actually closer than say Go because it supports fork() and exec(), but even it has issues with signals, EINTR, etc.)
I wrote a bunch of posts on how Oil does it:
How to Parse Shell Like a Programming Language - https://www.oilshell.org/blog/2019/02/07.html
posts tagged #parsing-shell: https://www.oilshell.org/blog/tags.html?tag=parsing-shell#pa...
Oil Is Being Implemented "Middle Out" https://www.oilshell.org/blog/2022/03/middle-out.html