要求:
(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]))