views:

219

answers:

6

How to Write a Scripting Language using C? Any Idea? Links?

+1  A: 

This is a really general question and will likely be closed out, but hey, I might as well post this answer: Lua ( http://www.lua.org ) is a very popular scripting language written in C. You can use that for inspiration.

EboMike
+3  A: 

Why not look at the C-Code for a (easy) scripting language? Maybe Lua? (link )

Maybe you should also read somesthing about parser generators, which are the first building block that your program can interpret a language. The second block is to actually do, what was being parsed (sometimes you can integrate that into the parser, it can callback functions at each token/each structure being parsed). Easy examples, like parsing and executing math formulas could help.

nob
+5  A: 

Do you mean, how to write a language that is interpreted in C?

If so, you'll want to check Wikipedia and other sources on the following topics:

  • Lexical Analysis (converting source code into tokens, like keywords, integer/float literals, string literals, etc.)
  • Parsing (checking those tokens against a predefined, usually context-free grammar to make sure the program is syntactically correct)
  • Syntax Trees (usually constructed during parsing; create an intermediate tree representation that can be traversed in order to execute the code)
  • Symbol Tables (Basically, how to look up variable names)
  • Peephole Optimizations (if you want to make your code run faster, in exchange for taking more time during the pre-execution stage)

It's a lot more work than you might think... Happy reading!

Platinum Azure
+2  A: 

There's a good, if old, book called "Constructing Language Processors for Little Languages" that could get you started. It's very pragmatic and explains the various options that you have as your needs become more complex.

Beyond that, you're going to essentially want to research a book or two on compilers and interpreters. If you want a path of lower resistance, and you're doing this to understand concepts rather than something you need for a class or work projects, look at an alternative to C, such as Lisp or Scheme, that will make it easier to build on small experiments. Or learn about building DSLs in languages that make that easier (Boo, Groovy).

JasonTrue
+2  A: 
OscarRyz
+1  A: 

My advice would be start with an Recursive Descent Parser.

If you like this stuff, then you should definitly get a copy of the Dragonbook.

stacker