文档库 最新最全的文档下载
当前位置:文档库 › 语法分析报告

语法分析报告

语法分析报告
语法分析报告

语法分析

一、实验目的

编制一个递归下降分析程序,实现对词法分析程序所提供的单词序列的语法检查和结构分析。

二、实验要求

利用C语言编制递归下降分析程序,并对简单语言进行语法分析。

2.1 待分析的简单语言的语法

用扩充的BNF表示如下:

⑴<程序>::=begin<语句串>end

⑵<语句串>::=<语句>{;<语句>}

⑶<语句>::=<赋值语句>

⑷<赋值语句>::=ID:=<表达式>

⑸<表达式>::=<项>{+<项> | -<项>}

⑹<项>::=<因子>{*<因子> | /<因子>

⑺<因子>::=ID | NUM | (<表达式>)

2.2 实验要求说明

输入单词串,以“#”结束,如果是文法正确的句子,则输出成功信息,打印“success”,否则输出“error”。

例如:

输入begin a:=9; x:=2*3; b:=a+x end #

输出success!

输入x:=a+b*c end #

输出error

2.3 语法分析程序的酸法思想

(1)主程序示意图如图2-1所示。

图2-1 语法分析主程序示意图

(2)递归下降分析程序示意图如图2-2所示。 (3)语句串分析过程示意图如图2-3所示。

图2-3 语句串分析示意图

图2-2 递归下降分析程序示意图

(4)statement 语句分析程序流程如图2-4、2-5、2-6、2-7所示。

图2-4 statement语句分析函数示意图图2-5 expression表达式分析函数示意图

图2-7 factor分析过程示意图

三、语法分析程序的C语言程序源代码:

#include "stdio.h"

#include "string.h"

char prog[100],token[8],ch;

char *rwtab[6]={"begin","if","then","while","do","end"};

int syn,p,m,n,sum;

int kk;

factor();

expression();

yucu();

term();

statement();

lrparser();

scaner();

main()

{

p=kk=0;

printf("\nplease input a string (end with '#'): \n");

do

{ scanf("%c",&ch);

prog[p++]=ch;

}while(ch!='#');

p=0;

scaner();

lrparser();

getch();

}

lrparser()

{

if(syn==1)

{

scaner(); /*读下一个单词符号*/

yucu(); /*调用yucu()函数;*/

if (syn==6)

{ scaner();

if ((syn==0)&&(kk==0))

printf("success!\n");

}

else { if(kk!=1) printf("the string haven't got a 'end'!\n");

kk=1;

}

}

else { printf("haven't got a 'begin'!\n");

kk=1;

}

return;

}

yucu()

{

statement(); /*调用函数statement();*/

while(syn==26)

{

scaner(); /*读下一个单词符号*/

if(syn!=6)

statement(); /*调用函数statement();*/

}

return;

}

statement()

{ if(syn==10)

{

scaner(); /*读下一个单词符号*/

if(syn==18)

{ scaner(); /*读下一个单词符号*/ expression(); /*调用函数statement();*/ }

else { printf("the sing ':=' is wrong!\n");

kk=1;

}

}

else { printf("wrong sentence!\n");

kk=1;

}

return;

}

expression()

{ term();

while((syn==13)||(syn==14))

{ scaner(); /*读下一个单词符号*/ term(); /*调用函数term();*/

}

return;

}

term()

{ factor();

while((syn==15)||(syn==16))

{ scaner(); /*读下一个单词符号*/ factor(); /*调用函数factor(); */ }

return;

}

factor()

{ if((syn==10)||(syn==11)) scaner();

else if(syn==27)

{ scaner(); /*读下一个单词符号*/

expression(); /*调用函数statement();*/ if(syn==28)

scaner(); /*读下一个单词符号*/

else { printf("the error on '('\n");

kk=1;

}

}

else { printf("the expression error!\n");

kk=1;

}

return;

}

scaner()

{ sum=0;

for(m=0;m<8;m++)token[m++]=NULL;

m=0;

ch=prog[p++];

while(ch==' ')ch=prog[p++];

if(((ch<='z')&&(ch>='a'))||((ch<='Z')&&(ch>='A')))

{ while(((ch<='z')&&(ch>='a'))||((ch<='Z')&&(ch>='A'))||((ch>='0')&&(ch<='9'))) {token[m++]=ch;

ch=prog[p++];

}

p--;

syn=10;

token[m++]='\0';

for(n=0;n<6;n++)

if(strcmp(token,rwtab[n])==0)

{ syn=n+1;

break;

}

}

else if((ch>='0')&&(ch<='9'))

{ while((ch>='0')&&(ch<='9'))

{ sum=sum*10+ch-'0';

ch=prog[p++];

}

p--;

syn=11;

}

else switch(ch)

{ case '<':m=0;

ch=prog[p++];

if(ch=='>')

{ syn=21;

}

else if(ch=='=')

{ syn=22;

}

else

{ syn=20;

p--;

}

break;

case '>':m=0;

ch=prog[p++];

if(ch=='=')

{ syn=24;

}

else

{ syn=23;

p--;

}

break;

case ':':m=0;

ch=prog[p++];

if(ch=='=')

{ syn=18;

}

else

{ syn=17;

p--;

}

break;

case '+': syn=13; break;

case '-': syn=14; break;

case '*': syn=15;break;

case '/': syn=16;break;

case '(': syn=27;break;

case ')': syn=28;break;

case '=': syn=25;break;

case ';': syn=26;break;

case '#': syn=0;break;

default: syn=-1;break;

}

}

四、结果分析:

输入begin a:=9; x:=2*3; b:=a+x end # 后输出success!如图4-1所示:

图4-1

输入x:=a+b*c end # 后输出error 如图4-2所示:

图4-2

五、总结:

通过本次试验,了解了语法分析的运行过程,主程序大致流程为:“置初值”→调用scaner 函数读下一个单词符号→调用IrParse→结束。递归下降分析的大致流程为:“先判断是否为begin”→不是则“出错处理”,若是则“调用scaner函数”→调用语句串分析函数→“判断是否为end”→不是则“出错处理”,若是则调用scaner函数→“判断syn=0&&kk=0是否成立”成立则说明分析成功打印出来。不成立则“出错处理”。

大学英语语法--被动态-练习题

练习题 1) It is said that a new robot ____by him in a few days. A) designed B) has been designed C) will be designed D) will have been designed 2)We are late. I expect the film ____by the time we get to the cinema. A) will already have started B) would already have started C) shall have already started D) has already been started 3) She will stop showing off if no notice____ of her. A) is taken B) takes C) will be taken D) has taken 4) Diamond ____in Brazil in 1971. A) is found B) has been found

