实验内容
1、算术表达式文法:
VN = { E, T, E’, F, T’ }
其中:E为开始符号;
VT = { +, -, , /, (, ), id, num }
其中:id代表标识符,要求是长度不超过10的字母序列;num代表常数,要求是正整数;
文法规则集如下:
E→TE’
E’→+TE’| -TE’ |ε
T→FT’
T’→FT’| /FT’ |ε
F→(E) | id | num
2、实验步骤
(1)按照文法求出每个非终结符的FIRST和FOLLOW集(手工计算并在实验报告中抄写即可,不要求程序实现);
(2)针对给出的表达式文法,构造递归下降分析程序;
3、程序功能要求
(1)程序通过标准输入按行读取用户输入,表达式在1行内读完。
(2)程序对用户输入的内容首先进行词法分析处理(可以复用实验一的部分代码,由于词法规则更简单,可以大大简化),词法分析得到的词法单位对应文法中的终结符。
(3)对于用户输入的表达式,如果经过分析后语法正确,给出相应提示。如果分析过程中遇到错误不需要尝试恢复分析,停止该次分析过程即可,但应尽量给出说明性较强的错误提示。
下面给出一些验证语法分析结果正确性的测试用例:
正确:a+3*( b + c/10) -4 +5
错误:a+3 ( b + c/10)
错误:(1+2)(3-4)
错误:(1+2
#include <bits/stdc++.h>
using namespace std;
const int N=1e5+10;
string s1[11]={"begin","end","if","then","while","do","const","var","call","procedure","odd"};
string s2[14]={"+","-","*","/","=","#","<",">",":=","(",")",",",".",";"};
string str1;
int d1=0,len=11,len2=14,x=0;
vector<string>G;
struct node
{
string type;
string value;
};
vector<node>G1;
bool E();
bool E1();
bool T();
bool T1();
bool F();
bool isao(int i)
{
//cout<<"NOW+"<<i<<endl;
for(int j=0;j<len2;j++)
{
if(str1[i]==s2[j][0]){
return 1;
}
}
return 0;
}
int main()
{
#ifdef local
freopen("D://r.txt","r",stdin);
#endif
string str;
int h=0,cc=0;
while(getline(cin,str)){
h++;
stringstream ss(str);
while(ss>>str1){
int i=0,len1=str1.size();
int p1=0;
if(str1[i]=='/'&&i+1<len1&&str1[i+1]=='/'){
break;
}
while(i<len1){
while(d1&&i<len1){
if(str[i]=='*'&&i+1<len1&&str1[i+1]==')'){
d1=0;
i+=2;
break;
}
i++;
}
if(i>=len1) break;
if(isalpha(str1[i])){
if(d1) {
i++;
continue;
}
string temp;
while(i<len1&&isalpha(str1[i]))
{
temp+=str1[i];
if(i==len1-1)
for(int j=0;j<len;j++){
if(temp==s1[j]){
cout<<"("<<temp<<",)"<<endl;
G1.push_back({"word",temp});
temp="";
}
}
i++;
}
if(temp!=""){
cout<<"(标识符,";
int k=0;
for(k=0;k<G.size();k++){
if(G[k]==temp){
break;
}
}
if(k==G.size()){
G.push_back(temp);
cout<<G.size()-1;
}
else {
cout<<k;
}
G1.push_back({"id",temp});
cout<<")"<<endl;
}
}
else if(isdigit(str1[i])){
if(d1) {
i++;
continue;
}
string temp;
for(i;i<len1;i++){
if(isdigit(str1[i]))
{
temp+=str1[i];
}
else {
if(isalpha(str1[i]))
{
cout<<"ERROR"<<endl;
return 0;
}
else
break;
}
}
if(temp!=" ")
{
cout<<"(常数,"<<temp<<")"<<endl;
G1.push_back({"num",temp});
}
}
else if(isao(i)){
if(str1[i]=='('&&i+1<len1&&str1[i+1]=='*'){
d1=1;
}
if(d1){
i++;
continue;
}
if(str1[i]==':'){
if(i+1<len1&&str1[i+1]=='='){
cout<<"(:=,)"<< endl;
G1.push_back({"fu",":="});
}
else {
cout<<"ERROR"<<endl;
}
i+=2;
}
else{
for(int j=0;j<len2;j++){
if(str1[i]==s2[j][0]){
cout<<"("<<str1[i]<<",)"<<endl;
string te1;
te1+=str1[i];
G1.push_back({"fu",te1});
}
}
i++;
}
}
else{
if(d1){
continue;
}
cout<<"ERROR"<<endl;
return 0;
}
}
}
}
for(int i=0;i<G1.size();i++){
node t=G1[i];
cout<<t.type<<' '<<t.value<<endl;
}
x=0;
if(E()){
cout<<"RIGHT"<<endl;
}
else {
cout<<"ERROR"<<endl;
}
return 0;
}
bool E()
{
if(x>=G1.size()){
cout<<"e长度超限"<<endl;
return 0;
}
node t1=G1[x];
string type=t1.type;
string value=t1.value;
cout<<"e "<<value<<endl;
if(value=="("||type=="id"||type=="num"){
if(!T()){
return 0;
}
if(!E1()){
return 0;
}
}
else {
cout<<"匹配value时出错"<<endl;
return 0;
}
return 1;
}
bool E1()
{
if(x>G1.size()){
cout<<"e1长度超限"<<endl;
return 0;
}
if(x==G1.size()){
return 1;
}
node t1=G1[x];
string type=t1.type;
string value=t1.value;
cout<<"e1 "<<value<<endl;
if(value=="+"){
x++;
if(!T()){
return 0;
}
if(!E1()){
return 0;
}
}
else if(value=="-")
{
x++;
if(!T()){
return 0;
}
if(!E1()){
return 0;
}
}
else if(value==")"||value=="#"){
return 1;
}
else {
cout<<value<<"在E'处匹配失败"<<endl;
return 0;
}
return 1;
}
bool T()
{
if(x>=G1.size()){
cout<<"t长度超限"<<endl;
return 0;
}
node t1=G1[x];
string type=t1.type;
string value=t1.value;
cout<<"t "<<value<<endl;
if(value=="("||type=="id"||type=="num")
{
if(!F())
{
return 0;
}
if(!T1()){
return 0;
}
}
else {
cout<<value<<"在T处匹配失败"<<endl;
return 0;
}
return 1;
}
bool T1()
{
if(x>G1.size()){
cout<<"t1长度超限"<<endl;
return 0;
}
if(x==G1.size()){
return 1;
}
node t1=G1[x];
string type=t1.type;
string value=t1.value;
cout<<"t1 "<<value<<endl;
if(value=="*"||value=="/")
{
x++;
if(!F())
{
return 0;
}
if(!T1())
{
return 0;
}
}
else if(value=="+"||value=="-"||value==")")
{
return 1;
}
else{
cout<<value<<"在T'处匹配失败"<<endl;
return 0;
}
return 1;
}
bool F()
{
if(x>=G1.size()){
cout<<"f长度超限"<<endl;
return 0;
}
node t1=G1[x];
string type=t1.type;
string value=t1.value;
cout<<"f "<<value<<endl;
if(value=="(")
{
x++;
if(!E())
{
return 0;
}
if(x>=G1.size()){
cout<<"f长度超限"<<endl;
return 0;
}
node t2=G1[x];
if(t2.value!=")")
{
cout<<")在F处匹配失败"<<endl;
return 0;
}
else {
x++;
return 1;
}
}
else if(type=="id"||type=="num")
{
x++;
return 1;
}
else {
cout<<value<<"在F处匹配失败"<<endl;
return 0;
}
return 1;
}