#include <iostream>
#include<string>
#include<fstream>
#include<iomanip>
#define _for(i, a, b) for(int i = a; i<b; i++)
using namespace std;
string kw[32] = { "auto", "double", "int", "struct", "break", "else", "long", "switch", "case","enum", "register", "typedef", "char", "extern", "return", "union", "const", "float", "short","unsigned", "continue", "for", "signed", "void", "default", "goto", "sizeof", "volatile", "do", "if", "while", "static"};
string fj[8] = { ",", ";", "(", ")", "[", "]", "{", "}" };
string as[4] = {"+", "-", "*", "/"};
string rs[6] = {">", "<", ">=", "<=", "<>"};
int nm[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
char ch;
bool iskw(string s) {//是否为关键字
_for(i, 0, 12) {
if (s == kw[i])
return true;
}
return false;
}
bool isfj(string s) {//是否为分界符
_for(i, 0, 8)
if (s == fj[i])
return true;
return false;
}
bool isas(string s) {//是否为算术运算符
_for(i, 0, 4) {
if (s == as[i]) {
return true;
}
return false;
}
}
bool isrs(string s) {//是否为关系运算符
_for(i, 0, 6) {
if (s == rs[i]) {
return true;
}
return false;
}
}
bool isnum(char ch) {//是否为数字
if (ch <= '9'&&ch >= '0')
return true;
else
return false;
}
bool isle(char ch) {//是否为字母
if ((ch <= 'z'&&ch >= 'a') || (ch <= 'Z'&&ch >= 'A'))
return true;
else
return false;
}
void scanf(ifstream &in){
int line = 0;
int row = 1;
string s = "";
while (in.get(ch)) {//读取
if (ch == '\n') {//换行则行加一
row++;
line = 0;
}
else if(ch == ' '){}
else if (isle(ch)){//首字符为字母
s = "";
while (isle(ch) || isnum(ch)){//挨个读取字符赋给S
s = s + ch;
ch = in.get();
}
in.seekg(-1, ios::cur);
if (iskw(s)){//如果为关键字输出
line++;
cout.setf(ios::right, ios::adjustfield);
cout << s << "\t" << setw(20) << "(1," << s << ")" << "\t" << setw(12) << "关键字" << "\t" << setw(20) << "(" << row << "," << line << ")" << "\n";
}
else//为标识符
{
line++;
cout.setf(ios::right, ios::adjustfield);
cout << s << "\t" << setw(20) << "(2," << s << ")" << "\t" << setw(20) << "标识符" << "\t" << setw(20) << "(" << row << "," << line << ")" << "\n";
}
}
else if (isnum(ch)) {//首字符为数字
string s = "";
while (isnum(ch)){
s = s + ch;
ch = in.get();
}
line++;
if (isle(ch)){//数字后面有字母则错误
s += ch;
cout << s << "\t" << setw(20) << "error" << "\t" << setw(20) << "error" ;
cout << "\t" << setw(20) << "(" << row << "," << line << ")" << "\n";
}
else//否则为常数
{
in.seekg(-1, ios::cur);
cout << s << "\t" << setw(20) <<"(6," << s << ")" << "\t" << setw(18);
cout << "常数" << "\t" << setw(20) << "(" << row << "," << line << ")" << "\n";
}
}
else {//首字符既不是字母也不是数字
string s = "";
s = s + ch;
if (isas(s)) {
line++;
cout.setf(ios::right, ios::adjustfield);
cout << s << "\t" << setw(20) << "(3," << s << ")" << "\t" << setw(24)<< "算术运算符" << setw(20) << "(" << row << "," << line << ")" << "\n";
}
else if (isfj(s)) {
line++;
cout << s << "\t" << setw(20) <<"(2," << s << ")" << "\t" << setw(20);
cout << "分界符" << "\t" << setw(20) << "(" << row << "," << line << ")" << "\n";
}
else if (isrs(s)) {
line++;
switch (ch){
case '=':
ch = in.get();
if (ch == '='){
s += ch;
cout << s << "\t" << setw(20) << "(4," << s << ")" << "\t" << setw(20);
cout << "关系运算符" << "\t" << setw(20) << "(" << row << "," << line << ")" << "\n";
}
else{
cout << s << "\t" << setw(20) << "error" << "\t" << setw(20) << "error" ;
cout << "\t" << setw(20) << "(" << row << "," << line << ")" << "\n";
in.seekg(-1, ios::cur);
}
break;
case '<':
ch = in.get();
if (ch == '='){
s += ch;
cout << s << "\t" << setw(20) << "(4," << s << ")" << "\t" << setw(20);
cout << "关系运算符" << "\t" << setw(20) << "(" << row << "," << line;
cout << ")" << "\n";
}
else{
cout << s << "\t" << setw(20) << "(4," << s << ")" << "\t" << setw(20);
cout << "关系运算符" << "\t" << setw(20) << "(" << row << "," << line;
cout << ")" << "\n";
in.seekg(-1, ios::cur);
}
break;
case '>':
ch = in.get();
if (ch == '='){
s += ch;
cout << s << "\t" << setw(20) << "(4," << s << ")" << "\t" << setw(20);
cout <<"关系运算符" << "\t" << setw(20) << "(" << row << "," << line;
cout << ")" << "\n";
}
else{
cout << s << "\t" << setw(20) << "(4," << s << ")" << "\t" << setw(20) << ">" << "\t" << setw(20) << "(" << row << "," << line<< ")" << "\n";
in.seekg(-1, ios::cur);
}
break;
default:
cout << s << "\t" << setw(20) << "error" << "\t" << setw(20) << "error" ;
cout << "\t" << setw(20) << "(" << row << "," << line << ")" << "\n";
}
}
else{
line++;
cout << s << "\t" << setw(22) << "error" << "\t" << setw(19) << "error" ;
cout << "\t" << setw(20) << "(" << row << "," << line << ")" << "\n";
}
}
}
}
int main(){
ifstream in;
in.open("./test.txt");
scanf(in);
in.close();
getchar();
return 0;
}
编译原理实验1
猜你喜欢
转载自blog.csdn.net/u25th_engineer/article/details/102458531
今日推荐
周排行