473,799 Members | 2,746 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C/C++ interpreter

gr
hi.. I must implement an interpreter in C programming language that
will get the source code of a program in text file format and execute
it.

but i don't know C language enough to write this interpreter. so I need
this interpreter code that is written with C language.(or it can be
C++) I searched this codes in google and some websites, but codes which
I found are useless..

if you can help me, you gladden me.

please help me.. :(

*************** **********

a part from my homework;

Your interpreter program will accept and execute statements below:
· int variable_name
· cin>variable_na me
· cout<< variable_name
· variable_name = infix arithmetic expression (variable names, +, -,
*, /, and parentheses (..)).
· for loop
· if else statement
(your interpreter does not need to recognize and execute nested for and
if/else statements .
It should give error messages. Possible syntactic errors relating
source code input are:
· Undefined variable <variable_nam e>
· Paranthesis mismatch
· Curly brackets mismatch
For the arithmetic expression evaluation you are free to use directly
an existing evaluator program source code like polish notation within
your interpreter.

Jul 8 '06 #1
6 5515
"gr" writes:

hi.. I must implement an interpreter in C programming language that
will get the source code of a program in text file format and execute
it.

but i don't know C language enough to write this interpreter. so I need
this interpreter code that is written with C language.(or it can be
C++) I searched this codes in google and some websites, but codes which
I found are useless..

if you can help me, you gladden me.

please help me.. :(

*************** **********

a part from my homework;

Your interpreter program will accept and execute statements below:
· int variable_name
· cin>variable_na me
· cout<< variable_name
· variable_name = infix arithmetic expression (variable names, +, -,
*, /, and parentheses (..)).
· for loop
· if else statement
(your interpreter does not need to recognize and execute nested for and
if/else statements .
It should give error messages. Possible syntactic errors relating
source code input are:
· Undefined variable <variable_nam e>
· Paranthesis mismatch
· Curly brackets mismatch
For the arithmetic expression evaluation you are free to use directly
an existing evaluator program source code like polish notation within
your interpreter.

That looks to me like an unrealistically time consuming assignment for a
student. You have my sympathy.
Jul 8 '06 #2
Hi,

If you have time, maybe you could look at GNU flex and bison to help
you in your task...

http://flex.sourceforge.net/
http://www.gnu.org/software/bison/

Thanks and regards
Sonison James
osmium wrote:
"gr" writes:

hi.. I must implement an interpreter in C programming language that
will get the source code of a program in text file format and execute
it.

but i don't know C language enough to write this interpreter. so I need
this interpreter code that is written with C language.(or it can be
C++) I searched this codes in google and some websites, but codes which
I found are useless..

if you can help me, you gladden me.

please help me.. :(

*************** **********

a part from my homework;

Your interpreter program will accept and execute statements below:
· int variable_name
· cin>variable_na me
· cout<< variable_name
· variable_name = infix arithmetic expression (variable names, +, -,
*, /, and parentheses (..)).
· for loop
· if else statement
(your interpreter does not need to recognize and execute nested for and
if/else statements .
It should give error messages. Possible syntactic errors relating
source code input are:
· Undefined variable <variable_nam e>
· Paranthesis mismatch
· Curly brackets mismatch
For the arithmetic expression evaluation you are free to use directly
an existing evaluator program source code like polish notation within
your interpreter.

That looks to me like an unrealistically time consuming assignment for a
student. You have my sympathy.
Jul 10 '06 #3
"gr" <gr********@gma il.comwrote in message
news:11******** *************@m 73g2000cwd.goog legroups.com...
hi.. I must implement an interpreter in C programming language that
will get the source code of a program in text file format and execute
it.

but i don't know C language enough to write this interpreter. so I need
this interpreter code that is written with C language.(or it can be
C++) I searched this codes in google and some websites, but codes which
I found are useless..

if you can help me, you gladden me.

please help me.. :(

*************** **********

a part from my homework;

Your interpreter program will accept and execute statements below:
· int variable_name
· cin>variable_na me
· cout<< variable_name
· variable_name = infix arithmetic expression (variable names, +, -,
*, /, and parentheses (..)).
· for loop
· if else statement
(your interpreter does not need to recognize and execute nested for and
if/else statements .
It should give error messages. Possible syntactic errors relating
source code input are:
· Undefined variable <variable_nam e>
· Paranthesis mismatch
· Curly brackets mismatch
For the arithmetic expression evaluation you are free to use directly
an existing evaluator program source code like polish notation within
your interpreter.

---------

Very difficult assignment. I will say, though, that if you can get it done
you'll learn a lot.

A book I picked up years ago, The ART of C++, has the code for a mini C++
interpreter. The book itself actually isn't that good, but it does have
that.
Jul 12 '06 #4
Jim Langston wrote:
"gr" <gr********@gma il.comwrote in message
news:11******** *************@m 73g2000cwd.goog legroups.com...
hi.. I must implement an interpreter in C programming language that
will get the source code of a program in text file format and execute
it.

but i don't know C language enough to write this interpreter. so I need
this interpreter code that is written with C language.(or it can be
C++) I searched this codes in google and some websites, but codes which
I found are useless..

if you can help me, you gladden me.

please help me.. :(

*************** **********

a part from my homework;

Your interpreter program will accept and execute statements below:
· int variable_name
· cin>variable_na me
· cout<< variable_name
· variable_name = infix arithmetic expression (variable names, +, -,
*, /, and parentheses (..)).
· for loop
· if else statement
(your interpreter does not need to recognize and execute nested for and
if/else statements .
It should give error messages. Possible syntactic errors relating
source code input are:
· Undefined variable <variable_nam e>
· Paranthesis mismatch
· Curly brackets mismatch
For the arithmetic expression evaluation you are free to use directly
an existing evaluator program source code like polish notation within
your interpreter.

---------

Very difficult assignment. I will say, though, that if you can get it done
you'll learn a lot.

A book I picked up years ago, The ART of C++, has the code for a mini C++
interpreter. The book itself actually isn't that good, but it does have
that.

Horrible assignment. Can you get to a library? If so, try "Constructi ng Language
Processors for Little Languages" by Randy M. Kaplan. Another you might be able to get
a hold of is "Compiler Design in C" by Allen I Holub, though this is probably just a
bit too advanced but still readable. In both of these the code samples are written in
C. Another you might try (in C++ this time - though not in the modern idiomatic
style) is "Writing Compilers and Interpreters" by Ronald Mak.

To start with you will need a stack. This will help you with parenthesis matching and
expression evaluation (among other things).

To illustrate, a simple function to check whether or not an arbitrary string has
balanced parentheses can be easily constructed as follows (warning: this is not
idiomatic C++, does not do any argument checking and assumes a "C" locale):

bool hasMatchedParen theses (std:string const& in)
{
std::string::si ze_type const insize(in.size( ));
std::string::si ze_type nparens(0);

for (std::string::s ize_type i(0); i != insize; ++i)
{
switch (in[i])
{
case '(':
++nparens;
break;
case ')':
--nparens;
break;
default:
break;
}
}

return nparens != 0;
}

This function uses an unsigned integral type as a "stack" to check for matched
parentheses. A non-zero value indicates an unbalanced set. You might ask what happens
if the number of ')' characters is greater than the number of '(' characters or if a
')' appears before '(' in the string? Well nparens will wrap round to a large
positive value since the standard guarantees this for unsigned integral types. In
this case, nparens is still non-zero so the test in the return statement will still
catch the error. One case it does _not_ handle is a string containing no parentheses
at all.

This function of course needs reworking to indicate the position where the error
occurred and what kind of error it was but it is sufficient to illustrate the kind of
thing you will need to do.

Hope this helps,
Jim.
Jul 12 '06 #5
James Bannon wrote:
Jim Langston wrote:
>"gr" <gr********@gma il.comwrote in message
news:11******* **************@ m73g2000cwd.goo glegroups.com.. .
hi.. I must implement an interpreter in C programming language that
will get the source code of a program in text file format and execute
it.

but i don't know C language enough to write this interpreter. so I need
this interpreter code that is written with C language.(or it can be
C++) I searched this codes in google and some websites, but codes which
I found are useless..

if you can help me, you gladden me.

please help me.. :(

************** ***********

a part from my homework;

Your interpreter program will accept and execute statements below:
· int variable_name
· cin>variable_na me
· cout<< variable_name
· variable_name = infix arithmetic expression (variable names, +, -,
*, /, and parentheses (..)).
· for loop
· if else statement
(your interpreter does not need to recognize and execute nested for and
if/else statements .
It should give error messages. Possible syntactic errors relating
source code input are:
· Undefined variable <variable_nam e>
· Paranthesis mismatch
· Curly brackets mismatch
For the arithmetic expression evaluation you are free to use directly
an existing evaluator program source code like polish notation within
your interpreter.

---------

Very difficult assignment. I will say, though, that if you can get it
done you'll learn a lot.

A book I picked up years ago, The ART of C++, has the code for a mini
C++ interpreter. The book itself actually isn't that good, but it
does have that.

Horrible assignment. Can you get to a library? If so, try "Constructi ng
Language Processors for Little Languages" by Randy M. Kaplan. Another
you might be able to get a hold of is "Compiler Design in C" by Allen I
Holub, though this is probably just a bit too advanced but still
readable. In both of these the code samples are written in C. Another
you might try (in C++ this time - though not in the modern idiomatic
style) is "Writing Compilers and Interpreters" by Ronald Mak.

To start with you will need a stack. This will help you with parenthesis
matching and expression evaluation (among other things).

To illustrate, a simple function to check whether or not an arbitrary
string has balanced parentheses can be easily constructed as follows
(warning: this is not idiomatic C++, does not do any argument checking
and assumes a "C" locale):

bool hasMatchedParen theses (std:string const& in)
{
std::string::si ze_type const insize(in.size( ));
std::string::si ze_type nparens(0);

for (std::string::s ize_type i(0); i != insize; ++i)
{
switch (in[i])
{
case '(':
++nparens;
break;
case ')':
--nparens;
break;
default:
break;
}
}

return nparens != 0;
}

This function uses an unsigned integral type as a "stack" to check for
matched parentheses. A non-zero value indicates an unbalanced set. You
might ask what happens if the number of ')' characters is greater than
the number of '(' characters or if a ')' appears before '(' in the
string? Well nparens will wrap round to a large positive value since the
standard guarantees this for unsigned integral types. In this case,
nparens is still non-zero so the test in the return statement will still
catch the error. One case it does _not_ handle is a string containing no
parentheses at all.

This function of course needs reworking to indicate the position where
the error occurred and what kind of error it was but it is sufficient to
illustrate the kind of thing you will need to do.

Hope this helps,
Jim.
The return statement should read return nparens == 0; Surprised no-one picked me up
on that!
Jul 14 '06 #6
so***********@g mail.com wrote:
Hi,

If you have time, maybe you could look at GNU flex and bison to help
you in your task...

http://flex.sourceforge.net/
http://www.gnu.org/software/bison/
Good advice, but (I'm addressing the OP here) the terms of your
assignment may dictate that you have to hand-roll it all yourself. But
even if so you should definitely still structure your program as a
separate lexer to pick out tokens from the input, and a parser to make
sense of these tokens in the context of your grammar. If you're
unfamiliar with the concepts, a look at the Flex and Bison
documentation will clue you in.

Jul 14 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
5791
by: Neil Benn | last post by:
Hello, I'm looking at a small app which would need a very quick startup time for the Python interpreter. It doesn't do much (copying and caching of files, no GUI) but I need the Python interpreter to start up very quickly (<1 second on a Windows box). Is there a way to have a 'stripped' down Python interpreter which can start up very quickly on a windows box. Once thing I was thinking of was to use PyExe to make a quick startup (does...
12
2413
by: Anon | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hello all I am a beginner teaching myself python, and I am enjoying it immensely :) As a language it is great, I real treat to study, I actually find it fun to study the language heh Anyways to the point of my post.....
1
2916
by: Donnie Leen | last post by:
I wrote a program embbed boost.python, each thread running a sub-interpreter, i made a module implement by boost.python, and i wish this module imported in each sub-interpreter, i find that the module could initialize only once, otherwise the boost.python will throw a exception said that something already registered; second conversion method ignored. So I try another way, initialize and import the module in one interpreter such as the main...
12
12287
by: Rex Eastbourne | last post by:
Hi, I'm interested in running a Python interpreter in Emacs. I have Python extensions for Emacs, and my python menu lists "C-c !" as the command to run the interpreter. Yet when I run it I get the message "Spawning Child Process: invalid argument." What do I need to do/download to fix this? I read in a post in this group from a while back where someone had the following lines in his .emacs file:
4
1990
by: Ian Giblin | last post by:
I am an experienced C programmer, learning C++ by writinging a mathematical toolkit in the framework of a script interpreter. I am posting here to ask for advice (or references) on the object design and implimentation. Currently I have a portable "ScriptSession" class which contains the mechanics of looping with a user prompt, parsing a sentence and handling syntax errors, etc., and I wan this to be a class I can use for any script...
12
1889
by: ozbear | last post by:
If one were writing a C interpreter, is there anything in the standard standard that requires the sizeof operator to yield the same value for two different variables of the same type? Let's assume that the interpreter does conform to the range values for, say, type int, but allocates storage for the variables based on their value. So, for two variables foo and bar int foo = 0; /* interpreter allocates two bytes */ int bar =...
3
1573
by: Robin Becker | last post by:
As part of some django usage I need to get some ReportLab C extensions into a state where they can be safely used with mod_python. Unfortunately we have C code that seems incompatible with mod_python and I'm looking for ways to improve the situation. Basically the main things that are wrong are all privately cached copies of python variables. This sample code
40
2386
by: castironpi | last post by:
I'm curious about some of the details of the internals of the Python interpreter: I understand C from a hardware perspective. x= y+ 1; Move value from place y into a register Add 1 to the value in the register Move the addition's result to place x
5
21432
by: Erik Hahn | last post by:
I'm looking for a standalone Javascript interpreter like the ones of Perl and Python. Does such a thing exist for Linux? Thanks in advance. -Erik -- hackerkey://v4sw5hw2ln3pr5ck0ma2u7LwXm4l7Gi2e2t4b7Ken4/7a16s0r1p-5.62/-6.56g5OR
0
9543
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
10488
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10029
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
9077
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
6808
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
5467
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
5588
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4144
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3761
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.