C) was found D) had been found 5)“Have you moved into the new flat?” Not yet. The room____.” A) has been painted B) is painted C) paints D) is being painted 6) My pictures ____until next Friday. A) won't develop B) aren't developed C) don't develop D) won' t be developed 7) Tim ____since he lost his job three weeks ago. A) had been unemployed B) was unemployed C) has been unemployed D) has unemployed 8) A great number of colleges and universities ____since 1949. A) has been establish B) have been established C) have established D) had been established

编译原理实验报告《LL(1)语法分析器构造》

《LL(1)分析器的构造》实验报告 一、实验名称 LL(1)分析器的构造 二、实验目的 设计、编制、调试一个LL(1)语法分析器,利用语法分析器对符号串的识别,加深对语法分析原理的理解。 三、实验内容和要求 设计并实现一个LL(1)语法分析器,实现对算术文法: G[E]:E->E+T|T T->T*F|F F->(E)|i 所定义的符号串进行识别,例如符号串i+i*i为文法所定义的句子,符号串ii+++*i+不是文法所定义的句子。 实验要求: 1、检测左递归,如果有则进行消除; 2、求解FIRST集和FOLLOW集; 3、构建LL(1)分析表; 4、构建LL分析程序,对于用户输入的句子,能够利用所构造的分析程序进行分析,并显示出分析过程。 四、主要仪器设备 硬件:微型计算机。 软件: Code blocks(也可以是其它集成开发环境)。 五、实验过程描述 1、程序主要框架 程序中编写了以下函数,各个函数实现的作用如下: void input_grammer(string *G);//输入文法G

//将文法G预处理得到产生式集合P,非终结符、终结符集合U、u, int eliminate_1(string *G,string *P,string U,string *GG);//消除文法G中所有直接左递归得到文法GG int* ifempty(string* P,string U,int k,int n);//判断各非终结符是否能推导为空 string* FIRST_X(string* P,string U,string u,int* empty,int k,int n);求所有非终结符的FIRST集 string FIRST(string U,string u,string* first,string s);//求符号串s=X1X2...Xn的FIRST集 string** create_table(string *P,string U,string u,int n,int t,int k,string* first);//构造分析表 void analyse(string **table,string U,string u,int t,string s);//分析符号串s 2、编写的源程序 #include #include #include using namespace std; void input_grammer(string *G)//输入文法G,n个非终结符 { int i=0;//计数 char ch='y'; while(ch=='y'){ cin>>G[i++]; cout<<"继续输入?(y/n)\n"; cin>>ch; } } void preprocess(string *G,string *P,string &U,string &u,int &n,int &t,int &k)//将文法G预处理产生式集合P,非终结符、终结符集合U、u, { int i,j,r,temp;//计数 char C;//记录规则中()后的符号 int flag;//检测到() n=t=k=0; for( i=0;i<50;i++) P[i]=" ";//字符串如果不初始化,在使用P[i][j]=a时将不能改变,可以用P[i].append(1,a) U=u=" ";//字符串如果不初始化,无法使用U[i]=a赋值,可以用U.append(1,a) for(n=0;!G[n].empty();n++) { U[n]=G[n][0]; }//非终结符集合,n为非终结符个数 for(i=0;i

编译原理语法分析实验报告

编译原理语法分析实验报告 - 班级:XXX 学号:XXX 姓名:XXX 年月日 1、摘要: 用递归子程序法实现对pascal的子集程序设计语言的分析程序 2、实验目的: 通过完成语法分析程序,了解语法分析的过程和作用 3、任务概述 实验要求:对源程序的内码流进行分析,如为文法定义的句子输出”是”否则输出”否”,根据需要处理说明语句填写写相应的符号表供以后代码生成时使用 4、实验依据的原理 递归子程序法是一种自顶向下的语法分析方法,它要求文法是LL(1)文法。通过对文法中每个非终结符编写一个递归过程,每个过程的功能是识别由该非终结符推出的串,当某非终结符的产生式有多个候选式时,程序能够按LL(1)形式唯一地确定选择某个候选式进行推导,最终识别输入串是否与文法匹配。 递归子程序法的缺点是:对文法要求高,必须满足LL(1)文法,当然在某些语言中个别产生式的推导当不满足LL(1)而满足LL(2)时,也可以采用多向前扫描一个符号的办法;它的另一个缺点是由于递归调用多,所以速度慢占用空间多,尽管这样,它还是许多高级语言,例如PASCAL,C等编译系统常常采用的语法分析方法。

为适合递归子程序法,对实验一词法分析中的文法改写成无左递归和无左共因子的,,,如下: <程序>?<程序首部><分程序>。 <程序首部>?PROGRAM标识符; <分程序>?<常量说明部分><变量说明部分><过程说明部分> <复合语句> <常量说明部分>?CONST<常量定义><常量定义后缀>;|ε <常量定义>?标识符=无符号整数 <常量定义后缀>?,<常量定义><常量定义后缀> |ε <变量说明部分>?VAR<变量定义><变量定义后缀> |ε <变量定义>?标识符<标识符后缀>:<类型>; <标识符后缀>?,标识符<标识符后缀> |ε <变量定义后缀>?<变量定义><变量定义后缀> |ε <类型>?INTEGER | LONG <过程说明部分>?<过程首部><分程序>;<过程说明部分后缀>|ε <过程首部>?PROCEDURE标识符<参数部分>; <参数部分>?(标识符: <类型>)|ε <过程说明部分后缀>?<过程首部><分程序>;<过程说明部分后缀>|ε <语句>?<赋值或调用语句>|<条件语句>|<当型循环语句>|<读语句> |<写语句>|<复合语句>|ε <赋值或调用语句>?标识符<后缀> <后缀>?:=<表达式>|(<表达式>)|ε <条件语句>?IF<条件>THEN<语句> <当型循环语句>?WHILE<条件>DO <语句> <读语句>?READ(标识符<标识符后缀>)

(完整版)大学英语语法专项练习题

