首页 > 其他分享 >ir_generator.c

ir_generator.c

时间:2023-05-29 18:45:08浏览次数:33  
标签:inputs word string generator arg1 ir sym words

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string>
#include <map>
#include <sstream>
using namespace std;

pair<string, string> words[100]; // 词法分析结果,每个pair的first如"identifier",second如"a"
int total_of_words = 0; // words的数量

/*词法分析函数:读入一行字符串,词法分析后将结果保存至words*/
/*total_of_words终为words的长度,即单词数*/
void lexical_analysis()
{
cout << "Enter a statement, such as a1 * (3+c) or 5 * (3-1): " << endl;
string inputs;
cin >> inputs;

// 初始化词典
map<string, string> operators;
std::map<string, string>::iterator it;
operators["+"] = "plus";
operators["-"] = "minus";
operators["*"] = "times";
operators["/"] = "division";
operators[":="] = "equal";
operators["("] = "left_parentheses";
operators[")"] = "right_parentheses";

// 开始分析
int insize = inputs.length();
string word; // 输入符号,如"ab" "123"
for (int i = 0; i<insize; i++)
{
// 空白符跳过
while (inputs[i] == ' ' || inputs[i] == '\n')
i++;

// 标识符/基本字捕捉
if (isalpha(inputs[i])) {
// 拿出一个标识符/基本字
word = inputs[i++];
while (isalpha(inputs[i]) || isdigit(inputs[i]))
word += inputs[i++];
words[total_of_words++] = make_pair("identifier", word);
i--;
}

// 常数
else if (isdigit(inputs[i])) {
// 拿出常数
word = inputs[i++];
while (isdigit(inputs[i]))
word += inputs[i++];
words[total_of_words++] = make_pair("number", word);
//cout << "(number" << "," << word << ")" << endl;
i--;
}

// <、<=号
else if (inputs[i] == '<') {
word = inputs[i++];
if (inputs[i] == '>') {
word += inputs[i];
words[total_of_words++] = make_pair(operators[word], word);
// cout << "(" << operators[word] << "," << word << ")" << endl;
}
else if (inputs[i] == '=') {
word += inputs[i];
words[total_of_words++] = make_pair(operators[word], word);
// cout << "(" <<operators[word] << "," << word << ")" << endl;
}
else if (inputs[i] != ' ' || !isdigit(inputs[i]) || !isalpha(inputs[i])) {
words[total_of_words++] = make_pair(operators[word], word);
// cout << "(" << operators[word] << "," << word << ")" << endl;
}
else {
//cout << "error!" << endl;
}
i--;
}

// >、>=
else if (inputs[i] == '>') {
word = inputs[i++];
if (inputs[i] == '=') {
word += inputs[i];
words[total_of_words++] = make_pair(operators[word], word);
// cout<<"("<<operators[word]<<","<<word<<")"<<endl;
}
else if (inputs[i] != ' ' || !isdigit(inputs[i]) || !isalpha(inputs[i])) {
words[total_of_words++] = make_pair(operators[word], word);
// cout<<"("<<operators[word]<<","<<word<<")"<<endl;
}
else {
//cout<<"error!"<<endl;
}
i--;
}

//:=
else if (inputs[i] == ':') {
word = inputs[i++];
if (inputs[i] == '=') {
word += inputs[i];
words[total_of_words++] = make_pair(operators[word], word);
// cout<<"("<<operators[word]<<","<<word<<")"<<endl;
}
else {
//cout<<"error!"<<endl;
}
//i--;
}

//其它的基本字
else {
word = inputs[i];
it = operators.find(word);
if (it != operators.end()) {
words[total_of_words++] = make_pair(operators[word], word);
// cout<<"("<<operators[word]<<","<<word<<")"<<endl;
}
else {
//cout<<"error!"<<endl;
}
}
}

}

/**四元式结构**/
struct quad {
string result;
string arg1;
string arg2;
string op;
};
struct quad quad[50]; // 四元式数组
int total_of_quad = 0; // 四元式数组里面的四元式数量

