开发者

How to Write a Scripting Language using C? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.

Want to improve this question? Update the question so it focuses on 开发者_如何学Goone problem only by editing this post.

Closed 8 years ago.

Improve this question

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


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!


You can take a look at the book "The UNIX programming environment"

How to Write a Scripting Language using C? [closed]


(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


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.


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).


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.


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.


You may check out ANTLR[link]

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