网站建设资讯

NEWS

网站建设资讯

词法分析器(C++)[编译原理实验1]-创新互联

实验1词法分析程序的设计与实现

资源下载:词法分析器C++.zip

10年积累的成都网站建设、做网站经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先建设网站后付款的网站建设流程,更有汕尾免费网站建设让你可以放心的选择与我们合作。1.实验目的和要求

设计、编写、调试一个具体的词法分析程序,加深对词法分析原理的理解。

2.实验内容

编译器的实现通常分成几个阶段来完成,本次实验,是为根据给定的文法实现一个小型编译器的词法分析阶段要完成的任务。请根据给定的文法设计并实现词法分析程序,从源程序中识别出单词,记录其单词类别和单词值,输入输出及处理要求如下:
(1)数据结构和与语法分析程序的接口请自行定义;类别码需按下表格式统一定义;
(2)为了方便进行自动评测,输入的被编译源文件统一命名为testfile.txt(注意不要写错文件名);输出的结果文件统一命名为output.txt(注意不要写错文件名),结果文件中每行按如下方式组织:
单词类别码 单词的字符/字符串形式(中间仅用一个空格间隔)
单词的类别码请统一按如下形式定义:
注:词法分析和语法分析实验都针对此文法,语法分析实验需在词法分析实验的基础上进行增量式开发。
[文法定义]:
<标识符>::=<字母>{<字母>|<数字>} //标识符和关键字都不区分大小写,比如if和IF均为关键字,不允许出现与关键字相同的标识符
<字母>::=_|a|...|z|A|...|Z
<数字>::=0|1|...|9
<整数>::=[+|-]<无符号整数>
<无符号整数>::=<数字>{<数字>}
<字符>::=‘<加法运算符>’|’<乘法运算符>’|’<字母>’|’<数字>’
<加法运算符>::=+|-
<乘法运算符>::=*|/
<字符串>::=“{十进制编码为32,33,35-126的ASCII字符}” //字符串中要求至少有一个字符
(3)输入形式:testfile.txt中的符合文法要求的测试程序。
(4)输出形式:要求将词法分析结果输出至output.txt中。
特别提醒:
(1)读取的字符串要原样保留着便于输出,特别是数字,这里输出的并不是真正的单词值,其实是读入的字符串,单词值需另行记录。
(2)本次实验只考核对正确程序的处理,但需要为今后可能出现的错误情况预留接口。
(3)如果为今后的编译器的进一步设计考虑,需要考虑错误处理,需要输出错误的行号,则在词法分析的时候注意记录该信息。
(4)单词的类别和单词值以及其他关注的信息,在词法分析阶段获取后,后续的分析阶段会使用,请注意记录;当前要求的输出只是为了便于评测,完成编译器中无需出现这些信息,请设计为方便打开/关闭这些输出的方案。
在这里插入图片描述

3.实验结果
  1. 算法描述
    本词法分析器按照边读入边输出的方式工作。将输入文件一个字符一个字符读入,按照给定文法确定其类型,按输出要求输出其单词类别码、单词的字符/字符串形式。
  2. 程序结构
    在这里插入图片描述
#include#include#include#include#include#include#include#includeusing namespace std;

string ReservedWordList[] = {"const", "int", "char", "void", "main", "if",
"else", "do", "while", "for", "scanf", "printf", "return" };

string category[36] = {"IDENFR", "INTCON", "CHARCON", "STRCON", "CONSTTK",
"INTTK", "CHARTK", "VOIDTK", "MAINTK", "IFTK", "ELSETK", "DOTK", "WHILETK",
"FORTK", "SCANFTK", "PRINTFTK", "RETURNTK", "PLUS", "MINU", "MULT", "DIV", "LSS",
"LEQ", "GRE", "GEQ", "EQL", "NEQ", "ASSIGN", "SEMICN", "COMMA", "LPARENT", "RPARENT",
"LBRACK", "RBRACK", "LBRACE", "RBRACE" };

vectorcodes;
//vectorW;
//vectorC;

bool IsLetter(char c) {if (isalpha(c) || (c == '_'))
		return true;
	return false;
}
bool IsDigit(char c) {return isdigit(c);
}
int Reserved(string s) {for (int i = 0;i< 13;i++) {if (s == ReservedWordList[i])
			return i + 4;
	}
	return 0;
}

