Re: mathematical expressions

tmk@netvision.net.il (Michael Tiomkin)
15 May 2003 12:01:11 -0400

          From comp.compilers

Related articles
mathematical expressions _Maverick_@web.de (Seidl Markus) (2003-05-06)
Re: mathematical expressions thipse@sasken.com (Rajesh N Thipse) (2003-05-12)
Re: mathematical expressions bobduff@shell01.TheWorld.com (Robert A Duff) (2003-05-13)
Re: mathematical expressions vbdis@aol.com (2003-05-13)
Re: mathematical expressions tmk@netvision.net.il (2003-05-15)
Re: mathematical expressions guillermo.phillips@marsman.demon.co.uk (Guillermo Phillips) (2003-05-15)
| List of all articles for this month |

From: tmk@netvision.net.il (Michael Tiomkin)
Newsgroups: comp.compilers
Date: 15 May 2003 12:01:11 -0400
Organization: http://groups.google.com/
References: 03-05-033
Keywords: parse
Posted-Date: 15 May 2003 12:01:10 EDT

"Seidl Markus" <_Maverick_@web.de> wrote in message news:03-05-033...
> I want to write a parser which converts sth like "5+5^2" in an image >
> 5+5² < (i mean converting from mathematical expression to how you
> would write them with your own hand...)


    Well, what you want is not a parser but rather an interpreter that
is capable to parse an expression and to execute it as a text
formatting directive. The well known languages, e.g. TeX or troff, do
this. If you want to display an expression on screen, you can create
a Postscript code with TeX and display it using Ghostscript.
    If you want to do everything by yourself, you'll need to write
your own text/symbol formatter.


> My Problem: for example I write a rule like
>
> <term> ::= 'sqrt(' <term> ')'
> | <number>
>
> How can i draw the squareroot, when i have the following expression:
> sqrt(sqrt(sqrt(5))) ? I think im stuck there.I think i cannot draw
> the first square because i dont know how big and long i have to made
> it, and i cannot draw the last term, because i don't know where i have
> to draw them ( distance between the left edge of the image) ....


    The simplest way is to postpone execution until the parsing is done.
This means that at the first stage your parser can create an expression tree,
and at the second stage you can perform formatting of an expression.
You'll need the possibility to to know the size of an expression
when it's drawn. Then you'll be able to draw the 'sqrt(t)' in three
or four moves:
a. draw the 't' expression, find its length and height
b. draw the left part of the square root sign
c. draw the upper part of the square root


a. estimate the length and height of 't' expression
b. draw the left part of the square root sign
c. draw the upper part of the square root
d. draw the 't' expression


  The drawing with four moves can look a bit better because
you'll have a possibility to use the right width of the sqrt sign.
It only needs the possibility to draw with 'white ink',
without influencing the picture.


Notice also that drawing requires good graphic design. For example,
larger expressions need larger spaces between them, but
the dependency is less than linear.


    You can also see that your text can be translated directly into TeX
during the parsing, with a simple changing of syntax
and then the resulting TeX expression can be displayed
with an external tool.


    Michael


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.