C/C++ interpreter

G

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.
 
O

osmium

:

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.
 
J

Jim Langston

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.
 
J

James Bannon

Jim said:
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)
{
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.
 
J

James Bannon

James said:
Jim said:
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)
{
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!
 
T

tragomaskhalos

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,269
Latest member
vinaykumar_nevatia23

Latest Threads

Top