📄 lex.cpp
字号:
//该模块完成词法分析
#include <windows.h>
#include "DFA.h"
#include <iostream.h>
#include "StatusFunc.h"
#include "consts.h"
//定义状态转换表
CONVERT_STATUS cs255[]={{IsBlank,255},{IsAlpha,1},{IsDigtal,3},{IsEqu,5},{IsAdd,6},{IsMul,7},{IsSub,8},{IsDiv,9},{IsLeftBracket,10},
{IsRightBracket,11},{IsLess,12},{IsMore,13},{IsNot,14},{IsOr,15},{IsAnd,16},{0,0}};
CONVERT_STATUS cs1[]={{IsAlphaOrDigtal,1},{NotAlphaAndDigtal,2},{0,0}};
CONVERT_STATUS cs3[]={{IsDigtal,3},{NotDigtal,4},{0,0}};
STATUS_ENTRY se[]={{255,cs255},{1,cs1},{3,cs3}};
STATUS_CONVERT_TABLE sct={3,se};
//所有的操作符,第一个为空字符,保证后面的字符从1开始索引
char Op[]={0,'=','+','-','*','/','(',')','<','>','!','|','&'};
//保存词法分析识别的数字,本程序要求输入字符串中数字的个数不超过100个。
long Num[MAX_ITEM]={0};
//保存词法分析识别的表示符,本程序要求输入字符串中不同标识符的个数不超过100个,
//且标识符的长度不超过15个字符,否则只取前15个字符。
char Identifier[MAX_ITEM][16]={0};
//保存词法分析器的结果
//每识别出一个符号用一个编号表示,运算符从1开始编号,标识符从1001开始编号,数字从2001开始编号。
//关键字从3000开始编号
int LexResult[100]={0};
int nResultIndex=0; //指向结果串中下一个将要被填入的位置的指示器。
long lNumIDMax=2000; //到目前为止的最大编号
long lIdentifierIDMax=1000;
char * str="while 0+9-0 do a=3+9*8 ";
extern process_S();
//在标识符表中查找给定的标识符,找到则返回标识符在表中的索引,没找到则返回0
int lookupIdentifier(char * si)
{
int i=0;
while(i<MAX_ITEM && Identifier[i]!=0)
{
if(strcmp(si,Identifier[i])==0)
{
return i;
}
i++;
}
return 0;
}
//将标识符插入到标识符表中并返回其索引,若标识符已经存在,则返回存在的索引。
//若标识符的长度大于15,则只取前15个字符。
int addIdentifier(char *chStart, char *chEnd)
{
int n;
char t[16]={0};
int cb=chEnd-chStart;
if(cb>15) cb=15;
memcpy(t,chStart,cb);
if(!strcmpi("while",t))
return 2001;
if(!strcmpi("do",t))
return 2002;
n=lookupIdentifier(t);
if(n)
return n;
lIdentifierIDMax++;
n=lIdentifierIDMax-IDENTIFIER_ID_BASE;//得到下一个空的索引
strcpy(Identifier[n],t);
return n;
}
//将指针pch移到第一个非空格字符
void MoveToAvailable(char * &pch)
{
while(*pch!=0 && *pch==' ')
pch++;
}
main (int argc, char * argv[])
{
CDFA dfa;
int n;
char *chStart,*chEnd;
dfa.Initial(&sct,0);
chStart=str;
MoveToAvailable(chStart);
dfa.PrepareDFA(chStart,255);
STATUS s;
while(1)
{
if(*dfa.GetNextCasePoint()==0) break;
s=dfa.StepForward();
if(!s) break;
switch(s)
{
case 2:
chEnd=dfa.GetNextCasePoint()-1;
n=addIdentifier(chStart,chEnd);
chStart=chEnd;
MoveToAvailable(chStart);
dfa.PrepareDFA(chStart,255);
LexResult[nResultIndex]=IDENTIFIER_ID_BASE+n;
nResultIndex++;
continue;
case 4:
chEnd=dfa.GetNextCasePoint()-1;
n=addIdentifier(chStart,chEnd);
chStart=chEnd;
MoveToAvailable(chStart);
dfa.PrepareDFA(chStart,255);
LexResult[nResultIndex]=IDENTIFIER_ID_BASE+n;
nResultIndex++;
continue;
case 5:
chStart=dfa.GetNextCasePoint();
MoveToAvailable(chStart);
dfa.PrepareDFA(chStart,255);
LexResult[nResultIndex]=1;
nResultIndex++;
continue;
case 6:
chStart=dfa.GetNextCasePoint();
MoveToAvailable(chStart);
dfa.PrepareDFA(chStart,255);
LexResult[nResultIndex]=2;
nResultIndex++;
continue;
case 7:
chStart=dfa.GetNextCasePoint();
MoveToAvailable(chStart);
dfa.PrepareDFA(chStart,255);
LexResult[nResultIndex]=4;
nResultIndex++;
continue;
case 8:
chStart=dfa.GetNextCasePoint();
MoveToAvailable(chStart);
dfa.PrepareDFA(chStart,255);
LexResult[nResultIndex]=3;
nResultIndex++;
continue;
case 9:
chStart=dfa.GetNextCasePoint();
MoveToAvailable(chStart);
dfa.PrepareDFA(chStart,255);;
LexResult[nResultIndex]=5;
nResultIndex++;
continue;
case 10:
chStart=dfa.GetNextCasePoint();
MoveToAvailable(chStart);
dfa.PrepareDFA(chStart,255);
LexResult[nResultIndex]=6;
nResultIndex++;
continue;
case 11:
chStart=dfa.GetNextCasePoint();
MoveToAvailable(chStart);
dfa.PrepareDFA(chStart,255);
LexResult[nResultIndex]=7;
nResultIndex++;
continue;
case 12:
chStart=dfa.GetNextCasePoint();
MoveToAvailable(chStart);
dfa.PrepareDFA(chStart,255);
LexResult[nResultIndex]=8;
nResultIndex++;
continue;
case 13:
chStart=dfa.GetNextCasePoint();
MoveToAvailable(chStart);
dfa.PrepareDFA(chStart,255);
LexResult[nResultIndex]=9;
nResultIndex++;
continue;
case 14:
chStart=dfa.GetNextCasePoint();
MoveToAvailable(chStart);
dfa.PrepareDFA(chStart,255);
LexResult[nResultIndex]=10;
nResultIndex++;
continue;
case 15:
chStart=dfa.GetNextCasePoint();
MoveToAvailable(chStart);
dfa.PrepareDFA(chStart,255);
LexResult[nResultIndex]=11;
nResultIndex++;
continue;
case 16:
chStart=dfa.GetNextCasePoint();
MoveToAvailable(chStart);
dfa.PrepareDFA(chStart,255);
LexResult[nResultIndex]=12;
nResultIndex++;
continue;
}
}
LexResult[nResultIndex]=0; //置串结束标记
process_S();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -