实验目的:
针对给定的上下文无关文法,编制一个递归下降分析程序。
分析:
- 递归下降语法分析的前提是保证LL(1)文法
递归下降的思路就是暴力dfs。对每个程序直接不管三七二十一搜进去,只要能搜到就继续搜。搜不到就return搜其他的。
先看文法:
① : lexp->atom|list
② :atom->number|identifier
③ :list->(lexp-seq)
④ :lexp-seq->lexp-seq lexp|lexp
很明显左递归,先消除一下
① : lexp->atom|list
② :atom->number|identifier
③ :list->(lexp-seq)
④ :lexp-seq->lexp lexp-seq’
⑤ : lexp-seq’->lexp lexp-seq’|ε
然后直接对input字符流开始递归调用。
#include<stdio.h>
#include<cstdlib>
#include<string.h>
#define maxn 1000
using namespace std;
int idx=0;
char str[maxn];
int lexp();
void atom();
void list();
void lexp_sep();
void lexp_sep_();
int lexp(){
if( (str[idx]>='0'&&str[idx]<='9')||(str[idx]>='a'&&str[idx]<='z') ){
printf("lexp->atom\n");
atom();
return 1;
}
else if(str[idx]=='('){
printf("lexp->list\n");
list();
return 1;
}
else return 0;
}
void atom(){
if(str[idx]>='0'&&str[idx]<='9'){
printf("atom->number\n");
idx++;
}
else if(str[idx]>='a'&&str[idx]<='z'){
printf("atom->identifier\n");
idx++;
}
}
void list(){
if(str[idx]!='('){
printf("list()error!\n");
exit(-1);
}
printf("list->(lexp_seq)\n");
idx++;
lexp_sep();
if(str[idx]!=')'){
printf("list()error!\n");
exit(-1);
}
idx++;
}
void lexp_sep(){
printf("lexp_sep->lexp lexp_seq'\n");
lexp();
lexp_sep_();
}
void lexp_sep_(){
if(lexp()==1){
lexp_sep_();
}
else return;
}
int main(int argc,char* argv[]){
scanf("%s",str);
int len=strlen(str);
str[len++]='$';str[len]='\0';
lexp();
if(str[idx]=='$') printf("accept!\n");
else printf("wrong answer!\n");
return 0;
}
测试数据:
(a(b(2))c)
输出结果:
lexp->list
list->(lexp_seq)
lexp_sep->lexp lexp_seq'
lexp->atom
atom->identifier
lexp->list
list->(lexp_seq)
lexp_sep->lexp lexp_seq'
lexp->atom
atom->identifier
lexp->list
list->(lexp_seq)
lexp_sep->lexp lexp_seq'
lexp->atom
atom->number
lexp->atom
atom->identifier
accept!
可以看到匹配是能成功的,但是对于想要我们输出该语法树就存在问题了。
经过思考发现,直接递归下降分析是无法完成语法树的构建输出的,虽然能匹配正确结果。
原因在于其需要判定递归有效才会输出,因此在决定是否进入的时候不能保证输出此次进去还是不进去。单纯一次递归有效可以用栈判定,但是多次就不行了。
因此需要构建first集合和follow集合来构建预测分析表。
当然cyc给每个token额外加了括号来直接dfs搜保证了正确的输出。在这里膜一下
#include<iostream>
#include<stack>
#include<algorithm>
#include<string>
using namespace std;
stack<string>input;
stack<string>st;
bool number(const string& s){
for(int i=0;i<(int)s.size();i++){
if(!(s[i]>='0'&&s[i]<='9')) return false;
}
return true;
}
bool identifier(const string& s){
for(int i=0;i<(int)s.size();i++){
if(!(s[i]>='a'&&s[i]<='z')) return false;
}
return true;
}
int main(){
input.push("$");
string s;cin>>s;
for(int i=(int)s.size()-1;i>=0;i--){
string tmp;tmp+=s[i];
input.push(tmp);
}
st.push("$");
st.push("lexp");
while(st.size()>1){
string now=st.top();st.pop();
string cnt=input.top();
if(now=="lexp"){
if(cnt=="("){
cout<<"lexp -> list"<<endl;
st.push("list");
}
else if(number(cnt)){
cout<<"lexp -> atom"<<endl;
st.push("atom");
}
else if(identifier(cnt)){
cout<<"lexp -> atom"<<endl;
st.push("atom");
}
else{
cout<<"lexp()->next error!"<<endl;
exit(-1);
}
}
else if(now=="atom"){
if(number(cnt)){
cout<<"atom -> number"<<endl;
input.pop();
}
else if(identifier(cnt)){
cout<<"atom -> identifier"<<endl;
input.pop();
}
else{
cout<<"atom()->next error!"<<endl;
exit(-1);
}
}
else if(now=="list"){
if(cnt=="("){
cout<<"list -> ( lexp-seq )"<<endl;
input.pop();
st.push(")");
st.push("lexp-seq");
}
else{
cout<<"list()-> next errpr!"<<endl;
exit(-1);
}
}
else if(now=="lexp-seq"){
if(cnt=="("||number(cnt)||identifier(cnt)){
cout<<"lexp-seq -> lexp lexp-seq'"<<endl;
st.push("lexp-seq'");
st.push("lexp");
}
else{
cout<<"lexp-seq()->next error!"<<endl;
exit(-1);
}
}
else if(now=="lexp-seq'"){
if(cnt=="("||number(cnt)||identifier(cnt)){
cout<<"lexp-seq' -> lexp lexp-seq'"<<endl;
st.push("lexp-seq'");
st.push("lexp");
}
else{
cout<<"lexp-seq' -> NULL"<<endl;
}
}
else if(now==")"&&cnt==")"){
input.pop();
}
else{
cout<<"Wrong answer"<<endl;
exit(-1);
}
}
cout<<"Accept!"<<endl;
}
输入:
(a(b(2))c)
python生成图片: