编译原理语法分析之LR分析

这篇博客探讨了编译原理中的LR分析方法,详细介绍了如何构造识别活前缀的DFA,生成LR分析表,并通过具体示例展示了如何分析算术表达式的正确性。此外,还提到了程序实现,包括从text文件导入数据和文件格式。
摘要由CSDN通过智能技术生成

要求:

(1)根据给定文法,先对文法进行解析,构造识别活前缀的DFA并输出;
(2)根据DFA构造LR分析表并输出;
(3)分析给定表达式是否是该文法识别的正确的算术表达式(要求输出归约过程)
(4)假如给定表达式文法为:

G(E’): E’→#E#
E→E+T | T
T→T*F |F
F→(E)|i

分析的句子可为:

(i+i)*i和i+i)*i

程序实现text文件导入数据,文件格式:
在这里插入图片描述

源码:

// 编译原理实验三_思路二.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include<stdio.h>
#include<malloc.h>
#include<iostream>
#include<string>
using namespace std;

#define PROJECT_SET_SIZE 20   
#define PROJECT_ID_POS 0
#define GRAMMER_ID_POS 1
#define GRAMMER_START_CHAR_POS 5
#define BFCHAR_POS 2
#define AFCHAR_POS 3
#define PROJECT_LEN_POS 4
#define ID 10000
#pragma warning(disable:4996)
char G[20][30];
int r;

struct grammer
{
   
	char** g;
	char vt[10];
	char vn[10];
	char s;
	int line;
};

typedef struct
{
   
	int id;
	char ch;
}SElemType;


struct gprjt
{
   
	char** gp;
	char s;
	int line;
};

typedef struct prjset
{
   
	int id;
	struct prjset* next;
	char prjt[PROJECT_SET_SIZE + 1];
	char pointafter[PROJECT_SET_SIZE + 1];
	struct prjset* actorgo[PROJECT_SET_SIZE];
	char pointbefore;
}prjset, * pprjset;

struct head
{
   
	prjset* I;
	prjset* tail;
	int size;
};


char DOT = '~';
grammer g;
gprjt project;
head root;
void Input();
int OpenFile(char* s);
void CreateProjectSet();
void Closure(prjset* prjset);
int go(int rk, prjset* prjset);
void PrintPojectSet();
int IsInSet(char* s, char ch);
void JoinSet(char* s, char ch);

#define MAX 50
static int Number = 0;

static char zhongjiefuhao[20];
static int nzhongjie = 0;

static char feizhongjiefuhao[20];
static char nfeizhongjie = 0;

static int numP = 0;

static int numafter[50] ;

static int LRlen = 0;

void delesample() {
   
	for (int i = 0; i < nzhongjie; i++) {
   
		for (int j = i + 1; j < nzhongjie; j++) {
   
			if (zhongjiefuhao[i] == zhongjiefuhao[j]) {
   
				
				for (int k = j; k < nzhongjie; k++) {
   
					zhongjiefuhao[k]=zhongjiefuhao[k+1];
				}
				nzhongjie--;
			}
		}
	}

	for (int i = 0; i <= nfeizhongjie; i++) {
   
		for (int j = i + 1; j <= nfeizhongjie; j++) {
   
			if (feizhongjiefuhao[i] == feizhongjiefuhao[j]) {
   

				for (int k = j; k < nfeizhongjie; k++) {
   
					feizhongjiefuhao[k]=feizhongjiefuhao[k+1];
				}
				nfeizhongjie--;

			}
		}
	}

}


string LR_table[20][20];

void LR() {
   
	for (int i = 0; i < 20; i++) {
   
		for (int j = 0; j < 20; j++) {
   
			LR_table[i][j] = "";
		}
	}

	for (int i = 0; i <= Number; i++) {
   
		for (int j = 0; G[i][j] != '#'; j++) {
   
			if ((G[i][j] < 'A' || G[i][j]>'Z') && G[i][j] != '|' && G[i][j] != '-' && G[i][j] != '>') {
   
				zhongjiefuhao[nzhongjie++] = G[i][j];
			}
			if (G[i][j] >= 'A' && G[i][j] <= 'Z') {
   
				feizhongjiefuhao[nfeizhongjie++] = G[i][j];
			}
		}
	}

	for (int i = 0; i < 50; i++) {
   
		delesample();
	}

	int j = 0;
	for (int i = 1; i <= nzhongjie + 1; i++) {
   
		LR_table[0][i] = zhongjiefuhao[i - 1];
		j = i;
	}

	LR_table[0][j++] = '#';
	int num_jianghao = j - 1;

	for (j; j <= nzhongjie + nfeizhongjie + 2; j++) {
   
		LR_table[0][j] = feizhongjiefuhao[j - nzhongjie - 2];
	}

	LRlen = j;

	for (int i = 1; i <= numP; i++) {
   
		LR_table[i][0] = "I" + std::to_string(i-1);
	}

	pprjset p;
	p = root.I;

	for (int i = 0; i <= p->pointafter[0]; i++) {
   
		if (p->pointafter[i] == 'E') {
   
			//cout << "=======================================" << i;
			LR_table[i+1][num_jianghao] = "acc";
		}
	}

	int num = 0;
	while (p) {
   
		for (int i = 0; i <= p->pointafter[0]; i++) {
   
			/*if(p->pointafter[i]==)*/
			for (int j = 0; j < nzhongjie; j++) {
   
				if ((p->pointafter[i] == zhongjiefuhao[j])) 
  • 1
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值