473,654 Members | 3,239 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 4184
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
2151
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 interpreter does not influence the other. In essence, the interpreter can be used in different modules of a single application. It would be nice to isolate them. There are two possibilities:
6
2580
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 pointers -- Hrvoje
2
2166
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 python scripts via a well defined interface (i.e. The onCreate script will be called when the object is created). So...Is it necessary to use multiple interpreters since each script I import will be given its own module name and therefore the...
1
3172
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 looking for, but nothing that really solves or explains all this. I'll admit to being stumped, hence my question here. I'm also trying to make this post as clear and detailed as possible. Unfortunately, that means it's come out like a book. I...
12
2450
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, I want to know about them. Are interpreters of C available, and if yes, how can I get them. Madhura
8
2183
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 which weresically used by the computers. I want to know about the basic compiler software.
0
4784
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 branch of CS and I don't want the article(s) to be the size of a book, so we have to keep things a bit simple. On the other hand the entire thing must have practical use more or less so the code to be developed must be extensible so that you can...
3
9158
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 arguments that would tell on which interpreter to run the string...? Marcin
0
4056
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 simply was no time left for writing. we've come a long way; the previous articles discussed the following aspects of compilers:
7
12543
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: C++ main thread 1 Py_Initialize() thread 2 Py_Initialize()
0
8294
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8709
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8596
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7309
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5627
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4150
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4297
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1924
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1597
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.