📄 lexicalforc.cpp
字号:
#include <iostream>
#include <fstream>
#include <string>
/*姓名: 赵晓东
*学号:063372
*班级:RJ010606*/
using namespace std;
string keyWord[10] = {"else", "if", "int", "return", "void", "while"}; //关键字集合
string symbol[30] = { "+", "-", "*", "/", "<", "<=", ">", ">=","=", "==", "!=",
";", ",", "(", ")", "[", "]", "{", "}", "/*", "*/" }; //专用符号集合
bool isKeyWord(string s){ //查询是否是关键字
for(int i = 0; i < 6; i++){
if(s == keyWord[i]){
return true;
}
}
return false;
}
bool isSymbol(string s){ //查询是否是专门符号
for(int i = 0; i < 21; i++){
if(s == symbol[i]){
return true;
}
}
return false;
}
void main(){
cout<<"请输入源文件名:"<<endl;
string fileName;
cin>>fileName;
char token[30];
string word;
int i = 0, line = 1;
ifstream infile;
ofstream outfile;
infile.open (fileName.c_str(), ifstream::in);
outfile.open("out.txt",ofstream::trunc);
char ch = infile.get();
while (infile.good()){
i = 0;
if(((ch>='A')&&(ch<='Z'))||((ch>='a')&&(ch<='z'))) { //第一个字符为字母时可能为关键字或者标示符ID
while(((ch>='A')&&(ch<='Z'))||((ch>='a')&&(ch<='z'))) {
token[i++]=ch;
ch=infile.get();
}
if(ch >= '0'&&ch <= '9'){ //标示符中不能出现数字,否则报错
while((ch>='0')&&(ch<='9')){
token[i++]=ch;
ch = infile.get();
}
token[i] = '\0';
outfile<<"there is a error "<<token<<" in line "<<line<<endl;
infile.seekg(-1L,ios::cur); //对扫描位置回溯,判断其他的可能性
//此种方法在本例中多次用到
}
else{
token[i] = '\0';
word = token;
if(isKeyWord(word)) //判断是否是关键字
outfile<<"KEYWORD"<<"\t\t"<<word<<endl;
else
outfile<<"ID"<<"\t\t"<<word<<endl; //非关键字就是标示符
if(ch != EOF)
infile.seekg(-1L,ios::cur);
}
}
else if(ch>='0'&&ch<='9'){ //第一个字符以数字开头的序列
while(ch>='0'&&ch<='9') {
token[i++]=ch;
ch=infile.get();
}
if(((ch>='A')&&(ch<='Z'))||((ch>='a')&&(ch<='z'))) {
while(((ch>='A')&&(ch<='Z'))||((ch>='a')&&(ch<='z'))) {
token[i++]=ch;
ch=infile.get();
}
token[i] = '\0';
outfile<<"there is a error "<<token<<" in line "<<line<<endl;
infile.seekg(-1L,ios::cur);
}
else{
token[i] = '\0';
word = token;
outfile<<"NUMBER"<<"\t\t"<<word<<endl;
if(ch != EOF)
infile.seekg(-1L,ios::cur);
}
}
else if((ch==' ')||(ch=='\t'))
; //遇到空格符和制表符跳过
else if(ch=='\n')
line++;
else if(ch=='/') { //跳过注释
ch=infile.get();
if(ch!='*') { //以“/”开头的还可能是除号
outfile<<"SYMBOL"<<"\t\t"<<'/'<<endl;
infile.seekg(-1L,ios::cur);
}
else if(ch=='*')
{
int count=0;
ch=infile.get();
while(count!=2) //寻找注释的另一半标志“*/”
{
count=0;
while(ch!='*')
ch=infile.get();
count++;
ch=infile.get();
if(ch=='/')
count++;
else
ch=infile.get();
}
}
}
else if(ch=='"'){ //消除双引号中的常量
outfile<<"SYMBOL"<<"\t\t"<<'"'<<endl;
ch=infile.get();
while(ch!='"')
ch=infile.get();
outfile<<"SYMBOL"<<"\t\t"<<'"'<<endl; //消除单引号中的常量
}
else if(ch=='\''){
outfile<<"SYMBOL"<<"\t\t"<<'\''<<endl;
ch=infile.get();
while(ch!='\'')
ch=infile.get();
outfile<<"SYMBOL"<<"\t\t"<<'\''<<endl;
}
else{
token[0]=ch;
ch=infile.get(); //继续读入第二个字符,判断是否是组合符号
if(ch!=EOF)
{
token[1]=ch;
token[2]='\0';
word = token;
if(!isSymbol(word))
{ //非组合符号的话检查单个符号
token[1] = '\0';
word = token;
if(!isSymbol(word))
{
outfile<<"there is a error "<<word<<" in line "<<line<<endl;
infile.seekg(-1L,ios::cur);
}
else
{
outfile<<"SYMBOL"<<"\t\t"<<word<<endl;
infile.seekg(-1L,ios::cur);
}
}
else
{
outfile<<"SYMBOL"<<"\t\t"<<word<<endl;
}
}
else
{
token[1] = '\0';
word = token;
if(!isSymbol(word))
outfile<<"there is a error "<<word<<" in line "<<line<<endl;
else
{
outfile<<"SYMBOL"<<"\t\t"<<word<<endl;
}
}
}
ch=infile.get();
}
infile.close();
outfile.close();
return;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -