栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

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

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【编译原理与技术】第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来控制缩减。如样例所示,相同颜色表示在语法树种他们在同一层。

测试集 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 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;

void read_prog(string& prog)
{
    char c;
    while(scanf("%c",&c)!=EOF){
        prog += c;
    }
}


void Analysis()
{
    string prog;
    read_prog(prog);
    
    


    

}
LLparserMain
#include "LLparser.h"
int main()
{
    Analysis();
    return 0;
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/836063.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号