资源简介

非常基础的词法分析器,能够将各个单词分开,判断各个单词的类型,并输出相应的tokens和符号表。还存在一些BUG,希望大家不吝赐教。

资源截图

代码片段和文件信息

#include“fileIO.h“
#include“table.h“
using namespace std;
int typeCodetokenNumlength;         //分别表示种别码,当前单词中的字符位置的下一位以及代码长度
int presentChar=0row=1;       //分别表示当前读取到的文件的字符的下一位,读取代码当前行数
char token[15];                //表示当前读取的单词
char ch;                       //表示当前读取的字符
char* keywords[13]={“int““float““double““char““string““while““if““else““for““return““break““switch““case“};
char str[50];                  //专门用于保存字符串或者字符
tokenTable table[200];
void analyzer(string code)
{
for(int i=0;i<15;i++)
token[i]=NULL;
ch=code[presentChar++];
while(ch==‘ ‘||ch==‘\t‘)
{       //过滤空格符和制表符
if(presentChar==length)
{    
typeCode=0;             //typeCode=0来表示代码已到末尾
return;                   //跳出函数
}
ch=code[presentChar++];
}
tokenNum=0;
if(ch>=‘a‘&&ch<=‘z‘){            //判断是否是关键字,否则为标识符
while((ch>=‘a‘&&ch<=‘z‘)||(ch>=‘0‘&&ch<=‘9‘)||(ch>=‘A‘&&ch<=‘Z‘)||ch==‘_‘)
{
token[tokenNum++]=ch;
ch=code[presentChar++];
}
token[tokenNum]=‘\0‘;
presentChar--;                        //计算完一个token后,presentChar会指向空格符的下一个符号,所以必须减一
typeCode=IDENTIFIER;
for(int i=0;i<13;i++){
if(!strcmp(tokenkeywords[i])){
typeCode=i+1;
break;
}
}
}
else if((ch>=‘A‘&&ch<=‘Z‘)||ch==‘_‘||ch<0)
{          //上一个if已经包含了a到z开头的标识符,进入这个if的也都是标识符
while((ch>=‘a‘&&ch<=‘z‘)||(ch>=‘A‘&&ch<=‘Z‘)||(ch>=‘0‘&&ch<=‘9‘)||ch==‘_‘||ch<0)
{
if(ch<0)
{
token[tokenNum++]=ch;
token[tokenNum++]=code[presentChar++];
}
else
token[tokenNum++]=ch;
ch=code[presentChar++];
}
token[tokenNum++]=‘\0‘;
presentChar--;                            //计算完一个token后,presentChar会指向空格符的下一个符号,所以必须减一
typeCode=IDENTIFIER;
}
else if(ch>=‘0‘&&ch<=‘9‘)
{
while((ch>=‘0‘&&ch<=‘9‘)||ch==‘.‘)
{
token[tokenNum++]=ch;
ch=code[presentChar++];
}
token[tokenNum++]=‘\0‘;
presentChar--;                            //计算完一个token后,presentChar会指向空格符的下一个符号,所以必须减一
typeCode=NUMBER;
}
else switch(ch)
{
case ‘+‘:
typeCode=ADD;token[tokenNum++]=ch;
ch=code[presentChar];
if(ch==‘+‘)
{
token[tokenNum]=ch;
typeCode=DADD;
presentChar++;
}
break;
case ‘-‘:
typeCode=SUB;token[tokenNum++]=ch;
ch=code[presentChar];
if(ch==‘-‘)
{
token[tokenNum]=ch;
typeCode=DSUB;
presentChar++;
}
break;
case ‘*‘:typeCode=MUL;token[0]=ch;break;
case ‘/‘:typeCode=DIV;token[0]=ch;break;
case ‘%‘:typeCode=MOD;token[0]=ch;break;
    case ‘=‘:
typeCode=ASSIGN;token[tokenNum++]=ch;
ch=code[presentChar];
if(ch==‘=‘)
{
token[tokenNum]=ch;
typeCode=EQUAL;
presentChar++;
}
break;
case ‘!‘:
typeCode=EXC;token[tokenNum++]=ch;
ch=code[presentChar];
if(ch==‘=‘)
{
token[tokenNum]=ch;
typeCode=NEQ;
presentChar++;
}
break;
case ‘>‘:
typeCode=MORETHAN;token[tokenNum++]=ch;
ch=code[presentChar];
if(ch==‘=‘)
{
token[tokenNum]=ch;
typeCode=MOE;
presentC

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件        602  2013-04-12 17:15  lee\Debug\cl.command.1.tlog

     文件      23052  2013-04-12 17:15  lee\Debug\CL.read.1.tlog

     文件        264  2013-04-12 17:15  lee\Debug\CL.write.1.tlog

     文件        406  2013-03-02 13:04  lee\Debug\lee.exe.embed.manifest

     文件        472  2013-03-02 13:04  lee\Debug\lee.exe.embed.manifest.res

     文件        381  2013-04-12 17:15  lee\Debug\lee.exe.intermediate.manifest

     文件         52  2013-04-12 17:15  lee\Debug\lee.lastbuildstate

     文件       1998  2013-04-12 17:15  lee\Debug\lee.log

     文件        196  2013-03-02 12:54  lee\Debug\lee_manifest.rc

     文件          2  2013-04-12 17:15  lee\Debug\link-cvtres.read.1.tlog

     文件          2  2013-04-12 17:15  lee\Debug\link-cvtres.write.1.tlog

     文件          2  2013-04-12 17:15  lee\Debug\link.10020-cvtres.read.1.tlog

     文件          2  2013-04-12 17:15  lee\Debug\link.10020-cvtres.write.1.tlog

     文件          2  2013-04-12 17:15  lee\Debug\link.10020.read.1.tlog

     文件          2  2013-04-12 17:15  lee\Debug\link.10020.write.1.tlog

     文件          2  2013-04-12 17:15  lee\Debug\link.10252-cvtres.read.1.tlog

     文件          2  2013-04-12 17:15  lee\Debug\link.10252-cvtres.write.1.tlog

     文件          2  2013-04-12 17:15  lee\Debug\link.10252.read.1.tlog

     文件          2  2013-04-12 17:15  lee\Debug\link.10252.write.1.tlog

     文件          2  2013-04-12 17:15  lee\Debug\link.11924-cvtres.read.1.tlog

     文件          2  2013-04-12 17:15  lee\Debug\link.11924-cvtres.write.1.tlog

     文件          2  2013-04-12 17:15  lee\Debug\link.11924.read.1.tlog

     文件          2  2013-04-12 17:15  lee\Debug\link.11924.write.1.tlog

     文件          2  2013-04-12 17:15  lee\Debug\link.13988-cvtres.read.1.tlog

     文件          2  2013-04-12 17:15  lee\Debug\link.13988-cvtres.write.1.tlog

     文件          2  2013-04-12 17:15  lee\Debug\link.13988.read.1.tlog

     文件          2  2013-04-12 17:15  lee\Debug\link.13988.write.1.tlog

     文件          2  2013-04-12 17:15  lee\Debug\link.14432-cvtres.read.1.tlog

     文件          2  2013-04-12 17:15  lee\Debug\link.14432-cvtres.write.1.tlog

     文件          2  2013-04-12 17:15  lee\Debug\link.14432.read.1.tlog

............此处省略218个文件信息

评论

共有 条评论