大学英语语法专项练习题 一、时态 1. By the end of April Peter here for three months. A. will have stayed B. will stay C. stays D. has stayed 2. I'm awfully sorry, but I had no alternative. I simply _____ what I did. A. ought to have done B. have to do C. had to do D. must do 3. We ________our breakfast when an old man came to the door. A. just have had B. have just had C. just had D. had just had 4. Ever since the family moved to the suburbs last year, they________ better health. A. could have enjoyed B. had enjoyed C. have been enjoying D. are enjoying 5. I bought a new house last year, but I ______my old house yet, so at the moment I have two houses. A. did not sell B. have not sold C. had not sold D. do not sell 6. I decided to go to the library as soon as I ________. A. finish what I did B. finished what I did C. would finish what I was doing D. finished what I was doing 7. He _________when the bus came to a sudden stop. A. was almost hurt B. was hurt himself C. was to hurt himself D. was hurting himself 8. I suppose that when I come back in ten years' time all those old houses _______down. A. will have been pulled B. will have pulled C. will be pulling D. will be pulled 9. Bob's leg got hurt ________the Purple Mountains. A. while he is climbing up B. while we were climbing up C. while we climbed up D. while he climbed up 10. Pick me up at 8 o'clock. I _______ my bath by then. A. may have B. will be having C. can have had D. will have had 11. If you smoke in a non-smoking section people________. A. will object B. objected C. must object D. have objected 12. By the end of this month, we surely _______ a satisfactory solution to the problem A. have found B. will be finding C. are finding D. will have found 13. We __________to start our own business, but we never had enough money. A. have hope B. hope C. had hoped D. should hope 14. The gray building is where the workers live, and the white one is where the spare parts______ A. are producing B. are produced C. produced D. being produced 15. While people may refer to television for up-to-the-minute news, it is unlikely that television __________the newspaper completely. A. will replace B. have replaced C. replace D. replaced 16. It's reported that by the end of this month the output of cement in the factory ________by about 10%. A. will have risen B. has risen C. will be rising D. has been rising 17. Until then, his family___________ from him for six months. A. didn't hear B. hasn’t been hearing C. hasn't heard D. hadn't heard

语法分析程序报告

xx理工大学 《编译原理》 题目语法分析程序 姓名: 学号: 班级:

