Re: Making an interpreter
Re: Making an interpreter
- Subject: Re: Making an interpreter
- From: Jason Alexander <email@hidden>
- Date: Sat, 7 Jun 2003 09:09:20 +0100
At 5:12 PM +0200 6/6/03, Daniele M. wrote:
hello, i would to make an interpreter for a "new scripting language"
of mine. Unfortunatly i don't know more about this topic
(parse/translate and code interpreting) so i'm searching for books/pdf
and other docs. Anyon can help me?
Is there around the net some example? book?
If you actually want to write your own interpreter, and the language
isn't too complex, use lex and yacc or their more modern GNU
equivalents, flex and bison. I think they are included "for free" on a
standard OS X install. If not, get them via fink.
Flex is a lexical analyzer use it to write the interpreter that
converts an input stream to tokens. Bison generates a parser the
tool that provides the semantics for the sequence of tokens.
Both allow you to specify the language using regexs and BNF-type
notation. They are very powerful and should suffice, as long as the
language isn't too complex (i.e., context dependent). I think it would
be a real chore to write perl in Bison, for example.
You'll probably want a book explaining how to use them. The GNU
documentation is pretty good. Alternatively, you can get the book: Lex
& Yacc by John R. Levine, Tony Mason, Doug Brown. (Flex and Bison are
supersets of lex and yacc, so this will get you started.)
In short, here's how they work. You write the tokenizer using a
special file format understood by flex. You name this file something
like tokenizer.l. You write the parser using a special file format
understood by bison. You name this file something like parser.y. Then
process both tokenizer.l and parser.y with flex and bison,
respectively. This generates two C files which you can compile and
integrate in your program.
It may sound like a lot of work, at first, but the fact that you can
specify the language constructs in the parser like
NUMBER: DIGITS
| DIGITS .
| SIGN DIGITS
| SIGN DIGITS .
| DIGITS . DIGITS
| SIGN DIGITS . DIGITS
and not worry about how to handle the fine details of each of those
cases is a real savings. Especially when your language grows and
collects new features (which it probably will).
Cheers,
Jason
--
J. McKenzie Alexander
Department of Philosophy, Logic and Scientific Method
London School of Economics and Political Science
Houghton Street, London WC2A 2AE
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.