//为quad结构类型重载<<运算
ostream & operator<<(ostream & out, struct quad & q) {
out << '(' << q.op << ',' <<q.arg1 << ',' << q.arg2 << ',' << q.result << ')' << endl;
return out;
}
/*发射一行四元式*/
void emit(string op, string arg1, string arg2, string result)
{
quad[total_of_quad].op = op;
quad[total_of_quad].arg1 = arg1;
quad[total_of_quad].arg2 = arg2;
quad[total_of_quad].result = result;
total_of_quad++;
}

int number_of_temp = 0; // 四元式中临时结果的编号,1,2,3,...
/*产生一个串,t1,t2,t3,...*/
string newT()
{
number_of_temp++;
stringstream ss;
ss << number_of_temp;
string ti = "t" + ss.str();
return ti;
}

/**非算术表达式的递归下降分析及四元式生成**/
// 指针前进
int sym = 0; // 正在处理的单词
void advance()
{
++sym;
if (sym > total_of_words) {
cout << "ERROR! The sym pointer is beyond the range! ";
exit(0);
}
}
string E();
string T();
string F();

/*因子分析*/
string F()
{
string arg;
if (words[sym].first == "identifier") {
arg = words[sym].second;
advance();
}
else if (words[sym].first == "number") {
arg = words[sym].second;
advance();
}
else if (words[sym].first == "left_parentheses") {
advance();
arg = E();
if (words[sym].first == "right_parentheses") {
advance();
}
else {
cout << "ERROR! Failure to match the right bracket! \n";
exit(0);
}
}
return arg;
}

/*项分析*/
string T()
{
string op, arg1, arg2, result;
arg1 = F();
while (words[sym].first == "times" || words[sym].first == "division") {
op = words[sym].second;
advance();
arg2 = F();
result = newT();
emit(op, arg1, arg2, result);
arg1 = result;
}
return arg1;
}

/*表达式分析*/
string E()
{
string op, arg1, arg2, result;
if (words[sym].first == "plus" || words[sym].first == "minus") {
advance();
}
arg1 = T();
while (words[sym].first == "plus" || words[sym].first == "minus") {
op = words[sym].second;
advance();
arg2 = T();
result = newT();
emit(op, arg1, arg2, result);
arg1 = result;
}
return arg1;
}


/**算术表达式的递归下降分析及四元式生成**/
int E_();
int T_();
int F_();

int F_()
{
int arg;
if (words[sym].first == "identifier") {
cout << "The arithmetic expression contains variables and cannot be calculated! \n";
exit(0);
}
else if (words[sym].first == "number") {
arg = atoi(words[sym].second.c_str());
advance();
}
else if (words[sym].first == "left_parentheses") {
advance();
arg = E_();
if (words[sym].first == "right_parentheses") {
advance();
}
else {
cout << "ERROR! Failure to match the right bracket! \n";
exit(0);
}
}
return arg;
}

int T_()
{
string op;
int arg1, arg2, result;
arg1 = F_();
while (words[sym].first == "times" || words[sym].first == "division") {
op = words[sym].second;
advance();
arg2 = F_();
if (op == "*") {
result = arg1 * arg2;
arg1 = result;
}
else {
if (arg2 != 0) {
result = arg1 / arg2;
arg1 = result;
}
else {
cout << "The divisor is 0, error! \n";
exit(0);
}
}
}
return arg1;
}

int E_()
{
string op;
int arg1, arg2, result;
if (words[sym].first == "plus" || words[sym].first == "minus") {
advance();
}
arg1 = T_();
while (words[sym].first == "plus" || words[sym].first == "minus") {
op = words[sym].second;
advance();
arg2 = T_();
if (op == "+") {
result = arg1 + arg2;
arg1 = result;
}
else {
result = arg1 - arg2;
arg1 = result;
}
}
return arg1;
}

int main()
{
lexical_analysis();

if (words[0].first == "number") {
cout << E_()<<endl;
}
else {
E();
for (int i = 0; i<total_of_quad; i++) {
cout << quad[i];
}
}
return 0;
}

