Solved

Basic to C !

Posted on 2003-11-02
11
266 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

623 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