logoalt Hacker News

andaiyesterday at 5:16 PM1 replyview on HN

>term rewriting

In uni we had to make a spreadsheet software.

I volunteered to do the formula parser, thinking it sounded like a fun challenge.

I was stumped for a week, until I realized I could rewrite the formulas into a form I knew how to parse. So it would rewrite 1+1 into ADD(1,1) and so on.

I also refused to learn regex, so the parsing code was "interesting" ;)

I recall a comment from a colleague. "Okay, Andy says it works. Don't touch it." XD

Guy from another group used regex and his solution was 20x shorter than mine.


Replies

gf000yesterday at 9:35 PM

Regular expressions are probably not enough for parsing formulas (depending of course on the exact task given), they usually are at least a context free language.