by chasil · 2y ago · 0 comments
https://pubs.opengroup.org/onlinepubs/9699919799/utilities/l...
https://pubs.opengroup.org/onlinepubs/9699919799/utilities/y...
All the POSIX.2 standards for shell utilities can be found here:
https://pubs.opengroup.org/onlinepubs/9699919799/utilities/
The original introduction to lex and yacc was in the book by Kernigan and Pike:
https://scis.uohyd.ac.in/~apcs/itw/UNIXProgrammingEnvironmen...
One, because actually building the lexer and parser from scratch is a useful exercise in a learning context, which is what this is.
Two, because the book wants to teach Pratt parsers, rather than LALR parsers. There’s a lot of literature out there on LALR parsers and generated parsers in general, but precious little on handrolled parsers. Covering material that hasn’t been covered to hell and back is a Good Thing.
Three, because lex and yacc generate C, and the book has two implementations of the interpreter, in C and Java. You could’ve used ANTLR to generate both parsers, but lex/yacc would only cater to the C version
And finally, because not everybody is on a POSIX system. Before WSL, using anything Unix-y on Windows was miserable. These days it’s mostly fine, but using WSL means you’re not actually building windows-native stuff anymore.
> We will abstain from using [parser generators] here. I want to ensure there are no dark corners where magic and confusion can hide, so we’ll write everything by hand. As you’ll see, it’s not as bad as it sounds, and it means you really will understand each line of code and how both interpreters work.
^ https://craftinginterpreters.com/introduction.html#the-code
POSIX utilities are not great. Lex and Yacc included.
I say this only as it has been many years since I have written one, but I thought this OCaml presentation on a POSIX shell held the structure in high regard.
https://archive.fosdem.org/2018/schedule/event/code_parsing_...
I am partial to hand-coded recursive descent purely because I struggled with Lex and Yacc too much when I tried.
My take on POSIX utilities would be only to use them on Linux platforms where they effectively form a "native" part of the platform.
https://en.m.wikipedia.org/wiki/Microsoft_POSIX_subsystem
Microsoft had previously been the largest AT&T UNIX licensee with Xenix sales on the TRS-80 model 2, so they were familiar with the need.
Yes, you should only use them on POSIX.
My utility does build natively on Windows, though.
Does Windows claim POSIX compatibility these days? I didn't think so? I remember NT's old POSIX subsystem, but is that even a thing anymore?
> My take on POSIX utilities would be only to use them on Linux platforms
I mean, only use them on POSIX-compliant systems. Linux is just one of them.
At the risk of sounding childishly derisive, when someone is talking about "portability", I think they're generally not bothering to include Windows in the mix, as Windows doesn't really share all that much of a common denominator with much else.
[0]: https://github.com/ruby/prism
[1]: https://railsatscale.com/2023-06-12-rewriting-the-ruby-parse...
A language with as much going on as Ruby was not one I would have picked as a candidate for yacc
But indeed, last I checked, recursive descent was the most common choice overall.