help-bison
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: simple loop construct, advice needed


From: Hans Aberg
Subject: Re: simple loop construct, advice needed
Date: Fri, 13 Aug 2004 19:34:02 +0200

At 09:54 +0200 2004/08/13, address@hidden wrote:
>I am new to the flex/bison -- lex/yacc world.

Welcome.

>In order to create certain files at run-time, we are using a very simple
>"programming language" that is described by a set of
>flex rules and a bison grammar.
>This "language" then is evaluated and determines the structure of the files
>to be created.
>
>This "language" accepts only a very limited set of commands, e.g. DEFINE and
>CALL commands and an IF-ELSE-structure.
>
>In many cases, some commands are repeated (with absolutely no changes) a
>number of times.
>
>For performance reasons, I would like to add a very simple loop
>functionality to this existing flex/bison environment.
>
>For example:
>
>FOR 10
>  command1
>  command2
>  command3
>ENDFOR

This is the "loop-question", commonly asked here:

In order to implement a loop using Bison, you must build a "closure" for
the code, and execute it later, i.e., sometime after the whole loop
construct has been parsed. This also produces the fastest code.

So in your example (and you are starting correctly), you need to build such
"closures", or runtime objects, for the loop and what's in it, i.e., an
object "FOR N ... ENDFOR", a sequence object {c_1, ..., c_n}, as well as
such closures for the commands in this sequence.

If you do not want to produce such a runtime closure directly, you can make
your program produce output in a language that already has the feature, and
the compile and run that program. For example, you might decide your
program to produce C-code as output, compile that output using GCC and then
execute it. This is an approach used by some compilers, for example, the
GHC Haskell compiler, <http://haskell.org>.

  Hans Aberg






reply via email to

[Prev in Thread] Current Thread [Next in Thread]