Writing Compilers (was: Re: OLC i hope the final one)

From: Daniel A. Koepke (dkoepke@california.com)
Date: 12/24/99


On Fri, 24 Dec 1999, Francis Hotchkiss wrote:

> Does anyone know of any good examples on how to use lex and yacc (or
> flex and bison) to write compilers?  The reason I ask is that I'd like
> to replace the stock Circle magic system with a custom spell language
> I'm developing. Thanks in advance for any help.

flex/bison (lex/yacc, etc.) are not tools for writing compilers, per se.
flex is a tool for writing lexical analysers, i.e., something which
analyses an input and identifies the lexicographic signifance of tokens.
yacc is a tool for writing parsers (or "grammars"), i.e., something which,
with the help of a lexical analyser, is able to match entire grammatical
constructs consisting of tokens.

In other words, with flex you provide what is, for all intents and
purposes, a dictionary of "words" and associated meanings of those
"words".  This works on the scale of phonemes.  With yacc you provide a
grammar for the words you have just defined and associated meanings for
those grammatical constructs (a "sentence", if you will).

The being said, the "info" files for flex and bison are full of examples
of using the tools.  After you understand the concepts and format for
using the tools, the real task is in writing the compiler and VM for
executing the code (assuming, of course, you're compiling to byte
code and not to some other language).

Where to go from there is highly dependent upon what exactly you really
want to do with the language.  If you're just defining the fields of the
spell (i.e., basically the function of the spello()-ish functions), then
it can be a pretty simple:

    spell "fireball" :
        thief-level           -1
        warrior-level         -1
        cleric-level          -1
        mage-level            12
        is-violent            true
        .
        .
        .
    ;

or whatever your preferred format is.  If that's really all your language
is going to be used for, then it's not really a computer language, per se,
and is really a database format.  No compiler or VM would be necessary and
a straight-forward parser could be implemented in minutes with flex and
bison, or, since you wouldn't really be reaping any benefit from using
these tools, just plain old ANSI C.

In short: elaborate on what you want to do.

-dak


     +------------------------------------------------------------+
     | Ensure that you have read the CircleMUD Mailing List FAQ:  |
     |  http://qsilver.queensu.ca/~fletchra/Circle/list-faq.html  |
     +------------------------------------------------------------+



This archive was generated by hypermail 2b30 : 12/15/00 PST