Solved

Basic to C !

Posted on 2003-11-02
11
263 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
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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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 45

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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

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…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
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 and use conditional statements in the C programming language.

828 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