void read(string codefile) {ifstream input;
	input.open(codefile.c_str());
	if (!input.is_open()) {cout<< "Fail to open testfile.txt."<< endl;;
		exit(0);
	}
	while (input.peek() != EOF) {char c = input.get();
		codes.push_back(c);
	}
	codes.push_back(' ');

	//测试 
	//for(int i=0;i//		cout<ofstream out(tokenfile);
	unsigned i = 0;
	char c = codes[i++];
	while (i< codes.size()){string word = "";
		while (c == ' ' || c == '\t' || c == '\n')
			c = codes[i++];
		if (IsLetter(c)) {	while (IsLetter(c) || IsDigit(c)) {		word.push_back(c);
				c = codes[i++];
			}
			if (Reserved(word)) {//保留字 
				//W.push_back(word);
				//C.push_back(category[Reserved(word)]);
				out<< category[Reserved(word)]<< " "<< word<< endl;
			}
			else {//标识符 
				//W.push_back(word);
				//C.push_back(category[0]);
				out<< category[0]<< " "<< word<< endl;
			}
		}
		else if (IsDigit(c)) {//整数 
				while (IsDigit(c)) {word.push_back(c);
					c = codes[i++];
				}
				//W.push_back(word);
				//C.push_back(category[1]);
				out<< category[1]<< " "<< word<< endl;
			}
		else {	if (c == '+') {		word.push_back(c);
				//W.push_back(word);
				//C.push_back(category[17]);
				out<< category[17]<< " "<< word<< endl;
			}
			else if (c == '-') {		word.push_back(c);
				//W.push_back(word);
				//C.push_back(category[18]);
				out<< category[18]<< " "<< word<< endl;
			}
			else if (c == '*') {		word.push_back(c);
				//W.push_back(word);
				//C.push_back(category[19]);
				out<< category[19]<< " "<< word<< endl;
			}
			else if (c == '/') {		word.push_back(c);
				//W.push_back(word);
				//C.push_back(category[20]);
				out<< category[20]<< " "<< word<< endl;
			}
			else if (c == '<') {		word.push_back(c);
				c = codes[i++];
				if (c == '=') {word.push_back(c);
					//W.push_back(word);
					//C.push_back(category[22]);
					out<< category[22]<< " "<< word<< endl;
				}
				else
				{//W.push_back(word);
					//C.push_back(category[21]);
					out<< category[21]<< " "<< word<< endl;
				}
			}
			else if (c == '>') {		word.push_back(c);
				c = codes[i++];
				if (c == '='){word.push_back(c);
					//W.push_back(word);
					//C.push_back(category[24]);
					out<< category[24]<< " "<< word<< endl;
				}
				else
				{//W.push_back(word);
					//C.push_back(category[23]);
					out<< category[23]<< " "<< word<< endl;
				}
			}
			else if (c == '=') {		word.push_back(c);
				c = codes[i++];
				if (c == '=')
				{word.push_back(c);
					//W.push_back(word);
					//C.push_back(category[25]);
					out<< category[25]<< " "<< word<< endl;
				}
				else
				{//W.push_back(word);
					//C.push_back(category[27]);
					out<< category[27]<< " "<< word<< endl;
				}
			}
			else if (c == '!') {		word.push_back(c);
				c = codes[i++];
				word.push_back(c);
				if (c == '=')
				{//W.push_back(word);
					//C.push_back(category[26]);
					out<< category[26]<< " "<< word<< endl;
				}
			}
			else if (c == ';') {		word.push_back(c);
				//W.push_back(word);
				//C.push_back(category[28]);
				out<< category[28]<< " "<< word<< endl;
			}
			else if (c == ',') {		word.push_back(c);
				//W.push_back(word);
				//C.push_back(category[29]);
				out<< category[29]<< " "<< word<< endl;
			}
			else if (c == '(') {		word.push_back(c);
				//W.push_back(word);
				//C.push_back(category[30]);
				out<< category[30]<< " "<< word<< endl;
			}
			else if (c == ')') {		word.push_back(c);
				//W.push_back(word);
				//C.push_back(category[31]);
				out<< category[31]<< " "<< word<< endl;
			}
			else if (c == '[') {		word.push_back(c);
				//W.push_back(word);
				//C.push_back(category[32]);
				out<< category[32]<< " "<< word<< endl;
			}
			else if (c == ']') {		word.push_back(c);
				//W.push_back(word);
				//C.push_back(category[33]);
				out<< category[33]<< " "<< word<< endl;
			}
			else if (c == '{') {		word.push_back(c);
				//W.push_back(word);
				//C.push_back(category[34]);
				out<< category[34]<< " "<< word<< endl;
			}
			else if (c == '}') {		word.push_back(c);
				//W.push_back(word);
				//C.push_back(category[35]);
				out<< category[35]<< " "<< word<< endl;
			}
			else if (int(c) == 34) {		c = codes[i++];
				while (int(c) != 34) {word.push_back(c);
					c = codes[i++];
				}
				//W.push_back(word);
				//C.push_back(category[3]);
				out<< category[3]<< " "<< word<< endl;
			}
			else if (int(c) == 39) {		c = codes[i++];
				while (int(c) != 39) {word.push_back(c);
					c = codes[i++];
				}
				//W.push_back(word);
				//C.push_back(category[2]);
				out<< category[2]<< " "<< word<< endl;
			}
			c = codes[i];
			i++;
		}
		//else ProcError();
	}
	codes.clear();
	out.close();
}

