472,968 Members | 1,533 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,968 software developers and data experts.

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_name
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_name>
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 5464
"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_name
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_name>
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_name
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_name>
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********@gmail.comwrote in message
news:11*********************@m73g2000cwd.googlegro ups.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_name
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_name>
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********@gmail.comwrote in message
news:11*********************@m73g2000cwd.googlegro ups.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_name
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_name>
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 "Constructing 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 hasMatchedParentheses (std:string const& in)
{
std::string::size_type const insize(in.size());
std::string::size_type nparens(0);

for (std::string::size_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********@gmail.comwrote in message
news:11*********************@m73g2000cwd.googlegr oups.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_name
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_name>
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 "Constructing
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 hasMatchedParentheses (std:string const& in)
{
std::string::size_type const insize(in.size());
std::string::size_type nparens(0);

for (std::string::size_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***********@gmail.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
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...
12
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...
1
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...
12
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...
4
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...
12
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...
3
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...
40
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...
5
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 --...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
3
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.