根据设计的DFA完成手工词法分析器的c++实现
共有四组测试样例,要求格式完全一致。
int main(){
int a = 10;
double b = 20.9;
if(a <= b)
a+=b;
else a = 0;
return a;
}
line1:(type, int)
line1:(keyword, main)
line1:(bracket, ()
line1:(bracket, ))
line1:(bracket, {)
line2:(type, int)
line2:(identify, a)
line2:(OPT, =)
line2:(integer, 10)
line2:(bracket, ;)
line3:(type, double)
line3:(identify, b)
line3:(OPT, =)
line3:(decimal, 20.9)
line3:(bracket, ;)
line4:(keyword, if)
line4:(bracket, ()
line4:(identify, a)
line4:(OPT, <=)
line4:(identify, b)
line4:(bracket, ))
line5:(identify, a)
line5:(OPT, +=)
line5:(identify, b)
line5:(bracket, ;)
line6:(keyword, else)
line6:(identify, a)
line6:(OPT, =)
line6:(integer, 0)
line6:(bracket, ;)
line7:(keyword, return)
line7:(identify, a)
line7:(bracket, ;)
line8:(bracket, })
==================================
int main()
{
float i_1 = 1.005e-3;
/* this is remark*/
}
line1:(type, int)
line1:(keyword, main)
line1:(bracket, ()
line1:(bracket, ))
line2:(bracket, {)
line3:(keyword, float)
line3:(identify, i_1)
line3:(OPT, =)
line3:(float, 1.005e-3)
line3:(bracket, ;)
line5:(bracket, })
=====================================
int main()
{
float i = 1.05e;
}
Error at Line 3: Illegal floating point number "1.05e".
=====================================
void main(){
int a, b, c;
char d;
float e;
scanf("%d%d%d%c",&a,&b,&c,&d);
if(a <= b){
a+=b;
printf("%d",a);}
else printf("%c",d);
e = sqrt(abs(b));
return e;
}
line1:(type, void)
line1:(keyword, main)
line1:(bracket, ()
line1:(bracket, ))
line1:(bracket, {)
line2:(type, int)
line2:(identify, a)
line2:(bracket, ,)
line2:(identify, b)
line2:(bracket, ,)
line2:(identify, c)
line2:(bracket, ;)
line3:(type, char)
line3:(identify, d)
line3:(bracket, ;)
line4:(keyword, float)
line4:(identify, e)
line4:(bracket, ;)
line5:(keyword, scanf)
line5:(bracket, ()
line5:(bracket, ")
line5:(typeidentify, %d)
line5:(typeidentify, %d)
line5:(typeidentify, %d)
line5:(typeidentify, %c)
line5:(bracket, ")
line5:(bracket, ,)
line5:(typeidentify, &a)
line5:(bracket, ,)
line5:(typeidentify, &b)
line5:(bracket, ,)
line5:(typeidentify, &c)
line5:(bracket, ,)
line5:(typeidentify, &d)
line5:(bracket, ))
line5:(bracket, ;)
line6:(keyword, if)
line6:(bracket, ()
line6:(identify, a)
line6:(OPT, <=)
line6:(identify, b)
line6:(bracket, ))
line6:(bracket, {)
line7:(identify, a)
line7:(OPT, +=)
line7:(identify, b)
line7:(bracket, ;)
line8:(keyword, printf)
line8:(bracket, ()
line8:(bracket, ")
line8:(typeidentify, %d)
line8:(bracket, ")
line8:(bracket, ,)
line8:(identify, a)
line8:(bracket, ))
line8:(bracket, ;)
line8:(bracket, })
line9:(keyword, else)
line9:(keyword, printf)
line9:(bracket, ()
line9:(bracket, ")
line9:(typeidentify, %c)
line9:(bracket, ")
line9:(bracket, ,)
line9:(identify, d)
line9:(bracket, ))
line9:(bracket, ;)
line10:(identify, e)
line10:(OPT, =)
line10:(keyword, sqrt)
line10:(bracket, ()
line10:(keyword, abs)
line10:(bracket, ()
line10:(identify, b)
line10:(bracket, ))
line10:(bracket, ))
line10:(bracket, ;)
line11:(keyword, return)
line11:(identify, e)
line11:(bracket, ;)
line12:(bracket, })
#include
#include
#include
#include
#include
using namespace std;
string KEYWORD[14]={"main","float","if","else","return","while","then","for","do","case","scanf","printf","sqrt","abs"};
string TYpE[4]={"int","void","char","double"};
char BRACKET[9]={',','{','}','[',']','(',')','\"',';'};
string OpT2[31]={"+","-","*","/",">","<","=","!","+=","-=","*=","/=","<=",">=","==","&&","||","<<",">>","~","++","--","%","!=","^","&=","|=","^=",">>=","<<=","&"};
char OpT[13]={'+','-','*','/','>','<','=','!','|','~','%','^','&'};
char FILTER[4]={' ','\t','\r','\n'}; //过滤符
char p[10000];
int errortype=0;
//关键字判定
bool IsKeyword(string word){
for(int i=0;i<14;i++){
if(KEYWORD[i]==word){
return true;
}
}
return false;
}
bool IsType(string word){
for(int i=0;i<4;i++){
if(TYpE[i]==word){
return true;
}
}
return false;
}
bool IsBracket(char ch){
for(int i=0;i<9;i++){
if(BRACKET[i]==ch){
return true;
}
}
return false;
}
bool IsOpT(char ch){
for(int i=0;i<13;i++){
if(OpT[i]==ch){
return true;
}
}
return false;
}
bool IsOpT2(string word){
for(int i=0;i<31;i++){
if(OpT2[i]==word){
return true;
}
}
return false;
}
bool IsFilter(char ch){
for(int i=0;i<4;i++){
if(FILTER[i]==ch){
return true;
}
}
return false;
}
//字母判断
bool IsLetter(char i){
if(i >='a' && i<='z' || i >='A' && i<='Z') return true;
return false;
}
//数字判定
bool IsDigit(char ch){
if(ch>='0' && ch<='9') return true;
return false;
}
void analyse(){
int line=1;
char ch=' ';
string token="";
ch=cin.get();
while(ch!='\0')
{
if(ch=='\n'){
line++;
ch=cin.get();
}
token="";
if(IsFilter(ch)){
ch=cin.get();
}
else if(IsLetter(ch)){
if(ch >='a' && ch<='z' ){
token += ch;
ch=cin.get();
while(ch >='a' && ch<='z' ){
token += ch;
ch=cin.get();
}
if(IsKeyword(token)){
sprintf(p+strlen(p),"line%d:(keyword, %s)\n",line,token.c_str());
}
else if(IsType(token)){
sprintf(p+strlen(p),"line%d:(type, %s)\n",line,token.c_str());
}
else if(ch=='_'||IsDigit(ch)){
while(ch=='_'||IsDigit(ch)){
token +=ch;
ch=cin.get();
}
sprintf(p+strlen(p),"line%d:(identify, %s)\n",line,token.c_str());
}
else{
sprintf(p+strlen(p),"line%d:(identify, %s)\n",line,token.c_str());
}
}
else{
while(ch >='A' && ch<='Z'){
token += ch;
ch=cin.get();
}
sprintf(p+strlen(p),"line%d:(identify, %s)\n",line,token.c_str());
}
}
else if(IsDigit(ch)){
while(IsDigit(ch)){
token += ch;
ch=cin.get();
}
//小数
if(ch=='.'){
token += ch;
ch=cin.get();
while(IsDigit(ch)){
token += ch;
ch=cin.get();
}
if(ch=='e'){
token += ch;
ch=cin.get();
if(ch=='-'){
token += ch;
ch=cin.get();
}
else {
errortype=1;
cout<<"Error at Line "<='a'&& ch<='z'){
token += ch;
sprintf(p+strlen(p),"line%d:(typeidentify, %s)\n",line,token.c_str());
ch=cin.get();
}
}
while(IsOpT(ch)){
token += ch;
ch=cin.get();
}
if(IsOpT2(token)){
sprintf(p+strlen(p),"line%d:(OPT, %s)\n",line,token.c_str());
}
}
else if(IsBracket(ch)){ //问题日志:1.对于复合的bracket和OpT,只显示其中后半部分,2.只能正常显示《= (已解决)
token += ch;
sprintf(p+strlen(p),"line%d:(bracket, %s)\n",line,token.c_str());
ch=cin.get();
}
else break;
}
}
int main()
{
analyse();
if(errortype==0){
cout<