一、实验目的 编制一个递归下降分析程序,实现对词法分析程序所提供的单词序列的语法检查和结构分析。 二、实验要求 利用C语言编制递归下降分析程序,并对简单语言进行语法分析。 2.1 待分析的简单语言的语法 用扩充的BNF表示如下: ⑴<程序>::=begin<语句串>end ⑵<语句串>::=<语句>{;<语句>} ⑶<语句>::=<赋值语句> ⑷<赋值语句>::=ID:=<表达式> ⑸<表达式>::=<项>{+<项> | -<项>} ⑹<项>::=<因子>{*<因子> | /<因子> ⑺<因子>::=ID | NUM | (<表达式>) 2.2 实验要求说明 输入单词串,以“#”结束,如果是文法正确的句子,则输出成功信息,打印“success”,否则输出“error”。 例如: 输入begin a:=9; x:=2*3; b:=a+x end # 输出success 输入x:=a+b*c end # 输出error 2.3 语法分析程序的酸法思想 ⑴主程序示意图如图2-1所示。 图2-1 语法分析主程序示意图 ⑵递归下降分析程序示意图如图2-2所示。 ⑶语句串分析过程示意图如图2-3所示。

图2-2 递归下降分析程序示意图 图2-3 语句串分析示意图 ⑷statement 语句分析程序流程如图2-4、2-5、2-6、2-7所示。 图2-4 statement 语句分析函数示意图 图2-5 expression 表达式分析函数示意图

图2-7 factor分析过程示意图三、语法分析程序的C语言程序源代码 #include "stdio.h" #include "string.h" char prog[100],token[8],ch; char *rwtab[6]={"begin","if","then","while","do","end"}; int syn,p,m,n,sum; int kk; factor(); expression(); yucu(); term(); statement(); lrparser(); scaner(); main() { p=kk=0; printf("\nGrade:05 Class:03 Name:Qiyubing Number:200507096 \n"); printf("\n----Please input the string end with '#':-------- \n"); do

编译原理-编写递归下降语法分析器

学号107 成绩 编译原理上机报告 名称:编写递归下降语法分析器 学院:信息与控制工程学院 专业:计算机科学与技术 班级:计算机1401班 姓名:叶达成 2016年10月31日

一、上机目的 通过设计、编制、调试一个递归下降语法分析程序,实现对词法分析程序所提供的单词序列进行语法检查和结构分析,掌握常用的语法分析方法。通过本实验,应达到以下目标: 1、掌握从源程序文件中读取有效字符的方法和产生源程序的内部表示文件的方法。 2、掌握词法分析的实现方法。 3、上机调试编出的词法分析程序。 二、基本原理和上机步骤 递归下降分析程序实现思想简单易懂。程序结构和语法产生式有直接的对应关系。因为每个过程表示一个非终结符号的处理,添加语义加工工作比较方便。 递归下降分析程序的实现思想是:识别程序由一组子程序组成。每个子程序对应于一个非终结符号。 每一个子程序的功能是:选择正确的右部,扫描完相应的字。在右部中有非终结符号时,调用该非终结符号对应的子程序来完成。 自上向下分析过程中,如果带回溯,则分析过程是穷举所有可能的推导,看是否能推导出待检查的符号串。分析速度慢。而无回溯的自上向下分析技术,当选择某非终结符的产生时,可根据输入串的当前符号以及各产生式右部首符号而进行,效率高,且不易出错。 无回溯的自上向下分析技术可用的先决条件是:无左递归和无回溯。 无左递归:既没有直接左递归,也没有间接左递归。 无回溯:对于任一非终结符号U的产生式右部x1|x2|…|x n,其对应的字的首终结符号两两不相交。 如果一个文法不含回路(形如P?+ P的推导),也不含以ε为右部的产生式,那么可以通过执行消除文法左递归的算法消除文法的一切左递归(改写后的文法可能含有以ε为右部的产生式)。 三、上机结果 测试数据: (1)输入一以#结束的符号串(包括+—*/()i#):在此位置输入符号串例如:i+i*i# (2)输出结果:i+i*i#为合法符号串 (3)输入一符号串如i+i*#,要求输出为“非法的符号串”。 程序清单: #include #include char str[50]; int index=0; void E(); //E->TX; void X(); //X->+TX | e void T(); //T->FY void Y(); //Y->*FY | e void F(); //F->(E) | i int main() /*递归分析*/ { int len; int m;

大学英语语法试题及答案(2).doc

大学英语语法试题及答案(2) 第 2 单元题目: 1.More people visit the Air and Space Museum honoring men and women who have pioneered flight and the exploration of space than _____ any other monument or museum in the entire country. A) visit B) to visit C) visited D) visiting 题目: 2.I work in a little room off the main entrance _____ museum, checking coats and other articles which people do not want to carry around as they tour the building. A) to B) of C) at D) for 题目: 3.What apparently had happened, three years ago, was that Kate _____ to a different building. A) went B) has gone C) had gone D) would go 题目: 4.She had waited at another museum for days and had spent all her money _____ to find Sidney. A) tried B) trying C) to try D) try 题目: 5.Without facts we cannot form a worthwhile opinion for we need to have factual knowledge _____ our thinking. A) which to be based B) which to base upon C) upon which to base D) to which to be based 题目: 6.Helen believes if a man robs her of five dollars it is the same as if he _____ a hundred. A) takes B) will take C) took D) has taken 题目: 7.He had a cottage which consists _____ three rooms, a bathroom and kitchen. A) of B) with C) in D) by 题目: 8.Behind him Paul could hear the angry man _____ to break the door open. A) trying B) to try C) tried D) try 题目: 9.Some companies have introduced flexible working time with less emphasis on pressure _____. A) than more on efficiency B) and more efficiency C) and more on efficiency D) than efficiency 题目: 10.The gardener is taking care of the place, no one _____ there at present. A) living B) lives C) lived D) to live 题目: 11.The City Bank will pa fifty pounds to _____ who helps the police to catch the man. A) someone B) nobody C) anyone D) somebody 题目: 12.When Paul Carson saw the big red American car coming towards him, he stopped his won car at the side, _____ room for it to pass. A) to make B) made C) making D) make 题目: 13.What bothers me is _____ I paid for all this stuff that we don't want anymore. A) what B) that C) which D) who 题目: 14.A hinge joint is _____ permits forward and backward movement of a door. A) the B) whose C) what D) those 题目:15.Garage sales in the United States serve many purposes _____ cleaning out unwanted items and making money. A) besides B) except for C) except D) apart from 题目: 16.This is _____ the most difficult job I have ever tackled. A) by rights B) by itself C) by oneself D) by far

