算符优先系列之(二)算符优先关系表
Time Limit: 1000 ms Memory Limit: 65536 KiB
Problem Description
学过编译原理的菊苣们都知道算符优先文法,作为一个有点深度的分析方法,我们怎么能只止步于理论呢,实践才是王道哦。
已知文法G[S]的表达式,求算符优先关系表。因为某些特殊的原因,我们在这规定一下输入输出格式。
已知文法G[S]为:
S`->#S#(拓展文法,不是题目给出的文法)
S->a|@|(T)
T->T,S|S
表达式算符优先关系表中从左到右(从上到下)的顺序为从表达式文法中从左到右从上到下的顺序即为
a @ ( ) , # (我们默认把#放在最右边或者最下边)
Input
多组输入。第一行输入一个n,表示表达式的个数,接下来n行每行一个表达式(记得还有一个要在自己程序中添加的拓展文法哦)
Output
根据上述的格式,输出算符优先关系表(输出的格式用水平制表符\t)
Sample Input
2
S->a|@|(T)
T->T,S|S
Sample Output
a @ ( ) , #
a > > >
@ > > >
( < < < = <
) > > >
, < < < > >
# < < < =
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <set>
#include <queue>
#include <stack>
#include <map>
#include <bitset>
#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long LL;
const int inf=0x3f3f3f3f;
const double pi= acos(-1.0);
const double esp=1e-6;
using namespace std;
const int Maxn=110;
const int maxn=20;
char str[maxn][Maxn];//输入文法
char st[maxn];//输入串
char stac[maxn];//模拟栈的数组
char nstr[maxn][maxn];//储存转化文法
char mstr[maxn][maxn];
char fin[maxn];//存储终结符
char firstvt[maxn][maxn],lastvt[maxn][maxn];
char cmp[maxn][maxn];//存储表中的比较符
int firstflag[maxn],lastflag[maxn];//非终结符的firstvt,lastvt是否求出
int fcnt[maxn],lcnt[maxn];//非终结符firsvt和lastvt的个数
int is_fin(char c) {
//判断终结符
for(int i=0; fin[i]!='\0'; i++) {
if(fin[i]==c)
return 1;
}
return 0;
}
int site(char c) {
//求在表中的下标
for(int i=0; fin[i]!='\0'; i++) {
if(fin[i]==c)
return i;
}
}
void get_firstvt(char s,int t) {
//求s非终结符的firstvt值
int i,j,ii,jj,tt;
for(i=0; i<t; i++) {
if(str[i][0]==s)
break;
}
if(!firstflag[i]) {
int k=fcnt[i];
for(j=0; str[i][j]!='\0'; j++) {
if(j==2||str[i][j]=='|') {
if(is_fin(str[i][j+1])) {
firstvt[i][k++]=str[i][j+1];
} else {
if(is_fin(str[i][j+2])) {
firstvt[i][k++]=str[i][j+2];
}
if(str[i][j+1]!=s) {
get_firstvt(str[i][j+1],t);
for(ii=0; ii<t; ii++)