主题:跪求词法扫描器
			
 gavinioro
				 [专家分:0]  发布于 2006-10-23 10:44:00							
			本人欲作一个词法扫描器希望有贤士能够相助! 
程序设计要求: 
1.可以从输入的SIMPLE语言源程序中,识别出各个具有独立意义的单词,即保留字,标识符,常数,运算符,界限符五大类; 
2.提供程序输入界面; 
3.词法后可查看符号表和TOKEN串表; 
4.保存符号表和TOKEN串表(如:文本文件) 
5.遇到错误时可显示提示信息,然后跳过错误部分继续进行分析.
马上就要交作业了!!我还是做不出来!!哪位有符合我的要求的源文件希望帮帮小弟!!
急死我了T T
我的QQ14769377,邮箱ghostcry1314@21cn.com
哪位能帮助下我真是不胜感激!!先谢谢各位了!
						
					 
		
			
回复列表 (共1个回复)
		
								
				沙发
				
					
colina [专家分:0]  发布于 2006-10-25 21:46:00				
				#include <process.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <malloc.h>
#include <ctype.h>
#include <conio.h>
FILE *fp;
char ch;
char *keyword[]={"do","begin","if","else","for","while","int","char","float","double"};
char *limit[]={",",";","(",")","{","}","()"};
char *process[]={"+","-","*","/","<","<=","=",">",">=","!=",":=",".","&","&&","%","_"} ;
 char bsfb[100][20];
 char  numb[100][20];
 static int countb=0;
int search(char a[],int type)
{
 int i;
 switch (type)
 {
 case 0:
     for(i=0;i<10;i++)
     {
      if(strcmp(keyword[i],a)==0)
       return(1);            
      }
     break;
 case 1:
    for(i=0;i<7;i++)
    {
     if(strcmp(limit[i],a)==0)
      return(1);
    }
    break;
case 2:
    for(i=0;i<16;i++)
    {
     if(strcmp(process[i],a)==0)
      return(1);
    }
    break;
 }
 return(0);
}
char letterprocess (char ch)
{
 int i=0;
 char letter[20];
 while ((isalnum(ch)!=0)||(ch=='_'))
 {
  letter[i++]=ch;
  ch=fgetc(fp);   
 };
  letter[i]='\0';
 if (search(letter,0))
 {
  printf("     <  ±£Áô×Ö      %s   >\n",letter);
 }
 else
 {
  printf("     <  ±êʶ·û      %s   >\n",letter);
 
  strcpy(bsfb[countb++],letter);
 }
 return(ch);
}
char numberprocess(char ch)
{
 int i=0;
 char num[20];
 while((isdigit(ch)!=0)||(ch=='.'))
 {
  num[i++]=ch;
  ch=fgetc(fp);
 }
 if(isalpha(ch)!=0) 
 {
  while(isspace(ch)==0)
  {
            num[i++]=ch;
            ch=fgetc(fp);
  }
  num[i]='\0';
  printf("     <  ·Ç·¨±êʶ·û    %s   >\n",num);
return(ch);
 }
    num[i]='\0';
    printf("     <  Êý×Ö        %s   >\n",num);
 return(ch);
}
char otherprocess(char ch)
{
 int i=0;
 char other[20];
 if (isspace(ch)!=0)
 {
  ch=fgetc(fp);
  return (ch);
 }
 while ((isspace(ch)==0)&&(isalnum(ch)==0))
 {
  other[i++]=ch;
  ch=fgetc(fp);
 }
 other[i]='\0';
 if (search(other,1))
  printf("     <  ½ç·û        %s   >\n",other);
 else
     if (search(other,2))
         printf("     <  ÔËËã·û      %s   >\n",other);
else
     printf("     <  ·Ç·¨×Ö·û     %s   >\n",other);
 return (ch);
}
void main ()
{
 char str,c;
 int i=0;
 printf("**********************************´Ê·¨·ÖÎöÆ÷************************************\n");
 printf("\n");
 printf("---------------------------------·ÖÎö½á¹û-------------------------------\n");
 printf("\n");
 if ((fp=fopen("1.txt","r"))==NULL)
  printf("ÎÞ·¨´ò¿ªÎļþ£¡\n");
 else
 {
  str =fgetc(fp);
  while (str!=EOF)
  {
     if ((isalpha(str)!=0)||(str=='_'))
    str=letterprocess(str);
   else
   {
    if (isdigit(str)!=0)
     str=numberprocess(str);
    else
     str=otherprocess(str);
   }
    
  }; 
  printf("\n");
 for(i=0;i<countb;i++)
  {
      printf("%s\n",bsfb[i]);
  }
 }
   c=getch();
}
后来改的老师检查完了就找不到了
这个也不是很全面,你可以自己改改
[em1][em1][em1]
							 
									
			
我来回复