I'm writing a quazi calculator in Parse::RecDescent. It will function as a real calculator for a larger project I'm working on eventually, but only if I can figure this problem out ;)
My grammar so far is below. The problem is that it loops for a _really_ long time if you use ()'s in the string. And when I say long, I'm talking generating 2.8 megs of tracing data. And even then, it ends up ignoring it (as far as I can tell). My thought process was it shouldn't loop unless there are nested ()'s because the first ( should be consumed upon a successful match right?
Anyway here's my grammar. Can someone find what's wrong?
startrule : lv1expr
lv1expr : lv2expr ADD lv1expr {$return = $item[1] + $item[3]}
| lv2expr SUB lv1expr {$return = $item[1] - $item[3]}
| lv2expr
lv2expr : lv3expr MULT lv2expr {$return = $item[1] * $item[3]}
| lv3expr DIV lv2expr {$return = $item[1] / $item[3]}
| lv3expr
lv3expr : lv4expr EXP lv3expr {$return = $item[1] ** $item[3]
+}
| lv4expr
lv4expr : '(' startrule ')' {$return = $item[2]}
| NUM
EXP : '^'
MULT : '*'
DIV : '/'
ADD : '+'
SUB : '-'
NUM : /\d+(?:\.\d+)?/
Once bread becomes toast, it can never be bread again.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.