[MUD-Dev] Storing tokens with flex & bison

Per Vognsen vognsen at mindless.com
Sat Jan 1 16:39:17 New Zealand Daylight Time 2000

Hello everybody and a happy new year to all! I'm sorry if I don't make
sense at times in this post -- I just got home from a party and I'm
experiencing a rather bad hangover, I'm afraid.

Christer Enfors spake thusly:
> I'm not sure if this is the proper forum for this kind of question, but I
> can't find any mailing lists for lex & yacc, and it is a MUD I'm
> developing...

I'm not sure it is either, but since you've already brought it up, I'll
try to answer to the best of my ability. I've written a few complete
languages (with interpreters and so on) and a number of other parsers in
the past, using lex and yacc mostly.

> When flex reads a file, would it be a good idea to store the tokens
> returned from yylex() in a list? I was thinking one list for each function
> definition, and then having bison read from that list when executing that
> function, instead of reading directly from the file using flex?

What I think you'd rather want to do here is to use some intermediate
form for storage - it would be completely irrational to scan, parse and
interpret the code each time you want to execute it. For this task you
can use an AST (abtract syntax syntax), byte-code (usually generated
from some other form, like ASTs), or other even more exotic options.

> If so, how would I get yylex() to return tokens from my list instead of
> tokens from the file? Or would that be going about it the wrong way?

You could do it by writing your own yylex() function which in turn
interfaced to the 'real' lexer function generated by lex (you can
specify and alternate name for the generated yylex() - see the man

> Maybe someone could point me to a lex & yacc tutorial that includes
> writing an interpreted language that allowes user defined functions
> written in that language, since that is basically what I'm trying to do
> myself.

If you're interested in writing languages in general, I think the Dragon
book is still the classic (although a bit dated in some areas).
"Compilers: Principles, Techniques, and Tools" by Aho, Sethi, and Ullman
from Addison-Wesley. If you want to learn more about lex and yacc (flex
and bison are the GNU versions) try checking out "lex & yacc" by Levine,
Mason, and Brown from O'Reilly

>                      -=-=- Christer Enfors -=-=-
>        "I do my music in pure machine code using an assembler."
>                             - Rob Hubbard

_/_ Per Vognsen <vognsen at mindless.com> (C++ and Linux Programmer)
 /  "It was a hobbit hole, and that meant comfort." - JRR Tolkien

MUD-Dev maillist  -  MUD-Dev at kanga.nu

More information about the MUD-Dev mailing list