//void write(string tokenfile) {//	ofstream output;
//	output.open(tokenfile.c_str());
//	if (!output.is_open()) {//		cout<< "Fail to open the output.txt."<< endl;
//		exit(0);
//	}
//	for (unsigned i = 0;i< W.size();i++)
//		output<< C[i]<< " "<< W[i]<< endl;
//	output.close();
//	C.clear();
//	W.clear();
//}

int main() {	//读文件 
	read("testfile.txt");
	//cout<< "read is complete"<< endl;

	WordAnalysis("output.txt");
	//cout<< "WordAnalysis is complete"<< endl;

	//写文件 
	//write(tokenfile);
	//cout<< "write is complete"<< endl;

	return 0;
}
  1. 实验运行结果(这里用一个样例测试举例)
    (1) testfile.txt_01.in:
const int _begini = 0, _endi = +18, testfg = -6;
const char lowera = 'a', lowerz = 'z', capitalZ = 'Z', char2 = '2', minusSy = '-';

int fibicnum[20], random_name1, random_name2;
char alphaBet[26];

void init(){int i;
    printf("---- ---- ---- test for (0->17)");
    for(i = 0; i< _endi; i = i + 1){fibicnum[i] = 1;
        if( i - (+i/3)*3 == 0)
            printf("i = ", i);
    }
    printf("end i = ", i);
    printf("---- ---- ---- test for (15->-5)");
    for(i = 15; i >= -5; i = i - 1){printf("i = ", i);
    }
    printf("end i = ", i);
    return;
}

int fibic(int a){const int return1 = 1;
    if(a >return1){if(a-2 >1)
            if(fibicnum[a-2]<= 1){fibicnum[a-2] = fibic(a-2);
                printf("fibic ", a-2);
                printf(" = ", fibicnum[a-2]);
            }
        if(a-1 >1)
            if(fibicnum[a-1]<= 1){fibicnum[a-1] = fibic(a-1);
                printf("fibic ", a-1);
                printf(" = ", fibicnum[a-1]);
            }
        fibicnum[a] = fibicnum[a-1] + fibicnum[a-2];
        return (fibicnum[a]);
    }
    else{return (1);
    }
}

int abs(int kk){if(kk< 0){return (-kk);
    }
    return (kk);
}

char plus(){char plussy;
    plussy = '+';
    return (plussy);
}

int sum4(int a, int b, int c, int d){int sum;
    sum = a + b + c + d;
    printf("---------------");
    printf("a = ", a);
    printf("b = ", b);
    printf("c = ", c);
    printf("d = ", d);
    printf("sum = ", sum);
    printf("---------------");
    return (sum);
}

