Re: Peu Expression Analyzer
- Posted by Rom <kjehas at frisurf.no> Nov 21, 2002
- 448 views
This is a multi-part message in MIME format. ------=_NextPart_000_007B_01C291C8.970A0C20 charset="iso-8859-1" >I built a C compiler many years ago and used a non-recursive expression = >analyzer because it was faster, and my head aches (still!) when I try = to=20 >understand or debug recursive routines. I an working on a script language. What makes this work so easy is that = I practically don't have to write an expression analyzer. I can use some = freeware expression evaluater (Calc Express w/source... 6 pages of code) = to evaluate what is expressions for assignment. What I have to do is to = parse what is left. In the script code below.. everything in RED is done = by the expression evaluator (it handle parantheses and presedence very = well). Since the expression evaluation will basicly be the same for different = languages, I think it was a good idea the make language and expression = evaluation two separate modules. Rom A script language var i, j, a, b: Single; v: array of Single;=20 procedure exitProc; begin Print( 'finished' ); end; procedure main; begin a :=3D 100; b :=3D 22.5 * (a / (1.05^3)); for i :=3D 1 to 10 do begin for j :=3D 1 to a do begin print( FloatToStr( i*10 + j)); end; end; a :=3D nil; v( a + 19 ) :=3D 20; b :=3D sysfunc( 2 * (100 + sin( v(10) + 10)); if ((a < 2) or (a > 5) ) and (v( 2) =3D 22.33) then begin exit; end ifNot begin=20 exitProc; exit; end else begin // no conclusion exitProc; exit; end; end; ------=_NextPart_000_007B_01C291C8.970A0C20 Content-Type: text/html; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML><HEAD> <META http-equiv=3DContent-Type content=3D"text/html; = charset=3Diso-8859-1"> <META content=3D"MSHTML 6.00.2600.0" name=3DGENERATOR> <STYLE></STYLE> </HEAD> <BODY bgColor=3D#ffffff> <DIV><FONT face=3DArial></FONT> </DIV> <DIV><FONT face=3DArial>>I built a C compiler many years ago and used = a=20 non-recursive expression </FONT></DIV> <DIV><FONT face=3DArial>>analyzer because it was faster, and my head = aches=20 (still!) when I try to </FONT></DIV> <DIV><FONT face=3DArial>>understand or debug recursive = routines.</FONT></DIV> <DIV><FONT face=3DArial></FONT> </DIV> <DIV><FONT face=3DArial></FONT> </DIV> <DIV><FONT face=3DArial>I an working on a script language. What makes = this work so=20 easy is that I practically don't have to write an expression analyzer. I = can use=20 some freeware expression evaluater (Calc Express w/source... 6 = pages of=20 code) to evaluate what is expressions for assignment. What I have to do = is to=20 parse what is left. In the script code below.. everything = in RED=20 is done by the expression evaluator (it handle parantheses and = presedence very=20 well).</FONT></DIV> <DIV><FONT face=3DArial></FONT> </DIV> <DIV><FONT face=3DArial>Since the expression evaluation will basicly be = the same=20 for different languages, I think it was a good idea the make = language and=20 expression evaluation two separate modules.</FONT></DIV> <DIV><FONT face=3DArial></FONT> </DIV> <DIV><FONT face=3DArial>Rom</FONT></DIV> <DIV><FONT face=3DArial></FONT> </DIV> <DIV><B><FONT size=3D5> <P align=3Dcenter>A script language</P></B></FONT> <P><FONT face=3D"Courier New" size=3D2>var</FONT></P> <P><FONT face=3D"Courier New" size=3D2> = i, j,=20 a, b: Single;</FONT></P> <P><FONT face=3D"Courier New" size=3D2> = v:=20 array of Single; </FONT></P> <P><FONT face=3D"Courier New" size=3D2>procedure exitProc;</FONT></P> <P><FONT face=3D"Courier New" size=3D2>begin</FONT></P> <P><FONT face=3D"Courier New" size=3D2> = Print(=20 'finished' );</FONT></P> <P><FONT face=3D"Courier New" size=3D2>end;</FONT></P> <P><FONT face=3D"Courier New" size=3D2>procedure main;</FONT></P> <P><FONT face=3D"Courier New" size=3D2>begin</FONT></P> <P><FONT face=3D"Courier New" size=3D2> = a :=3D=20 <FONT color=3D#ff0000>100</FONT>;</FONT></P> <P><FONT face=3D"Courier New" size=3D2> = b :=3D=20 <FONT color=3D#ff0000>22.5 * (a / (1.05^3))</FONT>;</FONT></P> <P><FONT face=3D"Courier New" size=3D2> = for i=20 :=3D <FONT color=3D#ff0000>1</FONT> to <FONT color=3D#ff0000>10</FONT> = do=20 begin</FONT></P> <P><FONT face=3D"Courier New" size=3D2> = =20 for j :=3D <FONT = color=3D#ff0000>1</FONT> to=20 <FONT color=3D#ff0000>a</FONT> do begin</FONT></P> <P><FONT face=3D"Courier New" size=3D2> = =20 = =20 print( FloatToStr( <FONT color=3D#ff0000>i*10 + j</FONT>));</FONT></P> <P><FONT face=3D"Courier New" size=3D2> = =20 end;</FONT></P> <P><FONT face=3D"Courier New" size=3D2> = =20 end;</FONT></P> <P><FONT face=3D"Courier New" size=3D2> = a :=3D=20 <FONT color=3D#ff0000>nil</FONT>;</FONT></P> <P><FONT face=3D"Courier New" size=3D2> = v(=20 <FONT color=3D#ff0000>a + 19 </FONT>) :=3D <FONT = color=3D#ff0000>20</FONT>;</FONT></P> <P><FONT face=3D"Courier New" size=3D2> = b :=3D=20 sysfunc(<FONT color=3D#ff0000> 2 * (100 + sin( v(10) + = 10)</FONT>);</FONT></P> <P><FONT face=3D"Courier New" size=3D2> = if=20 <FONT color=3D#ff0000>((a < 2) or (a > 5) ) and (v( 2) =3D = 22.33)</FONT> then=20 begin</FONT></P> <P><FONT face=3D"Courier New"=20 size=3D2> &nbs= p; exit;</FONT></P> <P><FONT face=3D"Courier New" size=3D2> = =20 end</FONT></P> <P><FONT face=3D"Courier New" size=3D2> = ifNot=20 begin </FONT></P> <P><FONT face=3D"Courier New" size=3D2> = =20 exitProc; exit;</FONT></P> <P><FONT face=3D"Courier New" size=3D2> = =20 end</FONT></P> <P><FONT face=3D"Courier New"><FONT size=3D2> = =20 else begin <FONT color=3D#0000ff>// no = conclusion</P></FONT></FONT></FONT> <P><FONT face=3D"Courier New" size=3D2> = =20 exitProc; exit;</FONT></P> <P><FONT face=3D"Courier New" size=3D2> = =20 end;</FONT></P> <P><FONT face=3D"Courier New" size=3D2>end;</FONT></P> ------=_NextPart_000_007B_01C291C8.970A0C20--