WWW.ИСХОДНИКИ.РУ cpp.sources.ru
java.sources.ru web.sources.ru soft.sources.ru
jdbc.sources.ru asp.sources.ru api.sources.ru

  Форум на исходниках
  C / C++ / Visual C++
  HELP КУРСОВИК для ДРУГА

СПРОСИТЬ  ОТВЕТИТЬ
профайл | регистрация | faq

Автор Тема:   HELP КУРСОВИК для ДРУГА
kingos опубликован 21-12-2001 11:00 MSK   Click Here to See the Profile for kingos   Click Here to Email kingos  
необходимо написать транслятор (простейший)
исходник есть но при компилке в visuale 6 c++
ругается?
---------
#include <iostream>
#include <fstream>
#include <cctype>
#include <cstdlib>
#include <cstring>
using namespace std;

const int NUM_LAB = 100;
const int LAB_LEN = 10;
const int FOR_NEST = 25;
const int SUB_NEST = 25;
const int PROG_SIZE = 10000;

// The Small Basic token types.
enum typesT { UNDEFTOK, OPERATOR, NUMBER, VARIABLE, COMMAND,
STRING, QUOTE };

// The Small Basic command tokens.
enum SBtokensT { UNKNCOM, PRINT, INPUT, IF, THEN, FOR, NEXT, TO,
GOTO, GOSUB, RETURN, EOL, FINISHED, END };

/* These are the constants used to call serror() when
a syntax error occurs. Add more if you like.
NOTE: SERROR is a generic error message used when
nothing else seems appropriate. */
enum errorsT
{ SERROR, PARENS, NOEXP, DIV_ZERO, EQUAL_EXP,
NOT_VAR, LAB_TAB_FULL, DUP_LAB, UNDEF_LAB,
THEN_EXP, TO_EXP, TOO_MNY_FOR, NEXT_WO_FOR,
TOO_MNY_GOSUB, RET_WO_GOSUB, MISS_QUOTE };

char *prog; // points into the program
char *p_buf; // points to start of program

int variables[26]= { // 26 user variables, A-Z
0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0
};

// keyword lookup table
struct commands {
char command[20]; // string form
SBtokensT tok; // internal representation
} table[] = { // commands must be entered lowercase
"print", PRINT, // in this table.
"input", INPUT,
"if", IF,
"then", THEN,
"goto", GOTO,
"for", FOR,
"next", NEXT,
"to", TO,
"gosub", GOSUB,
"return", RETURN,
"end", END,
"", END // mark end of table
};

char token[80];
typesT token_type;
SBtokensT tok;

// label lookup table
struct label {
char name[LAB_LEN]; // label
char *p; // points to label's location in source file
} label_table[NUM_LAB];

// support for FOR loops
struct for_stack {
int var; // counter variable
int target; // target value
char *loc; // place in source code to loop to
} fstack[FOR_NEST]; // stack for FOR/NEXT loop

char *gstack[SUB_NEST]; // stack for gosub

int ftos; // index to top of FOR stack
int gtos; // index to top of GOSUB stack

void print();
void scan_labels();
void find_eol();
void exec_goto();
void exec_if();
void exec_for();
void next();
void fpush(struct for_stack i);
void input();
void gosub();
void greturn();
void gpush(char *s);
void label_init();
void assignment();
char *find_label(char *s);
char *gpop();
struct for_stack fpop();
bool load_program(char *p, char *fname);
int get_next_label(char *s);

// prototypes for functions in the parser file
void eval_exp(int &result);
typesT get_token();
void serror(errorsT error), putback();

int main(int argc, char *argv[])
{
if(argc!=2) {
cout << "Usage: sbasic <filename>\n";
return 1;
}

// allocate memory for the program
try {
prog = new char [PROG_SIZE];
} catch(bad_alloc xa) {
cout << "Allocation Failure\n";
return 1;
}

p_buf = prog;

// load the program to execute
if(!load_program(prog, argv[1])) return 1;

// begin main interpreter try block
try {
scan_labels(); // find the labels in the program
ftos = 0; // initialize the FOR stack index
gtos = 0; // initialize the GOSUB stack index
do {
token_type = get_token();
// check for assignment statement
if(token_type==VARIABLE) {
putback(); // return the var to the input stream
assignment(); // must be assignment statement
}
else // is command
switch(tok) {
case PRINT:
print();
break;
case GOTO:
exec_goto();
break;
case IF:
exec_if();
break;
case FOR:
exec_for();
break;
case NEXT:
next();
break;
case INPUT:
input();
break;
case GOSUB:
gosub();
break;
case RETURN:
greturn();
break;
case END:
return 0;
}
} while (tok != FINISHED);
} // end of try block

/* catch throws here. As implemented, only
serror() throws an exception. However,
when creating your own languages, you can
throw a variety of different exceptions.
*/
catch(int) {
return 1; // fatal error
}

return 0;
}

// Load a program.
bool load_program(char *p, char *fname)
{
ifstream in(fname, ios::in | ios::binary);
int i=0;

if(!in) {
cout << "File not found ";
cout << "-- be sure to specify .BAS extension.\n";
return false;
}

i = 0;
do {
*p = in.get();
p++; i++;
} while(!in.eof() && i<PROG_SIZE);

// null terminate the program
if(*(p-2)==0x1a) *(p-2) = '\0'; // discard eof marker
else *(p-1) = '\0';

in.close();
return true;
}

// Find all labels.
void scan_labels()
{
int addr;
char *temp;

label_init(); // zero all labels
temp = prog; // save pointer to top of program

// if the first token in the file is a label
get_token();
if(token_type==NUMBER) {
strcpy(label_table[0].name, token);
label_table[0].p = prog;
}

find_eol();
do {
get_token();
if(token_type==NUMBER) {
addr = get_next_label(token);
if(addr == -1

Flex Ferrum опубликован 21-12-2001 11:07 MSK     Click Here to See the Profile for Flex Ferrum  Click Here to Email Flex Ferrum     
А как ругается?

СПРОСИТЬ  ОТВЕТИТЬ
Перейти:


E-mail | WWW.ИСХОДНИКИ.RU

Powered by: Ultimate Bulletin Board, Freeware Version 5.10a
Purchase our Licensed Version- which adds many more features!
© Infopop Corporation (formerly Madrona Park, Inc.), 1998 - 2000.