问题描述
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++设计一程序实现词法分析。
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
Problem Source: 词法分析
实现
#include <iostream>
#include <map>
#include <string>
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 }; //无符号整数
int main() {
char c, temp;
int flag = 0;
while (1) {
if (flag == 1) {
c = temp;
flag = 0;
}
else if (flag == 0) {
cin.get(c);
}
if (c == '#') break;
if (c == ' ') continue;
for (int i = 0; i < boundaries.number; i++) // 识别分解符
if (c == boundaries.str[i][0]) {
printf("<3,%d>", i);
continue;
}
// 识别运算符
if (c == '+' || c == '*') {
cin.get(temp);
if (temp == '=') {
if (c == '+') printf("<2,%d>", 3);
else printf("<2,%d>", 4);
}
else {
if (c == '+') printf("<2,%d>", 0);
else printf("<2,%d>", 1);
flag = 1;
}
continue;
}
else if (c == '=') {
printf("<2,%d>", 2);
continue;
}
// 识别无符号整数
string sint;
if (c >= '0' && c <= '9') {
sint.push_back(c);
while (1) {
cin.get(temp);
if (temp >= '0' && temp <= '9') {
sint.push_back(temp);
}
else {
flag = 1;
break;
}
}
printf("<5,0>");
Unsigned_integer.str[Unsigned_integer.number++] = sint;
continue;
}
// 识别关键词和标识符
string ss;
if ((c >= 'a' && c <= 'z')||(c >= 'A' && c <= 'Z')) { // 规定了标识符是字母开头, 不包含下划线
ss.push_back(c);
while (1) {
cin.get(temp);
if ((temp >= 'a' && temp <= 'z') || (temp >= 'A' && temp <= 'Z')||
(temp >= '0' && temp <= '9')) {
ss.push_back(temp);
}
else {
flag = 1;
break;
}
}
bool isKeyword = false;
for (int i = 0; i < keywords.number; i++) {
if (ss == keywords.str[i]) {
printf("<1,%d>", i);
isKeyword = true;
break;
}
}
if (!isKeyword) {
printf("<4,0>");
identifieres.str[identifieres.number++] = ss;
}
continue;
}
}
printf("identifieres:");
for (int i = 0; i < identifieres.number; i++)
cout << identifieres.str[i] << " ";
printf("Unsigned_integer:");
for (int i = 0; i < Unsigned_integer.number; i++)
cout << Unsigned_integer.str[i] << " ";
return 0;
}