源程序设计语言
G[<程序>]
<程序>→<变量说明><BEGIN>
<语句表>
<END>.
<变量说明>→VAR<变量表>:<类型>;|<空>
<变量表>→<变量表>,<变量>|<变量>
<类型>→INTEGER
<语句表>→<语句>
|
<语句><语句表>
<语句>→<赋值语句>|<条件语句>|<WHILE语句>|<复合语句>
<赋值语句>→<变量>:=<算术表达式>
<条件语句>→IF<关系表达式>THEN<语句>ELSE<语句>
<WHILE语句>→WHILE<关系表达式>DO<语句>
<复合语句>→BEGIN<语句表>END
<算术表达式>→<项>|<算术表达式>+<项>|<算术表达式>-<项>
<项>→<因式>|<项>*<因式>|<项>/<因式>
<因式>→<变量>|<整数>|(<算术表达式>)
<关系表达式>→<算术表达式><关系符><算术表达式>
<变量>→<标识符>
<标识符>→<标识符><字母>|<标识符><数字>|<字母>
<整数>→0|<非零数字><泛整数>
<泛整数>→<数字>|<数字><泛整数>|ε
<关系符>→<|<=|==|>|>=|<>
<字母>
→A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z
<非零数字>→1|2|3|4|5|6|7|8|9
<数字>→<非零数字>|0
<空>→
要求和提示:
词法分析阶段,可以打开任意位置和名称的源文件进行词法分析,可以进行非法字符和数字后边跟字母的错误判断,如果没有错误则提示“词法分析正确完成!”,并且可以选择输出token.txt(token文件)string.txt(符号表)两个文件;
1.词法分析程序的主要任务如下:
①
组织源程序的输入,识别出源程序中的各个基本语法单位(也称为单词或语法符号),按规则转换成二元式的形式;
②
删除无用的空白字符、回车符、及其它非实质性符号;
③
删除注解行;
④
为后面的语法和语义分析提供二元式链表;
单词
编码
单词
编码
标识符
1
<
15
正整数
2
<=
16
BEGIN
3
>
17
END
4
>=
18
IF
5
<>
19
THEN
6
==
20
ELSE
7
;
21
WHILE
8
.
22
DO
9
:=
23
INTEGER
10
,
24
+
11
(
25
-
12
)
26
*
13
/
14
1)
对标识符的长度控制在8个字符(包括8个)以内,超过的做截断处理;
2)
数字不大于65535,否则报错;
3)
能跳过源程序中的空白格:两个单词之间的任何空格,制表符,回车,换行都是白空格,除了用来分隔单词以外,没有意义;
4)
能跳过注释:
a)
接连出现的/*到下一次接连出现的*/之间的任何文字都是注释(多行);
b)
从某行接连出现的//到该行的结尾的任何文字都是注释(单行)。
3.怎样编写词法分析程序:
1)
预处理:把源文件一个字符一个字符的读入词法分析程序设置的输入字符结构体数组中(输入缓冲区),读入过程要删除注释,删除多余的白空格;
2)
从源程序字符数组中获得单词,
编码为二元式.:
二元式采用结构体数组存储,
把单词类型和词元记录下来。
分解单词的方法:
1)
Case多路转换语句根据单词的特点直接编写;
2)
通过描述单词的正规文法得到相应的有穷自动机,通过case多路转换语句完成有穷自动机的处理流程。
3.编写词法分析程序要注意的问题:
1)
检查词法是否有错误
检查是否有非法字符:如
@,
&,
!
检查标志符和数字是否满足限制条件
检查注释符号是否配对
2)
符分隔单词
能够区分两个单词的符号为界符
有些界符不是单词:如白空格
有些界符仅仅用来分隔:如;
有些界符本身还是源程序不可缺少的单词,如(,
),
+,
/,
等等
有些界符包含两个字符:如<>,
>=等等
3)
输出词法错误
如果有错误,需要报告词法错误的原因。并且要能够越过错误,分解下一个单词,直到源程序结束。
4)
输出的二元式流保存在二元式结构体数组中。
#include <iostream>#include <string>
using namespace std
string key[6] = {"begin", "if", "then", "while", "do", "end"}
//关键字
bool isKey( string str, int &syn) //判断是否为关键字,若是传回相
应关键码的种别名
{
int i
for(i=0i<6i++)
{
if(str == key[i])
{
syn = i + 1
return true
}
}
return false
}
bool isLetter(char c) //是否为字母
{
if((c >= 'A' &&c <= 'Z') || (c >= 'a' &&c <= 'z'))
return true
else
return false
}
bool isDigit(char c) //是否为数字
{
if(c >= '0' &&c <= '9')
return true
else
return false
}
void analyse(FILE *fileP)
{
int n
char c
string str = ""
while((c = fgetc(fileP)) != EOF)
{
if(c == ' ' || c == '\n' || c == '\t')
continue
else if(isDigit(c)) //数字
{
while(isDigit(c))
{
str += c
c = fgetc(fileP)
}
fseek(fileP, -1, SEEK_CUR)
cout <<"(11, " <<str <<")" <<endl
str = ""
}
else if(isLetter(c)) //字母开头的
{
while(isDigit(c) || isLetter(c))
{
str += c
c = fgetc(fileP)
}
fseek(fileP, -1, SEEK_CUR)
if(isKey(str, n))
cout <<"(" <<n <<", " <<str <<")" <<endl//关键码
else
cout <<"(10, " <<"\'"<<str <<"\'" <<")" <<endl//标志符
str = ""
}
else //操作符等
{
switch(c)
{
case '+':
cout <<"(13, +)" <<endl
break
case '-':
cout <<"(14, -)" <<endl
break
case '*':
cout <<"(15, *)" <<endl
break
case '/':
cout <<"(16, /)" <<endl
break
case ':':
{
if(c=fgetc(fileP) == '=')
cout <<"(18, :=)" <<endl
else
{
cout <<"(17, :)" <<endl
fseek(fileP, -1, SEEK_CUR)
}
break
}
case '<':
{
c=fgetc(fileP)
if(c == '=')
cout <<"(22, <=)" <<endl
else if(c == '>')
cout <<"(21, <>)" <<endl
else
{
cout <<"(20, <)" <<endl
fseek(fileP, -1, SEEK_CUR)
}
break
}
case '>':
{
c=fgetc(fileP)
if(c == '=')
cout <<"(24, >=)" <<endl
else
{
cout <<"(23, >)" <<endl
fseek(fileP, -1, SEEK_CUR)
}
break
}
case '=':
cout <<"(25, =)" <<endl
break
case '':
cout <<"(26, )" <<endl
break
case '(':
cout <<"(27, ()" <<endl
break
case ')':
cout <<"(28, ))" <<endl
break
case '#':
cout <<"(0, #)" <<endl
break
}
}
}
}
int main()
{
FILE *fileP
fileP = fopen("test.txt", "r")
cout <<"------词法分析如下------" <<endl
analyse(fileP)
return 0
}
char operator[5]={'+','-','>','<','='}改为char oprator[6]={'+','-','>','<','='}#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
#define ERROR -2
#define OK 1
#define NULL 0
#define MAX_SIZE 100
#define INCREMENT 10
char letter[52]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'}
char *label[MAX_SIZE]
char digital[10]={'0','1','2','3','4','5','6','7','8','9'}
char delimiter[4]={',','','(',')'}
char operatour[6]={'+','-','>','<','='}
char *keyword[5]={"while","if","else"}
typedef struct{char *elem
int lengthint listsize}Number
int Init_NumList(Number *N)
{N->elem=(char *)malloc(MAX_SIZE*sizeof(char))if(!N->elem) exit(ERROR)N->length=0N->listsize=MAX_SIZEreturn OK}
int InputList(Number *N)
{int iN->length=0
for(i=0i<=9i++){N->elem[i]=digital[i]++N->length}
}int NumInsert(Number *N,int i,char s)
{char *newbase,*p,*qp=q=(char *)malloc(sizeof(char))
if(i<1||i>N->length+1) return(ERROR)if(N->length>=N->listsize)
{newbase=(char *)realloc(N->elem,(N->listsize+MAX_SIZE)*sizeof(char))if(!newbase) exit(ERROR)N->elem=newbase
N->listsize+=MAX_SIZE}q=&(N->elem[i-1])for(p=&(N->elem[N->length-1])p>=q--q) *(p+1)=*p*q=s++N->lengthreturn OK
}void Display()
{int iprintf("1-----------keyword\n")
printf("2-----------label\n")printf("3-----------number\n")
printf("4-----------operator\n")printf("5-----------delimiter\n")
printf("6-----------letter\n")for(i=0i<4i++){
printf("|1,%d,%s|\n",i,keyword[i])}
for(i=0i<=5i++){printf("|4,%d,%s|\n",i,operatour[i])}
for(i=0i<=4i++)printf("|5,%d,%s|\n",i,delimiter[i])}
int Lex_analysis(Number *N,char *s)
{int i=0,j,k=0,flagchar *SS=(char *)malloc(sizeof(char))
while(isalpha(s[i])){S[k]=s[i]if(isalpha(s[i+1])){S[k+1]=s[i+1]
for(j=0j<4j++){flag=strcmp(keyword[j],S)if(flag==0){
printf("(1,%d,%s)\t%s is a keyword!\n",j,keyword[j])break}
else{printf("The keyword is not exit!\n")}}}
else if(isdigit(s[i+1])){S[k+1]=s[i+1]label[i]=S
printf("(2,%d,%s)\t%s is a label!\n",i,label[i])}else{
for(j=0j<=52j++){if(s[i]==letter[j]){
printf("(6,%d,%s)\t%s is a letter!\n",j,s[i])break}}}++i}
while(isdigit(s[i])){S[k]=s[i]if(isdigit(s[i+1])){S[k+1]=s[i+1]
for(j=0j<=9j++){flag=strcmp(N->elem[j],*S)
if(flag){
printf("(3,%d,%s)\t%s is a keyword!\n",j,S)break}
else{
printf("The number is not exit!\n")NumInsert(N,j+1,s[i+1])}}}
else if(s[i+1]=='+'){for(j=0j<=5j++){if(operatour[j]==s[i+1])
{printf("(4,%d,%s)\t%s is addition oprator!\n",j,s[i+1],s[i+1])
break}}}
else if(s[i+1]=='-'){for(j=0j<=5j++){
if(operatour[j]==s[i+1]){
printf("(4,%d,%s)\t%s is subtraction oprator!\n",j,s[i+1],s[i+1])
break}
}
}