📄 lex.cpp
字号:
// Lex.cpp: implementation of the CLex class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "Lex.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CLex::CLex()
{
static Token TokenTable[]=
{
{CONST_ID,"PI",3.1415926,NULL},
{CONST_ID,"E",2.71828,NULL},
{T,"T",0.0,NULL},
{FUNC,"SIN",0.0,sin},
{FUNC,"COS",0.0,cos},
{FUNC,"TAN",0.0,tan},
{FUNC,"LN",0.0,log},
{FUNC,"EXP",0.0,exp},
{FUNC,"SQRT",0.0,sqrt},
{ORIGIN,"ORIGIN",0.0,NULL},
{SCALE,"SCALE",0.0,NULL},
{ROT,"ROT",0.0,NULL},
{IS,"IS",0.0,NULL},
{FOR,"FOR",0.0,NULL},
{FROM,"FROM",0.0,NULL},
{TO,"TO",0.0,NULL},
{STEP,"STEP",0.0,NULL},
{DRAW,"DRAW",0.0,NULL}
};
m_ptt = TokenTable;
m_LineNo = 1;
m_KeyCount = sizeof(TokenTable)/sizeof(Token);
}
CLex::CLex(FILE *p)
{
static Token TokenTable[]=
{
{CONST_ID,"PI",3.1415926,NULL},
{CONST_ID,"E",2.71828,NULL},
{T,"T",0.0,NULL},
{FUNC,"SIN",0.0,sin},
{FUNC,"COS",0.0,cos},
{FUNC,"TAN",0.0,tan},
{FUNC,"LN",0.0,log},
{FUNC,"EXP",0.0,exp},
{FUNC,"SQRT",0.0,sqrt},
{ORIGIN,"ORIGIN",0.0,NULL},
{SCALE,"SCALE",0.0,NULL},
{ROT,"ROT",0.0,NULL},
{IS,"IS",0.0,NULL},
{FOR,"FOR",0.0,NULL},
{FROM,"FROM",0.0,NULL},
{TO,"TO",0.0,NULL},
{STEP,"STEP",0.0,NULL},
{DRAW,"DRAW",0.0,NULL}
};
m_ptt = TokenTable;
m_Infile = p;
m_LineNo = 1;
m_KeyCount = sizeof(TokenTable)/sizeof(Token);
}
CLex::~CLex()
{
if(m_Infile)
fclose(m_Infile);
}
bool CLex::InitLex(char *FileName)
{
m_LineNo = 1;
m_Infile = fopen(FileName,"r");
if(m_Infile) return true;
return false;
}
void CLex::CloseLex()
{
if(m_Infile)
fclose(m_Infile);
}
Token CLex::GetToken()
{
Token token;
int c;
memset(&token,0,sizeof(Token));
EmptyTokenString();
token.lexeme = TokenBuffer;
for(;;)
{
c = GetChar();
if(c == EOF)
{
token.type = NONTOKEN;
return token;
}
if(c == '\n')
{
m_LineNo++;
}
if(!isspace(c))
break;
}
AddCharTokenString(c);
if(isalpha(c))
{
for(;;)
{
c = GetChar();
if(isalnum(c))
AddCharTokenString(c);
else
break;
}
BackChar(c);
token = JudgeKeyToken(TokenBuffer);
token.lexeme = TokenBuffer;
}
else if(isdigit(c))
{
for(;;)
{
c = GetChar();
if(isdigit(c))
AddCharTokenString(c);
else
break;
}
if(c == '.')
{
AddCharTokenString(c);
for(;;)
{
c = GetChar();
if(isdigit(c))
AddCharTokenString(c);
else
break;
}
}
BackChar(c);
token.type = CONST_ID;
token.value = atof(TokenBuffer);
}
else
{
switch(c)
{
case ';':token.type = SEMICO;break;
case '(':token.type = L_BRACKET;break;
case ')':token.type = R_BRACKET;break;
case ',':token.type = COMMA;break;
case '+':token.type = PLUS;break;
case '-':
c = GetChar();
if(c == '-')
{
while(c != '\n' && c != EOF)
c = GetChar();
BackChar(c);
return GetToken();
}
else
{
BackChar(c);
token.type = MINUS;
}
break;
case '/':
c = GetChar();
if(c == '/')
{
while(c != '\n' && c != EOF)
c = GetChar();
BackChar(c);
return GetToken();
}
else
{
BackChar(c);
token.type = DIV;
}
break;
case '*':
c = GetChar();
if(c == '*')
{
token.type = POWER;
}
else
{
BackChar(c);
token.type = MUL;
}
break;
default:token.type = NONTOKEN;break;
}
}
return token;
}
char CLex::GetChar()
{
int c;
c = getc(m_Infile);
return toupper(c);
}
void CLex::BackChar(char c)
{
if(c != EOF)
ungetc(c,m_Infile);
}
void CLex::AddCharTokenString(char c)
{
int TokenLength = strlen(TokenBuffer);
if((TokenLength + 1) >sizeof(TokenBuffer))
return;
TokenBuffer[TokenLength] = c;
TokenBuffer[TokenLength + 1] = '\0';
}
void CLex::EmptyTokenString()
{
memset(TokenBuffer,0,sizeof(TokenBuffer));
}
Token CLex::JudgeKeyToken(const char *IDString)
{
Token Errortoken;
int i;
for(i = 0;i <=m_KeyCount;i++)
{
if(strcmp(m_ptt[i].lexeme,IDString) == 0)
return m_ptt[i];
}
memset(&Errortoken,0,sizeof(Token));
Errortoken.type = ERRTOKEN;
return Errortoken;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -