龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > 软件开发 > C/C++开发 >

词法分析程序

时间:2009-12-22 15:42来源:未知 作者:admin 点击:
分享到:
这个是我编译原理课程设计的 内容,呵呵 我遍的是词法分析,基本的功能都能够实现,如词法分析,词法查错,删除无实质意义的字符等,不懂编译的就不用看了,编译真的很难!!

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

#include

  #include

  #include

  #include

  #include

#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

   {

   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]='

  

精彩图集

赞助商链接