Добавил:
Studfiles2
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:
#define TEMPLATE
#define DEBUG
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <ctype.h>
#include <fstream.h>
#include <stdlib.h>
#include <strng.h>
#include <deques.h>
#include "lex.h"
struct Ident
{
String name;
static char *types[3];
enum {INT,FLOAT,UNKNOWN} type;
int initialized;
int idx;
Ident(const char *buf):name(buf)
{ type=UNKNOWN;
initialized=0;
idx=-1;
};
friend ostream& operator<<(ostream &o,Ident i);
};
char* Ident::types[3]={"INT","FLOAT","UNKNOWN"};
ostream& operator<<(ostream &o,Ident i)
{
o<<i.name<<'\t'<<i.types[i.type]<<'\t'<<i.initialized<<'\t'<<i.idx<<'\n';
return o;
}
int sost=0;
int type;
int fMove=1;
int fFail=0;
char ch;
char Buf[256];
int pBuf;
Ident* Idents[100];
int ConstI[100];
float ConstF[100];
int nIdents=-1,nConstI=-1,nConstF=-1;
BI_DequeAsDoubleList<Lex> Lexems;
int classify(char ch);
void InsertLex(int type,long idx)
{ Lex *p=new Lex(type,idx);
Lexems.putLeft(*p);
#ifdef DEBUG
output(*p);
#endif
};
void InsertLex(int type,void *p)
{ Lex *ptr=new Lex(type,p);
Lexems.putLeft(*ptr);
#ifdef DEBUG
output(*ptr);
#endif
};
int InsertId(const char *buf)
{Ident *p=new Ident(buf);
Idents[++nIdents]=p;
return nIdents;
};
int InsertConstI(const char *buf)
{
ConstI[++nConstI]=atoi(buf);
return nConstI;
}
int InsertConstF(const char *buf)
{
ConstF[++nConstF]=atof(buf);
return nConstF;
}
void Error()
{ String *s=new String(Errors[type][sost]);
InsertLex(ERROR,s);
fFail=1;
};
void BeginNbr() {sost=_Nbr_;fMove=1;pBuf=0;Buf[pBuf]=ch;}
void BeginId() {sost=_Id_;fMove=1;pBuf=0;Buf[pBuf]=ch;}
void BeginOp() {sost=_Op_;fMove=1;pBuf=0;Buf[pBuf]=ch;}
void ProcessId(){Buf[++pBuf]=ch;}
void ProcessNbr(){Buf[++pBuf]=ch;}
void ProcessNbrF(){sost=_NbrF_;Buf[++pBuf]=ch;}
void ProcessOp()
{ if(ch=='=') Buf[++pBuf]=ch;
else { InsertLex(ERROR,new String("ЌҐЁ§ўҐбв п ®ЇҐа жЁп!"));
fFail=1;
}
}
void ProcessKey(){sost=_Key_;Buf[++pBuf]=ch;}
void EndId()
{int flag=1;
int ptr;
sost=_I_;fMove=0;
Buf[++pBuf]=0;
for(int i=0;flag&&(i<nKeyWord);i++)
if(!strcmp(Buf,KeyWord[i])) flag=0;
if(!flag)
InsertLex(KEY,i-1);
else
{
ptr=InsertId(Buf);
InsertLex(ID,ptr);
}
}
void EndKey()
{ int flag=1;
int i;
sost=_I_;fMove=1;
if(ch==')') Buf[++pBuf]=ch;
else fMove=0;
Buf[++pBuf]=0;
for(i=0;flag&&(i<nKeyWord);i++)
if(!strcmp(Buf,KeyWord[i])) flag=0;
if(flag){
InsertLex(ERROR,new String("ЌҐЄ®а४вл© Ё¤ҐвЁдЁЄ в®а!"));
fFail=1;
}
else
InsertLex(KEY,i-1);
}
void EndNbr()
{ int ptr;
sost=_I_;fMove=0;
Buf[++pBuf]=0;
ptr=InsertConstI(Buf);
InsertLex(NBRI,ptr);
}
void EndNbrF()
{ int ptr;
sost=_I_;fMove=0;
Buf[++pBuf]=0;
ptr=InsertConstF(Buf);
InsertLex(NBRF,ptr);
}
void EndRazd()
{
sost=_I_;fMove=1;
if(ch!=' ')
{
int type=classify(ch);
InsertLex(RAZD,type);
}
}
void EndOp()
{ int flag=1;
int i;
fMove=0;
sost=_I_;
Buf[++pBuf]=0;
for(i=0;flag&&(i<nOp);i++)
if(!strcmp(Buf,Op[i])) flag=0;
if(flag){
InsertLex(ERROR,new String("ҐЁ§ўҐбв п ®ЇҐа жЁп!"));
fFail=1;
}
else
{
InsertLex(OPER,i-1);
}
}
int classify(char ch)
{ register int tmp;
if(ch==' ') return SPACE;
if(tmp=isalpha(ch)) return CHAR;
if(tmp=isdigit(ch)) return DIGIT;
if(ch=='.') return DOT;
switch(ch)
{
case '>':case'<':case'!':case'=':case'+':case'-':case'/':case'*':
return OP;
case'(': return LEFT;
case')': return RIGHT;
case'{': return LEFTF;
case'}': return RIGHTF;
case';': return END;
case',': return COMA;
default:
InsertLex(ERROR,new String("ЌҐЁ§ўҐбвл© бЁ¬ў®«!"));
fFail=1;
return -1;
}
}
void output(Lex tmp)
{
switch(tmp.type)
{ case ID: cerr<<LexName[tmp.type]<<Idents[tmp.ptr.idx]->name<<'\n';break;
case KEY: cerr<<LexName[tmp.type]<<KeyWord[tmp.ptr.idx]<<'\n';break;
case NBRI: cerr<<LexName[tmp.type]<<ConstI[tmp.ptr.idx] <<'\n';break;
case NBRF: cerr<<LexName[tmp.type]<<ConstF[tmp.ptr.idx] <<'\n';break;
case RAZD: cerr<<LexName[tmp.type]<<RazdName[tmp.ptr.idx]<<'\n';break;
case OPER: cerr<<LexName[tmp.type]<<Op[tmp.ptr.idx]<<'\n';break;
case ERROR: cerr<<LexName[tmp.type]<<*((String*)tmp.ptr.ptr)<<'\n';break;
}
}
void main()
{
ifstream f("input.tst");
char ch1;
clrscr();
cerr<<"\n ‹…Љ‘€—…‘Љ€‰ ЂЌЂ‹€‡Ђ’Ћђ version 0.1 beta\n";
if(!f)
{ cerr<<"ЋиЁЎЄ ®вЄалвЁп д ©« ¤ ле!";
exit(-1);
}
do
{
if(fMove)
{
ch=f.get();
if(ch==' '||ch=='\n'||ch=='\t')
{ do
ch1=f.get();
while(ch1==' '||ch1=='\n'||ch1=='\t');
if(ch==' ') f.putback(ch1);
else ch=ch1;
}
}
if((ch!=EOF)&&!fFail)
{
type=classify(ch);
if(!fFail) Table[type][sost]();
}
}while((ch!=EOF)&&!fFail);
Lex tmp;
ofstream f2("output.dat");
f2.setf(ios::showpoint);
while(!Lexems.isEmpty())
{
tmp=Lexems.getRight();
switch(tmp.type)
{
case ID: f2<<LexName[tmp.type]<<Idents[tmp.ptr.idx]->name<<'\n';break;
case KEY: f2<<LexName[tmp.type]<<KeyWord[tmp.ptr.idx]<<'\n';break;
case NBRI: f2<<LexName[tmp.type]<<ConstI[tmp.ptr.idx] <<'\n';break;
case NBRF: f2<<LexName[tmp.type]<<ConstF[tmp.ptr.idx] <<'\n';break;
case RAZD: f2<<LexName[tmp.type]<<RazdName[tmp.ptr.idx]<<'\n';break;
case OPER: f2<<LexName[tmp.type]<<Op[tmp.ptr.idx]<<'\n';break;
case ERROR: f2<<LexName[tmp.type]<<*((String*)tmp.ptr.ptr)<<'\n';break;
}
}
f2<<"\n’ЂЃ‹€–Ђ €„…Ќ’€”€ЉЂ’ЋђЋ‚\n";
f2<<"\t€Њџ \t ’€Џ \t €Ќ€–€Ђ‹€‡€ђЋ‚ЂЌ \t ‘‘›‹ЉЂ ЌЂ ’Џ\n";
for(int i=0;i<=nIdents;f2<<'#'<<i<<' '<<*(Idents[i++]));
f2<<"\n’ЂЃ‹€–Ђ –…‹›• ЉЋЌ‘’ЂЌ’\n";
for(i=0;i<=nConstI;f2<<'#'<<i<<' '<<ConstI[i++]<<'\n');
f2<<"\n’ЂЃ‹€–Ђ „ђЋЃЌ›• ЉЋЌ‘’ЂЌ’\n";
for(i=0;i<=nConstF;f2<<'#'<<i<<' '<<ConstF[i++]<<'\n');
}