1000. 词法分析程序设计 **
来源:互联网 发布:potplayer for mac 编辑:程序博客网 时间:2024/06/05 18:17
- 问题描述
- 实现
问题描述
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 bUnsigned_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;}
阅读全文
0 0
- 1000. 词法分析程序设计 **
- Sicily 1000. 词法分析程序设计 **
- 词法分析程序设计
- 词法分析程序设计
- sicily1000. 词法分析程序设计 **
- 实验一 简单词法分析程序设计
- 算法练习(14):词法分析程序设计
- 编译原理_词法分析程序设计
- 编译原理丨第七周 ——1000. 词法分析程序设计 **
- 实验一 源程序的预处理及词法分析程序设计
- 词法分析
- 词法分析
- 词法分析
- 词法分析
- 词法分析
- 词法分析
- 词法分析
- 词法分析
- Scrapy
- 迷宫问题
- Friend Circles
- C# SocketAsyncEventArgs 实现异步通信
- 类和对象的区别
- 1000. 词法分析程序设计 **
- Java垃圾回收器
- Frontier-based exploration
- TensorFlow实战——CNN(VGGNet19)——图像风格转化
- EmguCV Video
- servlet_jsp
- Shader(着色器)之各种空间概念
- IOS获取图片中某个像素点的像素值,包括ARGB的各个值
- Sublime Text 插件安装