help-bison
[Top][All Lists]
Advanced

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

Re: About the FOR loop semantic action


From: lfinsto1
Subject: Re: About the FOR loop semantic action
Date: Sun, 21 Oct 2007 20:11:56 +0200 (CEST)
User-agent: SquirrelMail/1.4.9a

Ilyes Gouta wrote:

> Hmm.. So bison is able to read from data buffers instead of a FILE*.
> How can I achieve that?

I don't remember off-hand.  It's documented in the manual, or you can look
in my source code.  It involves `yyin'.

>> I think it's usually not a good idea to execute code in the middle of
>
> I agree, but I didn't find another way, yet. Back to for loops, if I'm
> going to copy my block into a temporary buffer then I'll have to do
> something similar, i.e executing an action in the middle of a rule, at
> least to mark the beginning of a block and its end, i.e:
>
> block:
>     '{' { begin_marker(); } declarations statements { end_marker(); } '}'
> ;
>
> to save the content of the entire block and re-execute it as long as
> the condition is valid.

No, you can use more than one rule.  3DLDF is easier than C here, because,
like METAFONT, it uses `endfor' to mark the end of a loop and `fi' to mark
the end of a conditional.  The way C does it is reasonable for a
heavy-duty, general-purpose language, but not for a special-purpose
interpreter like 3DLDF or a toy language.  It wouldn't be that difficult
to implement a language in which all blocks for conditionals and loops are
enclosed in curly braces, but making it possible to have a single
statement instead makes things much more difficult.

To find the end of a block, if you want to, you can just scan ahead using
`yylex'.  Nobody said you have to pass the tokens to `yyparse'.  You can
then resume reading from your input at some previous point that you've
marked.  I don't actually do it this way.  For conditionals, it works like
this:

If the condition is false, skip over tokens until you find an `elif', an
`else', or a `fi'.  If it's an `else', just execute the following code,
discarding the `fi' when you find it.  If it's an `elif', do the same
thing as with an `if'.  If the condition is true, execute the code until
you find an `else', an `elif' or a `fi'.  If it's an `else' or an `elif',
discard everything up until the `fi'.  If it's a `fi', discard it.  Nested
conditionals are handled in exactly the same way, except that one needs to
keep track of one's position in the "stack".

Simple, really.

For the full story, have a look at my code.  You can find the rules in
`parser.output' in the `CWEB' directory and use grep to find the CWEB
files with the source.  Here's the link for the directory again:
http://cvs.savannah.gnu.org/viewvc/3dldf/3dldf/Group/CWEB/

Laurence











reply via email to

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