parsex.icn: Program to parse arithmetic expressions

June 10, 1988; Cheyenne Wills
This file is in the public domain.
 Adapted from C code written by Allen I. Holub published in the
 Feb 1987 issue of Dr. Dobb's Journal.

 General purpose expression analyzer.  Can evaluate any expression
 consisting of number and the following operators (listed according
 to precedence level):

 () - ! 'str'str'
 * / &
 + -
 < <= > >= == !=
 && ||

All operators associate left to right unless () are present.
The top - is a unary minus.


 <expr>   ::= <term> <expr1>
 <expr1>  ::= && <term> <expr1>
          ::= || <term> <expr1>
          ::= epsilon

 <term>   ::= <fact> <term1>
 <term1>  ::= <  <fact> <term1>
          ::= <= <fact> <term1>
          ::= >  <fact> <term1>
          ::= >= <fact> <term1>
          ::= == <fact> <term1>
          ::= != <fact> <term1>
          ::= epsilon

 <fact>   ::= <part> <fact1>
 <fact1>  ::= + <part> <fact1>
          ::= - <part> <fact1>
          ::= - <part> <fact1>
          ::= epsilon

 <part>   ::= <const> <part1>
 <part1>  ::= * <const> <part1>
          ::= / <const> <part1>
          ::= % <const> <part1>
          ::= epsilon

 <const>  ::= ( <expr> )
          ::= - ( <expr> )
          ::= - <const>
          ::= ! <const>
          ::= 's1's2'    # compares s1 with s2  0 if ~= else 1
          ::= NUMBER     # number is a lose term any('0123456789.Ee')

Source code | Program Library Page | Icon Home Page