How to Write a Scripting Language using C? Any Idea? Links?
-
8That's too general to be answered. – Darin Dimitrov Aug 26 '10 at 18:40
-
3Download the source for bash. Done. – S.Lott Aug 26 '10 at 18:41
-
1Ruby was written in C and is open source, so have a look at that... – Mischa Aug 26 '10 at 18:43
-
3IMO, the existing options that have been mentioned (bash, Lua, Ruby) are either too large and complex, or else have a long history with which to maintain backward compatibility. Either way, it ends up too large and complex to be really approachable. I'd probably pick up a copy of *The UNIX Programming Environment*, which includes a project to develop a small programming language in a number of discrete, nicely-explained steps. You probably don't want to use the language or code directly, but you can still learn from it. – Jerry Coffin Aug 26 '10 at 18:50
-
See my answer. There's a very good link there. – OscarRyz Aug 26 '10 at 18:59
-
I hate it when interesting questions that generate lots of answers and votes get closed. There's good information in here, so why cut it off? – Adrian McCarthy Aug 26 '10 at 19:40
-
@Adrian McCarthy: I can see that it's interesting. But it seems too vague to be answered. How could someone give an answer to this an "accepted"? What would the criteria for acceptance even be? – S.Lott Aug 26 '10 at 20:17
-
@S.Lott: The question specifically asks for links. Several of the many answers that appeared in the 20 minutes this question was open provided good links. I think that's clear evidence that the question was not too general, unanswerable, nor that it couldn't generate enough interest. – Adrian McCarthy Aug 27 '10 at 18:03
-
@Adrian McCarthy: I already agreed that it's interesting. How could someone give an answer to this an "accepted"? mark? What would the criteria for acceptance even be? A "link"? How does that make the question more specific? Any link to any interpreter? Seems like the choice of "acceptable" answers would then be random. Not a good question when there's no obvious acceptance criteria. – S.Lott Aug 27 '10 at 19:32
-
The canonical questions on compiler methods is [Learning to write a compiler](http://stackoverflow.com/questions/1669/learning-to-write-a-compiler), and the technique and resources list there also apply to interpreters, so it pretty much covers everything. – dmckee --- ex-moderator kitten Aug 28 '10 at 18:03
-
@S.Lott: Consider Jason True's answer which provides a link to a good book that explains how to write an interpreter. It seems to provide exactly what the questioner was looking for, and it's useful to others looking for good references on the topic. – Adrian McCarthy Aug 30 '10 at 12:45
-
@Adrian McCarthy: "seems to provide exactly what the questioner was looking for". Really? How can that be deduced from the question? What part of the question points to a specific answer as acceptable? Why isn't http://ftp.gnu.org/gnu/bash/ equally good? I don't see how to evaluate the answers given such a weak question. What criteria did you apply to recommend that answer over the others? – S.Lott Aug 30 '10 at 12:59
-
@S.Lott: I honestly don't understand your point. The question asked for ideas and links for how to write an interpreter. Several of the answers that managed to slip in before this was closed managed to provide good ideas and good links. What exactly is the problem? Your example of pointing to bash would not be a good link because that's not a how-to, it's an example-of. – Adrian McCarthy Aug 30 '10 at 16:27
-
@Adrian McCarthy: "good ideas and good links."? I don't understand how they can be called "good". I'm asking for the criteria you are using to judge them as "good". I don't understand how you're making that judgement. I'm just asking a question: How -- given a question this bad -- did just some answers as "good"? I don't understand how bash is not an example of an interpreter. It seems to fit the criteria. I'm asking what rule or process or feature you're using. I'm curious how people make judgements in cases like this. – S.Lott Aug 30 '10 at 18:14
-
@S.Lott: I didn't say bash wasn't an example of an interpreter. It is. I said it's not a "how-to" on writing interpreters. Which it isn't. The question specifically asked for how-to information. – Adrian McCarthy Aug 30 '10 at 19:46
-
@Adrian McCarthy: So you're saying "How to Write..." means "I want a resource that shows me how to write..." Interesting. Thanks. – S.Lott Aug 30 '10 at 23:02
-
Hey guys? I don't think it's a bad thing to respond to a "bad" question with a good answer, one that maybe covers many possibilities. Voting to close just because the answer isn't something you can Google in 10 seconds strikes me as a little weak. If you can't answer it yourself, leave it to someone who can! :-) – Platinum Azure Aug 31 '10 at 00:56
7 Answers
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!

- 45,269
- 12
- 110
- 134
-
2The second and third parts are the most complex to get your head around. – Nick Bedford Feb 09 '12 at 22:09
You can take a look at the book "The UNIX programming environment"
(source: bell-labs.com)
At the end of the book, and pretty much the last chapter, the authors, wrote a small interpreter in C with enough and detailed information step by step. Very interesting and very easy to follow.
The source code of that interpreter is here

- 21,988
- 13
- 81
- 109

- 196,001
- 113
- 385
- 569
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.

- 1,404
- 10
- 13
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).

- 19,244
- 4
- 34
- 61
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.

- 76,846
- 14
- 164
- 167
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.

- 68,052
- 28
- 140
- 210