前文回顾
【编译原理】LR(0)分析方法(c++实现)
【编译原理】SLR(1)分析方法(c++实现)
【编译原理】LR(1)分析方法(c++实现)
这几个程序的代码大部分是一样的,根据不同算法特点做了部分修改而已
代码
LALR(1)的代码就是在LR(1)的基础上合并了同心项
Item类
在LR(1)基础上搜索符由string
改成了vector<string>
#include <iostream>
#include <fstream>
#include <string.h>
#include <string>
#include <cstring>
#include <algorithm>
#include <set>
#include <map>
#include <stack>
#include <utility>
#include <iomanip>
using namespace std;
//返回s的第一个词
string firstWord(string s)
{
s+=" ";
string first=s.substr(0,s.find(" "));
return first;
}
//将字符串划分为一个个词
vector<string> split(string s,string separator)
{
vector<string>v;
string::size_type pos1, pos2;
pos2 = s.find(separator);
pos1 = 0;
while(string::npos != pos2)
{
v.push_back(s.substr(pos1, pos2-pos1));
pos1 = pos2 + separator.size();
pos2 = s.find(separator, pos1);
}
if(pos1 != s.length())
v.push_back(s.substr(pos1));
return v;
}
class Item
{
private:
string item;//项目
string left;//项目左部
string right;//项目右部
vector<string> symbol;//向前搜索符号
static int count;
public:
int id;
//参数是产生式
Item(string i)
{
id=count++;
left=i.substr(0,i.find("->"));
right=i.substr(i.find("->")+2);
item=left+"->"+right;
symbol.push_back("$");//初始向前搜索符为"$"
if(right.find(".")==string::npos)
addDot(0);
}
//参数是左部和右部
Item(string l,string r)
{
id=count++;
left=l;
right=r;
symbol.push_back("$");
item=left+"->"+right;
if(right.find(".")==string::npos)
addDot(0);
}
//参数是左部和右部和向前搜索符号
Item(string l,string r,string s)
{
id=count++;
left=l;
right=r;
symbol.push_back(s);
item=left+"->"+right;
if(right.find(".")==string::npos)
addDot(0);
}
string getLeft()
{
return left;
}
string getRight()
{
return right;
}
string getItem()
{
item=left+"->"+right;
return item;
}
vector<string> getSymbol()
{
return symbol;
}
//找点的位置
int getDot(string item)
{
return item.find(".");
}
//设置向前搜索符号
//Insert为1,则插入搜索符,为0,则重置搜索符
void setSymbol(int Insert,string new_symbol)
{
if(Insert&&find(symbol.begin(),symbol.end(),new_symbol)==symbol.end())
symbol.push_back(new_symbol);
else
{
symbol.clear();
symbol.push_back(new_symbol);
}
sort(symbol.begin(), symbol.end());
}
//给文法加点
void addDot(int pos)
{
if(right[pos]=='@')
right=".";
else if(pos==0)
right.insert(pos,". ");
else if(pos==right.size())
right.insert(pos," .");
else
right.insert(pos," . ");
}
//判断一个项目进度是否到结尾
int hasNextDot()
{
vector<string>buffer=split(right,".");
if(buffer.size()>1)
return 1;
else
return 0;
}
//得到"."后面的一个文法符号
string getPath()
{
vector<string>buffer=split(item,".");
buffer[1].erase(0,1);
string first=firstWord(buffer[1]);
return first;
}
//返回下一个点的串
string nextDot()
{
int dotPos=right.find(".");
vector<string>buffer=split(item,".");
buffer[1].erase(0,1);
string first=firstWord(buffer[1]);
int nextPos=dotPos+first.size();
right.erase(right.find("."),2);
right.insert(nextPos," .");
return right;
}
bool operator ==(Item &x)
{
return getItem()==x.getItem();
}
};
int Item::count=0;
LALR(1)语法分析过程
#include <iostream>
#include <fstream>
#include <string.h>
#include <string>
#include <cstring>
#include <algorithm>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <utility>
#include <iomanip>
#include "Item.h"
using namespace std;
//DFA的边
struct GOTO
{
int from;
int to;
string path;
GOTO(int s,string p,int t)
{
from=s;
path=p;
to=t;
}
};
//DFA中状态
struct State
{
int id;//状态编号
set<Item>items;//项目集
};
/*一些操作符的重载*/
bool operator <(const State &x,const State &y)
{
return x.id<y.id;
}
bool operator ==(const State &x,const State &y)
{
return x.id==y.id;
}
bool operator <(const GOTO &x,const GOTO &y)
{
if(x.from<y.from)
return true;
if(x.from==y.from && x.path<y.path)
return true;
if(x.from==y.from && x.path==y.path && x.to<y.to)
return true;
}
bool operator <(Item x,Item y)
{
return x.getItem()<y.getItem();
}
bool operator ==(const GOTO &x,const GOTO &y)
{
return x.from==y.from && x.path==y.path && x.to==y.to;
}
//两个项目集相等
bool operator ==(const set<Item> &x,const set<Item> &y)
{
auto it1=x.begin();
auto it2=y.begin();
for(; it1!=x.end(),it2!=y.end(); it1++,it2++)
{
Item a=*it1;
Item b=*it2;
if(a==b&&a.getSymbol()==b.getSymbol())
continue;
//有一个项目不相等,两项目集一定不相等
else
return false;
}
return true;
}
class LR1
{
private:
int number=0;
vector<string>T;//终结符号集合
vector<string>NT;//非终结符号集合
string S;//开始符号
map<string,vector<string>>production;//产生式
map<string,int>numPro;//编号的产生式集合,用于规约
vector<State>States;//状态集合
vector<GOTO>GO;//转换函数
map<string,set<string>>FIRST;//FIRST集
map<string,set<string>>FOLLOW;//FOLLOW集
map<pair<int,string>