TEST语言语法分析,词法分析实验报告

编译原理实验报告 实验名称:分析调试语义分析程序 TEST抽象机模拟器完整程序 保证能用!!!!! 一、实验目的 通过分析调试TEST语言的语义分析和中间代码生成程序,加深对语法制导翻译思想的理解,掌握将语法分析所识别的语法畴变换为中间代码的语义翻译方法。 二、实验设计

#include #include extern bool TESTparse(char *pFileName); extern int TESTScan(FILE *fin,FILE *fout); FILE *fin,*fout; //用于指定输入输出文件的指针 int main() { char szFinName[300]; char szFoutName[300]; printf("请输入源程序文件名(包括路径):"); scanf("%s",szFinName); printf("请输入词法分析输出文件名(包括路径):"); scanf("%s",szFoutName); if( (fin = fopen(szFinName,"r")) == NULL) { printf("\n打开词法分析输入文件出错!\n"); return 0;

} if( (fout = fopen(szFoutName,"w")) == NULL) { printf("\n创建词法分析输出文件出错!\n"); return 0; } int es = TESTScan(fin,fout); fclose(fin); fclose(fout); if(es > 0) printf("词法分析有错,编译停止!共有%d个错误!\n",es); else if(es == 0) { printf("词法分析成功!\n"); int es = 0; es = TESTparse(szFoutName); //调语法分析 if(es== true) printf("语法分析成功!\n"); else printf("语法分析错误!\n"); }

编译原理实验报告(语法分析器)

. 编译原理实验专业:13级网络工程

语法分析器1 一、实现方法描述 所给文法为G【E】; E->TE’ E’->+TE’|空 T->FT’ T’->*FT’|空 F->i|(E) 递归子程序法: 首先计算出五个非终结符的first集合follow集,然后根据五个产生式定义了五个函数。定义字符数组vocabulary来存储输入的句子,字符指针ch指向vocabulary。从非终结符E函数出发,如果首字符属于E的first集,则依次进入T函数和E’函数,开始递归调用。在每个函数中,都要判断指针所指字符是否属于该非终结符的first集,属于则根据产生式进入下一个函数进行调用,若first集中有空字符,还要判断是否属于该非终结符的follow集。以分号作为结束符。 二、实现代码 头文件shiyan3.h #include #include

#include using namespace std; #define num 100 char vocabulary[num]; char *ch; void judge_E(); void judge_EE(); void judge_T(); void judge_TT(); void judge_F(); 源文件 #include"shiyan3.h" void judge_E() { if(*ch==';') { cout<<"该句子符合此文法!"<

int a=0; cout<<"按1结束程序"<>a; if(a==1) exit(0); } else if(*ch=='('||*ch=='i') { judge_T(); judge_EE(); } else { cout<<"该句子不匹配此文法!"<>a; if(a==1) exit(0); }

大学英语语法试卷

<英语语法> 阅卷须知:阅卷用红色墨水笔书写,得分用阿拉伯数字写在每小题题号前,用正分表示,不得分则在题E号前写0;大题得分登录在对应的题号前;统一命题的课程应集体阅卷,流水作业;阅卷后要进行复核,发现漏评、漏记或总分统计错误应及时更正; Rewrite the follow ing senten ces using the two patters. (40%) We expect that he will be coming. a) It is expected that he will be coming. b) He is expected to be coming. 1. People think that he is coming. 2. They believe that he is hon est. 3. We know that you were in town on the night of the crime. 4. we un dersta nd that she was the best sin ger that Australia has ever produced. 5. People suppose that he is in Paris. 6. People say that the murderer is hidi ng in the woods. 7. There is a report that unidentified flying objects were seen over New Jersey last night. 8. There is a rumour that he has escaped to Dubli n. 9. People expect that electricity supply will be adequate n ext year. 10. People know him to be a good teacher. Part II A) Rewrite the following sentences using “ adjective+infinitive ” constructions.(10%) It is easy to remember this rule. This rule is easy to remember. 1.1 was sorry whe n I lear nt that he had had an accide nt. 2. You will be sad whe n you hear what I have to tell you. 3. They would be very surprised if they were to receive an invitation. 4. She is happy that she has found such a nice place to live in. 5. I was afraid at the thought of going past the hau nted house alone. 6. Bob was pleased whe n he heard he had bee n promoted. 7. I very much want to meet you. 8. We received your telegram and were delighted. 9. It was sen sible of you to stay in doors. 10 The clerk answered the call promptly. B) Replace the words in italics by an infin itive or an -ng con structi on .(10%) The headmaster suggested that I should try the exam in ati on aga in the followi ng year. The headmaster suggested my tryi ng the exam in ati on aga in the followi ng year.

