• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Making an interpreter
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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.

  • Follow-Ups:
    • Re: Making an interpreter
      • From: Brian Gilman <email@hidden>
    • XML plist to NSArray
      • From: Michael Hanna <email@hidden>
  • Prev by Date: Re: Comments & Documentation (was Re: semantics of init)
  • Next by Date: Very odd ScreenSaverView/setAnimationTimeInterval: problem
  • Previous by thread: Re: Making an interpreter
  • Next by thread: XML plist to NSArray
  • Index(es):
    • Date
    • Thread