编译原理自顶向下语法分析方法的实现Word格式文档下载.docx
《编译原理自顶向下语法分析方法的实现Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《编译原理自顶向下语法分析方法的实现Word格式文档下载.docx(14页珍藏版)》请在冰豆网上搜索。
2.运用编程的手段实现自顶向下语法分析。
三、实验内容和要求
四、实验环境
1.硬件环境:
PC机
2.软件环境:
Windows操作系统,VC++集成开发环境
五、算法设计思想
六、主要问题与解决方法
七、实验结果
以下是程序的用户运行界面截图:
八、体会、质疑、建议
九、源代码
#include<
fstream>
sstream>
iostream>
vector>
string>
windows.h>
iterator>
algorithm>
stdlib.h>
#defineM20
usingnamespacestd;
voidgotoxy(intx,inty){
COORDcoord;
coord.X=x;
coord.Y=y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),coord);
}
classtable
{
public:
voidread();
friendclassan_process;
stringfind_str(charrow,charcol);
private:
stringmatrix[M][M];
charstart;
vector<
vec_row;
vec_col;
};
classsentence
voidread_in();
stringstr_input;
classan_process
voidanalyse(consttableLL1_table,constsentenceinput);
voidwrite_result();
voidprint2(intx,inty);
int>
step;
vector<
char>
>
stack_note;
remain;
production;
voidtable:
:
read()
stringa,str;
charx,y;
intlinenum=0;
charline[1024]={0};
ifstreaminfile("
table1.txt"
);
if(!
infile)
{
cerr<
<
"
错误!
无法存储用户数据!
\n"
;
}
while(in(line,sizeof(line)))
stringstreamword(line);
if(linenum>
1)
{
if(word>
>
x&
&
word>
y&
str)
{
matrix[x-48][y-48]=str;
/*cout<
x<
"
cout<
y<
matrix[x-48][y-48]<
*/
}
}
while(word>
a&
linenum<
2)
if(linenum==0)
vec_col.push_back(a);
a<
elseif(linenum==1)
vec_row.push_back(a);
/*cout<
endl;
linenum++;
start=vec_col[0][0];
/*for(inticnt=0;
icnt<
vec_col.size();
icnt++)
cout<
vec_col[icnt]<
}*/
stringtable:
find_str(charrow,charcol)
for(intx=0;
x++)
for(inty=0;
vec_row.size();
y++)
if(row==vec_col[x][0]&
col==vec_row[y][0])
returnmatrix[x+1][y+1];
return"
voidan_process:
analyse(tableLL1_table,sentenceinput)
inticnt=0,jcnt=0,kcnt;
stringac,prod="
re="
stack;
charNow_word=input.str_input[icnt];
charNOW_state=LL1_table.start;
string:
const_iteratorEiter;
const_iteratorBiter;
stack.push_back('
#'
stack.push_back(NOW_state);
for(;
input.str_input.size();
)
step.push_back(++jcnt);
stack_note.push_back(stack);
for(kcnt=icnt;
kcnt<
kcnt++)
re=re+input.str_input[kcnt];
re=re+"
#"
remain.push_back(re);
re.erase();
Now_word=input.str_input[icnt];
NOW_state=stack.back();
if(NOW_state==Now_word)
stack.erase(stack.end()-1);
icnt++;
production.push_back("
(匹配)"
else
ac=LL1_table.find_str(NOW_state,Now_word);
Biter=ac.begin();
Biter--;
Eiter=ac.end();
Eiter--;
if(ac=="
ε"
prod=prod+NOW_state;
prod=prod+"
→"
stack.erase(stack.end()-1);
production.push_back(prod);
prod.erase();
elseif(ac!
="
prod.empty();
+ac;
Eiter!
=Biter;
Eiter--)
{
stack.push_back(*Eiter);
}
分析失败\n"
exit(0);
if(stack.size()!
=1||stack.back()!
='
cout<
exit(0);
write_result()
step.push_back(step.back()+1);
a;
a.push_back('
stack_note.push_back(a);
remain.push_back("
production.push_back("
接受"
gotoxy(27,1);
输入串"
'
remain[0];
\b"
的分析过程"
endl<
print2(step.size()+1,4);
const_iteratoriter1=step.begin();
const_iteratoriter2=stack_note.begin();
const_iteratoriter3=remain.begin();
const_iteratoriter4=production.begin();
gotoxy(8,4);
步骤"
gotoxy(27,4);
分析栈"
gotoxy(44,4);
剩余输入串"
gotoxy(63,4);
所用产生式"
for(intxy=0;
iter1!
=step.end();
iter1++)
gotoxy(9,xy+6);
(*iter1);
xy=xy+2;
for(xy=0;
iter2!
=stack_note.end();
iter2++)
gotoxy(27,xy+6);
for(inticnt=0;
(*iter2).size();
cout<
(*iter2)[icnt];
iter3!
=remain.end();
iter3++)
gotoxy(44,xy+6);
(*iter3);
iter4!
=production.end();
iter4++)
gotoxy(63,xy+6);
(*iter4);
print2(intx,inty)//简易画线代码,呵呵,画行直线
inti,j;
y-1;
for(i=0;
i<
37/y;
i++)
printf("
─"
printf("
┬"
putchar(10);
for(icnt=0;
│"
for(j=0;
j<
x-1;
j++)//画5行直线
printf("
┼"
putchar(10);
┴"
voidsentence:
read_in()
请输入需要分析的句子:
cin>
str_input;
system("
cls"
voidmain()
tableLL1_table;
sentenceinput;
an_processA;
input.read_in();
LL1_table.read();
A.analyse(LL1_table,input);
A.write_result();