当前位置:文档之家› 最新编译原理语法分析

最新编译原理语法分析

最新编译原理语法分析
最新编译原理语法分析

上机练习二:语法分析

一、根据上机练习一给出的PL/0语言扩充的巴克斯范式语法描述,利用递归下降的语法分析方法,编写PL/0语言的语法分析程序。

要求:

1.对给出的PL/0语言进行分析,证明其可以进行自上而下的语法分析;

2.对block、proc、statement、condition、expression、term、factor进行分析,画出语法

分析图,在此基础上描述这些子程序的设计思想;

3.具有一定的语法错误处理能力;

二、源代码

#include

#include

#include

#include

#include

using namespace std;

struct yufa

{

string SYM; //单词的类别

string strToken; //用户所定义的标识符的值

int l;//记录换行符的个数,即记录源文件的行数

}yufa0, yufa1, yufa2[1000];//yufa1用于保存各个单词,yufa1用于词法分析过程;yufa0主要用于语法分析;

char ch;

int mm= 0; //结构体数组下标

int line = 0;//出错位置

//char ch = ' ';

string key[15] = { "begin", "end", "if", "then", "else", "while", "write", "read", "do", "call", "const", "var", "procedure", "program", "odd" };//预设保留字

void prog();

void block();

void condecl();

void _const();

void vardecl();

void proc();

void body();

void statement();

void lexp();

void exp();

void term();

void factor();

void error0()

{

cout << "program标示符出错" << endl;

exit(0); //出错即退出

}

void error1()

{

cout << "第" << line << "行缺少分号" << endl;

}

void error2()

{

cout << "第" << line << "行标识符(变量名)出错" << endl; }

void error3()

{

cout << "第" << line << "常量未声明(缺少const)" << endl; }

void error4()

{

cout << "第" << line << "常量未赋值" << endl;

}

void error5()

{

cout << "第" << line << "赋值号出错或者缺少赋值号" << endl; }

void error6()

{

cout << "第" <

}

void error7()

{

cout << "第" << line << "行缺少右括号" << endl;

}

void error8()

{

cout << "第" << line << "行缺少左括号" << endl;

}

void error9()

{

cout << "第" << line << "行缺少参数" << endl;

}

void error10()

{

cout << "第" << line << "行procedure错误" << endl;

}

void error11()

{

cout << "第" << line << "行缺少end(begin与end不匹配)" << endl; }

void error12()

{

cout << "第" << line << "行缺少then(if与then不匹配)" << endl;

}

void error13()

{

cout << "第" << line << "行缺少do(while与do不匹配)" << endl; }

void error14()

{

cout << "第" << line << "行调用过程缺少标识符" << endl;

}

void error15()

{

cout << "第" << line << "行缺少分号" << endl;

}

void error16()

{

cout << "第" << line << "行条件语句出错(缺少界符)" << endl;

}

void prog()//程序的递归子程序实现

{

line++;

if (yufa0.SYM == "program")// program

{

mm++; yufa0.SYM = yufa2[mm].SYM;

if (yufa0.SYM == "biaoshifu") // ,即标识符

{

mm++; yufa0.SYM = yufa2[mm].SYM;

if (yufa0.SYM == ";")//;

{

mm++; yufa0.SYM = yufa2[mm].SYM;

block();

}

else error1();//缺少;

}

else error2();//缺少标识符或者标识符错误

}

else error0();

cout << "\n\n--------------------------语法分析结束------------------------\n\n" << endl;

}

void block()//程序体的递归子程序实现, →[][][] {

if (yufa0.SYM == "const")

condecl();

if (yufa0.SYM == "var")

vardecl();

if (yufa0.SYM == "procedure")

proc();

body();

}

void condecl()//常量说明的递归子程序实现, →const {,};

{

line++;

if (yufa0.SYM == "const") // const

{

mm++; yufa0.SYM = yufa2[mm].SYM;

_const();//

while (yufa0.SYM == ",")//,逗号

{

mm++; yufa0.SYM = yufa2[mm].SYM;

_const();

}//

if (yufa0.SYM == ";")//;

{

mm++; yufa0.SYM = yufa2[mm].SYM;

}

else error1();//缺少;

}

else error3();//缺少常量定义const

}

void _const()//常量,:=

{

if (yufa0.SYM == "biaoshifu") //

{

mm++; yufa0.SYM = yufa2[mm].SYM;

相关主题
文本预览
相关文档 最新文档