基本流程
当然也要求非终极符号的FIRST和FOLLOW集,这个步骤在构建分析表之前做就好,没什么固定顺序
算法
来自龙书第二版
代码
代码和上一篇【编译原理】LR(0)分析方法(c++实现)有很多重复的部分,区别主要在构建SLR(1)分析表部分加入了FOLLOW集的使用,以及修复了部分bug
Item类
和前文没有区别
#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;
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;
if(right.find(".")==string::npos)
addDot(0);
}
Item(string l,string r)
{
id=count++;
left=l;
right=r;
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;
}
//找点的位置
int getDot(string item)
{
return item.find(".");
}
//给文法加点
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;
SLR(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 Item &x,const Item &y)
{
return x.id<y.id;
}
bool operator <(const GOTO &x,const GOTO &y)
{
return x.from<y.from;
}
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();<