5 ms·
(3) Exp -> Exp2 (4) Exp2 -> Exp2 * Exp3 (5) Exp2 -> Exp2 / Exp3 (6) Exp2 -> Exp3 This is horrible to read and to maintain. Real grammars use names
by tom_mellior 6y ago
(3) Exp -> Exp2
(4) Exp2 -> Exp2 * Exp3
(5) Exp2 -> Exp2 / Exp3
(6) Exp2 -> Exp3
This is horrible to read and to maintain. Real grammars use names like "additive_expr", "multiplicative_expr" etc., or maybe "term" and "factor", though I can never remember which is which.
- nxpnsv 6y agoYes it is horrible, but it doesn't make a grammar less real, labels don't matter...
- spinningslate 6y agolabels definitely matter. Naming is one of the two hardest things [0]. The point in this case is that the names are hard because they're making up for limitations in left-recursive grammars. Personally, I still find "Addable_expr" to be more intuitive than "additive_expr": where it means "an expression that can be added". Both are more intuitive (to me) than "Expr2". But that's personal preference, and part of why naming is hard :). [0] https://martinfowler.com/bliki/TwoHardThings.html https://martinfowler.com/bliki/TwoHardThings.html
- srtjstjsj 6y agoTry your idea. It won't work. The reason is that they aren't "addable", they are precedence1 and precedence2. You can change Exp to Precedence if you wanted but that's just longer.
- tom_mellior 6y ago"It won't work" is a hilarious claim seeing how actual language standards do exactly this, and it works well. See the C standard for example: https://web.archive.org/web/20161223125339/http://flash-gordon.me.uk/ansi.c.txt https://web.archive.org/web/20161223125339/http://flash-gord... . Search for "additive-expression" to see the relevant grammar rules.
- nxpnsv 6y agoI am not disagreeing with you, I just think that the "grammar" still works even with terrible naming. It is harder to read, it is harder to maintain, but it is still real. Perhaps I am missing some subtler point here...
- tom_mellior 6y agoWhen I wrote "real grammars" above I meant the grammars that people write to specify "real" widely used production-quality programming languages. I linked to the C standard elsethread: https://web.archive.org/web/20161223125339/http://flash-gordon.me.uk/ansi.c.txt https://web.archive.org/web/20161223125339/http://flash-gord... Here is the Java specification: https://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.17 https://docs.oracle.com/javase/specs/jls/se8/html/jls-15.htm... Here is the Python language reference (with terser names, but definitely "m" for multiplicative and "a" for additive expressions): https://docs.python.org/3/reference/expressions.html#binary-arithmetic-operations https://docs.python.org/3/reference/expressions.html#binary-... These are "real" grammars. As opposed to a "toy" grammar that someone throws on GitHub to demonstrate an "X in 100 lines" thing.
- nxpnsv 6y agoThanks, i am enjoying reading this!
- raverbashing 6y ago> though I can never remember which is which If you can't remember which is which, this is a good indication that the names don't matter, and in fact they don't, because it's an artifact of the way you need to write the grammar. I'm much more in favor of Exp/Exp2/Exp3.
- ImprobableTruth 6y agoFor me it's actually the opposite, I find using the priority makes it actually a lot more readable, especially once you have a lot of operators. It's also quite close to a generalized (and potentially dynamic) version that is just a function that takes in a priority and returns the correct parser.
- tom_mellior 6y agoIt's not that I'm opposed to priorities, it's just that I think it's unreasonably hard to see the typos here: (3) Exp -> Exp2 (4) Exp2 -> Exp2 * Exp3 (5) Exp3 -> Exp2 / Exp2 (6) Exp2 -> Exp3 There are reasons why we usually give variables more meaningful names than var1, var2, var3. (Obviously I agree that if the parser can be expanded dynamically with operators with numeric priorities, then you need numeric priorities. But that is a very different case.)
- srtjstjsj 6y agoIt's not term and factor, it's just operator precedence. Hence 1,2,3 is appropriate.