?
Solved

Basic to C !

Posted on 2003-11-02
11
Medium Priority
?
267 Views
Last Modified: 2011-09-20
Hello!

How can I make it in Ansi C:

I have a bas file
x=2
y=3
here:
d=x+y
print d
end
if x>y goto here
.
.

Run like this:  ./basic <example.bas and it print 5!!

Thx!
0
Comment
Question by:spy1234
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
11 Comments
 
LVL 2

Expert Comment

by:proziath
ID: 9666416
Surely it is something like this:



#include<stdio.h>
void main()
{int x=2,y=3,d;
  d=x+y;
  do{

     printf("%i",d);

     }while(x>y);

 }

0
 
LVL 2

Expert Comment

by:proziath
ID: 9666435
I think I misplaced the d=x+y in the last attempt


#include<stdio.h>
void main()
{int x=2,y=3,d;

 do{
     d=x+y;
    printf("%i",d);

    }while(x>y);

}
0
 

Author Comment

by:spy1234
ID: 9666727
I think you don't understand me well!

I have an example. bas file with this lines
x=2
y=3

here:
d=x+y
print d
end

there:
d=x-y
print d
end

if x>y goto here
if y>x goto there
exit
------------------------
How can I write a C file (that is the problem) to run it!! Like a Basic lexer in C!
If  I run it like this   ./a.out <example.bas
I get this message:
-1

Thx
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 2

Expert Comment

by:proziath
ID: 9668152
spy1234 sorry for misunderstanding your original request. If I do understand correctly now, what you will have to do is to build the correct grammar for the the subset of BASIC that you want to parse and interpret and then use that grammar to write the C language for it. I have found a likely source of the the grammar here:

http://caml.inria.fr/oreilly-book/html/book-ora058.html

the grammar being as follows:

Unary_Op ::= -    |    !
Binary_Op ::= +    |    -    |    *    |    /    |    %
  | =    |    <    |    >    |    <=    |    >=    |    <>
  | &    |    ' | '
Expression ::= integer
  | variable
  | "string"
  | Unary_Op   Expression
  | Expression   Binary_Op   Expression
  | ( Expression )
Command ::= REM string
  | GOTO integer
  | LET variable = Expression
  | PRINT Expression
  | INPUT variable
  | IF Expression THEN integer
 
Line ::= integer Command
 
Program ::= Line
  | Line Program
 
Phrase ::= Line | RUN | LIST | END


converting this to the C program takes a bit of doing because it takes compiler theory. The reason why it would take such effort would be that your resulting program needs to be able to cater for any basic program you throw at it. I can't see a way of simplifying this process.

However , how about giving you a C program that interprets BASIC code and runs it. You will find it here:

http://www.programmersheaven.com/zone6/cat700/16060.htm

I hope this points you in the right direction. Anyone out there with better idea?


0
 

Expert Comment

by:cgeb
ID: 9668252
Just a quick note....are you getting an error message or are you getting the output -1 because the way you have that program set up you should get -1.

x=2
y=3

here:
d=x+y
print d
end

there:
d=x-y
print d
end

if x>y goto here
if y>x goto there
exit

2 < 3 so it should goto there and there prints 2 - 3 = -1.

not trying to be cute but maybe it is working the way you want it to
0
 
LVL 6

Expert Comment

by:Ajar
ID: 9669004
#include <stdio.h>
int main(int argc,char * argv[])
{
int x=2,y=3,d;
here:
d=x+y; printf("%d \n",d);
if (x>y) goto here;
}
0
 

Expert Comment

by:Omeger
ID: 9671747
Maybe you can take a look at wxBasic and see how they do it.
http://wxbasic.sourceforge.net/
0
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 9679424

If you already have a basic compiler you can have the C program invoke the compiler.

#include <stdlib.h>

main (int argc, char ** argv)
{
  char StringBuffer[200];

  if (argc == 2)
  {
    strcpy (StringBuffer, "./basic <");
    strcat (StringBuffer, argv[1]);
    system  (StringBuffer);
  }
  else
    printf ("Usage:  %s filename\n", argv[0]);
}


Kent

./basic <example.bas
0
 

Author Comment

by:spy1234
ID: 9716271
I made it! Here is a sample for While!
Goto work's same only there we search a label

int findwhilebackward( int pcounter ){
int nestedlevels=1;
  while( nestedlevels!=0 || memory[pcounter].opcode!=I_WHILE){
    /* Visszafel&#9500;&#281;.
     */
    --pcounter;
   
    if( pcounter<0 ){
       fprintf( stderr, "Hiba: while nem tal&#9500;ílhat&#9500;&#9474;.\n" );
       exit( EXIT_FAILURE );
    }/*if*/
   
    if( memory[pcounter].opcode==I_WEND ) ++nestedlevels;
   
    if( memory[pcounter].opcode==I_WHILE) --nestedlevels;
  }/*while*/
  return( pcounter );
}/*findwhilebackward*/
0
 

Accepted Solution

by:
PashaMod earned 0 total points
ID: 10485499
Closed, 250 points refunded.
PashaMod
Community Support Moderator
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Suggested Courses

718 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question