标签:inputs,word,string,generator,arg1,ir,sym,words
From: https://www.cnblogs.com/tangcoder/p/17441338.html

相关文章

  • theoretical physics - first sight
    1.外代数1.3.wedgeproduct包含vectorspaces的直和,性质\((1,\text{a})\)以及如下性质叫做外代数(exterioralgebra)a). 双线性(bilinear)\[\begin{aligned}\left(\varphi_{1}+\varphi_{2}\right)\wedge\psi&=\varphi_{1}\wedge\psi+\varphi_{2}\wedge\psi\......
  • calico-node 报错calico/node is not ready: BIRD is not ready: BGP not established
    今天不知道怎么回事,一台机器的calico-node报错,也就是无法初始化正常  Events:TypeReasonAgeFromMessage-------------------------NormalScheduled45sdefault-schedulerSuccessfullyassignedk......
  • 推断题(D - The BOSS Can Count Pairs)
    D-TheBOSSCanCountPairs#include<bits/stdc++.h>usingnamespacestd;typedeflonglongll;#defineendl"\n"//数学题关注边界条件和推断其他的值枚举算答案//nlogn做法//https://zhuanlan.zhihu.com/p/633006114//--------------------------------------------......
  • firewalld防火墙管理
    1、firewalld的基本使用启动:systemctlstartfirewalld关闭:systemctlstopfirewalld查看状态:systemctlstatusfirewalld 开机禁用 :systemctldisablefirewalld开机启用 :systemctlenablefirewalld#查看当前所有tcp端口netstat-ntlp#查看已开放端口情况firewall-c......
  • 什么是 JavaScript 里的循环引用(circular references)
    JavaScript的循环引用(circularreferences)是指在对象之间存在相互引用的情况,形成一个闭环,导致对象无法被完全释放和垃圾回收。循环引用发生在当一个对象的属性或成员引用另一个对象,并且这个被引用的对象又直接或间接地引用回原始对象,从而形成一个循环。当存在循环引用时,JavaScrip......
  • Mybatis Generator 配置详解
    因原版观感不佳,搬运至此。作者:Jimin链接:https://www.imooc.com/article/21444来源:慕课网<?xmlversion="1.0"encoding="UTF-8"?><!DOCTYPEgeneratorConfiguration PUBLIC"-//mybatis.org//DTDMyBatisGeneratorConfiguration1.0//EN"&quo......
  • virsh常用命令
     一、virsh常用命令一些常用命令参数[root@kvm-server~]#virsh--help#查看命令帮忙[root@kvm-server~]#virshlist#显示正在运行的虚拟机[root@kvm-server~]#virshlist--all... 张俊营同学(EDWARD)  · 2021-07-0523:36:31一、virsh常用命令一些常用命令参......
  • Firefox 插件:网页剪贴簿(继承自 Firefox 旧版插件 ScrapBook X)
    Firefox插件:网页剪贴簿(继承自Firefox旧版插件ScrapBookX)获取网页至本地或后端服务器供检索、组织、加注、编辑。https://addons.mozilla.org/zh-CN/firefox/addon/webscrapbook/ 网页剪贴簿(WebScrapBook)可以忠实撷取网页内容,支援多种储存格式与客制化设定,还可以对撷......
  • Python generator 构建协程,实现异步编程(使用yield构建消息处理者和消息创造者)
    协程的定义理解Python协程可以在单个处理机或多个处理机上运行,这取决于具体实现方式。在Python中,主要有两种协程实现方式:生成器协程和asyncio协程。生成器协程只能在单个处理机上运行,因为生成器协程是通过生成器函数实现的,而生成器函数在单个线程中执行。生成器协程也称为......
  • Windows安装VirtualBox教程(图文版)
    VirtualBox是一款免费的虚拟化软件,可以在一台计算机上运行多个操作系统。它可以在Windows、Linux、MacOSX和Solaris等操作系统上运行。VirtualBox支持多种虚拟硬件设备,包括网络适配器、USB控制器、显卡等。用户可以通过VirtualBox创建一个虚拟的计算机环境,安装并运行另一个操作系......