【SEU&SE】编译原理 - 词法分析器实验报告

README

本篇文章仅供参考,严禁直接抄袭!

一、 实验目的

  1. 巩固有限自动机理论与正规文法、正规式三者之间的关系和相关的知识点;
  2. 了解词法分析器具体的实现方法及所用算法的原理;
  3. 编写相关的词法分析程序实现对C++语言程序文件的部分词法分析;

二、 实验环境

1. 开发环境:

  • OS:macOS Sierra 10.12.5
  • 语言:C++
  • 编译器:Xcode 8.3.3
  • 作图:ProccessOn

2. 运行环境

  • Windows、macOS或者Linux;

三、 实验内容

1. 主要内容

编写代码实现一个词法分析程序,可以对C++语言中的 .cpp 或者 .h 文件中的部分单词,如关键字、操作运算符、界符、标识符、字符串、整数、浮点数、注释等进行识别并提取出相关的单词,形成token序列,并对一些可能出现的错误进行相应的处理。

2. 主要功能

  • 读写文件:该词法分析器可以读取指定的文件,进行词法分析过程,并将得到的词法分析的token流在控制台中打印出来并写到词法分析程序根目录下的输出文件中;
  • 程序预处理:删除无用的空白字符、回车字符以及其他非实质性字符,删除两种形式的注释;
  • 词法分析:该词法分析器逐个扫描读进的文件字符流,然后进行相应的词法分析。通过扫描,找出相应的单词以及其相应的种别码,形成token序列,为输出到语法分析器中做准备;
  • 错误报告:报告词法检查中遇到的错误以及错误出现的行数,错误包括浮点数小数点错误、数字中混杂字母、字符位数过多、字符丢失单引号、字符串丢失双引号、注释格式不正确、标识符命名错误等等;

3. 种别码

单词符号种别码
main1
if2
else3
while4
do5
for6
int7
double8
float9
char10
long11
short12
enum13
static14
bool15
void16
switch17
case18
break19
continue20
signed21
unsigned22
return23
default24
const25
union26
struct27
auto28
include29
define30
class31
virtual32
friend33
public34
private35
protected36
this37
false38
true39
try40
catch41
throw42
goto43
using44
template45
new46
namespace47
operator48
register49
inline50
+51
-52
++53
54
*55
/56
%57
<58
<=59
>60
>=61
=62
==63
!=64
<<65
>>66
&&67
||68
!69
^70
,71
;72
.73
{74
}75
[76
]77
(78
)79
#80
\81
?82
标识符表83
字符常量表84
字符串常量表85
整数常量表86
浮点数常量表87

其中,1~50为基本关键字、51~70为操作运算符、71~82为界符。

4. DFA

DFA1DFA2

5. 设计思路

  1. 打开源文件,读取文件内容,直至遇上‘$’文件结束符,然后读取结束。
  2. 对读取的文件进行预处理,从头到尾进行扫描,去除 // 和 /* */ 的内容,以及一些无用的、影响程序执行的符号如换行符、回车符、制表符等。
  3. 接下来对源文件从头到尾进行扫描。从头开始扫描,这个时候分析程序首先去除开头的所有空格,直到扫描出的字符不是空格。然后判断当前字符是不是字母,若是则进行标识符和保留字的识别;若这个字符是数字,则进行数字的判断;若是界符则直接收集;若是操作运算符则进行操作运算符的判断;若是单引号则进行字符判断;若是双引号则进行字符串判断。若是将所有可能都走了一遍还是没有知道它是谁,则认定为错误符号,输出该错误符号,然后结束。每次成功识别了一个单词后,单词都会存在token[ ]中。然后确定这个单词的种别码,最后进行下一个单词的识别。
  4. 主控程序主要负责对每次识别的种别码syn进行判断,对于不同的种别做出不同的反应。如对于识别符则将其插入标识符表中;对于保留字则输出该保留字的种别码等等。直至遇到 syn = 0 ,程序结束。

6. 主要数据结构

名称类型描述
reserveWordsconst static string*存放C++部分关键字
operatorsconst static string*存放C++部分操作运算符
delimitersconst static string*存放C++部分界符
identitierTablevector<string>存放标识符
charTablevector<string>存放字符常量
stringTablevector<int>存放字符串常量
intTablevector<int>存放整数常量
floatTablevector<float>存放浮点数常量

7. 主要算法

RE to NFA to DFA to DFA’

四、 实验结果

程序1:

include <iostream>

