I'm using this code in a little project I started out of boredom. Its purpose is to print function graphs on the commandline, like gnuplot can do as well.

At the current point it parses mathematical expressions into a kind of abstract syntax tree and plots them onto the cmdl. I knew that I could have defined a grammar for it and implemented a serious parser and lexer, but I wanted to do it myself first and then see what I could have done better.

So far, the supported expressions included only +-*/ and nested parenthesis. Since I wanted to include more expressions and functions, I figured it would be a pain to deal with operator precedence unless I take on a more general approach of precedence levels.
The initial question is because I want to find the first operator with the lowest precedence and recursively build subtrees from the prefix and postfix terms of the found operator.
The difficult question for me here was how to handle the parenthesis-terms atomically during this operator lookup.


In reply to Re: Regex: first match that is not enclosed in parenthesis by monkprentice
in thread Regex: first match that is not enclosed in parenthesis by monkprentice

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.