Searching \ for '[PIC] Simple expression evalutator in C (Preferabl' in subject line. ()
Make payments with PayPal - it's fast, free and secure! Help us get a faster server
FAQ page: www.piclist.com/techref/microchip/languages.htm?key=c
Search entire site for: 'Simple expression evalutator in C (Preferabl'.

Exact match. Not showing close matches.
PICList Thread
'[PIC] Simple expression evalutator in C (Preferabl'
2009\04\22@212413 by Forrest W Christian

flavicon
face
I've got a temperature control application where I'd like to be able to
let the customers enter expressions (via serial) like:

T1>T2&T1>72

or

T1>T2+5&T2<32

Where T1 and T2 are temperature readings from a thermocouple.   The
result of these expressions will be used to turn on and off fans and/or
heaters in an application I am working on.   I could hardcode a lot of
options, but I think some simple customer-modfiable rules would be best
in this particular application.

So, before I write my own parser and the like I figured I'd ask if
there's code out there which would be suited for this, and is small (the
target is a PIC16F886 and CCS C - and it's not easy to change it in this
application).

I've found some example code but it's all overly big and not well suited
to what I'm looking for.    I know how to write this myself, but I'd
rather just use some existing code - or perhaps hear some other ideas.

Oh, I really only need a few variables (I'll probably use TA, TB instead
of T1, T2 just to simplify parsing), a few boolean functions (and, or,
greater than, less than, maybe >= or <=), and possibly addition,
subtraction, and less possibly multiplication and/or division.

-forrest

2009\04\22@223851 by M. Adam Davis

face picon face
It took me a lot longer to find mine than I expected!  I posted one
simple equation parser here:

http://www.ubasics.com/simple_c_equation_parser

The code and compiled test executable (windows) can also be
downloaded, http://www.ubasics.com/files/equation.zip

It uses a stack method with no operator precedence.  It handles binary
operators (+, -, *, /, |, &, ^), unary operators(!), parenthesis((,)),
and numbers.  Parenthesis can be used to force precedence, everything
else is strictly left to right evaluation.

You'll need to add < and > binary operators (and, or, not, xor,
already exist), and you'll need to either add symbols to represent
your inputs, or modify the parse routine to accept characters and use
the correct variable when a word occurs in the equation.

You'll also need to instruct users to use parenthesis rigorously:

T1>T2&T1>72 becomes (T1>T2)&(T1>72)
T1>T2+5&T2<32 becomes (T1>(T2+5))&(T2<32)

The program I posted compiles under tcc (tiny c compiler) and should
be standard ansi C, so it ought to port fine.  Of course you'll need
to remove the example main() at the bottom, and perhaps the odd
debugging printf scattered through the code (I believe they are all
commented out...).

Good luck, let me know how it works out, and if you do add the ability
to use variables and <, > operators, feel free to send me the code and
I'll drop it on the website for others.

-Adam

On Wed, Apr 22, 2009 at 9:26 PM, Forrest W Christian <spam_OUTforrestcTakeThisOuTspamimach.com> wrote:
{Quote hidden}

>

2009\04\23@074015 by olin piclist

face picon face
Forrest W Christian wrote:
> I know how to write this myself,
> but I'd rather just use some existing code

Software reuse for such simple things is overrated.  Yes, maybe you can find
code that sortof does what you want, but by the time you integrate it into
your code, adjust the interface to your needs, test it, you could have
written exactly what you wanted.


********************************************************************
Embed Inc, Littleton Massachusetts, http://www.embedinc.com/products
(978) 742-9014.  Gold level PIC consultants since 2000.

2009\04\23@075414 by Isaac Marino Bavaresco

flavicon
face
Nice code!

Out of curiosity I downloaded your code and tried to compile with
Hi-Tech PICC.

The function 'calc_number' calls itself  recursively because of the NOT
operator, so I commented it out. I had to do other minor PIC specific
changes.

Compiled and run OK.

Occupied 2087 words of FLASH and 140 bytes of RAM in a PIC16F648A.

It calculates "(123+234)*34" in 11.89ms @ 20MHz.

Regards,

Isaac


M. Adam Davis escreveu:
{Quote hidden}

__________________________________________________
Faça ligações para outros computadores com o novo Yahoo! Messenger
http://br.beta.messenger.yahoo.com/

2009\04\23@080425 by Tamas Rudnai

