实验一词法分析器的设计
更新时间:2023-05-13 11:43:01 阅读量: 实用文档 文档下载
实验一 词法分析器的设计
一、实验目的
(1)学会针对转换图实现相应的高级语言源程序。
(2)深刻领会状态转换图的含义,逐步理解有限自动机。
二、实验内容
(1)某计算机语言的编译程序的词法分析部分实现。
(2)从左到右扫描每行该语言源程序的符号,拼成单词,换成统一的内部表示(token),送给语法分析程序。
三、实现原理
程序中先判断这个句语句中每个单元为关键字、常数、运算符、界符,对与不同的单词符号给出不同编码形式的编码,用以区分之。
PL/0语言的EBNF表示
<常量定义>::=<标识符>=<无符号整数>;
<标识符>::=<字母>={<字母>|<数字>};
<加法运算符>::=+|-
<乘法运算符>::=*|/
<关系运算符>::==|#|<|<=|>|>=
<字母>::=a|b| |X|Y|Z
<数字>::=0|1|2| |8|9
四、设计过程
1. 关键字:void,main,if,then,break,int,Char,float,include,for,while,printfscanf 并为小写。
2."+”;”-”;”*”;”/”;”:=“;”:”;”<“;”<=“;”>“;”>=“;”<>“;”=“;”(“;”)”;”;”;”#”为运算符。
3. 其他标记 如字符串,表示以字母开头的标识符。
4. 空格符跳过。
5. 各符号对应种别码
关键字分别对应1-13
运算符分别对应401-418,501-513。
字符串对应100
常量对应200
结束符#
五、心得体会
其实匹配并不困难,主要是C++知识要求相对较高,只要把握住指针就好了。
附源程序:
#include<iostream.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int i,j,k,flag,number,status;
/*status which is use to judge the string is keywords or not!*/
char words[10] = {" "};
char program[500];
int Scan(char program[])
{
char *keywords[13] = {"void","main","if","then","break","int", "char","float","include","for","while","printf",
"scanf"};
number = 0;
status = 0;
j = 0;
ch = program[i++];
/* To handle the lettle space ands tab*/
/*handle letters*/
if ((ch >= 'a') && (ch <= 'z' ))
{
while ((ch >= 'a') && (ch <= 'z' ))
{
words[j++]=ch;
ch=program[i++];
}
i--;
words[j++] = '\0';
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:{
status = 1;
break;
}
case 4:{
flag = 5;
status = 1;
break;
}
case 5:{
flag = 6;
status = 1;
break;
}
case 6:{
flag = 7;
status = 1;
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;
}
flag = 13;
status = 1;
break;
}
}
if (status == 0)
{
flag = 100;
}
}
/*handle digits*/
else if ((ch >= '0') && (ch <= '9')) {
number = 0;
while ((ch >= '0' ) && (ch <= '9' )) {
number = number*10+(ch-'0');
ch = program[i++];
}
flag = 200;
i--;
}
/*opereation and edge handle*/
else switch (ch)
{
case '=':{
if (ch == '=')
words[j++] = ch;
words[j] = '\0';
ch = program[i++]; if (ch == '=')
{
words[j++] = ch;
words[j] = '\0';
flag = 401;
}
else
{
i--;
flag = 402;
}
break;
}
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++]; 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;
}
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;
}
case';':{
words[j] = ch;
words[j+1] = '\0';
flag = 501;
break;
}
case'(':{
words[j] = ch;
words[j+1] = '\0';
flag = 502;
break;
}
case')':{
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;
words[j+1] = '\0';
flag = 509;
break;
}
case'%':{
if (ch == '%')
words[j++] = ch;
words[j] = '\0';
ch = program[i++]; if (ch == '=')
{
words[j++] = ch;
words[j] = '\0';
flag = 510;
}
else
{
i--;
flag = 511;
}
break;
}
case',':{
words[j] = ch;
words[j+1] = '\0';
flag = 512;
break;
}
case'#':{
words[j] = ch;
words[j+1] = '\0';
flag = 513;
break;
}
case'@':{
words[j] = '#';
flag = 0;
break;
}
default:{
flag = -1;
break;
}
}
return flag;
}
main()
{
i=0;
printf("please input a program end with @"); do
{
ch = getchar();
program[i++] = ch;
}while(ch != '@');
i = 0;
do{
flag = Scan(program);
if (flag == 200)
{
printf("(%2d,%4d)",flag,number); }
else if (flag == -1)
{
printf("(%d,error)",flag);
}
else
{
printf("(%2d,%4s)",flag,words);
}
}while (flag != 0);
system("pause");
}
正在阅读:
实验一词法分析器的设计05-13
丰收小学生作文06-15
诺西常见告警04-17
装在套子里的人说课稿09-21
抗震计算 - 图文05-29
市农业局2021年法治建设工作总结报告08-17
建筑隔震橡胶支座JG119--200007-24
欧赔研习之路的伪方法05-29
- 教学能力大赛决赛获奖-教学实施报告-(完整图文版)
- 互联网+数据中心行业分析报告
- 2017上海杨浦区高三一模数学试题及答案
- 招商部差旅接待管理制度(4-25)
- 学生游玩安全注意事项
- 学生信息管理系统(文档模板供参考)
- 叉车门架有限元分析及系统设计
- 2014帮助残疾人志愿者服务情况记录
- 叶绿体中色素的提取和分离实验
- 中国食物成分表2020年最新权威完整改进版
- 推动国土资源领域生态文明建设
- 给水管道冲洗和消毒记录
- 计算机软件专业自我评价
- 高中数学必修1-5知识点归纳
- 2018-2022年中国第五代移动通信技术(5G)产业深度分析及发展前景研究报告发展趋势(目录)
- 生产车间巡查制度
- 2018版中国光热发电行业深度研究报告目录
- (通用)2019年中考数学总复习 第一章 第四节 数的开方与二次根式课件
- 2017_2018学年高中语文第二单元第4课说数课件粤教版
- 上市新药Lumateperone(卢美哌隆)合成检索总结报告
- 词法
- 分析器
- 实验
- 设计
- 初中生喝酒检讨书范文
- 利用尾气分析法解决发动机故障
- 浅析群众文化对社会和谐发展的重要性
- 基于线性模型的风电场发电量计算与分析
- 2013年温控开关市场分析报告
- 刘贤辉可可西里观后感
- 数据结构与软件方法试卷C参考答案
- 系统概要设计说明书
- 7大最常见的怀孕的征兆
- “十三五”规划重点-三酸甘油酯项目建议书(立项报告)
- 第七章导游员的服务技能1
- 澳大利亚政府审计扫描
- 010-958-数学基础综合考试大纲-学科教学
- 高速公路收费员培训心得体会
- 电喷柴油机的发展现状及前景展望
- 推荐人造皮革靠垫项目可行性研究报告(技术工艺+设备选型+财务概算+厂区规划)标准方案设计
- 3.4.2实际问题与一元一次方程(2)
- 大学生网购行为分析 论文开题报告
- 护理从学校到实习心得
- 新能源公司保洁员管理制度