📄 word.cpp
字号:
/*本词法分析器功能:将从制定文件读取的程序段的单词、符号识别出并存入save文件,另外也将数字
从字符串格式转化为整型或浮点型格式,同样存入文件,按顺序,如果存在文法中不允许的字符将会报错
和提示*/
#include<iostream.h>
#include<fstream.h>
#include<stdlib.h>
#include<string.h>
#include<stdio.h>
FILE *fp;
int IsLetter(char ch)
{
if(ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z') return 1;
else return 0;
}
int IsDigit(char ch)
{
if(ch >= '0' && ch <= '9') return 1;
else return 0;
}
int main()
{
ofstream outFile("save.txt");
if(!outFile){
cerr<< "cannot" << endl;
exit(1);
}
char ch;
char fname[20];
char save[15];
int i = 0, j = 0, digit = 0;
float digitf = 0, e = 1;
int a[10];
int error = 0;
cout << "Please input file name : " ;
cin >> fname;
if((fp = fopen(fname, "r+")) == NULL){
printf("Cannot open the file!\n");
return 0;
}
//0
while((ch = fgetc(fp)) != EOF)
{
//cout << ch;
if(IsLetter(ch)) //1
{
while(IsLetter(ch) || IsDigit(ch))
{
save[i++] = ch;
ch = fgetc(fp);
}
save[i] = '\0';
if(!strcmp(save,"program"))
outFile << save << endl;
else if(!strcmp(save,"begin"))
outFile << save << endl;
else if(!strcmp(save,"end"))
outFile << save << endl;
else if(!strcmp(save,"const"))
outFile << save << endl;
else if(!strcmp(save,"int"))
outFile << save << endl;
else if(!strcmp(save,"array"))
outFile << save << endl;
else if(!strcmp(save,"if"))
outFile << save << endl;
else if(!strcmp(save,"then"))
outFile << save << endl;
else if(!strcmp(save,"while"))
outFile << save << endl;
else if(!strcmp(save,"do"))
outFile << save << endl;
else if(!strcmp(save,"for"))
outFile << save << endl;
else if(!strcmp(save,"to"))
outFile << save << endl;
else outFile << '%' << save << endl; //写文件 //2
//save[0] = '\0';
if(ch == EOF) break;
i = 0;
fseek(fp,-1L,SEEK_CUR);
}
else if(IsDigit(ch)) //3
{
while(IsDigit(ch))
{
a[j++] = ch - 48;
//outFile << a[j-1] << "#" << endl;
ch = fgetc(fp); //0~9的ASCII码是48~57
}
for(int k =j-1; k >= 0; k--)
{
//outFile << a[k];
digit += a[k] * e;
e = e * 10;
}
if(ch != '.')
outFile << digit << endl; //!!!已换算成数字 //4
else
{
a[10] = 0;
j = 0;
e = 1; //清空
ch = fgetc(fp);
while(IsDigit(ch))
{
a[j++] = ch - 48;
ch = fgetc(fp);
}
for(k =0; k <= j-1; k++)
{
//outFile << a[k];
e = e / 10;
digitf += a[k] * e;
}
digitf = digitf + digit;
outFile << digitf << endl;
}
a[10] = 0;
j = 0;
digit = 0;
e = 1;
fseek(fp,-1L,SEEK_CUR); //清零工作
}
else if(ch == '+') //5
outFile << "+" << endl;
else if(ch == '-') //6
outFile << "-" << endl;
else if(ch == '*') //7
outFile << "*" << endl;
else if(ch == '/') //8
outFile << "/" << endl;
else if(ch == '=') //9
outFile << "=" << endl;
else if(ch == ':') //10
{
ch = fgetc(fp);
if(ch == '=')
outFile << ":=" << endl; //11
else
{
outFile << ":" << endl;//为语法分析器而改 cout << "There exists a sign which cannot be recognized :" << endl; // 22 也可以考虑break 然后在程序最后报错
fseek(fp,-1L,SEEK_CUR);
}
}
else if(ch == '<') //12
{
ch = fgetc(fp);
if(ch == '=')
outFile << "<=" << endl; //13
else if(ch == '>')
outFile << "<>" << endl; //14
else outFile << "<" << endl; //15
}
else if(ch == '>') //16
{
ch = fgetc(fp);
if(ch == '=') //17
outFile << ">=" << endl;
else
{
outFile << ">" << endl; //18
fseek(fp,-1L,SEEK_CUR);
}
}
else if(ch == '(')
outFile << "(" << endl; //19
else if(ch == ')')
outFile << ")" << endl; //20
else if(ch == '[')
outFile << "[" << endl; //21
else if(ch == ']')
outFile << "]" << endl; //22
else if(ch == ';')
outFile << ";" << endl; //为语法分析时加的!
else if(ch == ',')
outFile << "," << endl; //为语法分析时加的!
else if(ch != ' ' && ch != '\n' && ch != '\t') //0
{
cout << "There exists a sign which cannot be recognized " << ch << endl; //23
error = 1;
}
}
outFile << "#";
fclose(fp);
if(error == 1)
cout << "You should have a check on your program." << endl << "But you also have the words analyzed in the file: save.txt! " << endl;
else
cout << "Succeed! The words have been output into the file: save.txt ! :)" << endl;
return 1;
}
/*ofstream outFile("save.txt");
if(!outFile){
cerr<< "cannot" << endl;
exit(1);
}
int n = 50;
float f = 20.3;
outFile << "n:" << n << endl;
outFile << "f:" << f << endl;*/
// 0372283 王茜
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -