Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 35213
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T14:12:01+00:00 2026-05-10T14:12:01+00:00

I’ve developed an equation parser using a simple stack algorithm that will handle binary

  • 0

I’ve developed an equation parser using a simple stack algorithm that will handle binary (+, -, |, &, *, /, etc) operators, unary (!) operators, and parenthesis.

Using this method, however, leaves me with everything having the same precedence – it’s evaluated left to right regardless of operator, although precedence can be enforced using parenthesis.

So right now "1+11*5" returns 60, not 56 as one might expect.

While this is suitable for the current project, I want to have a general purpose routine I can use for later projects.

Edited for clarity:

What is a good algorithm for parsing equations with precedence?

I’m interested in something simple to implement and understand that I can code myself to avoid licensing issues with available code.

Grammar:

I don’t understand the grammar question – I’ve written this by hand. It’s simple enough that I don’t see the need for YACC or Bison. I merely need to calculate strings with equations such as "2+3 * (42/13)".

Language:

I’m doing this in C, but I’m interested in an algorithm, not a language specific solution. C is low level enough that it’ll be easy to convert to another language should the need arise.

Code Example

I posted the test code for the simple expression parser I was talking about above. The project requirements altered and so I never needed to optimize the code for performance or space as it wasn’t incorporated into the project. It’s in the original verbose form, and should be readily understandable. If I do anything further with it in terms of operator precedence, I’ll probably choose the macro hack because it matches the rest of the program in simplicity. If I ever use this in a real project, though, I’ll be going for a more compact/speedy parser.

Related question

Smart design of a math parser?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T14:12:02+00:00Added an answer on May 10, 2026 at 2:12 pm

    The hard way

    You want a recursive descent parser.

    To get precedence you need to think recursively, for example, using your sample string,

    1+11*5 

    to do this manually, you would have to read the 1, then see the plus and start a whole new recursive parse ‘session’ starting with 11… and make sure to parse the 11 * 5 into its own factor, yielding a parse tree with 1 + (11 * 5).

    This all feels so painful even to attempt to explain, especially with the added powerlessness of C. See, after parsing the 11, if the * was actually a + instead, you would have to abandon the attempt at making a term and instead parse the 11 itself as a factor. My head is already exploding. It’s possible with the recursive decent strategy, but there is a better way…

    The easy (right) way

    If you use a GPL tool like Bison, you probably don’t need to worry about licensing issues since the C code generated by bison is not covered by the GPL (IANAL but I’m pretty sure GPL tools don’t force the GPL on generated code/binaries; for example Apple compiles code like say, Aperture with GCC and they sell it without having to GPL said code).

    Download Bison (or something equivalent, ANTLR, etc.).

    There is usually some sample code that you can just run bison on and get your desired C code that demonstrates this four function calculator:

    http://www.gnu.org/software/bison/manual/html_node/Infix-Calc.html

    Look at the generated code, and see that this is not as easy as it sounds. Also, the advantages of using a tool like Bison are 1) you learn something (especially if you read the Dragon book and learn about grammars), 2) you avoid NIH trying to reinvent the wheel. With a real parser-generator tool, you actually have a hope at scaling up later, showing other people you know that parsers are the domain of parsing tools.


    Update:

    People here have offered much sound advice. My only warning against skipping the parsing tools or just using the Shunting Yard algorithm or a hand rolled recursive decent parser is that little toy languages1 may someday turn into big actual languages with functions (sin, cos, log) and variables, conditions and for loops.

    Flex/Bison may very well be overkill for a small, simple interpreter, but a one off parser+evaluator may cause trouble down the line when changes need to be made or features need to be added. Your situation will vary and you will need to use your judgement; just don’t punish other people for your sins [2] and build a less than adequate tool.

    My favorite tool for parsing

    The best tool in the world for the job is the Parsec library (for recursive decent parsers) which comes with the programming language Haskell. It looks a lot like BNF, or like some specialized tool or domain specific language for parsing (sample code [3]), but it is in fact just a regular library in Haskell, meaning that it compiles in the same build step as the rest of your Haskell code, and you can write arbitrary Haskell code and call that within your parser, and you can mix and match other libraries all in the same code. (Embedding a parsing language like this in a language other than Haskell results in loads of syntactic cruft, by the way. I did this in C# and it works quite well but it is not so pretty and succinct.)

    Notes:

    1 Richard Stallman says, in Why you should not use Tcl

    The principal lesson of Emacs is that a language for extensions should not be a mere ‘extension language’. It should be a real programming language, designed for writing and maintaining substantial programs. Because people will want to do that!

    [2] Yes, I am forever scarred from using that ‘language’.

    Also note that when I submitted this entry, the preview was correct, but SO’s less than adequate parser ate my close anchor tag on the first paragraph, proving that parsers are not something to be trifled with because if you use regexes and one off hacks you will probably get something subtle and small wrong.

    [3] Snippet of a Haskell parser using Parsec: a four function calculator extended with exponents, parentheses, whitespace for multiplication, and constants (like pi and e).

    aexpr   =   expr `chainl1` toOp expr    =   optChainl1 term addop (toScalar 0) term    =   factor `chainl1` mulop factor  =   sexpr  `chainr1` powop sexpr   =   parens aexpr         <|> scalar         <|> ident  powop   =   sym '^' >>= return . (B Pow)         <|> sym '^-' >>= return . (\x y -> B Pow x (B Sub (toScalar 0) y))  toOp    =   sym '->' >>= return . (B To)  mulop   =   sym '*' >>= return . (B Mul)         <|> sym '/' >>= return . (B Div)         <|> sym '%' >>= return . (B Mod)         <|>             return . (B Mul)  addop   =   sym '+' >>= return . (B Add)          <|> sym '-' >>= return . (B Sub)  scalar = number >>= return . toScalar  ident  = literal >>= return . Lit  parens p = do              lparen              result <- p              rparen              return result 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 102k
  • Answers 102k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You could create your own class inherited from DataGridViewCell, and… May 11, 2026 at 8:14 pm
  • Editorial Team
    Editorial Team added an answer The question is unclear, but here is some information from… May 11, 2026 at 8:14 pm
  • Editorial Team
    Editorial Team added an answer I think you'll have to do this in the same… May 11, 2026 at 8:14 pm

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.