/*
 让n+1并返回
 */
int inc(int n)
{
    return ++n;
}

int main()
{
    //声明三个变量
    int x = inc(0);
    string y = "test!";
    float z = 6.66;
    
    return 0;
}

输出1:
<include , 29> ----- 关键字
<< , 58> ----- 操作运算符
<iostream , 83> ----- 标识符
<> , 60> ----- 操作运算符
<int , 7> ----- 关键字
<inc , 83> ----- 标识符
<( , 78> ----- 界符
<int , 7> ----- 关键字
<n , 83> ----- 标识符
<) , 79> ----- 界符
<{ , 74> ----- 界符
<return , 23> ----- 关键字
<++ , 53> ----- 操作运算符
<n , 83> ----- 标识符
<; , 72> ----- 界符
<} , 75> ----- 界符
<int , 7> ----- 关键字

五、 遇到的问题及解决方法

问题:程序预处理之后进行词法分析的时候,一直提示“错误的数字”等错误。
解决方法:经过排查之后发现是程序在预处理的时候将所有空格都删除了,导致所有词素都连在了一起,所以一直报错。修改预处理部分使得空格不会被删除之后发现程序即可正常运行了。

六、 实验感想

在本次实验中,我使用C++实现了C++语言的子集的词法分析器。通过本次实验,我对词法分析的流程有了更为清晰的认识,也对课本上所学的知识有了更为深刻的理解,同时也懂得了许多在课堂上没有学到的关于编译的新知识。但总体来看本次完成的词法分析器还是比较低级的,本想可以做到提示错误并作出相应的错误处理,可是写到一半的时候才发现预处理的时候已经吧所有换行符删除了,所以这部分功能无法实现,以后若有机会的话我会将本次实验结果加以改善!

