Git Product home page Git Product logo

expression_parser's Introduction

expression_parser

A simple C expression parser. Hand-rolled recursive descent style algorithm implements the parser, removing the need for external tools such as lex/yacc. Reads mathematical expression in infix notation (with a few built-in mathematical functions) and produces double-precision results.

The library handles:

  • standard arithmetic operations (+,-,*,/) with operator precedence
  • exponentiation ^ and nested exponentiation
  • unary + and -
  • expressions enclosed in parentheses ('(',')'), optionally nested
  • built-in math functions: pow(x,y), sqrt(x), log(x), exp(x), sin(x), asin(x), cos(x), acos(x), tan(x), atan(x), atan2(y,x), abs(x), fabs(x), floor(x), ceil(x), round(x), with input arguments checked for domain validity, e.g. 'sqrt( -1.0 )' returns an error.
  • standard boolean operations (==,!=,>,<,>=,<=,&&,||,!) using the convention that False := fabs(value) <= PARSER_BOOLEAN_EQUALITY_THRESHOLD and True = !False := fabs(value) > PARSER_BOOLEAN_EQUALITY_THRESHOLD
  • predefined named variables and functions via a callback interface (see below)

Operator precedence and syntax matches the C language as closely as possible to allow straightforward validation of code-correctness. I.e. the parser should produce the same result as the C language to within rounding errors when only operations from C are used.

Boolean operations may be excluded by defining the preprocessor symbol PARSER_EXCLUDE_BOOLEAN_OPS.

The library is also thread safe, allowing multiple parsers to be operated (on different inputs) simultaneously.

Error handling is achieved using the setjmp() and longjmp() commands. To the best of my knowledge these are available on nearly all platforms, including embedded, platforms so the library should run happily even on AVRs (this has not been tested).

Licence: GPLv2 for non-commercial use. Contact me for commercial licensing. This code is provided as-is, with no warranty whatsoever.

Predefined variables and functions are accomodated with a callback interface that allows driver code to look up named variables and evaluate functions as required by the parser. These callbacks must match the call-signature for the parser_variable_callback and parser_function_callback types below. The variable callback takes the name of the variable to be looked up and returns true if the named variable value was copied into the output argument, returning false otherwise. The function callback operates similarly, taking the name of the function to evaluate as well as a list of arguments to that function and (if successful) placing the evaluated function value in the return argument and returning true. Function calls may be arbitrarily nested.

expression_parser's People

Contributors

jamesgregson avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.