词法分析器报告
一、任务与目的
·任务:
1、使用C/C++程序设计语言和递归下降子程序的方法编写该函数绘图语言的词法分析器。并要求设计一个词法分析器的测试小程序来调用自己编写的词法分析器测试各种不同的输入。
2、词法分析的任务是对输入的字符串形式的源程序按顺序进行扫描,在扫描的同时,根据源语言的词法规则识别具有独立意义的单词(符号),并产生与其等价的属性字流(内部编码)作为输出。通常属性字流即是对识别的单词给出的标记符号的集合。
·目的:
通过自己动手编写词法分析器,掌握记号、模式与单词,掌握正规式与正规集,掌握有限自动机,掌握如何从正规式到词法分析器的各种算法。理解如何理论联系实际以及明白理论与实际的差别。
二、分析与设计
词法分析器的本质:基本任务是进行模式匹配,其关键在于分析过程中的模式说明和模式识别方法,在编译分析中即正规表达式和有限自动机。
构造词法分析器方法:1、手工构造;2、利用自动生成工具LEX。但是无论用那种方法,其内在工作原理都是相同的,都要经过正规式到最小状态DFA的转换。
词法分析器可有两种:一种是把词法分析器作为语法分析的一个子程序,一种是把词法分析器作为编译程序的独立一遍.在前一种情况下,词法分析器不断地被语法分析器调用,每调用一次词法分析器将从源程序的字符序列拼出一个单词,并将其Token值返回给语法分析器.后一种情况则不同,词法分析器不是被语法分析器不断地调用,而是一次扫描全部单词完成编译器的独立一遍任务。
词法分析程序一般具有如下功能:读入字符串形式的源程序;识别出具有独立意义的最小语法单位:单词。
事实上,由正规表达式到最小化DFA的转换源程序中的测试生成串部分就是对所输入的单词进行判断,看其是否能被生成的DFA接受(也就是这个单词是否符合正规式定义的要求)。这本质上就是一个简单的词法分析。
定义某种语言的单词,并给出编号。该语言单词包括:保留字、运算符、标识符、常量、格式符等。根据给定的语言子集构造词法分析器。输出为中间文件。
在设计时为了便于理解,不使用内部编码而用枚举对同类型的单词进行标识。例如所有的常量统一用“CONST_ID”对其进行标识,当扫描时遇到常量就输出该常量的值和“CONST_ID”标识。
这里给出词法分析程序大概的设计方法:
1、根据要求写出词法分析的正规文法G;
2、根据正规文法G,写出正则式RE;
3、根据正则式RE,画出NFA;
4、将NFA转化为DFA;
5、将DFA转化为mininum state DFA;
6、mininum state DFA就是词法分析程序的流程图,根据此流程图编写相应的词 法分析程序。
以下是较为详细的设计:
①总体结构与模块划分
②重要数据结构
·枚举记号种类
·记号与符号表结构
·符号表
③关键思想与算法
·构造NFA的Thompson算法
·模拟NFA的“并行”算法
·从NFA构造DFA:构造DFA的子集法,smove(S, a)函数和e_闭包(T)的计算
·DFA的最小化:利用可区分的概念,将所有不可区分的状态看作是一个状态
三、测试例程设计
·测试程序(scannermain.cpp)
·测试数据(test.txt)
四、测试结果及分析
·结果分析
该词法分析器的输出为一堆记号流,这些记号流正确的反映出了绘图语言源程序中的各个单词的类型。例如:“FOR”被识别为“关键字”类别;“SUYANG”被识别为错误的TOKEN等等。并且对注释性语句也正确的识别了。
在测试过程中需要说明三点出现的问题及错误:
1、“**”POWER的正确识别
2、和开发环境有关的错误
这个问题只针对我的机器,在别的机器上不一定会出现该问题,我想是由于硬件平台的问题(可能和我的CPU是双核有关)。
3、在VS2005中生成工程时会产生两个警告(C4996, C4313)
这个问题比较容易解决,只需简单的屏蔽掉这两个警告即可。在scanner.h文件中加上以下两条语句:
五、总结与体会
主要学习和体会了基于编译器构造技术中的由正规表达式到最小化DFA的算法设计和实现技术;主要包括由正规表达式构造NFA所用到的Thompson构造法、把NFA转化为与其等价的DFA所使用的子集构造算法以及把DFA最小化的算法,最后实现词法分析。Thompson构造法根据读入的正规表达式的不同字符进入相应的转换处理。NFA转化为与其等价的DFA需分两步进行:a、构造NFA N的状态K的子集的算法;b、计算e-closure。完成这些子模块的设计后,再通过某一中间模块的总控程序对其调用,最后再由主程序合并调用。在算法实现过程中,主要使用visual C++进行编程。正规式与自动机理论在词法构造乃至整个编译器构造过程中起着至关重要的作用,同时它们被广泛应用于计算机科学的各个领域,它们与计算机其它学科之间也有着很大的联系。
第二篇:c词法分析器
C语言词法分析器的设计与实现
一.实验目的:
1.强化对系统软件综合工程实现能力、规划能力的训练;
2.加强对词法分析原理、方法和基本实现技术的理解;
二.实验内容:
用C语言(或 C++ )作为宿主语言完成:
C语言(ANSI C或turbo C 2.0)词法分析器的设计和实现。
三、程序简要说明:
1、属性字说明:
共9种
KEYWORD: 关键字
"auto","break","case","char","const","continue",
"default","do","double","else","enum","extern",
"float","for","goto","if","int","long","register",
"return","short","signed","sizeof","static","struct",
"switch","typedef","union","unsigned","void",
"volatile","while","include","define"
共34个
详见IsKeyWord()函数
NUMBER: 数字
包括十进制整数,八进制整数,二进制整数,十六进制整数
还可返回几种错误
详见IsNumber()函数
CONSTANT: 常量
宏定义标识符,不识别C++关键字const,简单处理一层宏定义。
详见IsDefined()函数
ERROR: 错误
可以识别几种简单的错误,数字四种,单双引号不匹配,注释符不匹配等。
FUNCTIONNAME: 函数名
可以识别自定义函数和库函数,不识别C++语法
详见IsFunctionName()函数
VARIABLE: 变量名
所有其他属性字特征之外的属性
TRANSLATION: 转义字符
'\b', \'n', '\r', '\t', '\"', '\'', '\0', '\\', '\v', '\a', '\o', '\x'共12个
详见源代码417-444行
OPERATOR: 运算符
+-*/%等
INCLUDEFILE: 包含的文件
#include <>或””中的字符串
详见IsInclude()函数
2、原程序主要流程:
源程序使用纯C语法,提供了一个字符界面,重写了scanf(源程序中为scanff())函数,以方便用户界面的交互。
总流程:
分析并生成终结果文件流程:
3、主要数据结构:
本程序大量采用缓冲机制,这样做的好处显而易见,提高了程序的执行效率,简化了代码编写的难度,最重要的是,缓解了C语言对内存支配限制的压力。
文件打开以后,对行设立行缓冲区,对属性字设置属性字缓冲区,同时对于判断中的常量储存,关键字存储均以缓冲提供进算法,程序没有任何全局变量,初始化界面后,程序只有一个入口getFileName();这是用户界面与核心程序之间的接口,而程序的核心算法同样只有一个接口analyse(),接口提供的参数只是文件的文件名,这样做的好处是,程序通过analyse()可以随处使用。
另外程序设置了大量的函数,在获得完整的属性字后,通过函数判断属性,并返回属性编号,在输出时,并未对编号进行处理,因为程序之后并没有更进一步的语法分析衔接。
判断采用自动机理论,满足终态条件则返回以获得的属性字。
程序在输出上同样提供了非常人性化的设置,如输出先输出每一行的内容,之后在列出属性字流。
4、部分附加功能处理技巧说明:
由于程序配合了行列号的输出,行号非常容易处理,但是列号要涉及很多空格,回车,制表符等,其中最难处理的是制表符,制表符的长度在判断算法中为8,则通过语句
col += ((TAB - (*col+1) % TAB) % TAB);
可以使col获得期望中的列号。
对单层宏定义,则必然是#define * *的格式,算法将第一个*中的内容存入数组(definebuf),后面接一个空白符,再存入第二个*的内容
definebuf的结构
对函数名的判断,这个算法并不是简单的字符串匹配,而是要看这个标识符后面的内容,
由于程序对单行判断,除多行注释,并未提供匹配记忆,所以对于一下写法程序无能为力
printf /*******函数调用信息另起了一行,将不能正确识别*******/
(“abcd”);
所以在使用本程序时,请提供尽量规范的代码
对于C++语法规则int i(3);程序将会把i判断为函数名,所以请使用标准C语言的代码。本人能力有限,部分算法有待改进。
5、函数列表和简单说明
void init(); //初始化图形界面
void messageBox(); //画框函数,用于图形界面
void button(); //生成一个图形按钮
void rightBox(); //更新界面右侧打印信息
int scanff(); //从键盘获得字符串
void getFileName(); //图形交互接口,提供了TAB,UPARROW等按钮的功能
void analyse(); //内核接口
void checkOutNotes(); //检查并生成注释文件
void checkOutWordsStream(); //词法分析函数
void checkDefine(); //检查并保存宏定义
void fputline(); //在生成文件中写入当前分析中的行内容
void fputEnd(); //在生成文件中打印一个回车
void fputTip(); //在生成文件中打印行列号
void fputcc(); //在生成的文件中打印制表符并返回虚拟列号
void fputWord(); //在生成文件中写入完整的一行
void fputdefine(); //在生成的文件中写入常量及其值
int noUse(); //判断是否为无用字符'\t','\n','\r','\0',' '
int isKeyWord(); //判断是否为关键字
int isNumber(); //判断是否为数字
int isFunctionName(); //判断是否是函数名
int isInclude(); //判断是否是包含文件
int isDefined(); //判断是否为宏(即常量)
int readLine(); //读行
int readWord(); //读入一个属性字
int cton(); //将字符转化为数字
int ntoc(); //将数字转化为字符
#include "stdio.h"
#include "string.h"
#include "dos.h"
#include "conio.h"
#include "ctype.h"
#include "sys/stat.h"
#define OK 1
#define TAB 8
void init();
int scanff();
void getFileName();
void analyse();
void checkOutNotes();
void checkOutWordsStream();
void checkDefine();
void fputEnd();
void fputTip();
void fputcc();
void fputWord();
void fputdefine();
void fputline();
int noUse();
void messageBox();
void button();
void rightBox();
int isKeyWord();
int isNumber();
int isFunctionName();
int isInclude();
int isDefined();
int readLine();
int readWord();
int cton();
int ntoc();
main()
{
char dealfile[200]="";
char outputfile[200]="";
char notefile[200]="";
textbackground(1);
clrscr();
init();
getFileName(dealfile,outputfile,notefile);
getch();
}
int scanff(char *str,int dislen) /**need file <string.h>,<dos.h>**/
{
int ch=0,lenth;
int i;
int x,y;
x=wherex();y=wherey();
lenth=strlen(str);
if(lenth>dislen&&dislen)
{
i =lenth - dislen;
}
else i=0;
for(i;i<lenth;i++)
printf("%c",str[i]);
while(ch!=7181)
{
ch=bioskey(0);
switch(ch)
{
case 3592:/**key_backspace**/
{
if(lenth==0);
else
{
printf("\b");
printf("%c",32);
printf("\b");
lenth--;
}
break;
}
case 19200:
case 19712:
case 283:
case 18432:
case 20480:
case 11520:
case 27392: return ch;break;
case 3849:/**key_tab**/ return ch;break;
case 7181:/**key_Enter**/return ch;break;
default: str[lenth++]=ch;
}
str[lenth]='\0';
if(lenth>dislen&&dislen)
{
i = lenth - dislen;
}
else i=0;
gotoxy(x,y);
for(i;i<lenth;i++)
printf("%c",str[i]);
}
}
/**********All DFA include in this function***********/
void analyse(char *filename,char *nfile,char *outputfile)
{
char *constant;
checkOutNotes(filename,nfile);
checkOutWordsStream(outputfile);
}
/*************DFA for notes message********************/
/**** there are some posibilities,string begins *******/
/**** with //,for simple line note,string begins*******/
/**** with /*,ends with the opposite form,for *******/
/**** multi-line note,DFA will discern it *******/
void checkOutNotes(char *filename,char *nfile)
{
FILE *fp,*fp1,*fp2;
char ch=0;
char dealline[5000];
int line=0,col,lenth,isnote=0,i,col1;
fp1=fopen(nfile,"w+");
rewind(fp1);
fp2=fopen("c:\\temp.c","w+");
rewind(fp2);
fp = fopen(filename,"r");
rewind(fp);
while(ch!=EOF)
{
ch=readLine(fp,dealline,&line);
lenth=strlen(dealline);
col1=0;
for(col=0;col<lenth;col++,col1++)
{
if(isnote) /******notes have changed line***/
{
if(dealline[col]=='*'&&dealline[col+1]=='/')
{
fputcc(&col1,dealline[col],fp1);
fputcc(&col1,dealline[col+1],fp1);
fputEnd(fp1);
isnote=!isnote;
col++;col1++;
}
else if(dealline[col]=='\n')
{
fputcc(&col1,dealline[col],fp1);
fputEnd(fp2);
}
else
{
fputcc(&col1,dealline[col],fp1);
}
}
else /***just find some notes******/
{
if(dealline[col]=='/')
{
if(dealline[col+1]=='/')
{
fputTip(fp1,line,col1);
for(i=col;i<lenth;i++)
fputcc(&col1,dealline[i],fp1);
fputEnd(fp2);
col=lenth;
}
else if(dealline[col+1]=='*')
{
fputTip(fp1,line,col1);
fputcc(&col1,dealline[col],fp1);
fputcc(&col1,dealline[col+1],fp1);
isnote=!isnote;
col++;col1++;
}
else
{
fputcc(&col1,dealline[col],fp2);
}
}
else
{
fputcc(&col1,dealline[col],fp2);
}
}
if(col==lenth&&isnote)
fputEnd(fp2);
}
}
fclose(fp1);
fclose(fp2);
fclose(fp);
}
/************Give an ending to the current line*********/
void fputEnd(FILE *fp)
{
fputc('\n',fp);
}
void fputTip(FILE *fp,int line, int row)
{
char num[10];
fputs("(line: ",fp);
ntoc(num,line);
fputs(num,fp);
fputs(" col: ",fp);
ntoc(num,row+1);
fputs(num,fp);
fputs(" )",fp);
}
void fputcc(int *col,char letter,FILE *fp)
{
if(letter=='\t')
*col += ((TAB - (*col+1) % TAB) % TAB);
fputc(letter,fp);
}
/*********exchange a string to a num***************/
int cton(char *str)
{
int len,num3;
len = strlen(str);
switch(len)
{
case 1: num3 = str[0]-48;break;
case 2: num3 = (str[0]-48)*10+(str[1]-48);break;
case 3: num3 = (str[0]-48)*100+(str[1]-48)*10+(str[2]-48);break;
case 4: num3 = (str[0]-48)*1000+(str[1]-48)*100+(str[2]-48)*10+(str[3]-48);break;
case 5: num3 = (str[0]-48)*10000+(str[1]-48)*1000+(str[2]-48)*100+(str[3]-48)*10+(str[4]-48);break;
dafult: gotoxy(1,1);printf("NOT ENOUGH MENORY");
}
return(num3);
}
/*********exchange a num to a string***************/
int ntoc(char *buf,int num1)
{
if(num1<10)
{buf[0] = num1+48;buf[1] = '\0';}
else if(num1<100)
{buf[0] = num1/10+48;buf[1] = (num1%10)+48;buf[2] = '\0';}
else if(num1<1000)
{buf[0] = num1/100+48;buf[1] = (num1%100)/10+48;buf[2] = ((num1%100)%10)+48;buf[3] = '\0';}
else if(num1<10000)
{buf[0] = num1/1000+48;buf[1] = (num1%1000)/100+48;
buf[2] = ((num1%1000)%100)/10+48;buf[3] = (((num1%1000)%100)%10)+48;
buf[4] = '\0';}
else if(num1<55000)
{buf[0] = 1+48;buf[1] = (num1%10000)/1000+48;
buf[2] = ((num1%10000)%1000)/100+48;
buf[3] = (((num1%10000)%1000)%100)/10+48;
buf[4] = ((((num1%10000)%1000)%100)%10)+48;
buf[5] = '\0';}
else exit(0);
return(OK);
}
/*******read from a file line by line,posite it in a array***/
/******* there is no notes in the file ***/
void checkOutWordsStream(char *filename)
{
int i,line=0,col,col1;
char tempa[100],dealline[5000],ch=0;
char definebuf[10000],valuebuf[100];
int lenth,winclude;
FILE *fp,*fp1;
checkDefine(definebuf);
fp=fopen("c:\\temp.c","r");
fp1=fopen(filename,"w+");
while(ch!=EOF)
{
ch=readLine(fp,dealline,&line);
lenth=strlen(dealline);
fputline(fp1,dealline,line);
col1=0;
for(col=0;col<lenth;col++,col1++)
{
switch(dealline[col])
{
case '\"':
case '\'':
{
i=0;
while(dealline[col+1]!='\''&&dealline[col+1]!='\"'&&col<(lenth-3))
{
tempa[i++]=dealline[col++];
col1++;
}
tempa[i++]=dealline[col++];col1++;
tempa[i++]=dealline[col];tempa[i]='\0';
if(isInclude(tempa))
fputWord(fp1,tempa,line,col1,"ERROR");
else if(tempa[0]=='\"'&&winclude)
fputWord(fp1,tempa,line,col1,"INCLUDEFILE");
else if(tempa[0]=='\"')
fputWord(fp1,tempa,line,col1,"STRING");
else
fputWord(fp1,tempa,line,col1,"ACHAR");
winclude=0;
break;
}
case '+':
case '-':
case '<':
{
if(winclude&&dealline[col]=='<') /*include<stdio.h> format*/
{
i=0;
while(dealline[col]!='>'&&col<(lenth-2))
{
tempa[i++]=dealline[col];
col++;col1++;
}
tempa[i++]=dealline[col];
tempa[i] = '\0';
if(isInclude(tempa))
fputWord(fp1,tempa,line,col1,"ERROR");
else
fputWord(fp1,tempa,line,col1,"INCLUDEFILE");
winclude=0;
break;
}
}
case '>':
{
i=0;
if(dealline[col]==dealline[col+1])/*deal with ++ -- >> <<*/
{
tempa[i++]=dealline[col++];
tempa[i++]=dealline[col];
col1++;
tempa[i]='\0';
}
else if(dealline[col]=='-'&&dealline[col+1]=='>')/*deal with ->*/
{
tempa[i++]=dealline[col++];
tempa[i++]=dealline[col];
col1++;
tempa[i]='\0';
}
else if(dealline[col+1]=='=')/*deal with += -= <= >=**/
{
tempa[i++]=dealline[col++];
tempa[i++]=dealline[col];
col1++;
tempa[i]='\0';
}
else /***only a + or a -*****/
{
tempa[i++]=dealline[col];
tempa[i]='\0';
}
fputWord(fp1,tempa,line,col1,"OPERATOR");
break;
}
case '=':
case '*':
case '/':
case '%':
case '!':
{
i=0;
if(dealline[col+1]=='=')/*deal with ==, *=,/=,%=,!=*/
{
tempa[i++]=dealline[col++];
tempa[i++]=dealline[col];
col1++;
tempa[i]='\0';
}
else
{
tempa[i++]=dealline[col];
tempa[i]='\0';
}
fputWord(fp1,tempa,line,col1,"OPERATOR");
break;
}
case '&':
case '|':
{
i=0;
if(dealline[col]==dealline[col+1])/*deal with && ||*/
{
tempa[i++]=dealline[col++];
tempa[i++]=dealline[col];
col1++;
tempa[i]='\0';
}
else
{
tempa[i++]=dealline[col];
tempa[i]='\0';
}
fputWord(fp1,tempa,line,col1,"OPERATOR");
break;
}
case '\\':
{
i=0;
switch(dealline[col+1])
{
case 'b':
case 'n':
case 'r':
case 't':
case '\"':
case '\'':
case '0':
case '\\':
case 'v':
case 'a':
case 'o':
case 'x':
{
tempa[i++]=dealline[col++];
tempa[i++]=dealline[col];
col1++;
tempa[i]='\0';
fputWord(fp1,tempa,line,col1,"TRANSLATION");
break;
}
default:; /*return Error*/
}
}
default:
{
i=0; /*deal with a string combined which numbers or letters*/
if(noUse(dealline[col]))
{
if(dealline[col]=='\t')
col1 += ((TAB - (col1+1) % TAB) % TAB);
}
else if(isalnum(dealline[col])||(dealline[col] == '_'))
{
while((isalnum(dealline[col])||(dealline[col] == '_'))&&(col<lenth))
{
tempa[i++]=dealline[col++];
col1++;
}
tempa[i]='\0';
col--;col1--;
if(isKeyWord(tempa))
{
if(!strcmp(tempa,"include")) winclude=1;
fputWord(fp1,tempa,line,col1,"KEYWORD");
}
else if(isNumber(tempa))
fputWord(fp1,tempa,line,col1,"NUMBER");
else if(isdigit(tempa[0]))
fputWord(fp1,tempa,line,col1,"ERROR");
else if(isDefined(definebuf,tempa,valuebuf))
fputdefine(fp1,tempa,valuebuf,line,col1,"CONSTANT");
else if(isFunctionName(col,dealline,tempa))
fputWord(fp1,tempa,line,col1,"FUNCTIONNAME");
else fputWord(fp1,tempa,line,col1,"VARIABLE");
}
else
{
tempa[i++]=dealline[col];
tempa[i]='\0';
fputWord(fp1,tempa,line,col1,"OPERATOR");
}
}
}
}
}
fclose(fp);
fclose(fp1);
}
void checkDefine(char *buf)
{
char ch=' ',temp[100];
FILE *fp;
fp=fopen("c:\\temp.c","r");
buf[0]='\0';
while(ch!=EOF&&strcmp(temp,"main"))
{
ch=readWord(fp,temp,ch);
if(!strcmp(temp,"#define")||!strcmp(temp,"define"))
{
ch=readWord(fp,temp,ch);
strcat(buf,temp);
strcat(buf," ");
ch=readWord(fp,temp,ch);
strcat(buf,temp);
strcat(buf," ");
}
}
fclose(fp);
}
int readWord(FILE *fp,char *temp,char ch)
{
int i=0;
while(noUse(ch)&&ch!=EOF)
ch=fgetc(fp);
while(!noUse(ch)&&ch!=EOF)
{
temp[i++]=ch;
ch=fgetc(fp);
}
temp[i]='\0';
return ch;
}
int isInclude(char *str)
{
int lenth;
lenth=strlen(str);
if(str[0]=='<'&&str[lenth-1]!='>')
return(3);
else if(str[0]!='<'&&str[0]!=str[lenth-1])
return(3);
else if(str[0]=='\''&&lenth<3)
return(3);
else if(str[0]=='\''&&str[1]!='\\'&&lenth>3)
return(3);
else if(str[1]=='\\'&&lenth>4)
return(3);
else return 0;
}
void fputWord(FILE *fp,char *stream,int line ,int col,char *str)
{
fputTip(fp,line,col-strlen(stream)+1);
fputs(" , ",fp);
fputs(str,fp);
fputs(" , \"",fp);
fputs(stream,fp);
fputc('\"',fp);
fputc('\n',fp);
}
void fputdefine(FILE *fp,char *stream1,char *stream2,int line ,int col,char *str)
{
fputTip(fp,line,col-strlen(stream1)+1);
fputs(" , ",fp);
fputs(str,fp);
fputs(" , \"",fp);
fputs(stream1,fp);
fputc('\"',fp);
fputs(" , VALUE = ",fp);
fputs(stream2,fp);
fputc('\n',fp);
}
int noUse(char ch)
{
int i;
char nouse[]={'\t','\n','\r','\0',' '};
for(i=0;i<5;i++)
if(ch==nouse[i])
return OK;
return 0;
}
int isKeyWord(char *str)
{
int i;
char *KeyWord[] = {
"auto","break","case","char","const","continue",
"default","do","double","else","enum","extern",
"float","for","goto","if","int","long","register",
"return","short","signed","sizeof","static","struct",
"switch","typedef","union","unsigned","void","volatile",
"while","string","new","class","include","define"
};
for (i=0;i<37;i++)
if(strcmp(str,KeyWord[i]) == 0)
return(1);
return(0);
}
/*****************about to read the numbers*******************/
/************it's too complex,so ignore many possibilities****/
int isNumber(char *str)
{
int i;
if(isdigit(str[0])&&str[1]=='\0')
return(2);
else if(str[0]=='0'&&str[1]=='x')
return(2);
else if(isdigit(str[0])&&isdigit(str[1]))
{
for(i=2;i<strlen(str);i++)
{
if(!isdigit(str[i]))
return(0);
}
return(2);
}
else if(!strcmp(str,"true")||!strcmp(str,"false"))
return(2);
else
return(0);
}
int isFunctionName(int pos, char *node,char *str)
{
int check;
if( isKeyWord(str) ) return(0);
for(check=0;(isalnum(str[check])||(str[check]=='_'))&&(str[check]!='\0');check++);
if(str[check]!='\0')
return(0);
else check = pos;
while((node[check]!= '\n')&& (node[check+1] != '(')&&((node[check+1]==' ')||(node[check+1]=='\t')))
check++;
if( node[check] != '\n'&&node[check+1] == '(')
return(3);
return(0);
}
int isDefined(char *buf,char *str,char *definevalue)
{
int i=0;
char word[100];
if(!strlen(buf))
return 0;
i=readArrayWord(buf,i,word);
while(i<strlen(buf)-1)
{
if(!strcmp(word,str))
{
readArrayWord(buf,i,definevalue);
return OK;
}
i=readArrayWord(buf,i,word);
i=readArrayWord(buf,i,word);
}
return 0;
}
int readArrayWord(char *thearray,int pos,char *buf)
{
int i=0;
if(thearray[pos]==' '&&pos<strlen(thearray))
pos++;
while(thearray[pos]!=' '&&pos<strlen(thearray))
{
buf[i++]=thearray[pos++];
}
buf[i]='\0';
return pos;
}
int readLine(FILE *fp,char *theline,int *line)
{
int ch,i=0;
*line+=1;
while((ch=fgetc(fp))!='\n'&&ch!=EOF)
{
theline[i++]=ch;
}
if(ch==EOF);
else theline[i++] = ch ;
theline[i]='\0';
return (ch);
}
void messageBox(int startx,int starty,int width,int hight,int infocolor,char *str) /*print a box*/
{
int i,endx,endy,len;
textcolor(WHITE);
endx=startx+width-1;
endy=starty+hight-1;
gotoxy(startx,starty); putch(0xda); /*the lefttop corner*/
for(i=startx+1;i<endx;i++)
putch(0xc4); /*the line---*/
putch(0xbf); /*the righttop corner*/
for(i=starty+1;i<endy;i++)
{
gotoxy(startx,i); putch(0xb3); /*the line|||*/
gotoxy(endx,i); putch(0xb3);
}
gotoxy(startx,endy); putch(0xc0); /*the leftbottom corner*/
for(i=startx+1;i<endx;i++)
putch(0xc4);
putch(0xd9); /*the rightbottom corner*/
len=strlen(str);
i=(width/2-len/2-1);
gotoxy(i,starty);
textcolor(infocolor);
cprintf(" %s",str);
textcolor(WHITE); /*the name of the box*/
}
void init()
{
window(7,3,40,18);
textbackground(2);
clrscr();
messageBox(1,1,33,16,0,"File Name Box");
gotoxy(2,4);cprintf("File Name To Analysis:");
gotoxy(2,7);cprintf("File Name To Output:");
gotoxy(2,10);cprintf("File Name To Savenotes:");
button(7,20,19,22,2,"O K");
button(23,20,35,22,2,"CANCEL");
rightBox("","","");
window(8,7,38,7);
textbackground(BLACK);
clrscr();
window(8,10,38,10);
textbackground(BLACK);
clrscr();
window(8,13,38,13);
textbackground(BLACK);
clrscr();
}
void button(int left,int top,int right,int bottom,int bkcolor,char *text)
{
window(left,top,right,bottom);
textbackground(bkcolor);
clrscr();
messageBox(1,1,12,3,0,"");
gotoxy(5,1);cprintf("%c",0xc4);
gotoxy(4,2);cprintf(text);
}
void rightBox(char *dealfile,char *ofile,char *nfile)
{
FILE *fp;
struct stat sbuff;
window(42,3,70,22);
textbackground(2);
clrscr();
messageBox(1,1,28,20,0,"OutPutInfo");
window(43,4,68,21);
textcolor(15);
gotoxy(1,2);
cprintf("TheFileToAnalyse :");
textcolor(1);
cprintf("%s\n\n\r",dealfile);
textcolor(15);
cprintf("TheNotesSavedFile :");
textcolor(1);
cprintf("%s\n\n\r",nfile);
if((fp=fopen(nfile,"r"))!=NULL)
{
fstat(fileno(fp),&sbuff);
textcolor(15);
cprintf("TheNotesFile'sLength :");
textcolor(1);
cprintf(" %ld Bytes\n\n\r",sbuff.st_size);
}
fclose(fp);
textcolor(15);
cprintf("TheOutputInfoFile :\n\r");
textcolor(1);
cprintf("%s\n\n\r",ofile);
if((fp=fopen(ofile,"r"))!=NULL)
{
fstat(fileno(fp),&sbuff);
textcolor(15);
cprintf("TheTerminalFile'sLength :");
textcolor(1);
cprintf(" %ld Bytes",sbuff.st_size);
}
fclose(fp);
textcolor(15);
}
void getFileName(char *dealfile,char *ofile,char *nfile)
{
int num=1,left=1;
int ch;
window(7,3,40,18);
gotoxy(2,5);
ch=scanff(dealfile,0);
while(1)
{
switch(ch)
{
case 19200:/***leftarrow***/
case 19712:/***rightarrow***/left=!left;break;
case 18432:/***uparrow***/(num==1)?4:(num--);break;
case 20480:/***downarrow***/(num==4)?1:(num++);break;
case 283:/***ESC***/
case 11520:/**ALT+x**/
case 27392:/**ALT+f4**/remove("c:\\temp.c");
exit(0);
case 3849:/**tab**/(num==4)?1:(num++);break;
case 7181:/***ENTER***/
{
if((num==4&&left)||num!=4)
if(strlen(dealfile)&&strlen(ofile)&&strlen(nfile))
{
analyse(dealfile,nfile,ofile);
rightBox(dealfile,ofile,nfile);
}
if(num==4&&!left)
{
remove("c:\\temp.c");
exit(0);
}
}
}
switch(num)
{
case 1: window(8,7,38,7);
textbackground(BLACK);
clrscr();
gotoxy(1,1);
ch=scanff(dealfile,30);
break;
case 2: window(8,10,38,10);
textbackground(BLACK);
clrscr();
gotoxy(1,1);
ch=scanff(ofile,30);
break;
case 3: window(8,13,38,13);
textbackground(BLACK);
clrscr();
gotoxy(1,1);
ch = scanff(nfile,30);
break;
case 4: if(left)
{
button(23,20,35,22,2,"CANCEL");
button(7,20,19,22,3,"O K");
}
else
{
button(7,20,19,22,2,"O K");
button(23,20,35,22,3,"CANCEL");
}
ch=bioskey(0);
}
}
}
void fputline(FILE *fp,char *dealline,int line)
{
char linenum[10];
ntoc(linenum,line);
fputs("\nline ",fp);
fputs(linenum,fp);
fputs(" : ",fp);
fputs(dealline,fp);
fputc('\n',fp);
}