Re: implementation languages, was Supporting multiple input syntaxes

Thomas Koenig <tkoenig@netcologne.de>
Mon, 24 Aug 2020 17:01:59 -0000 (UTC)

          From comp.compilers

Related articles
Supporting multiple input syntaxes mijoryx@yahoo.com.dmarc.email (luser droog) (2020-08-12)
Supporting multiple input syntaxes davidlovemore@gmail.com (David Lovemore) (2020-08-15)
Re: Supporting multiple input syntaxes mijoryx@yahoo.com.dmarc.email (luser droog) (2020-08-15)
Re: Supporting multiple input syntaxes davidlovemore@gmail.com (David Lovemore) (2020-08-16)
Re: Supporting multiple input syntaxes mijoryx@yahoo.com.dmarc.email (luser droog) (2020-08-20)
Re: Supporting multiple input syntaxes mijoryx@yahoo.com.dmarc.email (luser droog) (2020-08-23)
Re: implementation languages, was Supporting multiple input syntaxes mijoryx@yahoo.com.dmarc.email (luser droog) (2020-08-23)
Re: implementation languages, was Supporting multiple input syntaxes tkoenig@netcologne.de (Thomas Koenig) (2020-08-24)
Re: implementation languages, was Supporting multiple input syntaxes mijoryx@yahoo.com.dmarc.email (luser droog) (2020-08-28)
| List of all articles for this month |

From: Thomas Koenig <tkoenig@netcologne.de>
Newsgroups: comp.compilers
Date: Mon, 24 Aug 2020 17:01:59 -0000 (UTC)
Organization: news.netcologne.de
References: 20-08-002 20-08-009 20-08-010 20-08-011 20-08-012 20-08-014 20-08-015
Injection-Info: gal.iecc.com; posting-host="news.iecc.com:2001:470:1f07:1126:0:676f:7373:6970"; logging-data="31239"; mail-complaints-to="abuse@iecc.com"
Keywords: parse, comment
Posted-Date: 24 Aug 2020 15:12:10 EDT

luser droog <mijoryx@yahoo.com.dmarc.email.dmarc.email> schrieb:


[PostScript]


> But the language itself I just really enjoy. It's my "Lego blocks"
> language. The RPN syntax removes all ambiguity about precedence and
> sequencing.


I recently had the doubtful pleasure of evaluating the formula


x = ((a-b)*c^2+(-d^2+e^2-a^2+b^2)*c+a^2*b+(f^2-e^2-b^2)*a
        +(-f^2+d^2)*b)/((-2*d+2*e)*c+(2*f-2*e)*a-2*b*(f-d))


in Postscript. (Yes, really. Don't ask.)


It was the first time in more than a decade that I wrote a
flex/bison grammar (mostly copied from the bison manual). It was
faster and less error-prone than trying to do it directly.


The grammar actually generated fairly unidiomatic PostScript
because I made it give names to all the variables, so


a-b


became


a b sub


I'm sure a real PostScript aficionado would have done it all
on the stack :-)
[Turning infix into RPN is a pretty basic intro compiler course exercise. Conceptually,
you make the parse tree and then do a postorder tree walk. Or if you'rs using yacc or
bison, in the expression grammar you just print the operator or token in the code for each
rule because they are recognized in the right order, e.g.:


expr: VARIABLE { printf(" %s ", $1): }
    | expr '+' expr ( printf(" add "); }
    | expr '-' expr ( printf(" sub "); }
    | '(' expr ')' { /* print nothing */ }


-John]


Post a followup to this message

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