Question

Ok guys I thought I'd take my old CS notes and look through compiler theory a little more. I have to say I can't for the life in me remember how all this stuff works but I do have a nice sample application from my college days that helps me understand a few things.

This sample application takes the made up language and compiles it down to an intermediate assembly code like language. There is then a simple VM implementation that takes this intermediate language and executes the statements.

The thing I cant get my head around is, if I this were a straight up interpretor and not a compiler would it still be building up these intermeditary commands in memory to be executed at the end. Or does an interpretor actually "execute" descreet sections of the code chunks at a time?

Was it helpful?

Solution

Parsers don't compile. There are actually quite a few steps involved when translating a program (from say a high level language like C++ to machine code). It depends on the design if it executes in one go or after multiple passes over the input. Can you make your question a bit more specific? Meanwhile, however much you hate it, have a look here -- specially the frontend and backend sections.

OTHER TIPS

It depends on the language. Most modern interpreted languages (Perl, Python, and Ruby, to name a few) precompile the source code into some intermediary form to be executed at the end (citation).

I have written or worked with interpreters that act directly upon parsing tokens in the input, that act directly on an abstract-syntax tree (AST) built by the parser, and that translate the AST to a form designed for efficient execution. So the answer is, it depends.

  • If your target machine has 8K of RAM, a direct-parsing interpreter would be a sensible choice (think FORTH).
  • If you are using interpreters to teach or learn about the structure and semantics of programming languages, building and interpreting an AST is a good choice.
  • If you are using an interpreter for portability and you want fast execution, compiling to a register-based virtual machine is a good choice. (David Gregg and others have shown there is less interpretive overhead in a register-based VM like the Lua VM than in a stack-based VM like the Java VM.)

Most modern interpreters parse program to intermediate code which later is interpreted. Some store this intermediate code explicitly (eg. Python's .pyc). There are exceptions for example, shell scripts are directly interpreted, rather then parsed to intermediate format.

Some more advanced "interpreters" actually do not interpret, but do JIT (just-in-time) compiling (eg. Java or .NET).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top