词法分析程序

王朝other·作者佚名  2008-06-01
窄屏简体版  字體: |||超大  

这个是我编译原理课程设计的 内容,呵呵 我遍的是词法分析,基本的功能都能够实现,如词法分析,词法查错,删除无实质意义的字符等,不懂编译的就不用看了,编译真的很难!!!不过好象没什么用

#include<iostream.h>

#include<ctype.h>

#include<string.h>

#include<stdio.h>

#include<stdlib.h>

#define ID 6

#define INT 7

#define LT 8

#define LE 9

#define EQ 10

#define NE 11

#define GT 12

#define GE 13

#define FZ 14

#define DEV 15

strUCt KeyWord //要害字结构

{

char *word;

int id;

};

KeyWord keyword[]={ //要害字数组

,

,

,

,

,

,

,

};

char TOKEN[20];

int graphnum=1; //记录错误所在的位置

int lookup(char *string);

void out(int id ,char *string);

void report_error(char ERR_CH);

bool isalpha(char c) ;

bool isdigit(char c);

bool isalnum(char c);

void scanner_example(FILE *fp);

int lookup(char *string)

{

for(int i=0;i<sizeof(keyword)/sizeof(KeyWord);i++)

{

if(strcmp(string,keyword[i].word)==0)

return keyword[i].id;

}

return 0;

}

void out(int id ,char *string)

{

printf("(%d,%s) ",id,string);;

}

void report_error(char ERR_CH) //错误处理程序

{

printf("undeclared identifler %c int %d line! ",ERR_CH,graphnum);

}

bool isalpha(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;

}

bool isalnum(char c)

{

if( isalpha(c) isdigit(c) )

return true;

else

return false;

}

void scanner_example(FILE *fp)

{

char ch;

int i,c;

while(!feof(fp))

{

ch=fgetc(fp);

if(isalpha(ch))

{

TOKEN[0]=ch;

ch=fgetc(fp);

i=1;

while(isalnum(ch))

TOKEN[i]='

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航