【编译原理】SLR(1)分析方法(c++实现)

基本流程

Created with Raphaël 2.2.0 输入文法 拓广文法 构造DFA (识别活前缀的自动机) SLR(1)分析表 SLR(1)分析输入串

当然也要求非终极符号的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();<
评论 21
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值