编译原理词法分析和语法分析报告 代码(C语言版)

词法分析 三、词法分析程序的算法思想: 算法的基本任务是从字符串表示的源程序中识别出具有独立意义的单词符号,其基本思想是根据扫描到单词符号的第一个字符的种类,拼出相应的单词符号。 3.1 主程序示意图: 扫描子程序主要部分流程图 其他

词法分析程序的C语言程序源代码: // 词法分析函数: void scan() // 数据传递: 形参fp接收指向文本文件头的文件指针; // 全局变量buffer与line对应保存源文件字符及其行号,char_num保存字符总数。 void scan() { char ch; int flag,j=0,i=-1; while(!feof(fp1)) { ch=fgetc(fp1); flag=judge(ch); printf("%c",ch);//显示打开的文件 if(flag==1||flag==2||flag==3) {i++;buffer[i]=ch;line[i]=row;} else if(flag==4) {i++;buffer[i]='?';line[i]=row;} else if(flag==5) {i++;buffer[i]='~';row++;} else if(flag==7) continue; else cout<<"\n请注意,第"<

大学英语语法试卷.docx

<英语语法 > 阅卷须知:阅卷用红色墨水笔书写,得分用阿拉伯数字写在每小题题号前,用正分表示,不得分则在题B号前写0;大题得分登录在对应的题号前;统一命题的课程应集体阅卷,流水作业;阅卷后要进行复核,发现漏评、 漏记或总分统计错误应及时更正;对评定分数或统分记录进行修改时,修改人必须签名。 题号Part Part Part Part I II III IV 得分 阅卷人 . 得分评卷人 Part I Rewrite the following sentences using the two patters. We expect that he will be coming. a)It is expected that he will be coming. b)He is expected to be coming. 1.People think that he is coming. 总分复核人(40%) 2. They believe that he is honest. 3. We know that you were in town on the night of the crime. 4. we understand that she was the best singer that Australia has ever produced. 5. People suppose that he is in Paris. 6. People say that the murderer is hiding in the woods.

7. There is a report that unidentified flying objects were seen over New Jersey last night. 8. There is a rumour that he has escaped to Dublin. 9. People expect that electricity supply will be adequate next year. 10. People know him to be a good teacher. 得分评卷人 Part II A) Rewrite the foll owing sentences using “ adjective+infinitive ” constructions.(10%) It is easy to remember this rule. This rule is easy to remember. 1.I was sorry when I learnt that he had had an accident. 2.You will be sad when you hear what I have to tell you. 3.They would be very surprised if they were to receive an invitation. 4.She is happy that she has found such a nice place to live in. 5.I was afraid at the thought of going past the haunted house alone. 6.Bob was pleased when he heard he had been promoted. 7.I very much want to meet you. 8.We received your telegram and were delighted. 9.It was sensible of you to stay indoors. 10 The clerk answered the call promptly.

语法分析器实验报告