设计思想 (1)程序主体结构部分: 说明部分 %% 规则部分 %% 辅助程序部分 (2)主体结构的说明 在这里说明部分告诉我们使用的LETTER,DIGIT, IDENT(标识符,通常定义为字母开头的字母数字串)和STR(字符串常量,通常定义为双引号括起来的一串字符)是什么意思.这部分也可以包含一些初始化代码.例如用#include来使用标准的头文件和前向说明(forward ,references).这些代码应该再标记"%{"和"%}"之间;规则部分>可以包括任何你想用来分析的代码;我们这里包括了忽略所有注释中字符的功能,传送ID名称和字符串常量内容到主调函数和main函数的功能. (3)实现原理 程序中先判断这个句语句中每个单元为关键字、常数、运算符、界符,对与不同的单词符号给出不同编码形式的编码,用以区分之。 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 结束符# 四:举例说明 目标:实现对常量的判别 代码: digit [0-9] letter [A-Za-z] other_char [!-@\[-~] id ({letter}|[_])({letter}|{digit}|[_])* string {({letter}|{digit}|{other_char})+} int_num {digit}+ %% [ |\t|\n]+ "auto"|"double"|"int"|"struct"|"break"|"else"|"long"|"switch"|"case"|"enum"|"register"|"typedef"|"char"|"extern"|"return"|"union"|"const"|"float"|"short"|"unsigned"|"continue"|"for"|"signed"|"void"|"default"|"goto"|"sizeof"|"do"|"if"|"static"|"while"|"main" {Upper(yytext,yyleng);printf("%s,NULL\n",yytext);} \"([!-~])*\" {printf("CONST_string,%s\n",yytext);} -?{int_num}[.]{int_num}?([E][+|-]?{int_num})? {printf("CONST_real,%s\n",yytext);} "0x"?{int_num} {printf("CONST_int,%s\n",yytext);} ","|";"|"("|")"|"{"|"}"|"["|"]"|"->"|"."|"!"|"~"|"++"|"--"|"*"|"&"|"sizeof"|"/"|"%"|"+"|"-"|">"|"<"|">="|"<="|"=="|"!="|"&"|"^"|"|"|"&"|"||"|"+="|"-="|"*="|"/="|"%="|">>="|"<<="|"&="|"^="|"|="|"=" {printf("%s,NULL\n",yytext);} {id} {printf("ID,%s\n",yytext);} {digit}({letter})+ {printf("error1:%s\n",yytext);} %% #include <ctype.h> Upper(char *s,int l) { int i; for(i=0;i<l;i++) { s[i]=toupper(s[i]); } } yywrap() { return 1; } 五:DFA 六:数据测试 七:心得体会 其实匹配并不困难,主要是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 ch; 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:{ 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; 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; } } /*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; } 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++]; 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"); }
java编程 含有界面 以及完整代码 〈程序〉→ main()〈语句块〉 〈语句块〉→{〈语句串〉} 〈语句串〉→〈语句〉;〈语句串〉|〈语句〉; 〈语句〉→〈赋值语句〉|〈条件语句〉|〈循环语句〉 〈赋值语句〉→ ID =〈表达式〉; 〈条件语句〉→ if〈条件〉〈语句块〉 〈循环语句〉→ while〈条件〉〈语句块〉 〈条件〉→(〈表达式〉〈关系符〉〈表达式〉) 〈表达式〉→〈表达式〉〈运算符〉〈表达式〉|(〈表达式〉)|ID|NUM 〈运算符〉→+|-|*|/ 〈关系符〉→<|<=|>|>=|=|!> word.wordList包(存储了关键字): word:此类是定义了存储关键字的结构:包括String型的关键字,和int型的识别符。 wordList:此类存储了29个关键字,在构造函数中初始化。 2、word包(进行词法分析)中: basicFunction:此类定义了做词法分析的基本函数: GetChar()将下一输入字符读到ch中,搜索知识器前移一个字符位置 GetBC();检查ch中的字符是否为空白。若是,则调用GetChar直至不 是字符为止 Concat();将ch中的字符连接到strToken之后 IsLetter();判断ch中的字符是否为字母 IsDigit();判断ch中的字符是否为数字 Reserve();对strToken中的字符创查找保留字表,若是则返回它的编码,否则返回0 Retract();将搜索指示器回调一个字符位置 RetractStr();将strToken置空 lexAnalysis:此类是用来进行词法分析,将分析后的单词存入word数组中,(注:在词法分析中,若是一串字母,则认为是ID,若是数字,则认为是NUM。存储的时候识别符分别存ID与NUM的识别符,但是内容仍然是自己的内容) 其中的wordAnalysis函数就是词法分析函数(具体实现请看后面的重要函数分析) 3、stack包(定义栈)中: 栈是通过链表来定义的,因此 StringListElement:次类定义了链表的每一个节点 StringStrack:此类定义了栈,其中有长度属性,有函数: Top();用来取得栈顶 Push();压栈 Pop();出栈 4、sentence包(语法分析)中: juzi :定义了文法的句子的结构:key(左边部分) content[](右边推出的部分) lo(长度) grammar :存储了文法的27个关系式 AnalysisFB :定义了分析表的存储结构 AnalysisF :存储分析表 SentenceAnalysis :语法分析 JuProduction(word w):此函数是用来判断在当前栈与输入串的情况下,用哪一个产生式,返回产生式在数组中的下标 若输入串的第一个字符与栈顶字符相同则表示可以规约,则返回-1; 若不能过用产生式,则返回-2; AnalysisBasic(word w):此函数是分布进行语法分析,对栈操作 * 根据所需要的产生式对符号栈进行操作 * 返回0表示规约;返回1表示移进;否则表示输入串不是文法的句子 5.Main包(主界面)中 Main:此类定义了图形界面
package analysis; import java.util.ArrayList; import java.util.List; import library.Digit; import library.KeyWords; import library.Symbol; /** * * @author 周弘懿 * */ public class AnalyseWords { private List temp = new ArrayList(); public List getTemp() { return temp; } /* 关键字或者标识符的长度。 */ private static final int LENGTHOFKEYWORDS = 500; /* 整型数据的长度。 */ private static final int LENGTHOFINT = 500; /* 字符串常量长度。 */ private static final int LENGTHOFSTRING = 1024; String result = ""; int position; /** * 处理程序 * * @return 词法分析后的结果 */ public void process(String aLine) { /* 存放关键字和标识符 */ byte[] word = new byte[LENGTHOFKEYWORDS]; /* 存放数字 */ byte[] number = new byte[LENGTHOFINT]; /* 存放运算符 */ byte[] symbol = new byte[500]; /* 存放字符串常数或是字符常数 */ byte[] string = new byte[LENGTHOFSTRING]; byte temp[]; temp = aLine.getBytes(); if(new String(temp).trim().length() == 0) this.temp.add("请输入C语言程序!"); /** * 主要逻辑 */ for (position = 0; position= temp.length) { return; } /* 滤掉空格、制表键。 */ if (temp[position] != ' ' || temp[position] != '\t') { /* 允许大小写字母和下划线 */ if ((temp[position] >= 65 && temp[position] = 97 && temp[position] = 48 && temp[position] = temp.length) /* 如果已经超过界限就终止循环。 */{ String checkdWord = new String(word, 0, tempPosition + 1); decideWord(checkdWord); return position; } } while ((temp[position] >= 65 && temp[position] = 97 && temp[position] = 48 && temp[position] = 48 && temp[position] = temp.length) /* 如果已经超过界限就终止循环。 */{ String checkdNumber = new String(number, 0, tempPosition + 1); decideNum(checkdNumber); return position; } } while (temp[position] >= '0' && temp[position] <= '9' || temp[position] == '.'); String checkdNumber = new String(number, 0, tempPosition + 1); decideNum(checkdNumber); // 因为上面的已经移到末尾,因为for还要+1,所以要-1 position--; } return position; } /** * 分析字段是实型还是整型常数 * @param checkdNumber 分析数字字段 */ private void decideNum(String checkdNumber) { if (Digit.hasDot(checkdNumber)) { try { double num = Double.parseDouble(checkdNumber); this.temp.add("(" + num + ", 实型常数)\n"); } catch (Exception e) { this.temp.add("(" + checkdNumber + ", 错误,发现多个.)\n"); } } else { try { long num = Long.parseLong(checkdNumber); this.temp.add("(" + num + ", 整型常数)\n"); } catch (Exception e) { this.temp.add("(" + checkdNumber + ", 错误,不是整型常数!)\n"); } } } /** * 分析字符常数,字符串常数和边界符 * @param position 词法分析语句遍历指针 * @param string 字符,字符串和边界符的数组 * @param temp 词法分析语句数组 * @return 返回词法分析语句遍历指针的新位置 */ public int analysisLimitSymble(int position, byte[] string, byte temp[]) { // 过滤字符串常数 if (temp[position] == '"') { // 存储数组的指针 int tempPosition = -1; do { tempPosition++; string[tempPosition] = temp[position]; position++; } while (temp[position] != '"'); //要把最后一个"给过滤掉,所以要向后移一个位置。 tempPosition++; string[tempPosition] = '"'; String checkdNumber=new String(string,0,tempPosition+1); this.temp.add("(" + checkdNumber + ", 字符串常数)\n"); } // 过滤字符常数 else if (temp[position] == '\'') { // 存储数组的指针 int tempPosition = -1; do { tempPosition++; string[tempPosition] = temp[position]; position++; } while (temp[position] != '\''); tempPosition++; string[tempPosition] = '\''; //要把最后一个"给过滤掉,所以要向后移一个位置。 String checkdNumber=new String(string,0,tempPosition+1); //c语言语法规定字符常量的字符数只能是1,再加上2个’,刚好应该是3。 if(tempPosition+1= 2) { this.temp.add("(" + new String(symbol,0,tempPosition+1)+ ", 错误,字符长度仅限2位)\n"); return position; } position++; if(position >= temp.length) /* 如果已经超过界限就终止循环。 */{ String checkdWord = new String(symbol, 0, tempPosition+1); position = decideSymble(checkdWord, temp, position); return position; } } while(Symbol.isSingleSymble(new String(temp, position, 1))); String checkdWord = new String(symbol, 0, tempPosition+1); //单个运算符可能组成符合运算符 position = decideSymble(checkdWord, temp, position); position--; return position; } private int decideSymble(String checkdWord, byte temp[], int position) { if(Symbol.isComboSymbol(checkdWord)) { this.temp.add("(" + checkdWord + ", 复合运算符)\n"); } else if(Symbol.isSingleSymble(checkdWord)) { this.temp.add("(" + checkdWord + ", 运算符)\n"); }else if(checkdWord.equals("/*")) { //过滤注释 this.temp.add("(" + checkdWord + ", 前注释)\n"); do { position++; if(position >= temp.length) { return position; } } while(temp[position] != '*'); /* 直到阅读到*,表示将其间的注释都过滤 */ position++; if(position>=temp.length) { /*如果已经超过界限就终止循环。*/ this.temp.add("错误,后注释缺少结束符'/'\n"); return position; } if(temp[position]!='/') { this.temp.add("错误,后注释缺少结束符'/'\n"); } else { this.temp.add("(" + new String(temp, position-1, 2) + ", 后注释 )\n"); return position-1; } } return position; } public static void main(String[] args) { AnalyseWords aw = new AnalyseWords(); aw.process("sd"); for (String str : aw.temp) { System.out.println(str); } } }
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值