【编译原理与技术】第2关:LL parser

第2关:LL parser

本关任务:用C/C++编写一个LL(1)解析器

相关知识

为了完成本关任务,你需要掌握:

  1. LL文法
  2. C/C++ 编程语言基础
  3. C语言的基本结构知识

LL(1)解析器

在创建解析器之前,你应该创建一个下面文法的LL(1)分析表。

C/C++

本实训涉及函数、结构体,标准流输入输出,字符串等操作

实验要求

实验文法定义

program -> compoundstmt  
stmt ->  ifstmt  |  whilestmt  |  assgstmt  |  compoundstmt 
compoundstmt ->  { stmts }  
stmts ->  stmt stmts   |   E  
ifstmt ->  if ( boolexpr ) then stmt else stmt  
whilestmt ->  while ( boolexpr ) stmt  
assgstmt ->  ID = arithexpr ; 
boolexpr  ->  arithexpr boolop arithexpr  
boolop ->   <  |  >  |  <=  |  >=  | ==  
arithexpr  ->  multexpr arithexprprime  
arithexprprime ->  + multexpr arithexprprime  |  - multexpr arithexprprime  |   E  
multexpr ->  simpleexpr  multexprprime  
multexprprime ->  * simpleexpr multexprprime  |  / simpleexpr multexprprime  |   E  
simpleexpr ->  ID  |  NUM  |  ( arithexpr )  

起始符

Program

保留字

{ }  if ( ) then else  while ( )  ID =   > < >= <= ==  + -  * /  ID NUM  E 是'空'  

分隔方式

同一行的输入字符用一个空格字符分隔,例如: ID = NUM ; 红色标记为空格

错误处理

本实验需要考虑错误处理,如果程序不正确(包含语法错误),它应该打印语法错误消息(与行号一起),并且程序应该修正错误,并继续解析。
例如:

语法错误,第4行,缺少";"  

输入

要求:在同一行中每个输入字符用一个空格字符分隔,无其余无关符号。

样例1输入

{  ID = NUM ;  }    

样例2输入

{   If E1   then  s1  else  If E2  Then  S2  else   S3  }

并没有E1,E2等符号,这只是指代表达式

输出

样例1输出

输出要求:在语法树同一层的叶子节点,在以下格式中有相同的缩进,用tab来控制缩减。如样例所示,相同颜色表示在语法树种他们在同一层。
img

测试集

1

测试输入
{  ID = NUM ; }
预期输出
program
	compoundstmt
		{
		stmts
			stmt
				assgstmt
					ID
					=
					arithexpr
						multexpr
							simpleexpr
								NUM
							multexprprime
								E
						arithexprprime
							E
					;
			stmts
				E
		}

2

测试输入
{ 
ID = ID + NUM ; 
}
预期输出
program
	compoundstmt
		{
		stmts
			stmt
				assgstmt
					ID
					=
					arithexpr
						multexpr
							simpleexpr
								ID
							multexprprime
								E
						arithexprprime
							+
							multexpr
								simpleexpr
									NUM
								multexprprime
									E
							arithexprprime
								E
					;
			stmts
				E
		}

3

测试输入
{
while ( ID == NUM ) 
{ 
ID = NUM 
}
}
预期输出
语法错误,第4行,缺少";"
program
	compoundstmt
		{
		stmts
			stmt
				whilestmt
					while
					(
					boolexpr
						arithexpr
							multexpr
								simpleexpr
									ID
								multexprprime
									E
							arithexprprime
								E
						boolop
							==
						arithexpr
							multexpr
								simpleexpr
									NUM
								multexprprime
									E
							arithexprprime
								E
					)
					stmt
						compoundstmt
							{
							stmts
								stmt
									assgstmt
										ID
										=
										arithexpr
											multexpr
												simpleexpr
													NUM
												multexprprime
													E
											arithexprprime
												E
										;
								stmts
									E
							}
			stmts
				E
		}

4

测试输入
{
if ( ID == ID )
then
ID = NUM ;
else
ID = ID * NUM ;
}
预期输出
program
	compoundstmt
		{
		stmts
			stmt
				ifstmt
					if
					(
					boolexpr
						arithexpr
							multexpr
								simpleexpr
									ID
								multexprprime
									E
							arithexprprime
								E
						boolop
							==
						arithexpr
							multexpr
								simpleexpr
									ID
								multexprprime
									E
							arithexprprime
								E
					)
					then
					stmt
						assgstmt
							ID
							=
							arithexpr
								multexpr
									simpleexpr
										NUM
									multexprprime
										E
								arithexprprime
									E
							;
					else
					stmt
						assgstmt
							ID
							=
							arithexpr
								multexpr
									simpleexpr
										ID
									multexprprime
										*
										simpleexpr
											NUM
										multexprprime
											E
								arithexprprime
									E
							;
			stmts
				E
		}

代码文件

LLparser.h

// C语言词法分析器
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <string>
#include <fstream>
#include <sstream>
#include <vector>
using namespace std;
/* 不要修改这个标准输入函数 */
void read_prog(string& prog)
{
    char c;
    while(scanf("%c",&c)!=EOF){
        prog += c;
    }
}
/* 你可以添加其他函数 */

void Analysis()
{
    string prog;
    read_prog(prog);
    /* 请开始 */
    /********* Begin *********/


    /********* End *********/

}

LLparserMain

#include "LLparser.h"
int main()
{
    Analysis();
    return 0;
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
编译原理及实现第二版》是由孙悦红编写的一本介绍编译原理的经典教材。该教材中附录d部分包含了编译器的实现代码。 在这部分代码中,主要涵盖了语法分析器(parser)的实现。语法分析器是编译器中的重要组成部分,其作用是将源代码解析为一个抽象语法树(AST),以便后续的语义分析和代码生成等操作。 具体而言,附录d代码实现了基于自顶向下的递归下降分析方法来构建语法分析器。该方法的核心思想是通过一系列的语法产生式规则来逐步解析源代码,直到构建出一个完整的语法树。 在这份代码中,首先定义了一系列的语法产生式规则,每条规则用函数来表示。这些函数相互调用,按照规则的顺序递归地解析源代码。每个函数的返回值即为解析得到的语法树的一个子树。 此外,代码还包括了一些辅助函数,用于处理语法分析中的一些细节问题,如处理终结符(terminal)和非终结符(non-terminal)、错误处理等。 通过实现附录d代码中的语法分析器,读者可以深入理解编译器中的语法分析过程,并在此基础上进行进一步的优化和扩展。 总结来说,编译原理及实现第二版孙悦红附录代码d是一个实现了基于自顶向下的递归下降分析方法的语法分析器的代码。通过学习和理解这部分代码,读者可以加深对编译原理中语法分析的理解,并为编写自己的编译器打下基础。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值