void main() {const int maxFibic = 13;
    const char chartimes = '*';
    int cal;
    char outch, underline;
    int toPrinti;
    printf("---- ---- ---- test scanf(intput int)");
    scanf(toPrinti);
    printf(toPrinti);
    printf("---- ---- ---- test scanf(intput int char)");
    scanf(cal, outch);
    printf("\n");
    if(cal){printf("input cal != 0, cal = ", cal);
        cal = abs(cal);
    }
    if(cal >= maxFibic + 1){printf("cal value should<= ", maxFibic);
        cal = maxFibic;
    }
    init();
    printf("---- ---- ---- test call func");
    printf("fibic val = ",fibic(cal));
    printf("abs(-7) = ", abs(-7));
    printf("---- ---- ---- test while (0 ->6)");
    cal = 0;
    while(cal< 7){printf("cal = ", cal);
        cal = cal + 1;
    }
    printf("---- ---- ---- test do while (-17->3)");
    cal = -17;
    do{printf("cal = ", cal);
        cal = cal + 1;
    }while(cal< 4);
    printf("---- ---- ---- test char val");
    underline = '_';
    printf("+-*/_");
    printf(plus());
    printf(minusSy);
    printf(chartimes);
    printf('/');
    printf(underline);
    printf(outch);
    printf("---- ---- ---- test func call");
    printf("(expect 91)sum4=", sum4(1, sum4(2, sum4(3, 4, 5, 6), 7, 8), 9, sum4(10, 11, 12, 13)));
    if(sum4(1, 2, 3, 4)!=10){printf("1 + 2 + 3 + 4 != 10");
    }
    else{printf("1 + 2 + 3 + 4 == 10");
    }
}