face picon face
I my opinion it is much easier to implement a sort of RPN calc for that.
Also if you have a PC connected to the circuit you may want to do that on
the PC side generating a pcode (with converting the normal form to RPN and
do all possible pre-calculations) so then the PIC needs only a much lighter
pcode engine rather than a more complex parser.

Tamas


On Thu, Apr 23, 2009 at 12:54 PM, Isaac Marino Bavaresco <
isaacbavarescospamKILLspamyahoo.com.br> wrote:

{Quote hidden}

>

2009\04\23@101405 by M. Adam Davis

face picon face
Wow, 2k code is a lot.  I didn't do any optimizations, I bet that
could be significantly reduced in both time and space.  It is also a
very, very simplistic algorithm - I'm sure there are better.

The recursive call was laziness on my part, but you're right - it
would be better implemented without recursion.

Thinking about it further this morning, I realize that I didn't
implement a unary '-' either, which could be bad as it makes the
following test impossible:

TA<-1

Instead one has to work around it by saying:

TA < (0-1)

Which is a hack!

Oh well, I'll just leave that as an exercise for the reader.

-Adam

On Thu, Apr 23, 2009 at 7:54 AM, Isaac Marino Bavaresco
<EraseMEisaacbavarescospam_OUTspamTakeThisOuTyahoo.com.br> wrote:
{Quote hidden}

>

2009\04\23@102000 by M. Adam Davis

