Description
设一语言的关键词、运算符、分界符的个数与单词如下:
struct { int number; string str[10]; } keywords={3,"int","main","return"} ; //关键词
struct { int number; string str[10]; } operators ={5,"+","*","=","+=","*="}; //运算符
struct { int number; string str[10]; } boundaries ={6,"(",")","{","}",",",";"} ; //分界符
struct { int number; string str[100];} identifieres={0}; //标识符
struct { int number; string str[100];} Unsigned_integer={0}; //无符号整数
以上类号分别为1~5,序号从0开始;
标识符是字母开头的字母数字串;常量为无符号整数;
用C++设计一程序实现词法分析。
此题需要提交实验报告;“实验报告用“学号+姓名+51”
Input
输入一程序,结束符用”#”;
Output
输出单词数对:<类号,序号>。 输出标识符表,用空格分隔; 输出无符号整数表,用空格分隔;
Sample Input
main()
{ int a=2,b=3;
return 2*b+a;
}#
Sample Output
<1,1><3,0><3,1><3,2><1,0><4,0><2,2><5,0><3,4><4,1><2,2><5,1><3,5><1,2><5,0><2,1>
<4,1><2,0><4,0><3,5><3,3>
identifieres:a b
Unsigned_integer:2 3
Code
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
struct
{
int number;
string str[10];
} keywords = {3, "int", "main", "return"}; //关键词
struct
{
int number;
string str[10];
} operators = {5, "+", "*", "=", "+=", "*="}; //运算符
struct
{
int number;
string str[10];
} boundaries = {6, "(", ")", "{", "}", ",", ";"}; //分界符
struct
{
int number;
string str[100];
} identifieres = {0}; //标识符
struct
{
int number;
string str[100];
} Unsigned_integer = {0}; //无符号整数
string code;
string s;
// input the programme
void input()
{
char ch;
while (true)
{
ch = getchar();
if (ch == '#')
break;
// ignore '\n',' '
code.push_back(ch);
if (ch == '\n' || ch == ' ')
continue;
s.push_back(ch);
}
}
// char in operators? return index or -1
// int CheckOperators(int index){
// for(int i=0;i<operators.number;i++){
// if(operators.str[i] == s[index] && )
// return i;
// }
// return -1;
// }
int CheckBoundaries(int index)
{
for (int i = 0; i < boundaries.number; i++)
{
if (boundaries.str[i][0] == s[index])
return i;
}
return -1;
}
int CheckIdentifieres(string s)
{
for (int i = 0; i < identifieres.number; i++)
if (s == identifieres.str[i])
return i;
return -1;
}
int CheckInteger(string s)
{
for (int i = 0; i < Unsigned_integer.number; i++)
if (s == Unsigned_integer.str[i])
return i;
return -1;
}
void CheckEachWord()
{
for (int i = 0; i < s.size(); i++)
{
// "int"?
if (s.substr(i, 3) == "int")
{
i += 2;
cout << "<1,0>";
}
// "main"?
else if (s.substr(i, 4) == "main")
{
i += 3;
cout << "<1,1>";
}
// "return"?
else if (s.substr(i, 6) == "return")
{
i += 5;
cout << "<1,2>";
}
// operators?
else if (s[i] == '+' || s[i] == '*' || s[i] == '=')
{
if (s[i] == '+')
{
if (s[i + 1] == '=')
{
i++;
cout << "<2,3>";
}
else
cout << "<2,0>";
}
else if (s[i] == '*')
{
if (s[i + 1] == '=')
{
i++;
cout << "<2,4>";
}
else
cout << "<2,1>";
}
else
cout << "<2,2>";
}
// boundaries?
else if (CheckBoundaries(i) != -1)
{
cout << "<3," << CheckBoundaries(i) << ">";
}
// identifieres?
else if (isalpha(s[i]))
{
string temp;
temp.push_back(s[i]);
while (isalnum(s[i + 1]))
{
i++;
temp.push_back(s[i]);
}
if (CheckIdentifieres(temp) == -1)
{
identifieres.str[identifieres.number] = temp;
cout << "<4," << identifieres.number++ << ">";
}
else
{
cout << "<4," << CheckIdentifieres(temp) << ">";
}
}
// Unsigned_integer?
else if (isdigit(s[i]))
{
string temp;
temp.push_back(s[i]);
while (isdigit(s[i + 1]))
{
i++;
temp.push_back(s[i]);
}
if (CheckInteger(temp) == -1)
{
Unsigned_integer.str[Unsigned_integer.number] = temp;
cout << "<5," << Unsigned_integer.number++ << ">";
}
else
{
cout << "<5," << CheckInteger(temp) << ">";
}
}
}
cout << endl;
}
void print()
{
cout << "identifieres:";
for (int i = 0; i < identifieres.number - 1; i++)
{
cout << identifieres.str[i] << " ";
}
cout << identifieres.str[identifieres.number - 1] << endl;
cout << "Unsigned_integer:";
for (int i = 0; i < Unsigned_integer.number - 1; i++)
{
cout << Unsigned_integer.str[i] << " ";
}
cout << Unsigned_integer.str[Unsigned_integer.number - 1] << endl;
}
void run()
{
input();
CheckEachWord();
print();
}
int main(int argc, char const *argv[])
{
run();
return 0;
}