编译原理丨第七周 ——1000. 词法分析程序设计 **
来源:互联网 发布:金融机构利用电话网络 编辑:程序博客网 时间:2024/05/21 10:14
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: 词法分析
编译原理西西里的题目,还是比较简单的,需要注意的是要用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 <iostream>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<my_str.size();++i) { if(my_str[i] == ' ' || my_str[i] == '\n')//空格与换行不用处理 continue; //先处理分界符的情况 if(my_str[i] == '(') { cout<<"<3,0>"; 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,"<<j<<">"; temp_int = "";//记得复原temp_int flag = true; break; } } if(flag == false) { cout<<"<5,"<<Unsigned_integer.number<<">"; 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<<endl<<my_str[i+1]; continue; } bool flag1 = false; for(int j = 0;j<3;++j)//处理关键字 { if(temp_word == keywords.str[j]) { cout<<"<1,"<<j<<">"; flag1 = true; temp_word = ""; break; } } if(flag1) continue; bool flag = false;//处理标识符,过程与上面类似 for(int j = 0;j<identifieres.number;++j) { if(temp_word == identifieres.str[j]) { cout<<"<4,"<<j<<">"; temp_word = ""; flag = true; break; } } if(!flag) { cout<<"<4,"<<identifieres.number<<">"; identifieres.str[identifieres.number++] = temp_word; temp_word = ""; } } } cout<<endl; cout<<"identifieres:"; if(identifieres.number == 0) ; else for(int i = 0;i<identifieres.number;++i) cout<<identifieres.str[i]<<" "; cout<<endl; cout<<"Unsigned_integer:"; if(Unsigned_integer.number == 0) ; else for(int i = 0;i<Unsigned_integer.number;++i) cout<<Unsigned_integer.str[i]<<" "; cout<<endl;}int main(int argc, const char * argv[]) { char temp_char; string temp_string; while(cin.get(temp_char) && temp_char != '#') temp_string += temp_char; //cout<<temp_string; handle(temp_string); return 0;}
阅读全文
0 0
- 编译原理丨第七周 ——1000. 词法分析程序设计 **
- 编译原理_词法分析程序设计
- 编译原理——词法分析
- 编译原理 词法分析
- 编译原理词法分析
- 编译原理 词法分析
- 编译原理词法分析
- 编译原理词法分析
- 编译原理词法分析
- 编译原理 词法分析
- 编译原理--词法分析
- 【编译原理】词法分析
- 编译原理-词法分析
- 编译原理词法分析
- 编译原理—词法分析器
- 编译原理实验一——简单词法分析
- 哈工大编译原理实验1——词法分析
- 编译原理——词法分析(1)
- 【hdu1150】【Machine Schedule】二分图最小点覆盖+简单感性证明
- 粘合多款产品的微服务架构 阿里云函数计算再次升级
- Perl语言简述
- 提交表单kindeditor编辑器textarea为空解决办法
- Intellij IDEA 快捷键整理
- 编译原理丨第七周 ——1000. 词法分析程序设计 **
- 计算熵引发的一些讨论
- 客户信息管理系统2—客户信息的增加(一)
- The constructor FirefoxDriver(FirefoxBinary, FirefoxProfile) is undefined
- 【原创】纯干货,Spring-data-jpa详解,全方位介绍。
- Arcgis server配置oracle客户端
- 深度学习之RBM
- keras下的tensorboard
- TCP协议 拥塞控制算法