⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 新建 文本文档 (5).txt

📁 词法分析器的一个实验报告 附带的有一个词法分析器 新手可以随便看看!
💻 TXT
字号:
一、实验目的与基本要求
实验目标与要求:

1.       通过实验要学会用BNF范式定义一个简单的程序设计语言。
2.       学会用高级程序设计语言设计一词法分析器、语法分析器、中间代码产生器和解释执行代码的解释器。
3.       用这个简单的程序设计语言书写一段程序,并调试运行出来。

二、词法分析器功能:
该词法分析器能够识别两个数的相加运算的程序。输入源程序后,能输出单词符号。单词符号是一个程序语言的基本语法符号。在词法分析器所输出的单词符号表示为如下的二元式:   <单词种别,单词符号的属性值>
在单词类别中分为:
1.指向某一标识符的符号表项指针	  2.常数	3.界符	 4.运算符	 
5.关键字		6.未知单词
其中2~5的单词符号的属性值全部可以看做是同一种形式“--”。

三、原理
从左至右逐个字符地对源程序进行扫描,产生一个个单词符号,把作为字符的源程序改造成为单词符号串的中间程序。
四、	函数说明:
1.	ch:字符变量,存放最新读进的源程序字符。
2.	StrToken:字符数组,用于存放构成单词符号的字符串。
3.	GetBC( ):检查ch中的字符是否为空白。若是,则调用ch=fgetc(flp)直到ch中进入一个非空白字符。
4.	Concat( ):将ch中的字符连接到strToken之后。
5.	IsLetter( )和IsDigit( ):分别判断ch中的字符是否为字母和数字。若是则返回1,否则返回0。
6.	Reserve( ):对strToken中的字符串查找保留字表str,若它是一个保留字则返回1,否则返回0。
7.	Retract( ):将搜索指示器指针fp回调一个字符位置,并且将ch置为空白字符。
8.	text.txt文件用来存储需要被词法分析器编译的源程序。table.txt文件用来存储编译后的结果,即二元式表。

五、	编译源程序及编译后的结果
被编译的源程序zhongyuan.txt如下:

原程序:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if(x>y)
printf("%s",str);
else
scanf("%d",a);
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
输出结果:
if-->	-
9-->	-
标志符-->x	-
6-->	-
标志符-->y	-
10-->	-
printf-->	-
9--> 	-
11-->	-
7-->	-
标志符-->s	-
11-->	-
8-->	-
标志符-->str	-
10-->	-
12-->	-
else-->	-
scanf-->	-
9-->	-
11-->	-
7-->	-
标志符-->d	-
11--> 	-
8--> 	-
标志符-->a	-
10-->	-
12-->	-
********************************
单词符号	属性值
if		-
printf		-
else		-
scanf		-
标志符		地址
>		6
%		7
(		8
)		9
"		10
;		11
*********************************

六、	词法分析器的源代码如下所示:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
int   k,u;
char ch,StrToken[40]={" "},sign[40];
int q=0,p=20,a[40];
int aaa();
FILE *fp;
char *name[20]={"main","int","max","scanf","printf","if","else","return"," "};

void Getchar()
    {if(ch != EOF)
    ch=fgetc(fp);
    else
    return;
    }
void GetBC()
{
    while(ch==' ')
	{Getchar();
	}
	return;
}
void concat(char ch,char StrToken[],int u)
    {StrToken[u]=ch;
    u++;
    }
int  ISLetter (char ch)
    {if(ch<='Z'&& ch>='A'||ch<='z'&& ch>='a')
    return 1;
    else
    return 0;
    }
int IsDigit(char ch)
    {if(ch<='9'&& ch>='0')
    return 1;
    else
    return 0;
    }
int reserre(char StrToken[],char *name[])
{
	int i=0;
	while(!strcmp(name[i]," ")==0)
	{
	if(strcmp(name[i],StrToken)==0)
		return i;
		i++;
	}
	
	    return 0;


	
}
void retract(char ch)
    {fputc(ch,fp);
   ch=' ';
    }
int InsertId(char StrToken[],char sign[])
    {
    strcpy(sign,StrToken);
    sign++;
    q++;
    return q;
    }
int InsertConst(char StrToken[],int p)
    {
    int j3=0;
    a[j3]=(int)StrToken;
    p++;
    j3++;
    return p;
    }
void print1(int code)
    {printf("%d\t-->\t-\n",code);
    }
void print2(int code,int value)
    {printf("%d\t-->\t%d\n",code,value);
     }

main()
{int aaa();
fp=fopen("xw1.txt","r+");
k=aaa();
  if(k==0)
    printf("error!\n");
  else
    printf("task is over\n");
  fclose(fp);
}

int aaa()
{int code,value;
while(ch!=EOF)
{  Getchar();
 GetBC();
 u=0;
 if(ISLetter(ch))
 {
	 while(ISLetter(ch)||IsDigit(ch))
	 {    concat(ch,StrToken,u);
         Getchar();
		 GetBC();
	 }
  retract(ch);
  StrToken[u]='\0';
  code=reserre(StrToken,name);
 
   if(code==0)
  {value=InsertId(StrToken,sign);
  
  printf("关键字\t-->\t%s--> -\n",StrToken);
  }
    else
  printf("标志符\t-->\t%s--> -\n",StrToken);

 }
 else if(IsDigit(ch))
 {  
	while (IsDigit(ch))
   {concat(ch,StrToken,u);
   Getchar();
   GetBC();
   }
  retract(ch);
  code=10;
  value=InsertConst(StrToken,p);
  print2(code,value);
 }
 else if(ch=='>')
 {  
    code=6;
    print1(code);
 }
 
 else if(ch=='%')
 {code=7;
 print1(code);
 }
 else if(ch==',')
 {code=8;
 print1(code);
 }
 else  if(ch=='(')
 {code=9;
 print2(code,value);
 }
 else if(ch==')')
 {code=10;
 print1(code);
 }
 else if(ch=='"')
 {code=11;
 print1(code);
 }
 else if(ch==';')
 {code=12;
 print1(code);
 }
 else if(ch==EOF)
 return 1;
 }
 return 1;
 }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -