📄 main.cpp
字号:
/////////////////////////////////////////////////////////////////////////////////////
// Writing in C++
#include <fstream.h> //文件流类定义在此头文件内
#include <string.h>
char chinput;
int ipointer;
char* word;
char* string;
void GetChar()
{
chinput=*(string+ipointer++);
}
void Retract()
{
if((chinput==' '||chinput=='\t'||chinput=='\n')?false:true)
{
ipointer--;
chinput='\0';
}
}
void main()
{
char filename[128];
cout<<"Please input the name of the file you want to open:";
cin>>filename;
ifstream openFile(filename);
if(openFile.fail())
{
cout<<"Error! Cannot open given file.\n";
return;
}
while(openFile.peek()!=EOF)
{
openFile.ignore();
ipointer++;
}
string=new char[ipointer+1];
openFile.seekg(0);
openFile.read(string,ipointer);
*(string+ipointer)='\0';
chinput='\0';
ipointer=0;
cout<<"Source codes in given file are:\n"<<string<<endl;
char* temp=new char[2];
GetChar();
while(chinput!='\0')
{
switch(chinput)
{
case ';':
{
cout<<"(4,\";\")"<<endl;
break;
}
case '{':
{
cout<<"(5,\"{\")"<<endl;
break;
}
case '}':
{
cout<<"(6,\"}\")"<<endl;
break;
}
case '(':
{
cout<<"(7,\"(\")"<<endl;
break;
}
case ')':
{
cout<<"(8,\")\")"<<endl;
break;
}
case '+':
{
cout<<"(10,\"+\")"<<endl;
break;
}
case '*':
{
cout<<"(11,\"*\")"<<endl;
break;
}
case '=':
{
GetChar();
if(chinput=='=')
{
cout<<"(13,\"==\")"<<endl;
}
else
{
cout<<"(12,\"=\")"<<endl;
Retract();
}
break;
}
case '>':
{
GetChar();
if(chinput=='=')
{
cout<<"(13,\">=\")"<<endl;
}
else
{
cout<<"(13,\">\")"<<endl;
Retract();
}
break;
}
case '<':
{
GetChar();
if(chinput=='>')
{
cout<<"(13,\"<>\")"<<endl;
}
else if(chinput=='=')
{
cout<<"(13,\"<=\")"<<endl;
}
else
{
cout<<"(13,\"<\")"<<endl;
Retract();
}
break;
}
case '-':
{
GetChar();
if((chinput>='0'&&chinput<='9')?true:false)
{
word=new char[128];
*word='\0';
*temp=chinput;
*(temp+1)='\0';
strcat(word,temp);
GetChar();
cout<<"(15,\"-"<<word<<"\")"<<endl;
}
else
{
cout<<"error,\"-\""<<endl;
Retract();
}
break;
}
default:
{
if((chinput==' '||chinput=='\t'||chinput=='\n')?true:false)
break;
else if((chinput>='0'&&chinput<='9')?true:false)
{
word=new char[128];
*word='\0';
*temp=chinput;
*(temp+1)='\0';
strcat(word,temp);
GetChar();
cout<<"(15,\""<<word<<"\")"<<endl;
Retract();
break;
}
else if(((chinput>='a'&&chinput<='z')||(chinput>='A'&&chinput<='Z'))?true:false())
{
word=new char[128];
*word='\0';
while((chinput>='0'&&chinput<='9')?true:false||((chinput>='a'&&chinput<='z')||(chinput>='A'&&chinput<='Z'))?true:false)
{
*temp=chinput;
*(temp+1)='\0';
strcat(word,temp);
GetChar();
}
if(strcmp(word,"if")==0)
{
cout<<"(0,\""<<word<<"\")"<<endl;
}
else if(strcmp(word,"else")==0)
{
cout<<"(1,\""<<word<<"\")"<<endl;
}
else if(strcmp(word,"while")==0)
{
cout<<"(2,\""<<word<<"\")"<<endl;
}
else if(strcmp(word,"do")==0)
{
cout<<"(3,\""<<word<<"\")"<<endl;
}
else
{
cout<<"(14,\""<<word<<"\")"<<endl;
}
Retract();
break;
}
else
{
cout<<"error,\""<<chinput<<"\""<<endl;
break;
}
}
}
word=NULL;
GetChar();
}
cout<<"词法分析结束,谢谢!";
}
// 该程序没能将“赋值语句”辨认出来,希望老师帮忙修改后发回,谢谢!:)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -