looking for a math engine, an interpreter written in C++, similar to TIcalc's  - Page 1
Forum section: electronics / general

author: legacy
author_rank: Super Contributor
timestamp: April 01, 2015, 11:15:52 am
content: hi guysi am looking for a pretty piece of C++ code similar to the math engine of the TIcalc's pocked calculators (Texas Instruments, e.g. TI89, TI92)i do not need any CAS, just an interpreter able to handle integer, float, in list and matrix, plus common expressionsit's for hobby purposes, i'd like to use it with a linux embedded board, text interface only.

author: legacy
author_rank: Super Contributor
timestamp: April 02, 2015, 02:19:11 pm
content: how complex is to write an interpreter withAST?

author: Howardlong
author_rank: Super Contributor
timestamp: April 02, 2015, 03:23:17 pm
content: Do people still use Lex and Yacc or this stuff, or have things moved on in the 20+ years since I last wrote an interpreter?

author: legacy
author_rank: Super Contributor
timestamp: April 02, 2015, 03:52:11 pm
content: yes, sometimes i use them, but i have also built a library with my lexer and tokenizer.what i'd like to focus on is how to generate an AST, probably i have to read more about.

author: alexanderbrevig
author_rank: Frequent Contributor
timestamp: April 02, 2015, 03:58:01 pm
content: I would make abytecode interpreterand then a simple translator for the syntax you want i.e TIcalc to YourAwesomeByteLang.

author: IanB
author_rank: Super Contributor
timestamp: April 02, 2015, 04:11:30 pm
content: Quote from: legacy on April 02, 2015, 03:52:11 pmMathematical expressions work quite well with a simple recursive descent parser, but more complex language expressions (like C++) are impractical to roll by hand and work much better with compiler generation tools like yacc.

author: JacquesBBB
author_rank: Frequent Contributor
timestamp: April 02, 2015, 06:20:56 pm
content: Quote from: legacy on April 01, 2015, 11:15:52 amIs an executable OK ? Or you absolutely need the source code ?What are the memory size requirements ?

author: alexanderbrevig
author_rank: Frequent Contributor
timestamp: April 02, 2015, 06:33:58 pm
content: what abouthttp://www.ticalc.org/programming/emulators/software.htmlmaybe one of them is FOSS

author: legacy
author_rank: Super Contributor
timestamp: April 02, 2015, 06:46:15 pm
content: Quote from: JacquesBBB on April 02, 2015, 06:20:56 pmthe target board is a MIPS SoC/linux with 64Mbyte of ram, but i do not want to put an executable on, i want to design something, so i'd like sources as "something that can provide me a get-started"

author: legacy
author_rank: Super Contributor
timestamp: April 02, 2015, 06:51:26 pm
content: Quote from: IanB on April 02, 2015, 04:11:30 pmwhat do you think about the RPN approach to evaluate "expressions" ?i have already implemented a very simple RPN engine.A lot of time ago i implementedthistoy-interpreter, and since then a few interpreters (for my job, also using yacc to reduce the "time to market", while "Little-interpreter" was for hobby purposes, designed from scratches without any tool), but i still have a lot to learn

author: daybyter
author_rank: Frequent Contributor
timestamp: April 02, 2015, 06:56:45 pm
content: yacc is a bit dated? Take a look at Antlr.

author: JacquesBBB
author_rank: Frequent Contributor
timestamp: April 02, 2015, 06:57:48 pm
content: We have used extensively Bison/Lex in order to overcome some limitations of yacc/lex for big projects.

author: paf
author_rank: Regular Contributor
timestamp: April 02, 2015, 08:58:59 pm
content: Take a look at Calc:http://www.isthe.com/chongo/tech/comp/calc/http://sourceforge.net/projects/calc/

author: JacquesBBB
author_rank: Frequent Contributor
timestamp: April 02, 2015, 09:36:17 pm
content: Quote from: paf on April 02, 2015, 08:58:59 pmFrom what I look (rapidly I confess), it does not seem to use a tool like bison/lex (or yacc/lex) to define a grammar in an abstract way,but just parse the input with dedicated C code.This will be very difficult to understand and maintain.

author: zapta
author_rank: Super Contributor
timestamp: April 02, 2015, 11:24:28 pm
content: Quote from: JacquesBBB on April 02, 2015, 09:36:17 pmRecursive decent parsing can be very simple and intuitive.

author: Dago
author_rank: Frequent Contributor
timestamp: April 03, 2015, 06:35:27 am
content: I personally have used Ragel for parser generation, it generates a stand-alone finite state machine in C so it has no dependencies so it should also be easy to integrate to an embedded project:http://www.colm.net/open-source/ragel/Writing a calculator with Ragel sounds actually kinda fun, might have to think about that.

author: ivaylo
author_rank: Frequent Contributor
timestamp: April 03, 2015, 07:51:50 am
content: If it's a calculator with a single data type you are after, it's fun and all of the above mentioned methods will work. If you allow variable assignments, function definitions, scope and such, you should be ready for a different level of fun.

author: legacy
author_rank: Super Contributor
timestamp: April 03, 2015, 10:56:14 am
content: Quote from: ivaylo on April 03, 2015, 07:51:50 amthat is

author: JacquesBBB
author_rank: Frequent Contributor
timestamp: April 04, 2015, 06:36:25 am
content: Then you are probably left with bison/flex.You can start easily a small calculator with it, but it can evolve to a full interpreted language.just google search for "flex bison calculator" and you will find many examples on the web thatyou can then expand to your needs.

author: legacy
author_rank: Super Contributor
timestamp: April 04, 2015, 12:27:48 pm
content: thank youumm, i'd also like to learn (just to learn, i mean read, for practical things i will use "building tools") the process to get a list of tokens into an abstract syntax tree, is there any paper that can teach me this ?

author: FrankBuss
author_rank: Unknown
timestamp: April 04, 2015, 12:54:16 pm
content: I did such an interpreter some time ago:http://www.frank-buss.de/formula/Sorry, webpage is in German, but the code and comments (not much) are in English. My idea was to create an object oriented and easy to extend interpreter, which can evaluate a formula with variables fast. First it parses the input with arecursive descent parser, written in C++ (Parser.cpp), which converts the infix input to a list of tokens in postfix format, and a list of variables. Then the variables can be set and the list can be evaluated very fast e.g. for drawing diagrams. The code comes with a Visual Studio project and CppUnit test case and a Windows example program for drawing diagrams to see how to use it. The parser and evaluator itself should be compilable without changes for any C++ target system.The standard book for learning how parsers, compilers etc. work isthe dragon book.

author: JacquesBBB
author_rank: Frequent Contributor
timestamp: April 04, 2015, 01:14:32 pm
content: I have not done it for a while, but apart from the quoted reference (dragon book), and its new edition,you may just search on the web for "bison/flex compilers". As an example, this looks like a usable tutorialhttp://epaperpress.com/lexandyacc/download/LexAndYaccTutorial.pdf

author: legacy
author_rank: Super Contributor
timestamp: April 04, 2015, 01:32:40 pm
content: @FrankBussi have written a pretty RPN evaluator for expressions, it's pretty able to handle variable, built-in functions (sin, cos, tan …), user-defined function (def my_f(…..))Code:lex("a=b a==b a!=b abc=/=bcd0 logicalNot ! shiftLeft >> notEqual end");- token: token= TokenAlpha- token: token=[=] TokenAssign- token: token= TokenAlpha- token: token= TokenAlpha- token: token=[==] TokenEqual- token: token= TokenAlpha- token: token= TokenAlpha- token: token=[!=] TokenNotEqual- token: token= TokenAlpha- token: token= TokenAlpha- token: token=[=/=] TokenNotEqual- token: token= TokenAlphaNum- token: token= TokenUnaryNot- token: token=[!] TokenUnaryNot- token: token= TokenShiftLeft- token: token=[>>] TokenShiftRight- token: token= TokenNotEqual- token: token= TokenRightBraceRPN engine, expression validatorCode:# calc "a=1+2+3*a*b*c"output: a12+3a*b*c*+=shunting_yard_analysis PASSEDstack_analysis PASSED########################## good expression ##########################Code:# calc "a++1"output: a+1+shunting_yard_analysis PASSEDstack_analysis FAILED########################## bad expression ##########################Code:# calc "a+((1+2)"Error: parentheses mismatchedshunting_yard_analysis FAILEDstack_analysis FAILED########################## bad expression ##########################i have a pretty tokenizer library, written to convert a script into a list of tokens, and i have already written an interpreter that look like pascal (see the Little project, it has conditional and loop statements, it does not have functions, yet)myLittleprojectexample scriptCode:Program factorialBeginInput nfactorial = 1While n > 1Beginfactorial = factorial * nn = n - 1EndOutput factorialEndi have studied on the first edition of thedragon book, but it misses the AST approach, i can't see anyAbstract Syntax Tree, while Yacc &C are using CST !

author: FrankBuss
author_rank: Unknown
timestamp: April 04, 2015, 01:58:22 pm
content: Quote from: legacy on April 04, 2015, 01:32:40 pmNice, looks like this is exactly what you were asking for, just add support for matrix operations etc.

author: legacy
author_rank: Super Contributor
timestamp: April 04, 2015, 03:01:38 pm
content: yes, it's a very old project, done as homework in order to get my university degree, it's very simple so i can re-use it, but first i want to rewrite it in the AST way. Also i have written the RPN engine and the new talker engine, 3 different projects that could be merged up into 1 project.i hope

author: zapta
author_rank: Super Contributor
timestamp: April 04, 2015, 04:36:32 pm
content: Quote from: FrankBuss on April 04, 2015, 12:54:16 pmFor embedded calculators it may be more memory efficient the do the calculation as part of the recursive decent parsing, passing the resolved values up the tree.BTW, with java I had good experience with JavaCC, you define the lexical, grammer and even the semantic if you want in a single file, everything is type safe, including custom abstract types, and you have the expression power of Java.

author: ivaylo
author_rank: Frequent Contributor
timestamp: April 05, 2015, 02:25:39 am
content: Quote from: zapta on April 04, 2015, 04:36:32 pmI hear the latest versions of JavaCC can generate C++ code nowadays. I didhttp://fraid.sourceforge.netusing JavaCC long time ago and IMO it is a much more modern tool than lex, yacc, bison, etc. Also instead of clunky syntaxes for the user entering and parsing matrices I stuck to systems of equations, but that fit my application may not fit yours...