编译实验1--词法分析
实验一 词法分析程序
目的和内容
- 实验目的:通过完成词法分析程序,了解词法分析的过程。
- 实验内容:用C/C++实现对Pascal的子集程序设计语言的词法识别程序。
- 实验要求:将该语言的源程序,也就是相应字符流转换成内码,并根据需要是否对于标识符填写相应的符号表供编译程序的以后各阶段使用。
程序设计语言的描述
程序设计语言的描述采用扩充的BNF表示:
<程序>→<程序首部><分程序>.
<程序首部>→PROGRAM标识符;
<分程序>→[<常量说明部分>][<变量说明部分>][<过程说明部分>]<复合语句>
<常量说明部分>→CONST<常量定义>{,<常量定义>};
<常量定义>→标识符=无符号整数
<变量说明部分>→VAR<变量定义>(;<变量定义>);
<变量定义>→标识符{,标识符}:<类型>
<类型>→INTEGER|LONG
<过程说明部分>→<过程首部><分程序>;{<过程首部><分程序>;}
<过程首部>→PROCEDURE标识符;| PROCEDURE标识符(标识符:<类型>);
<语句>→<赋值语句>|<条件语句>|<当型循环语句>|<过程调用语句>
|<读语句>|<写语句>|<复合语句>|ε
<赋值语句>→标识符:=<表达式>
<条件语句>→IF<条件>THEN<语句>
<当型循环语句>→WHILE<条件>DO<语句>
<过程调用语句>→标识符 | 标识符(<表达式>)
<读语句>→READ(标识符,{标识符})
<写语句>→WRITE(<表达式>{,<表达式>})
<复合语句>→BEGIN<语句>{;<语句>}END
<条件>→<表达式><关系运算符><表达式> | ODD<表达式>
<表达式>→[+|-]<项>{<加型运算符><项>}
<项>→<因子>{<乘型运算符><因子>}
<因子>→标识符 | 无符号整数 | (<表达式>)
<加型运算符>→+|-
<乘型运算符>→* | /
<关系运算符>→=|<>|<|<=|>|>=
其中:
< >:用左右尖括号括起的字符串表示非终结符号
::= : 定义为
{ }:表示该语法成分可以0—n次重复。
[ ]:表示方括号内为可选项,即0或1次。
程序设计语言单词的内部编码
下表为词法分析中的内码单词对照表
内码 |
单词 |
内码 |
单词 |
内码 |
单词 |
内码 |
单词 |
1 |
PROGRAM |
2 |
CONST |
3 |
VAR |
4 |
INTEGER |
5 |
LONG |
6 |
PROCEDURE |
7 |
IF |
8 |
THEN |
9 |
WHILE |
10 |
DO |
11 |
READ |
12 |
WRITE |
13 |
BEGIN |
14 |
END |
15 |
ODD |
16 |
+ |
17 |
- |
18 |
* |
19 |
/ |
20 |
= |
21 |
<> |
22 |
< |
23 |
<= |
24 |
> |
25 |
>= |
26 |
, |
27 |
. |
28 |
; |
29 |
: |
30 |
:= |
31 |
( |
32 |
) |
33 |
无符号整数 |
34 |
标识符 |
35 |
# |
实验流程
实验代码
#include<bits/stdc++.h>
using namespace std;
#define lenth1 15
#define lenth2 17
struct Ident
{char name[21];int type;int addr;
};
Ident indent[1000];struct St
{char name[21];int code;
};
St sym;int lenth = 0; // 表示标识符表长度
FILE *f1, * f2; //f1、f2分别指向输入输出文件int line = 0, row = 0, val;void getsym();
char getchr();
void error(int);int main(int argc,char *argv[])
{char ft[12], * fc;if ((f1 = fopen(argv[1], "r"))== NULL) { // 打开输入文件流cout << "can not open the input file!" << endl;exit(0);}if (argc <= 2) { // 没有指定输出文件名strcpy(ft, argv[1]); //ft目标中间文件 =》*.tmpif ((fc = strchr(ft, '.')) != NULL) {strcpy(fc, ".temp");}else {strcat(ft, ".temp");}}else {strcpy(ft, argv[2]);}if ((f2 = fopen(ft, "w")) == NULL) { // 打开输出文件流cout << "can not open the output file" << endl;exit(0);}while (!feof(f1)) {getsym();printf("%s ==>%d\n", sym.name, sym.code); //控制台打印fprintf(f2, "%s ==>%d\n", sym.name, sym.code); //输出文件}fclose(f1);fclose(f2);return 0;
}void getsym() {static char a[lenth1][10] = {"program","const","var","integer","long","procedure","if","then","while","do","read","write","begin","end","odd"},d[lenth2][3] = { "+","-","*","/","=","<>","<","<=",">",">=",".",",",";",":",":=","(",")" },str[21], ch = ' ';int i, n; // n判断单词长度,i判断是哪个关键字while (isspace(ch)){ch = getchr();}if (isalpha(ch)) { //判断字母开头的字符n = 0;while (isalpha(ch) || isdigit(ch)) {if (isalpha(ch))ch = tolower(ch);str[n++] = ch;ch = getchr();}str[n] = '\0';for (i = 0;i < lenth1;i++) {if (!strcmp(str, a[i]))break;}if (i < lenth1) { //找到匹配关键字strcpy(sym.name, a[i]);sym.code = i + 1;}else {for (i = 0;i < lenth;i++) { //遍历已记录的标识符记录表if (!strcmp(str, indent[i].name))break;}if (i == lenth) {strcpy(indent[i].name, str);}strcpy(sym.name, indent[i].name);sym.code = 34;}}else if (isdigit(ch)) { // 判断数字开头的字符val = 0;n = 0;while (isdigit(ch)) {val = val * 10 + ch - '0';sym.name[n++] = ch;ch = getchr();}sym.name[n] = '\0';sym.code = 33;}else { // 判断特殊字符if (ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '=' ||ch == '.' || ch == ',' || ch == ';' || ch == '(' || ch == ')') {str[0] = ch; str[1] = '\0';ch = getchr();for (i = 0;i < lenth2;i++) {if (!strcmp(str, d[i])) {strcpy(sym.name, str);sym.code = i + 16;break;}}}else if (ch == '#') {str[0] = ch; str[1] = '\0';ch = getchr();strcpy(sym.name, str);sym.code = 35;}else if (ch == '<') {char b = getchr();if (b == '>') {str[0] = '<';str[1] = '>';str[2] = '\0';strcpy(sym.name, str);sym.code = 21;ch = getchr();}else if (b == '=') {str[0] = '<';str[1] = '=';str[2] = '\0';strcpy(sym.name, str);sym.code = 23;ch = getchr();}else {str[0] = '<';str[1] = '\0';strcpy(sym.name, str);sym.code = 22;ch = b;}}else if (ch == '>') {char b = getchr();if (b == '=') {str[0] = '>';str[1] = '=';str[2] = '\0';strcpy(sym.name, str);sym.code = 25;ch = getchr();}else {str[0] = '>';str[1] = '\0';strcpy(sym.name, str);sym.code = 24;ch = b;}}else if (ch == ':') {char b = getchr();if (b == '=') {str[0] = ch;str[1] = b;str[2] = '\0';strcpy(sym.name, str);sym.code = 30;ch = getchr();}else {str[0] = ch;str[1] = '\0';strcpy(sym.name, str);sym.code = 29;ch = b;}}}
}char getchr() { // 从文件中获取下一个非空字符char ch = fgetc(f1);if (ch == '\n') {row = 1;line++;}else {if (ch != ' ' && ch != '\t') {row++;}}return ch;
}void error(int n) {printf("There are %d-error\n", n);exit(0);
}
编译实验1--词法分析相关推荐
- 编译实验(一)词法分析
编译原理课程即将结束,开始了在校中最麻烦的实验,编译实验......同在一个系,其他班的编译实验分成好几块,简短的文法, 完成一些小功能,就我们班的老师,撂下一句话:参考书本,把编译器实现了,可以一组 ...
- 编译原理实验:词法分析
编译原理实验:词法分析 1. 实验题目:词法分析 实验目的 实验内容 实验要求 输入输出 2. 设计思想 3.算法流程 4. 源程序 5. 调试数据 1. 实验题目:词法分析 实验目的 根据PL/0语 ...
- 编译实验(三)目标代码生成
通过词法分析,语法分析,语义分析,最后产生了四元式.而代码生成则是通过四元式来完成.我们先从简单开始做起. 编译实验项目下载链接:http://download.csdn.net/download/s ...
- 编译原理中词法分析的递归下降分析法实例--能被5整除的二进制数---c语言实现
一.前言 又到了一周一度的编译原理实验课,一次实验课上完了,又是大学生必备技能-写实验报告.行了,废话不多说,我直接展现,如何实现编译原理中词法分析的递归下降分析法实例–能被5整除的二进制数的思路.作 ...
- 编译原理中词法分析--部分实现
一.前言 某属于在校大学生,几天前老师布置了一个编译原理作业,将词法分析–部分实现,头疼,众所周之,编译原理是计算机专业中最令人头疼的课程,听懂已经很不容易了,TMD让我用C语言实现 ,头大.经过几个 ...
- 编译原理 C-Minus词法分析(FLEX)
C–源代码词法分析 文章目录 C--源代码词法分析 一.实现目标 二.C-Minus语法 三.Flex Flex简介 Flex正则表达式 Flex安装与使用 Flex文件编写 定义 规则 用户代码 四 ...
- c语言词法分析程序实验报告,实验一词法分析程序设计与实现
实验一 词法分析程序设计与实现 一.实验目的: 加深对词法分析器的工作过程的理解:加强对词法分析方法的掌握:能够采用一种编程语言实现简单的词法分析程序:能够使用自己编写的分析程序对简单的程序段进行词法 ...
- 编译linux源码报错,记录一次Linux内核源码编译实验
记录一次Linux内核源码编译实验 文章目录 记录一次Linux内核源码编译实验 0. 实验环境 1. 选择.下载内核源码 2. 安装必要的依赖软件以及性能要求 3. 解压.配置和编译内核源码 3.1 ...
- 编译原理画出c语言中注释的转化图,编译原理节词法分析DFANFA及其转换.ppt
编译原理节词法分析DFANFA及其转换 Step4 寻找可合并状态 ε ε 0 1 2 0 5 0 1 6 1 3 4 7 ε ε 0 8 9 1 1 0 10 11 0 0 1 1 1 0 12 1 ...
最新文章
- Blockchain与Big Data将碰撞出怎样的火花?
- 有哪些堪称神器的实用网站?这8个可以解你燃眉之急!
- HDU 4850 Wow! Such String! 【欧拉回路】【一顿乱构造】
- C#中Brush、Color、String相互转换
- 细数25个硅谷最热创业公司 寻找创业灵感
- 吕布机器人唤醒方式能换么_《王者荣耀》推吕布智能机器人,网友:小学生受到1万点暴击伤害...
- 【EI会议】 2021年图像处理与智能控制国际学术会议(IPIC 2021)
- python如何引用txt_python项目依赖库生成requirements.txt文件
- DBA,六个灾难恢复等级你都知道吗?
- matlab分析excel数据,基于MATLAB的EXCEL数据计算与分析
- 苹果手机投屏器 及如何使用设置连接电脑投屏教程
- 《UnityAPI.Animator动画器》(Yanlz+Unity+SteamVR+云技术+5G+AI+VR云游戏+Animator+avatar+CrossFade+Key+立钻哥哥++OK++)
- Low-Light Image and Video Enhancement Using Deep Learning: A Survey 论文阅读笔记
- 分析学中的若干空间:
- DigiCert和GlobalSign单域名OV SSL证书对比评测
- ios android md5加密,iOS中使用MD5加密
- 网站SEO怎么让网站收录翻倍,提升网站收录率
- SKYPE 网络电话试用手记
- 现代控制理论(3)——线性控制系统的能控性和能观性
- 3.前端开发就业前景