News for hackers. Stuff that matters.
Re:Evil Teacher: Code Injection in Moodle by userbinator on Tuesday June 12, 2018 @11:12PM (#17299950)
IMHO precedence climbing (a refactored version of recursive descent) would be much simpler and makes parsing seem almost trivial, since it can be made table-driven:

https://www.engr.mun.ca/~theo/Misc/exp_parsing.htm

Speaking of parsing simplicity and software design in general, you've coincidentally found a great example of how not to (ab)use OOP... one line of actual work wrapped in a dozen lines of boilerplate and replicated across multiple files with tiny variations:

https://github.com/ircmaxell/php-math-parser/tree/master/lib...

To put things into perspective, a precedence-climber for 13 levels of precedence from C's grammar, including a tokeniser, fits comfortably in <300 LoC of C, the bulk of that being the tokeniser. PHP's higher level nature (especially around string handling) would probably make a corresponding implementation even smaller.

There are no comments on this comment yet.