• C

Basic to C !

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!
spy1234Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

proziathCommented:
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
proziathCommented:
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
spy1234Author Commented:
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
Defend Against the Q2 Top Security Threats

Were you aware that overall malware worldwide was down a surprising 42% from Q1'18? Every quarter, the WatchGuard Threat Lab releases an Internet Security Report that analyzes the top threat trends impacting companies worldwide. Learn more by viewing our on-demand webinar today!

proziathCommented:
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
cgebCommented:
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
AjarCommented:
#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
OmegerCommented:
Maybe you can take a look at wxBasic and see how they do it.
http://wxbasic.sourceforge.net/
0
Kent OlsenDBACommented:

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
spy1234Author Commented:
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
PashaModCommented:
Closed, 250 points refunded.
PashaMod
Community Support Moderator
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.