face picon face
Yes, RPN is much easier to write code for (it's inherently stack based).

I created this because my client wanted to use simple algebra (he
didn't want to teach his customers RPN).  The actual equation goes
into a configuration file on a flash card, and that needed to be
editable using a text editor.

I can see more people doing web services, though.  No need to install
software, just go to this web interface, configure the device, then
download the resulting configuration file to the SD card.  This might
make it easy to precompile the equation and other configuration
parameters without unduly increasing the burden on the user by
requiring they install more software.

-Adam

On Thu, Apr 23, 2009 at 8:04 AM, Tamas Rudnai <@spam@tamas.rudnaiKILLspamspamgmail.com> wrote:
{Quote hidden}

>> -

2009\04\23@102210 by Isaac Marino Bavaresco

flavicon
face
I agree, RPN would be the most compact and efficient solution.

But I think if the users need to learn RPN for using his product then it
may reduce the number of potential clients.

If the OP plans to have some front-end application in a PC, then it is
easy to let the application convert the algebraic to RPN, but if the
device is stand alone then it is better if the device handle the
algebraic expression by itself.

Once (over 20 years ago) I wrote an expression parser in Z80 assembly. I
implemented operator precedence by storing a 4-bit operator ID and a
4-bit operator precedence together. It used two stacks, one for the
values and other for the operators. When the current operator had a
higher precedence than the one in the stack, it was pushed into the
stack, otherwise the operator already in the  stack was evaluated along
with its two arguments and the result pushed into the stack again. This
was repeated until the current operator was pushed into the stack and a
new value and operator was obtained. The end-of-string was considered as
the lowest precedence operator.

Unfortunately the original code is lost.

Regards,

Isaac

Tamas Rudnai escreveu:
> I my opinion it is much easier to implement a sort of RPN calc for that.
> Also if you have a PC connected to the circuit you may want to do that on
> the PC side generating a pcode (with converting the normal form to RPN and
> do all possible pre-calculations) so then the PIC needs only a much lighter
> pcode engine rather than a more complex parser.
>
> Tamas
>  
__________________________________________________
Faça ligações para outros computadores com o novo Yahoo! Messenger
http://br.beta.messenger.yahoo.com/

2009\04\23@105311 by Robert A LaBudde

flavicon
face
Thirty years ago I was a system architect and co-developer of a
commercial spreadsheet program for IBM minicomputers (alas, 6 months
before Lotus 1-2-3 came out!), and wrote an expression evaluator in
FORTRAN IV using in-fix notation. I first wrote one in RPN notation.
The difference in effort was minimal. The in-fix version was also
just as easy to understand in code.

The programming model was a finite-state machine, and it wasn't that
hard to do after you tokenized the input line.


At 10:15 AM 4/23/2009, Isaac Marino Bavaresco wrote:
>I agree, RPN would be the most compact and efficient solution.
>
>But I think if the users need to learn RPN for using his product then it
>may reduce the number of potential clients.
><snip>


================================================================
Robert A. LaBudde, PhD, PAS, Dpl. ACAFS  e-mail: spamBeGoneralspamBeGonespamlcfltd.com
Least Cost Formulations, Ltd.            URL: http://lcfltd.com/
824 Timberlake Drive                     Tel: 757-467-0954
Virginia Beach, VA 23464-3239            Fax: 757-467-2947

"Vere scire est per causas scire"
================================================================

2009\04\23@122644 by Peter

picon face
Robert A LaBudde <ral <at> lcfltd.com> writes:
> before Lotus 1-2-3 came out!), and wrote an expression evaluator in
> FORTRAN IV using in-fix notation. I first wrote one in RPN notation.

Speaking of FORTRAN, there was a neat shortcut expression evaluator designed for
FORTRAN I, it is on Wikipedia. I have tested it (just add parens to cope with
deeper expressions - eventually buffer size quickly limits this in small micros
- in FORTRAN the limit was likely the usable line length on a punched card):

See under 'Alternatives to Dijkstra's Algorythm':

 http://en.wikipedia.org/wiki/Operator-precedence_parser

Peter


2009\04\23@152303 by M. Adam Davis

face picon face
You'll find some more resources for equation parsers here:

http://stackoverflow.com/questions/28256/equation-expression-parser-with-precedence

On Thu, Apr 23, 2009 at 12:26 PM, Peter <TakeThisOuTplpeter2006EraseMEspamspam_OUTyahoo.com> wrote:
> Speaking of FORTRAN, there was a neat shortcut expression evaluator designed for
> FORTRAN I, it is on Wikipedia. I have tested it (just add parens to cope with
> deeper expressions - eventually buffer size quickly limits this in small micros
> - in FORTRAN the limit was likely the usable line length on a punched card):
>
> See under 'Alternatives to Dijkstra's Algorythm':
>
>  http://en.wikipedia.org/wiki/Operator-precedence_parser

That is really neat!  It makes perfect sense, and is very simlpe and
comprehensable.  I see what you mean in terms of buffer length, though
- it could also eat up a lot of equation stack in the parser I wrote.

Very nice, thanks for pointing that out!

-Adam

2009\04\23@184008 by solarwind

picon face
On Thu, Apr 23, 2009 at 7:41 AM, Olin Lathrop <RemoveMEolin_piclistspamTakeThisOuTembedinc.com> wrote:
> Forrest W Christian wrote:
>> I know how to write this myself,
>> but I'd rather just use some existing code
>
> Software reuse for such simple things is overrated.  Yes, maybe you can find
> code that sortof does what you want, but by the time you integrate it into
> your code, adjust the interface to your needs, test it, you could have
> written exactly what you wanted.

Exactly!

2009\04\23@223326 by Isaac Marino Bavaresco

flavicon
face
OK, just finished a C version (Hi-Tech PICC) of my ancient expression
parser:
<http://www.piclist.com/techref/member/IMB-yahoo-J86/ExpressionParser.htm>

I has operator precedence and variables and doesn't use recursion in the
C code (thus PIC worthy). No unary operators or parenthesis yet.
For now only +, -, *, /, % (mod), &.

Only 1320 FLASH words in a PIC16F648A with printf, and 774 FLASH words
without printf.

I made it just for fun, I hope it is useful for somebody.

Let the flames begin...

Regards,

Isaac

Forrest W Christian escreveu:
{Quote hidden}

__________________________________________________
Faça ligações para outros computadores com o novo Yahoo! Messenger
http://br.beta.messenger.yahoo.com/

2009\04\24@020542 by solarwind

picon face
On Thu, Apr 23, 2009 at 10:33 PM, Isaac Marino Bavaresco
<isaacbavarescoEraseMEspam.....yahoo.com.br> wrote:
> Let the flames begin...

I love how you have <pre> tags in the <title>...

2009\04\24@090756 by M. Adam Davis

face picon face
Very nice!  Thanks for contributing this and posting it online -
significantly better than mine in practically every way.  I don't see
where or if it handles parenthesis, but I imagine it wouldn't be hard
to add if it doesn't handle them now.

I added a link to it from my page.  Thanks for posting this!

-Adam

On Thu, Apr 23, 2009 at 10:33 PM, Isaac Marino Bavaresco
<EraseMEisaacbavarescospamyahoo.com.br> wrote:
{Quote hidden}

2009\04\24@131203 by Isaac Marino Bavaresco

flavicon
face
solarwind escreveu:
> On Thu, Apr 23, 2009 at 10:33 PM, Isaac Marino Bavaresco
> <RemoveMEisaacbavarescoEraseMEspamEraseMEyahoo.com.br> wrote:
>  
>> Let the flames begin...
>>    
>
> I love how you have <pre> tags in the <title>...
>  

It's the default for pages generated automatically by piclist.com.
I do little editing and often forget to fix it.

Regards,

Isaac

__________________________________________________
Faça ligações para outros computadores com o novo Yahoo! Messenger
http://br.beta.messenger.yahoo.com/

2009\04\24@131644 by Isaac Marino Bavaresco

flavicon
face
You are welcome!

I'm testing a version which supports floating point, to be released soon.

Parenthesis will come next.

Regards,

Isaac


M. Adam Davis escreveu:
{Quote hidden}

__________________________________________________
Faça ligações para outros computadores com o novo Yahoo! Messenger
http://br.beta.messenger.yahoo.com/

2009\04\24@145744 by Isaac Marino Bavaresco

flavicon
face
I just published the new version of the expression parser:
<http://www.piclist.com/techref/member/IMB-yahoo-J86/ExpressionParser.htm>

It now supports most common operators:
*, /, %
+, -
<<, >>
&, ^, |
<, <=, =, ==, >=, >, <>, !=
&&, ||

Now it is possible to chose at compile time the base type of data: char,
short, long, float or double.
It is also possible to disable support for groups of not needed
operators, to reduce code size.

TODO:
- Parse float values in the expression;
- Parenthesis;
- Unary operators: -, +, ~, !

Regards,

Isaac


Isaac Marino Bavaresco escreveu:
{Quote hidden}

__________________________________________________
Faça ligações para outros computadores com o novo Yahoo! Messenger
http://br.beta.messenger.yahoo.com/

2009\04\25@080248 by Murat Seven

picon face
ISIS presents some calculator examples.


murat
>

2009\04\25@092114 by sergio masci

flavicon
face


On Wed, 22 Apr 2009, Forrest W Christian wrote:

{Quote hidden}

Have you considered the possibilty of using a real compiler to generate
real code and storing that in program memory?

You could write a simple front end to the compiler that validates the
variables that the user supplys, passes the expression(s) to the compiler
and varifies that the compile completes successfully.

You could arrange for the executable to live at a given address within the
program memory and for the user variables to live at given addresses
within the RAM. It shouldn't be too difficult to get the compiler and
linker to do exactly what you want. You'd need some way of downloading
your expression anyway so the download code (within your PIC) could
actually be a modified bootloader.

Regards
Sergio Masci

2009\04\25@100927 by Isaac Marino Bavaresco

flavicon
face
Murat Seven escreveu:
> ISIS presents some calculator examples.
>
>
> murat

We could use a link here...

Isaac
__________________________________________________
Faça ligações para outros computadores com o novo Yahoo! Messenger
http://br.beta.messenger.yahoo.com/

2009\04\28@035515 by Murat Seven

picon face
sorry, I forgot.

the link:
http://www.labcenter.co.uk/download/prodemo_autodl_general.cfm

dowload Proteus, install and please look at the   ...\Examples.
there are much more perfect example applications.

murat



On Sat, Apr 25, 2009 at 5:09 PM, Isaac Marino Bavaresco <
RemoveMEisaacbavarescoTakeThisOuTspamspamyahoo.com.br> wrote:

{Quote hidden}

>


'[PIC] Simple expression evalutator in C (Preferabl'
2009\05\18@143002 by Isaac Marino Bavaresco
flavicon
face
M. Adam Davis escreveu:
> Very nice!  Thanks for contributing this and posting it online -
> significantly better than mine in practically every way.  I don't see
> where or if it handles parenthesis, but I imagine it wouldn't be hard
> to add if it doesn't handle them now.
>
> I added a link to it from my page.  Thanks for posting this!
>
> -Adam
>  

Adam,

Some days ago I posted a new version of the code. Now it is almost
complete, handles parenthesis and unary operators.
Lots of other small enhancements too.

The only thing I still miss is parsing of floating point values. Perhaps
some day I will do it.


Regards,

Isaac

__________________________________________________
Faça ligações para outros computadores com o novo Yahoo! Messenger
http://br.beta.messenger.yahoo.com/

More... (looser matching)
- Last day of these posts
- In 2009 , 2010 only
- Today
- New search...