473,503 Members | 1,655 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Compilers - 6B: Interpreters

11,448 Recognized Expert MVP
Greetings,

welcome back again. The part of the articles rounds up the discussion on the
Interpreter and disusses some code uploaded in the attachment.

The Interpreter again

The Interpreter class delegates the hard work to the Memo class; it has a Memo
object as one of its members to delegate to:

Expand|Select|Wrap|Line Numbers
  1. protected Memo memo= new Memo();
  2.  
  3. public Memo memo() { return memo; }
  4.  
The Interpreter has three major sub-components:

1) the Stack object on which all calculations take place;
2) the Scope object that takes care of all local variables;
3) the Memo object that memoizes previous user function calls.

The Interpreter itself doesn't do much, i.e. it just invokes the execute method
on all the Instructions in the Code list. The Instruction thing is an interface
which all real Instructions have to implement.

As you can conclude from the simplicity of the Interpreter: most, if not all, of
the 'intelligence' is implemented in the Instruction implementations. The Interpreter
only supplies the mechanics to run a compiled program.

The Instructions are very much unlike 'real' machine code instructions. Our
Instruction classes implement a much higher level functionality. The next part(s)
of this article show the most important instructions and the mechanism they
implement and how more instructions can be added to the set. Mostly Instructions
implement a function, a list function or a quoted object.

Concluding Remarks

This article part contains an attachment: a zip file with all the source code
so far in it. This code is supplied "as is" and I take no responsibility whatsoever for it. It is quite harmless though: there's a Main class in the compiler
package that you can use to play with the entire thing a bit.

This is what it says when you invoke it in an incorrect way:

Expand|Select|Wrap|Line Numbers
  1. usage: java compiler.Main <infile> [-i ] [-o <outfile>] [-d] [-m] [-r | -t]
  2.     <infile> a file that contains the source
  3.     -i consider <infile> compiled code
  4.     -o <outfile> save compiled code to <outfile>
  5.     -d dump compiled to to System.out
  6.     -m turn on memoization (default is off)
  7.     -r run the compiled code
  8.     -t trace the compiled code
  9.  
  10.     the content of the stack is displayed at the end
  11.  
The Main class is just a little driver for it all (the sources are included).
Scrutinize through the code and try to understand it all. An explanation can
be found in the Compiler articles but feel free to comment and/or ask questions.

The Main driver always reads from the <infile>. When you don't supply the -i
flag the driver assumes that it has to compile source code, stored in that file.
If you do supply the -i flag the driver assumes that the file contains compiled
code. If you supply the -o <outfile> arguments the driver will save the compiled
code to that file. When you supply the -r or the -t flag the driver will start
the Interpreter for you and runs the code. When execution has finished the stack
will be printed. When the code is run with the -t flag specified, every instruction
will be printed along with the current stack values just before it is executed.

The -m flag turns memoization on and the -d flag dumps the compiled code to the
System.out stream in a human readable form before execution starts (if the -r or
-t flag are passed on the command line) or just before the driver terminates.

Play with it a bit, e.g. for starters store the following content in a file and
try to compile and run it:

Expand|Select|Wrap|Line Numbers
  1. 1+2;
  2. 3+4;
  3. 5:6:+7+8;
  4.  
See you next week in the sequel of this ongoing article and

kind regards,

Jos
Jun 17 '07 #1
1 4177
sargaros
1 New Member
I'm sorry to ask, but the file is missed. Could you attach to the article?
Thank you.
Feb 25 '11 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
2141
by: Maciej Sobczak | last post by:
Hi, I'm interested in embedding the Python interpreter in a C++ application. What I miss is the possibility to create many different interpreters, so that the stuff that is running in one...
6
2572
by: Hrvoje Blazevic | last post by:
Are there any good books on Interpreter/Compiler construction, using Python as a defining language out there? Something like Essentials of Programming Languages 2e ? I would appreciate some...
2
2160
by: bmatt | last post by:
I am trying to support multiple interpreter instances within a single main application thread. The reason I would like separate interpreters is because objects in my system can be extended with...
1
3162
by: Craig Ringer | last post by:
Hi folks I'm a bit of a newbie here, though I've tried to appropriately research this issue before posting. I've found a lot of questions, a few answers that don't really answer quite what I'm...
12
2427
by: madhura | last post by:
Hello, I have a basic question about compilers. What are the different types of compilers, are they written by different companies, why there are different names of compilers, what is the purpose,...
8
2171
by: pransri2006 | last post by:
Hi guys! I think all of u know about the designing of compilers. Can any body tell me about the designing of the compilers. And also tell me the difference between the compilers and Interpreter...
0
4773
by: JosAH | last post by:
Greetings, last week's tip was a bit of playtime where we've built a Sudoku solver. This week we're going to build some complicated stuff: a compiler. Compiler construction is a difficult...
3
9121
by: Marcin Kalicinski | last post by:
How do I use multiple Python interpreters within the same process? I know there's a function Py_NewInterpreter. However, how do I use functions like Py_RunString etc. with it? They don't take any...
0
4041
by: JosAH | last post by:
Greetings, Introduction This part of the article is one week late; I apologize for that; my excuse is: bizzy, bizzy, bizzy; I attended a nice course and I had to lecture a bit and there...
7
12478
by: skip | last post by:
This question was posed to me today. Given a C/C++ program we can clearly embed a Python interpreter in it. Is it possible to fire up multiple interpreters in multiple threads? For example: ...
0
7201
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7278
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6988
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7456
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5578
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3166
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
734
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
379
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.