实验三语法分析器 一、实验目的: 理解和掌握LL(1)语法分析方法的基本原理;根据给出的LL(1)文法,掌握LL(1)分析表的构造及分析过程的实现,掌握语法分析方法和程序设计方法。 二、实验要求: 对每个非终极符按其产生式结构构造相应语法分析子程序,其中终极符产生匹配命令,而非终极符则产生过程调用命令。因为文法递归相应子程序也递归,所以称这种方法为递归子程序下降法或递归下降法。其中子程序的结构与。产生式结构几乎是一致的,通过设计、编程、调试出一个具体语法分析程序。 三、实验原理: 语法分析是编译过程的核心部分。它的任务是在词法分析识别出单词符号串的基础上,分析并判定程序的语法结构是否符合语法规则。语法分析器的工作本质上是按文法的产生式,识别输入串是否是一个句子。自上而下分析法的主旨是,对任何输入串,试图用一切可能的方法,从文法开始符号出发,自上而下地为输入串建立一棵语法树。这种方法本质上是一种试探过程,是反复使用不同产生式谋求匹配输入串的过程。 对于一个文法满足以下三个条件,则称该文法为LL(1)文法。 文法不含有左递归。 对于文法中的每一个非终结符A的各个产生式的侯选首符集两两不相交。即,若A->Q1|Q2|…|Qn 则FIRST(Qi) ^FIRST(Qj)=null (i!=j) 对文法中的每个非终结符号A,若他存在某个侯选首符集包含空串,则FIRST(A)^FOLLOW(A)=null 对于一个文法满足LL(1)条件时,我们就可以对其输入串进行有效的无回溯的自上而下分析程序,这个分析程序是由一组递归过程组成的,每个过程对应文法的一个非终结符号。 四、实验步骤: 1、功能描述: 根据给定的文法,由程序生成项集族和语法分析表,对输入的源程序进行词法分析,得到语法分析的输入串,经过语法分析后得到三个栈,它们分别是状态栈,字符栈,输入栈,从而分析出输入的源程序是否有语法错误。 2、构造自己设计的小语言的语法分析器: (1) 语言的语法描述(语法规则)的设计即文法的设计; (2) 把文法形式符号中所隐含的信息内容挖掘出来并用LL或LR的资料形式(分析表)表示出来; (3) 语法分析的数据输入形式和输出形式的确定;

编译原理-语法分析-算符优先文法分析器

编译原理实验报告 实验名称:编写语法分析分析器实验类型: 指导教师: 专业班级: 学号: 电子邮件: 实验地点: 实验成绩:

一、实验目的 通过设计、编制、调试一个典型的语法分析程序,实现对词法分析程序所提供的单词序列进行语法检查和结构分析,进一步掌握常用的语法分析方法。 1、选择最有代表性的语法分析方法,如LL(1) 语法分析程序、算符优先分析程序和LR分析分析程序,至少选一题。 2、选择对各种常见程序语言都用的语法结构,如赋值语句(尤指表达式)作为分析对象,并且与所选语法分析方法要比较贴切。 二、实验过程 编写算符优先分析器。要求: (a)根据算符优先分析算法,编写一个分析对象的语法分析程序。读者可根据自己的能力选择以下三项(由易到难)之一作为分析算法中的输入: Ⅰ:通过构造算符优先关系表,设计编制并调试一个算法优先分析程序Ⅱ:输入FIRSTVT,LASTVT集合,由程序自动生成该文法的算符优先关系矩阵。 Ⅲ:输入已知文法,由程序自动生成该文法的算符优先关系矩阵。(b)程序具有通用性,即所编制的语法分析程序能够使用于不同文法以及各种输入单词串,并能判断该文法是否为算符文法和算符优先文法。 (c)有运行实例。对于输入的一个文法和一个单词串,所编制的语法分析程序应能正确地判断,此单词串是否为该文法的句子,并要求输出分析过程。 三、实验结果 算符优先分析器: 测试数据:E->E+T|T T->T*F|F F->(E)|i 实验结果:(输入串为i+i*i+i)

四、讨论与分析 自下而上分析技术-算符优先分析法: 算符文法:一个上下无关文法G,如果没有且没有P→..QR...(P ,Q ,R属于非终结符),则G是一个算符文法。 FIRSTVT集构造 1、若有产生式P →a...或P →Qa...,则a∈FIRSTVT(P)。 2、若有产生式P→...,则FIRSTVT(R)包含在FIRSTVT(P)中。由优先性低于的定义和firstVT集合的定义可以得出:若存在某个产生式:…P…,则对所有:b∈firstVT(P)都有:a≦b。 构造优先关系表: 1、如果每个非终结符的FIRSTVT和LASTVT集均已知,则可构造优先关系表。 2、若产生式右部有...aP...的形式,则对于每个b∈FIRSTVT(P)都有

相关文档
相关文档 最新文档