编译原理实验(二)由上向下分析-预测分析--构造First和Follow


前言

编译原理上机实验系列(2)

一、预习准备

1.实验目的

掌握First和Follow集合求法,深入理解LL(1)文法分析过程。

2.实验要求

根据文法求出First和Follow集合

3.实验环境

Xcode

二、实验过程

1.实验步骤

(1)First集

假设 α–>tβ,求FIRST(α):
①如果首符号t是终结符则直接放入first集合中
②如果t不是非终结符:
如果t—>ε,则将ε加入FIRST(α),并且将β进行①②操作判断。
如果t不是ε,那么求first(t),并将first(t)的结果放入FIRST(α)中。

(2)Follow集

假设A -->αBβ,A -->αB,求follow(B):
①如果A是开始符, 将加入$ FOLLOW(A), 这里$是输入结束标记.
②对于A -->αBβ,首先判断β是否可以推出ε
如果不能推出ε,那么将first(β)加入follow(B)
如果推出ε,那么将{ first(β)- ε } U follow(A)加入follow(B)
③对于A -->αB,将follow(A)结果加入follow(B)。

2.实验数据记录

待输入数据:

4
S->ABC
A->a
B->@
C->c

预期结果:

first[S] = a 
first[A] = a 
first[B] = @ 
first[C] = c 
follow[S] = # 
follow[A] = c 
follow[B] = c 
follow[C] = # 

3.实验代码

在文法G[S]中使用’@’代表空。

//
//  main.cpp
//  exam2
//
//  Created by 小仙女 on 2021/6/7.
//

#include <cstdio>
#include <cstring>
#include <map>
#include <stack>
#include <algorithm>
 
using namespace std;
 
struct node
{
   
    char s1[50], s2[50];
}g[100];
 
char s[110];
int num;
map<char, int> Map1;
map<char, int> Map2;
 
int Map1_num;
int Map2_num;
 
char s1[110], s2[110];
 
int first[50][50];
int follow[50][50];
 
int vis[110];
int acc[50];
 
int select1[100][50];
 
int c[50][50];
char sta[110];
 
int cnt;
 
void dfs1(char ch, int dis[])
{
   
    if(acc[Map1[ch]])
    {
   
        for(int i = 1;i<=Map2_num;i++)
        {
   
            dis[i] = first[Map1[ch]][i];
        }
        return;
    }
    int value[20];
    memset(value
  • 4
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值