要做编译技术的实习了,想到csdn找几片相关的文章看看
没找到呀,这方面的也许很少
所以把我自己写的词法分析的源代码贴出来了
希望大家批评指正
#include "stdlib.h"
#include "stdio.h"
#include "iostream.h"
#include "fstream.h"
#include "string.h"
#include "ctype.h"
#include "malloc.h"
#define $ID 0
#define $ID_INT 1
#define $ID_CHAR 2
#define $ID_FLOAT 3
#define $ID_VOID 4
#define $ID_CONST 5
#define $ID_STATIC 6
#define $ID_IF 7
#define $ID_ELSE 8
#define $ID_DO 9
#define $ID_WHILE 10
#define $ID_SWITCH 11
#define $ID_CASE 12
#define $ID_DEFAULT 13
#define $ID_BREAK 14
#define $ID_CONTINUE 15
#define $ID_RETURN 16
#define $ID_DEFINE 17
#define $ID_INCLUDE 18
#define $INT 19
#define $ASSIGN 20
#define $ADD 21
#define $SUB 22
#define $MUL 23
#define $DIV 24
#define $LPAR 25
#define $RPAR 26
#define $LPAR_DIM 27
#define $RPAR_DIM 28
#define $LPAR_SEN 29
#define $RPAR_SEN 30
#define $EQUAL 31
#define $NOT_EQUAL 32
#define $LT 33
#define $GT 34
#define $LT_EQUAL 35
#define $GT_EQUAL 36
#define $COMMA 37
#define $DOT 38
#define $INTDIV 39
#define $COMMENT 40
#define $COMMENT_ASSIGN 41
#define $SENEND 42
#define $QUESTION 43
#define $NOT 44
struct Token
{
int ncode;
char *ntoken;
};
//////////////数组维数表及其处理
struct Dim
{
int uprange1;
int uprange2;
}Dim_table[20];
int Dimtable_index;
void Dimtable_Initiate()
{
for(int i=0;i<20;i++)
{
Dim_table[i].uprange1=0;
Dim_table[i].uprange2=0;
}
}
void Dimtable_Print()
{
cout<<"The CONTENTS of the Dim_table:\n";
for(int i=0;i<20;i++)
{
cout<
}
}
///////////关键字表及其处理
struct Reserve
{
int ncode;
char ntoken[20];
}word[20]={{0," "},{1,"int"},{2,"char"},{3,"float"},{4,"void"},{5,"const"},{6,"static"},
{7,"if"},{8,"else"},{9,"do"},{10,"while"},{11,"switch"},{12,"case"},{13,"default"},
{14,"break"},{15,"continue"},{16,"return"},{17,"define"},{18,"include"},{19," "}};
int Reserve_Search(char *search)
{
for(int i=0;i<20;i++)
{
//cout<<"i="<
if(strcmp(search,word[i].ntoken)==0)return i;
}
return 0;
}
void Reserve_Print()
{
for(int i=0;i<20;i++)
{
cout<
int index1=Idtable_Search(token);
if(index1==-1)
{
strcpy(Id_table[Id_table_index].name,token);
Id_table[Id_table_index].kind=1;
Id_table[Id_table_index].type=1;
Id_table[Id_table_index++].idform=1;
}
else
{
//重复定义error
}
}
void Idtable_Add_Uprange()
{
Id_table[Id_table_index-1].arrp=Dimtable_index;
}
void Idtable_Initiate()
{
for(int i=0;i<100;i++)
{
strcpy(Id_table[i].name,"");
Id_table[i].kind=0;
Id_table[i].type=0;
Id_table[i].idform=0;
Id_table[i].da=0;
Id_table[i].offset=0;
Id_table[i].arrp=-1;
}
}
void Idtable_Print()
{
cout<<"The CONTENTS of the Id_table:\n";
cout<<"i:="<<"i"<<"\t"<<"name"<<"\t"<<"kind"<<"\t"<<"type"
<<"\t"<<"idform"<<"\t"<<"da"<<"\t"<<"offset"<<"\t"
<<"arrp"< for(int i=0;i<100;i++)
{
cout<<"i:="<
<<"\t"<
<
}
}
static fstream infile;
char buffer[80];
int bufferlength=0;
int pbuffer=0;
char token[20];
char ch;
int line_number=0;
int error_number=0;
int isLetter(char ch)
{
if(((ch>='a')&&(ch<='z'))||((ch>='A')&&(ch<='Z')))
return 1;
else
return 0;
}
int isDigit(char ch)
{
if(ch>='0'&&ch<='9')
return 1;
else
return 0;
}
int String_To_Int(char * source)
{
int i,result;
result=0;
int length=strlen(source);
for(i=0;i
{
result=result*10+((*(source+i))-'0');
}
return result;
}
//readline()从文件中读取一行到buffer中
int readline()
{
if(!infile.eof())
{
infile.getline(buffer,sizeof(buffer));
//cout<
bufferlength=strlen(buffer);
}
return bufferlength;
}
//getch()从buffer中读取一个字符到ch中
void getch()
{
if(bufferlength==0)readline();
//cout<
ch=buffer[pbuffer++];
//cout<
if(pbuffer>bufferlength)
{
readline();
pbuffer=0;
}
}
void getnbc()
{
while(ch==' ')
{
getch();
getnbc();
}
}
void concat()
{
//cout< int i=strlen(token);
//cout<
token[i++]=ch;
token[i]='\0';
}
void retract()
{
pbuffer--;
}
void error(char * string)
{
cout<<"Line("<
}
struct Token nexttoken()
{
struct Token temp;
strcpy(token,"");
getch();
getnbc();
if(isLetter(ch))
{
while(isLetter(ch)||isDigit(ch))
{
concat();
getch();
}
retract();
int result=Reserve_Search((char *)token);
//cout<<"result="<
if(result!=0)
{
temp.ncode=result;
temp.ntoken=token;
return temp;
}
else{temp.ncode=$ID;
temp.ntoken=token;
return temp;}
}
else if(isDigit(ch))
{
while(isDigit(ch))
{
concat();
getch();
}
retract();
temp.ncode=$INT;
temp.ntoken=token;
return temp;
}
else
{
switch(ch)
{
//20~30+
case '=':
getch();
if(ch!='='){temp.ncode=$ASSIGN;retract();}
else{temp.ncode=$EQUAL;}
temp.ntoken=token;
return temp;
case '+':
temp.ncode=$ADD;temp.ntoken=token;
return temp;
case '-':
temp.ncode=$SUB;temp.ntoken=token;
return temp;
case '*':
temp.ncode=$MUL;temp.ntoken=token;
return temp;
case '/':
temp.ncode=$DIV;temp.ntoken=token;
return temp;
case '(':
temp.ncode=$LPAR;temp.ntoken=token;
return temp;
case ')':
temp.ncode=$RPAR;temp.ntoken=token;
return temp;
case '[':
temp.ncode=$LPAR_DIM;temp.ntoken=token;
return temp;
case ']':
temp.ncode=$RPAR_DIM;temp.ntoken=token;
return temp;
case '{':
temp.ncode=$LPAR_SEN;temp.ntoken=token;
return temp;
case '}':
temp.ncode=$RPAR_SEN;temp.ntoken=token;
return temp;
case '!':
getch();
if(ch!='='){temp.ncode=$NOT;retract();}
else{temp.ncode=$NOT_EQUAL;}
temp.ntoken=token;
return temp;
case '<':
getch();
if(ch!='='){temp.ncode=$LT;retract();}
else{temp.ncode=$LT_EQUAL;}
temp.ntoken=token;
return temp;
case '>':
getch();
if(ch!='='){temp.ncode=$GT;retract();}
else{temp.ncode=$GT_EQUAL;}
temp.ntoken=token;
return temp;
case ',':
temp.ncode=$COMMA;temp.ntoken=token;
return temp;
case '.':
temp.ncode=$DOT;temp.ntoken=token;
return temp;
case '%':
temp.ncode=$INTDIV;temp.ntoken=token;
return temp;
case ';':
temp.ncode=$SENEND;temp.ntoken=token;
return temp;
case '?':
temp.ncode=$QUESTION;temp.ntoken=token;
return temp;
case ':':
getch();
if(ch!='='){temp.ncode=$COMMENT;retract();}
else{temp.ncode=$COMMENT_ASSIGN;}
temp.ntoken=token;
return temp;
default:
line_number++;
cout<<"get line :"<
getnbc();
temp.ncode=100;temp.ntoken=token;
return temp;
}
}
}
void main()
{
Idtable_Initiate();
Dimtable_Initiate();
struct Token TokenReturn;
infile.open("D:\\VC\\CompileProject\\data.txt",ios::in);
if(!infile)
{
cout<<"Conn't open the SOURCE file ,please CHECK it!\n";
abort();
}
while(!infile.eof())
{ //变量说明语句处理
TokenReturn=nexttoken();
cout<
cout<
if(TokenReturn.ncode==$ID_INT)//整型
{//2
do
{//3
TokenReturn=nexttoken();
if(TokenReturn.ncode==$ID)
{//4
Idtable_Insert_Int(TokenReturn.ntoken);
TokenReturn=nexttoken();//是不是数组说明
if(TokenReturn.ncode==$LPAR_DIM)
{//5
TokenReturn=nexttoken();
if(TokenReturn.ncode==$INT)
{//6
//在Dim_table中插入一项
Dim_table[Dimtable_index].uprange1=String_To_Int(TokenReturn.ntoken);
Idtable_Add_Uprange();
TokenReturn=nexttoken();
if(TokenReturn.ncode==$RPAR_DIM)
{//7
//是不是二维数组说明
TokenReturn=nexttoken();
if(TokenReturn.ncode==$LPAR_DIM)
{//8
TokenReturn=nexttoken();
if(TokenReturn.ncode==$INT)
{//9
//在Dim_table中插入一项
Dim_table[Dimtable_index].uprange2=String_To_Int(TokenReturn.ntoken);
Idtable_Add_Uprange();
Dimtable_index++;
TokenReturn=nexttoken();
if(TokenReturn.ncode==$RPAR_DIM)
{//10
TokenReturn=nexttoken();
}
else
{
error("缺少二维右括号");break;
}
}
else
{
error(" 缺少二维维数");break;
}
}//8
else
{//只有一维
Dimtable_index++;
}
}
else
{
error(" 缺少一维右括号");
}
}//6
else
{
line_number++;
error(" 缺少一维维数");
break;
}
}//
else if(TokenReturn.ncode==$LPAR)//函数说明
{
}
//变量名合法
}
else
{
error("变量名不合法");
}//4
}
while(TokenReturn.ncode==$COMMA);
if(TokenReturn.ncode!=$SENEND)
{
error(" 缺少';'");
}//3
}//2
else if(TokenReturn.ncode==$ID_FLOAT)
{
//FLOAT CHULI
}
else if(TokenReturn.ncode==$ID_VOID)
{
//void CHULI
}
else if(TokenReturn.ncode==$ID)//表达式处理
{
cout<<"表达式的处理:\n";
}
}
Idtable_Print();
Dimtable_Print();
}
lastking联系方式:leefelicity@hotmail.com