编译原理词法语法语义分析 器设计打印版
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
int Scan(char program[]) {
char *keywords[13] = {"void","main","if","then","break","int","char","float","include","for","while","printf","sca
//关键字 number=0; status=0; j=0; ch=program[i++]; //遍历 if ((ch >= 'a') && (ch <= 'z' )) //字母 { while ((ch >= 'a') && (ch <= 'z' )) { words[j++]=ch; ch=program[i++]; } i--; words[j++] = '\0';
if (ch == '=') {
words[j++] = ch; words[j] = '\0'; flag = 412; } else if( ch == '-') { words[j++] = ch; words[j] = '\0'; flag = 413; } else { i--; flag = 414; } break; } case'*':{ if (ch == '*') words[j++] = ch; words[j] = '\0'; ch = program[i++]; if (ch == '=') { words[j++] = ch; words[j] = '\0'; flag = 415; } else { i--; flag = 416; } break; }
b.删除:从符号表中删除给定名字的表项。 (2)设计词法分析器
设计各单词的状态转换图,并为不同的单词设计种别码。 将词法分析器设计成供语法分析器调用的子程序。功能包括:
a. 能够拼出语言中的各个单词; b. 将拼出的标识符填入符号表; c. 返回(种别码, 属性值)。 (3)语法分析 要求用递归下降分析法、预测分析法或SLR分析法,实现对 表达式、各种说明语句、控制语句进行语法分析。 若语法正确,则输出一棵语法树。 若语法错误,要求指出出错性质和出错位置(行号)。出 错处理应设计成一个出错处理子程序。
} case'^':{
words[j] = ch; words[j+1] = '\0'; flag = 419; break; } case';':{ words[j] = ch; words[j+1] = '\0'; flag = 501; break; } case'(':{ words[j] = ch; words[j+1] = '\0'; flag = 502; break; } case')':{
flag = 401;
}
else
{
i--;
flag = 402;
}
break;
}
case'>':{
if (ch == '>')
words[j++] = ch;
words[j] = '\0';
ch = program[i++];
if (ch == '=') {
words[j++] = ch; words[j] = '\0'; flag = 403; } else { i--; flag = 404; } break; } case'<':{ if (ch == '<') words[j++] = ch; words[j] = '\0'; ch = program[i++]; if (ch == '=') { words[j++] = ch; words[j] = '\0'; flag = 405; } else { i--; flag = 406; } break; } case'!':{ if (ch == '!') words[j++] = ch; words[j] = '\0'; ch = program[i++]; if (ch == '=') { words[j++] = ch; words[j] = '\0';
flag = 407; } else {
i--; flag = 408; } break; } case'+':{ if (ch == '+')
words[j++] = ch; words[j] = '\0'; ch = program[i++]; if (ch == '=') {
words[j++] = ch; words[j] = '\0'; flag = 409; } else if (ch == '+') { words[j++] = ch; words[j] = '\0'; flag = 410; } else { i--; flag = 411; } break; } case'-':{ if (ch == '-') words[j++] = ch; words[j] = '\0'; ch = program[i++];
单词符号 int char float if else do while printf main
标识符 常数(整)
== = >= > <= < != !
+=
++
+
-=
--
-
*=
*
/=
种别编码 1 2 3 4 5 6 7 8 9 100 200 401 402 403 404 405 406 407 408
break; } case 7:{
flag = 8; status = 1; break; } case 8:{ flag = 9; status = 1; break; } case 9:{ flag = 10; status = 1; break; } case 10:{ flag = 11; status = 1; break; } case 11:{ flag = 12; status = 1; break; } case 12:{ flag = 13; status = 1; break; } } if (status == 0) { flag = 100; //标识符() }
case'/':{ if (ch == '/') words[j++] = ch; words[j] = '\0'; ch = program[i++]; if (ch == '=') { words[j++] = ch; words[j] = '\0'; flag = 417; } else { i--; flag = 418; } break;
}
else if ((ch >= '0') && (ch <= '9')) //数字()
{
number = 0;
while ((ch >= '0' ) && (ch <= '9' ))
{
number = number*10+(ch-'0');
ch = program[i++];
}
flag = 200;
i--;
}
else switch (ch)
//运算符和标点符号
{
case '=':{
if (ch == '=')
words[j++] = ch;
words[j] = '\0';
ch = program[i++];
if (ch == '=')
{
words[j++] = ch;wk.baidu.com
words[j] = '\0';
编译原理课程设计报告
小组成员:
2010年06月24日
一.任务及要求
课程设计要实现的内容: (1)设计符号表
确定符号表的组织方式,一般应包括名字栏和信息栏,其 中名字栏作为关键字。要考虑能够存储有关名字的信息,并可 以高效地完成如下操作:
a.查找:根据给定的名字,在符号表中查找其信息。如果
该名字在符号表中不存在,则将其加入到符号表中,否则返回 指向该名字的指针;
Follow(A)={#} Follow(B)={#} Follow(E)={>,<,),#} Follow(F)={*,(,+,-} Follow(G)={>,<,),#} Follow(T)={+,-,>,
Follow(S)={+,-,>,
吴晓凯2010022211
6. 源程序
//#include "stdafx.h" #include <iostream> #include<string> using namespace std; #include<stdio.h> #include<stdlib.h> #include<sstream> int i,j,k,flag,number,status; /*status which is use to judge the string is keywords or not!*/ char ch; char words[10] = {" "}; char program[500]; int flags[500]; //存储输入句子 string cnt[500];//标识符 int temp=0; //数组下标 int is_right; //判断输出信息
1. 词法分析器 产生下述小语言的单词序列 这个小语言的所有的单词符号,以及它们的种别编码和内 部值如下表:
单词符号
DIM IF DO STOP END
种别编码
1 2 3 4 5
助记符
$DIM $IF $DO $STOP $END
内码值
-
标识符
6
常数(整)
7
=
8
+
9
*
10
**
11
,
12
(
13
)
14
使用的算法可以是:预测分析法;递归下降分析法;算符优先分析法;LR分析法等。 (1)A->EB (2)B->>EB|<EB|ε (3)E->TG (4)G->+TG|-TG|ε (5)T->FS (6)S->*FS|/FS|ε (7)F->(E)|i |i=E
2010022211 吴晓凯
3 具体的种别编码和内部值:
409 410
411
412
413
414
415
416
417
单词值 内部字符串 二进制数值表示
/
418
^
419
;
501
(
502
)
503
[
504
]
505
{
506
}
507
:
508
“
509
%=
510
%
511
,
512
#
513
@
514
空格
515
for (k = 0; k < 13; k++) if (strcmp (words,keywords[k]) == 0) //判断是否为关键字 switch(k) {
case 0:{ flag = 1; status = 1; break;
} case 1:{
flag = 2; status = 1; break; } case 2:{ flag = 3; status = 1; break; } case 3:{ flag = 4; status = 1; break; } case 4:{ flag = 5; status = 1; break; } case 5:{ flag = 6; status = 1; break; } case 6:{ flag = 7; status = 1;
words[j] = ch; words[j+1] = '\0'; flag = 503; break; } case'[':{ words[j] = ch; words[j+1] = '\0'; flag = 504; break; } case']':{ words[j] = ch; words[j+1] = '\0'; flag = 505; break; } case'{':{ words[j] = ch; words[j+1] = '\0'; flag = 506; break; } case'}':{ words[j] = ch; words[j+1] = '\0'; flag = 507; break; } case':':{ words[j] = ch; words[j+1] = '\0'; flag = 508; break; } case'"':{ words[j] = ch;
$ID $INT $ASSIGN $PLUS $STAR $POWER $COMMA $LPAR $RPAR
内部字符串 标准二进形式 -
2010022220
朱恒恒
2010022226 刘穗清
2. 语法分析器 能识别由大于>小于<加+ 减- 乘* 除/ 括号()赋值=操作数所组成的 算术表达式,其文法如下:
$
0
4. 流程图: 主流程图
扫描程序流程图:
(a),标识符词法分析流程图 (b), 数字(整)词法分析流程图 (c), 其他字符流程图
(a)
(b)
(c)
主流程图
刘穗清 2010022226
5. First/Follow集 First(A)={(,i} First(B)={>,<,ε} First(E)={(,i} First(F)={(,i} First(G)={+,-,ε} First(T)={c,i} <,),#} First(S)={*,(,ε} <,),#}
char *keywords[13] = {"void","main","if","then","break","int","char","float","include","for","while","printf","sca
//关键字 number=0; status=0; j=0; ch=program[i++]; //遍历 if ((ch >= 'a') && (ch <= 'z' )) //字母 { while ((ch >= 'a') && (ch <= 'z' )) { words[j++]=ch; ch=program[i++]; } i--; words[j++] = '\0';
if (ch == '=') {
words[j++] = ch; words[j] = '\0'; flag = 412; } else if( ch == '-') { words[j++] = ch; words[j] = '\0'; flag = 413; } else { i--; flag = 414; } break; } case'*':{ if (ch == '*') words[j++] = ch; words[j] = '\0'; ch = program[i++]; if (ch == '=') { words[j++] = ch; words[j] = '\0'; flag = 415; } else { i--; flag = 416; } break; }
b.删除:从符号表中删除给定名字的表项。 (2)设计词法分析器
设计各单词的状态转换图,并为不同的单词设计种别码。 将词法分析器设计成供语法分析器调用的子程序。功能包括:
a. 能够拼出语言中的各个单词; b. 将拼出的标识符填入符号表; c. 返回(种别码, 属性值)。 (3)语法分析 要求用递归下降分析法、预测分析法或SLR分析法,实现对 表达式、各种说明语句、控制语句进行语法分析。 若语法正确,则输出一棵语法树。 若语法错误,要求指出出错性质和出错位置(行号)。出 错处理应设计成一个出错处理子程序。
} case'^':{
words[j] = ch; words[j+1] = '\0'; flag = 419; break; } case';':{ words[j] = ch; words[j+1] = '\0'; flag = 501; break; } case'(':{ words[j] = ch; words[j+1] = '\0'; flag = 502; break; } case')':{
flag = 401;
}
else
{
i--;
flag = 402;
}
break;
}
case'>':{
if (ch == '>')
words[j++] = ch;
words[j] = '\0';
ch = program[i++];
if (ch == '=') {
words[j++] = ch; words[j] = '\0'; flag = 403; } else { i--; flag = 404; } break; } case'<':{ if (ch == '<') words[j++] = ch; words[j] = '\0'; ch = program[i++]; if (ch == '=') { words[j++] = ch; words[j] = '\0'; flag = 405; } else { i--; flag = 406; } break; } case'!':{ if (ch == '!') words[j++] = ch; words[j] = '\0'; ch = program[i++]; if (ch == '=') { words[j++] = ch; words[j] = '\0';
flag = 407; } else {
i--; flag = 408; } break; } case'+':{ if (ch == '+')
words[j++] = ch; words[j] = '\0'; ch = program[i++]; if (ch == '=') {
words[j++] = ch; words[j] = '\0'; flag = 409; } else if (ch == '+') { words[j++] = ch; words[j] = '\0'; flag = 410; } else { i--; flag = 411; } break; } case'-':{ if (ch == '-') words[j++] = ch; words[j] = '\0'; ch = program[i++];
单词符号 int char float if else do while printf main
标识符 常数(整)
== = >= > <= < != !
+=
++
+
-=
--
-
*=
*
/=
种别编码 1 2 3 4 5 6 7 8 9 100 200 401 402 403 404 405 406 407 408
break; } case 7:{
flag = 8; status = 1; break; } case 8:{ flag = 9; status = 1; break; } case 9:{ flag = 10; status = 1; break; } case 10:{ flag = 11; status = 1; break; } case 11:{ flag = 12; status = 1; break; } case 12:{ flag = 13; status = 1; break; } } if (status == 0) { flag = 100; //标识符() }
case'/':{ if (ch == '/') words[j++] = ch; words[j] = '\0'; ch = program[i++]; if (ch == '=') { words[j++] = ch; words[j] = '\0'; flag = 417; } else { i--; flag = 418; } break;
}
else if ((ch >= '0') && (ch <= '9')) //数字()
{
number = 0;
while ((ch >= '0' ) && (ch <= '9' ))
{
number = number*10+(ch-'0');
ch = program[i++];
}
flag = 200;
i--;
}
else switch (ch)
//运算符和标点符号
{
case '=':{
if (ch == '=')
words[j++] = ch;
words[j] = '\0';
ch = program[i++];
if (ch == '=')
{
words[j++] = ch;wk.baidu.com
words[j] = '\0';
编译原理课程设计报告
小组成员:
2010年06月24日
一.任务及要求
课程设计要实现的内容: (1)设计符号表
确定符号表的组织方式,一般应包括名字栏和信息栏,其 中名字栏作为关键字。要考虑能够存储有关名字的信息,并可 以高效地完成如下操作:
a.查找:根据给定的名字,在符号表中查找其信息。如果
该名字在符号表中不存在,则将其加入到符号表中,否则返回 指向该名字的指针;
Follow(A)={#} Follow(B)={#} Follow(E)={>,<,),#} Follow(F)={*,(,+,-} Follow(G)={>,<,),#} Follow(T)={+,-,>,
Follow(S)={+,-,>,
吴晓凯2010022211
6. 源程序
//#include "stdafx.h" #include <iostream> #include<string> using namespace std; #include<stdio.h> #include<stdlib.h> #include<sstream> int i,j,k,flag,number,status; /*status which is use to judge the string is keywords or not!*/ char ch; char words[10] = {" "}; char program[500]; int flags[500]; //存储输入句子 string cnt[500];//标识符 int temp=0; //数组下标 int is_right; //判断输出信息
1. 词法分析器 产生下述小语言的单词序列 这个小语言的所有的单词符号,以及它们的种别编码和内 部值如下表:
单词符号
DIM IF DO STOP END
种别编码
1 2 3 4 5
助记符
$DIM $IF $DO $STOP $END
内码值
-
标识符
6
常数(整)
7
=
8
+
9
*
10
**
11
,
12
(
13
)
14
使用的算法可以是:预测分析法;递归下降分析法;算符优先分析法;LR分析法等。 (1)A->EB (2)B->>EB|<EB|ε (3)E->TG (4)G->+TG|-TG|ε (5)T->FS (6)S->*FS|/FS|ε (7)F->(E)|i |i=E
2010022211 吴晓凯
3 具体的种别编码和内部值:
409 410
411
412
413
414
415
416
417
单词值 内部字符串 二进制数值表示
/
418
^
419
;
501
(
502
)
503
[
504
]
505
{
506
}
507
:
508
“
509
%=
510
%
511
,
512
#
513
@
514
空格
515
for (k = 0; k < 13; k++) if (strcmp (words,keywords[k]) == 0) //判断是否为关键字 switch(k) {
case 0:{ flag = 1; status = 1; break;
} case 1:{
flag = 2; status = 1; break; } case 2:{ flag = 3; status = 1; break; } case 3:{ flag = 4; status = 1; break; } case 4:{ flag = 5; status = 1; break; } case 5:{ flag = 6; status = 1; break; } case 6:{ flag = 7; status = 1;
words[j] = ch; words[j+1] = '\0'; flag = 503; break; } case'[':{ words[j] = ch; words[j+1] = '\0'; flag = 504; break; } case']':{ words[j] = ch; words[j+1] = '\0'; flag = 505; break; } case'{':{ words[j] = ch; words[j+1] = '\0'; flag = 506; break; } case'}':{ words[j] = ch; words[j+1] = '\0'; flag = 507; break; } case':':{ words[j] = ch; words[j+1] = '\0'; flag = 508; break; } case'"':{ words[j] = ch;
$ID $INT $ASSIGN $PLUS $STAR $POWER $COMMA $LPAR $RPAR
内部字符串 标准二进形式 -
2010022220
朱恒恒
2010022226 刘穗清
2. 语法分析器 能识别由大于>小于<加+ 减- 乘* 除/ 括号()赋值=操作数所组成的 算术表达式,其文法如下:
$
0
4. 流程图: 主流程图
扫描程序流程图:
(a),标识符词法分析流程图 (b), 数字(整)词法分析流程图 (c), 其他字符流程图
(a)
(b)
(c)
主流程图
刘穗清 2010022226
5. First/Follow集 First(A)={(,i} First(B)={>,<,ε} First(E)={(,i} First(F)={(,i} First(G)={+,-,ε} First(T)={c,i} <,),#} First(S)={*,(,ε} <,),#}