• 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?
 
PashaModConnect With a Mentor Commented:
Closed, 250 points refunded.
PashaMod
Community Support Moderator
0
 
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
The Lifecycle Approach to Managing Security Policy

Managing application connectivity and security policies can be achieved more effectively when following a framework that automates repeatable processes and ensures that the right activities are performed in the right order.

 
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
 
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 OlsenData Warehouse Architect / DBACommented:

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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.