Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c

scripting

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

like image 603
user432213 Avatar asked Aug 26 '10 18:08

user432213


People also ask

Can you use C as a scripting language?

C/C++ can be used for maximal performance and complicated systems programming tasks. Scripting languages can be used for rapid prototyping, interactive debugging, scripting, and access to high-level data structures such associative arrays.

Why is C not a scripting language?

The only theoretical difference is that a scripting language does not include the compilation step- it is interpreted instead. For instance, one needs to first compile a C program before running it. On the other hand, one does not need to compile a scripting language such as PHP or JavaScript.


3 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!

like image 177
Platinum Azure Avatar answered Sep 17 '22 16:09

Platinum Azure


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

like image 37
OscarRyz Avatar answered Sep 20 '22 16:09

OscarRyz


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.

like image 34
nob Avatar answered Sep 21 '22 16:09

nob