设一语言的关键词、运算符、分界符的个数与单词如下:
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++设计一程序实现词法分析。
输入一程序,结束符用”#”;
输出单词数对:<类号,序号>。 输出标识符表,用空格分隔; 输出无符号整数表,用空格分隔;
main() { int a=2,b=3; return 2*b+a; }#
<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: 词法分析
编译原理西西里的题目,还是比较简单的,需要注意的是要用cin.get(ch),不然读不进空格
// Problem#: 20907
// Submission#: 5186361
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
//
// main.cpp
// cifa
//
// Created by xie on 2017/10/18.
// Copyright © 2017年 xie. All rights reserved.
//
#include
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}; //无符号整数
void handle(string my_str)
{
string temp_int = "";//用来暂时存储无符号整数的一部分
string temp_word = "";//用来暂时存储字符串
for(int i = 0;i";
continue;
}
if(my_str[i] == ')')
{
cout<<"<3,1>";
continue;
}
if(my_str[i] == '{')
{
cout<<"<3,2>";
continue;
}
if(my_str[i] == '}')
{
cout<<"<3,3>";
continue;
}
if(my_str[i] == ',')
{
cout<<"<3,4>";
continue;
}
if(my_str[i] == ';')
{
cout<<"<3,5>";
continue;
}
//再到运算符
if(my_str[i] == '=')
{
cout<<"<2,2>";
continue;
}
if(my_str[i] == '+' )
{
if(my_str[i+1] == '=')
{
cout<<"<2,3>";
continue;
}
else
{
cout<<"<2,0>";
continue;
}
}
if(my_str[i] == '*')
{
if(my_str[i+1] == '=')
{
cout<<"2,4";
continue;
}
else
{
cout<<"<2,1>";
continue;
}
}
//再到无符号整数
if(isdigit(my_str[i]))
{
temp_int += my_str[i];
if(isdigit(my_str[i+1]))//如果下一位还是数字的话,留到下一次再处理
continue;
else
{
bool flag = false;//判断数字在Unsigned_integer中是否已经存在
for(int j = 0;j < Unsigned_integer.number;++j)
{
if(temp_int == Unsigned_integer.str[j])
{
cout<<"<5,"<";
temp_int = "";//记得复原temp_int
flag = true;
break;
}
}
if(flag == false)
{
cout<<"<5,"<";
Unsigned_integer.str[Unsigned_integer.number++] = temp_int;
temp_int = "";
}
}
}
if(isalpha(my_str[i]))
{
temp_word += my_str[i];
if(('a'<=my_str[i+1]&&my_str[i+1]<='z')||('A'<=my_str[i+1]&&my_str[i+1]<='Z'))
{
//cout<";
flag1 = true;
temp_word = "";
break;
}
}
if(flag1) continue;
bool flag = false;//处理标识符,过程与上面类似
for(int j = 0;j";
temp_word = "";
flag = true;
break;
}
}
if(!flag)
{
cout<<"<4,"<";
identifieres.str[identifieres.number++] = temp_word;
temp_word = "";
}
}
}
cout<