testfile.txt_01.out:
CONSTTK const
INTTK int
IDENFR _begini
ASSIGN =
INTCON 0
COMMA ,
IDENFR _endi
ASSIGN =
PLUS +
INTCON 18
COMMA ,
IDENFR testfg
ASSIGN =
MINU -
INTCON 6
SEMICN ;
CONSTTK const
CHARTK char
IDENFR lowera
ASSIGN =
CHARCON a
COMMA ,
IDENFR lowerz
ASSIGN =
CHARCON z
COMMA ,
IDENFR capitalZ
ASSIGN =
CHARCON Z
COMMA ,
IDENFR char2
ASSIGN =
CHARCON 2
COMMA ,
IDENFR minusSy
ASSIGN =
CHARCON -
SEMICN ;
INTTK int
IDENFR fibicnum
LBRACK [
INTCON 20
RBRACK ]
COMMA ,
IDENFR random_name1
COMMA ,
IDENFR random_name2
SEMICN ;
CHARTK char
IDENFR alphaBet
LBRACK [
INTCON 26
RBRACK ]
SEMICN ;
VOIDTK void
IDENFR init
LPARENT (
RPARENT )
LBRACE {
INTTK int
IDENFR i
SEMICN ;
PRINTFTK printf
LPARENT (
STRCON ---- ---- ---- test for (0->17)
RPARENT )
SEMICN ;
FORTK for
LPARENT (
IDENFR i
ASSIGN =
INTCON 0
SEMICN ;
IDENFR i
LSS<
IDENFRendi
SEMICN ;
IDENFR i
ASSIGN =
IDENFR i
PLUS +
INTCON 1
RPARENT )
LBRACE {
IDENFR fibicnum
LBRACK [
IDENFR i
RBRACK ]
ASSIGN =
INTCON 1
SEMICN ;
IFTK if
LPARENT (
IDENFR i
MINU -
LPARENT (
PLUS +
IDENFR i
DIV /
INTCON 3
RPARENT )
MULT *
INTCON 3
EQL ==
INTCON 0
RPARENT )
PRINTFTK printf
LPARENT (
STRCON i =
COMMA ,
IDENFR i
RPARENT )
SEMICN ;
RBRACE }
PRINTFTK printf
LPARENT (
STRCON end i =
COMMA ,
IDENFR i
RPARENT )
SEMICN ;
PRINTFTK printf
LPARENT (
STRCON ---- ---- ---- test for (15->-5)
RPARENT )
SEMICN ;
FORTK for
LPARENT (
IDENFR i
ASSIGN =
INTCON 15
SEMICN ;
IDENFR i
GEQ >=
MINU -
INTCON 5
SEMICN ;
IDENFR i
ASSIGN =
IDENFR i
MINU -
INTCON 1
RPARENT )
LBRACE {
PRINTFTK printf
LPARENT (
STRCON i =
COMMA ,
IDENFR i
RPARENT )
SEMICN ;
RBRACE }
PRINTFTK printf
LPARENT (
STRCON end i =
COMMA ,
IDENFR i
RPARENT )
SEMICN ;
RETURNTK return
SEMICN ;
RBRACE }
INTTK int
IDENFR fibic
LPARENT (
INTTK int
IDENFR a
RPARENT )
LBRACE {
CONSTTK const
INTTK int
IDENFR return1
ASSIGN =
INTCON 1
SEMICN ;
IFTK if
LPARENT (
IDENFR a
GRE >
IDENFR return1
RPARENT )
LBRACE {
IFTK if
LPARENT (
IDENFR a
MINU -
INTCON 2
GRE >
INTCON 1
RPARENT )
IFTK if
LPARENT (
IDENFR fibicnum
LBRACK [
IDENFR a
MINU -
INTCON 2
RBRACK ]
LEQ<=
INTCON 1
RPARENT )
LBRACE {
IDENFR fibicnum
LBRACK [
IDENFR a
MINU -
INTCON 2
RBRACK ]
ASSIGN =
IDENFR fibic
LPARENT (
IDENFR a
MINU -
INTCON 2
RPARENT )
SEMICN ;
PRINTFTK printf
LPARENT (
STRCON fibic
COMMA ,
IDENFR a
MINU -
INTCON 2
RPARENT )
SEMICN ;
PRINTFTK printf
LPARENT (
STRCON =
COMMA ,
IDENFR fibicnum
LBRACK [
IDENFR a
MINU -
INTCON 2
RBRACK ]
RPARENT )
SEMICN ;
RBRACE }
IFTK if
LPARENT (
IDENFR a
MINU -
INTCON 1
GRE >
INTCON 1
RPARENT )
IFTK if
LPARENT (
IDENFR fibicnum
LBRACK [
IDENFR a
MINU -
INTCON 1
RBRACK ]
LEQ<=
INTCON 1
RPARENT )
LBRACE {
IDENFR fibicnum
LBRACK [
IDENFR a
MINU -
INTCON 1
RBRACK ]
ASSIGN =
IDENFR fibic
LPARENT (
IDENFR a
MINU -
INTCON 1
RPARENT )
SEMICN ;
PRINTFTK printf
LPARENT (
STRCON fibic
COMMA ,
IDENFR a
MINU -
INTCON 1
RPARENT )
SEMICN ;
PRINTFTK printf
LPARENT (
STRCON =
COMMA ,
IDENFR fibicnum
LBRACK [
IDENFR a
MINU -
INTCON 1
RBRACK ]
RPARENT )
SEMICN ;
RBRACE }
IDENFR fibicnum
LBRACK [
IDENFR a
RBRACK ]
ASSIGN =
IDENFR fibicnum
LBRACK [
IDENFR a
MINU -
INTCON 1
RBRACK ]
PLUS +
IDENFR fibicnum
LBRACK [
IDENFR a
MINU -
INTCON 2
RBRACK ]
SEMICN ;
RETURNTK return
LPARENT (
IDENFR fibicnum
LBRACK [
IDENFR a
RBRACK ]
RPARENT )
SEMICN ;
RBRACE }
ELSETK else
LBRACE {
RETURNTK return
LPARENT (
INTCON 1
RPARENT )
SEMICN ;
RBRACE }
RBRACE }
INTTK int
IDENFR abs
LPARENT (
INTTK int
IDENFR kk
RPARENT )
LBRACE {
IFTK if
LPARENT (
IDENFR kk
LSS<
INTCON 0
RPARENT )
LBRACE {
RETURNTK return
LPARENT (
MINU -
IDENFR kk
RPARENT )
SEMICN ;
RBRACE }
RETURNTK return
LPARENT (
IDENFR kk
RPARENT )
SEMICN ;
RBRACE }
CHARTK char
IDENFR plus
LPARENT (
RPARENT )
LBRACE {
CHARTK char
IDENFR plussy
SEMICN ;
IDENFR plussy
ASSIGN =
CHARCON +
SEMICN ;
RETURNTK return
LPARENT (
IDENFR plussy
RPARENT )
SEMICN ;
RBRACE }
INTTK int
IDENFR sum4
LPARENT (
INTTK int
IDENFR a
COMMA ,
INTTK int
IDENFR b
COMMA ,
INTTK int
IDENFR c
COMMA ,
INTTK int
IDENFR d
RPARENT )
LBRACE {
INTTK int
IDENFR sum
SEMICN ;
IDENFR sum
ASSIGN =
IDENFR a
PLUS +
IDENFR b
PLUS +
IDENFR c
PLUS +
IDENFR d
SEMICN ;
PRINTFTK printf
LPARENT (
STRCON ---------------
RPARENT )
SEMICN ;
PRINTFTK printf
LPARENT (
STRCON a =
COMMA ,
IDENFR a
RPARENT )
SEMICN ;
PRINTFTK printf
LPARENT (
STRCON b =
COMMA ,
IDENFR b
RPARENT )
SEMICN ;
PRINTFTK printf
LPARENT (
STRCON c =
COMMA ,
IDENFR c
RPARENT )
SEMICN ;
PRINTFTK printf
LPARENT (
STRCON d =
COMMA ,
IDENFR d
RPARENT )
SEMICN ;
PRINTFTK printf
LPARENT (
STRCON sum =
COMMA ,
IDENFR sum
RPARENT )
SEMICN ;
PRINTFTK printf
LPARENT (
STRCON ---------------
RPARENT )
SEMICN ;
RETURNTK return
LPARENT (
IDENFR sum
RPARENT )
SEMICN ;
RBRACE }
VOIDTK void
MAINTK main
LPARENT (
RPARENT )
LBRACE {
CONSTTK const
INTTK int
IDENFR maxFibic
ASSIGN =
INTCON 13
SEMICN ;
CONSTTK const
CHARTK char
IDENFR chartimes
ASSIGN =
CHARCON *
SEMICN ;
INTTK int
IDENFR cal
SEMICN ;
CHARTK char
IDENFR outch
COMMA ,
IDENFR underline
SEMICN ;
INTTK int
IDENFR toPrinti
SEMICN ;
PRINTFTK printf
LPARENT (
STRCON ---- ---- ---- test scanf(intput int)
RPARENT )
SEMICN ;
SCANFTK scanf
LPARENT (
IDENFR toPrinti
RPARENT )
SEMICN ;
PRINTFTK printf
LPARENT (
IDENFR toPrinti
RPARENT )
SEMICN ;
PRINTFTK printf
LPARENT (
STRCON ---- ---- ---- test scanf(intput int char)
RPARENT )
SEMICN ;
SCANFTK scanf
LPARENT (
IDENFR cal
COMMA ,
IDENFR outch
RPARENT )
SEMICN ;
PRINTFTK printf
LPARENT (
STRCON
RPARENT )
SEMICN ;
IFTK if
LPARENT (
IDENFR cal
RPARENT )
LBRACE {
PRINTFTK printf
LPARENT (
STRCON input cal != 0, cal =
COMMA ,
IDENFR cal
RPARENT )
SEMICN ;
IDENFR cal
ASSIGN =
IDENFR abs
LPARENT (
IDENFR cal
RPARENT )
SEMICN ;
RBRACE }
IFTK if
LPARENT (
IDENFR cal
GEQ >=
IDENFR maxFibic
PLUS +
INTCON 1
RPARENT )
LBRACE {
PRINTFTK printf
LPARENT (
STRCON cal value should<=
COMMA ,
IDENFR maxFibic
RPARENT )
SEMICN ;
IDENFR cal
ASSIGN =
IDENFR maxFibic
SEMICN ;
RBRACE }
IDENFR init
LPARENT (
RPARENT )
SEMICN ;
PRINTFTK printf
LPARENT (
STRCON ---- ---- ---- test call func
RPARENT )
SEMICN ;
PRINTFTK printf
LPARENT (
STRCON fibic val =
COMMA ,
IDENFR fibic
LPARENT (
IDENFR cal
RPARENT )
RPARENT )
SEMICN ;
PRINTFTK printf
LPARENT (
STRCON abs(-7) =
COMMA ,
IDENFR abs
LPARENT (
MINU -
INTCON 7
RPARENT )
RPARENT )
SEMICN ;
PRINTFTK printf
LPARENT (
STRCON ---- ---- ---- test while (0 ->6)
RPARENT )
SEMICN ;
IDENFR cal
ASSIGN =
INTCON 0
SEMICN ;
WHILETK while
LPARENT (
IDENFR cal
LSS<
INTCON 7
RPARENT )
LBRACE {
PRINTFTK printf
LPARENT (
STRCON cal =
COMMA ,
IDENFR cal
RPARENT )
SEMICN ;
IDENFR cal
ASSIGN =
IDENFR cal
PLUS +
INTCON 1
SEMICN ;
RBRACE }
PRINTFTK printf
LPARENT (
STRCON ---- ---- ---- test do while (-17->3)
RPARENT )
SEMICN ;
IDENFR cal
ASSIGN =
MINU -
INTCON 17
SEMICN ;
DOTK do
LBRACE {
PRINTFTK printf
LPARENT (
STRCON cal =
COMMA ,
IDENFR cal
RPARENT )
SEMICN ;
IDENFR cal
ASSIGN =
IDENFR cal
PLUS +
INTCON 1
SEMICN ;
RBRACE }
WHILETK while
LPARENT (
IDENFR cal
LSS<
INTCON 4
RPARENT )
SEMICN ;
PRINTFTK printf
LPARENT (
STRCON ---- ---- ---- test char val
RPARENT )
SEMICN ;
IDENFR underline
ASSIGN =
CHARCON _
SEMICN ;
PRINTFTK printf
LPARENT (
STRCON ±*/

RPARENT )
SEMICN ;
PRINTFTK printf
LPARENT (
IDENFR plus
LPARENT (
RPARENT )
RPARENT )
SEMICN ;
PRINTFTK printf
LPARENT (
IDENFR minusSy
RPARENT )
SEMICN ;
PRINTFTK printf
LPARENT (
IDENFR chartimes
RPARENT )
SEMICN ;
PRINTFTK printf
LPARENT (
CHARCON /
RPARENT )
SEMICN ;
PRINTFTK printf
LPARENT (
IDENFR underline
RPARENT )
SEMICN ;
PRINTFTK printf
LPARENT (
IDENFR outch
RPARENT )
SEMICN ;
PRINTFTK printf
LPARENT (
STRCON ---- ---- ---- test func call
RPARENT )
SEMICN ;
PRINTFTK printf
LPARENT (
STRCON (expect 91)sum4=
COMMA ,
IDENFR sum4
LPARENT (
INTCON 1
COMMA ,
IDENFR sum4
LPARENT (
INTCON 2
COMMA ,
IDENFR sum4
LPARENT (
INTCON 3
COMMA ,
INTCON 4
COMMA ,
INTCON 5
COMMA ,
INTCON 6
RPARENT )
COMMA ,
INTCON 7
COMMA ,
INTCON 8
RPARENT )
COMMA ,
INTCON 9
COMMA ,
IDENFR sum4
LPARENT (
INTCON 10
COMMA ,
INTCON 11
COMMA ,
INTCON 12
COMMA ,
INTCON 13
RPARENT )
RPARENT )
RPARENT )
SEMICN ;
IFTK if
LPARENT (
IDENFR sum4
LPARENT (
INTCON 1
COMMA ,
INTCON 2
COMMA ,
INTCON 3
COMMA ,
INTCON 4
RPARENT )
NEQ !=
INTCON 10
RPARENT )
LBRACE {
PRINTFTK printf
LPARENT (
STRCON 1 + 2 + 3 + 4 != 10
RPARENT )
SEMICN ;
RBRACE }
ELSETK else
LBRACE {
PRINTFTK printf
LPARENT (
STRCON 1 + 2 + 3 + 4 == 10
RPARENT )
SEMICN ;
RBRACE }
RBRACE }

4.设计技巧

边读文件边词法分析输出,会占用更少的内存,时间更快,效率更高。

5.心得体会

通过这次词法分析器设计实验,我对于课堂上学到的词法分析有了更深刻的理解。
在这次实验中,加强了边写代码边测试输出的意识,这样子写出的代码就不会最后出错不知道从哪查起,这次实验动手编程提高了自己的编程能力和检错能力。

你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧


本文题目:词法分析器(C++)[编译原理实验1]-创新互联
当前网址:http://njwzjz.com/article/dgsidd.html