40

Is there any canonical question about parsing mathematical expressions1 with the proper tools and methods, which can be used for duplicate closure?

1 A somewhat OK answer should be able to parse an expression with the basic operators + (binary and unary), - (binary and unary), *, / and arbitrarily nesting (). A better answer would layout the idea and framework to add support for mathematical functions and other binary operators, plus handling associativity.

It would be great if there is a list of language-specific Q&A, plus a language agnostic Q&A.

Such questions show up frequently in the , and they are answered in minutes with regex. Those are usually terrible solutions which breaks like twig on slightly more complex input.

12
  • 1
    Is it really that frequent? And for what kind of math expressions? Just nested number arithmetics, and/or functions etc.? And I believe it makes sense to finally differentiate between "parsing" and "matching". Only Perl can really utilize regexps for parsing.
    – mario
    Jan 20, 2015 at 10:39
  • 2
    @mario: It is less frequent that HTML/XML and JSON, but it accumulates over the time. I think a canonical question also benefits other languages which don't have built-in facility to parse expressions, where such kind of question may pop up from time to time. What use is there for a bunch of questions with bad solution? It is spreading the incorrect knowledge and bad implementation.
    – nhahtdh
    Jan 20, 2015 at 10:45
  • Canonical questions are great. And there's nothing wrong with making new/artifical ones. Just not sure if this topic can be covered language-agnostic though. It's potentially even a little broad, even if it's just an expression parser.
    – mario
    Jan 20, 2015 at 10:51
  • 3
    Java: stackoverflow.com/questions/3422673/… Jan 20, 2015 at 10:59
  • 1
    @FlorentBayle: The accepted answer there is not a good answer in my opinion. The rest, well, probably needs a lot of polishing, imo.
    – nhahtdh
    Jan 20, 2015 at 11:05
  • 3
    Python: canonical question and "somewhat OK answer".
    – jfs
    Jan 20, 2015 at 11:33
  • @J.F.Sebastian: That one is good. Would you please post that as an answer to this question?
    – nhahtdh
    Jan 20, 2015 at 11:35
  • @nhahtdh: a canonical answer would be the approach from Unix programming environment book.
    – jfs
    Jan 20, 2015 at 11:37
  • Java: in practice stackoverflow.com/questions/2605032/…
    – Raedwald
    Jan 20, 2015 at 15:19
  • 1
    C language: questions about mathematical expression parsing show up periodically (I saw one today), but a good expression parser in C is too broad for SO, and a generic overview won't help the newbie's that are writing the questions. OTOH, if you want to write and post the canonical shunting-yard parser for C (with full error handling, operator precedence, and nested parentheses), go for it :-) Jan 20, 2015 at 19:17
  • Delphi - JclExprEval (currently accepted). Might be taken for C# as well.
    – TLama
    Jan 20, 2015 at 19:32
  • At least one company in Cambridge(UK) used(maybe still does) this as a coding test that has to completed before shortlisting is done. Feb 10, 2016 at 16:22

0

You must log in to answer this question.

Browse other questions tagged .