編譯原理上機作業1——詞法分析器

塵埃092發表於2013-09-23
/*
  單詞符號   種別碼       單詞符號     種別碼 
		#        0    		    +          27 
   main       1              -          28   
   if         2              *          29    
   include    3              /          30   
   while      4              :          31  
   do         5              ?          32 
   static     6				<          33  
	  int        7           	!=         34  
	  double     8              ==         35
	  struct     9				<=         36
	  break      10				>          37   
	  else       11				>=         38   
	  long       12				=          39
	  switch     13				;          40  
	  case       14				(          41  
	  typedef    15				)          42 
	  char       16             {          43
	  return     17             }          44
	  const      18			    [          45	
	  float      19			    ]          46
	  short      20            value       47
	  continue   21		    const number   48
	  for        22
	  void       23
	  sizeof     24   
   default    25             
   hehe       26                              
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <malloc.h>
#include <ctype.h>


#define CLR(val) memset(val,0,sizeof(val))
char keyword[26][15]=
{
	"main","if","include","while",
	"do","static","int","double",
	"struct","break","else","long",
	"switch","case","typedef","char",
	"return","const","float","short",
	"continue","for","void","sizeof",
	"default","hehe"
};
typedef struct n
{
	char token[20];
	int num;
	struct n * next;
}node, *Node;
Node head = NULL;
Node next_node;
int cmp( const void * a, const void *b )
{
	return strcmp( (char*)a, (char*)b );
};
void init_list( )
{
	head = (Node)malloc(sizeof(node));
	head->num = -1;
	//strcpy( head->token,  );
};
void insert_list( int num, char * temp )
{
	Node newnode = (Node)malloc(sizeof(node));
	//----------------------------
	newnode->num = num;
	strcpy( newnode->token, temp );
	newnode->next = NULL;
	//----------------------------
	next_node->next = newnode;
	next_node = next_node->next;
};
int is_keyword( char * temp )
{
	for( int i = 0; i < 26; i++ )
		if( strcmp(temp,keyword[i]) )
			return i+1;
	else
		return 47;
};
void scaner()
{
	int word_plus;
	char word[100];
	char temp[100];
	int sum;
	char input[100];
	char *p;
	while( fgets(input,100,stdin) )	
	{
		strcpy( word, input );
		//處理一行 
		p = word;
		while( *p != '\n' )
		{
			word_plus = 0;
			while( *p == ' ' || *p == 9 )
					p++;
			if( isalpha(*p) )				//	字母 
			{
				CLR(temp);
				//p++;
				while( isalpha(*p) || (*p >=0 && *p <= 9) )
				{
					temp[word_plus++] = *p;
					p++;	
				}
				temp[word_plus] = '\0';
				insert_list( is_keyword(temp), temp );	//判斷關鍵字函式 
				//插入連結串列 
			}
			else if( *p >= '0' && *p <= '9' )		//數字 
			{
				CLR(temp);
				sum = 0;
				while( (*p>='0' && *p<='9') )
				{
					sum = sum * 10 + *p-'0';
					p++;
				}
				if( isalpha(*p) )
				{
					printf( "wrong " );
					return ;
				}
				if( sum >= 32767 )
				{
					printf( "int too big" );
					return ;
				}
				else
				{
					itoa(sum,temp,10);
					insert_list( 48, temp );
				}
			}
			else
			{ 
				switch( *p )
				{
					case '#':
                        insert_list( 0, "#" );                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              
						break;
					case '+':
						insert_list( 27, "+" );
						break;
					case '-':
						insert_list( 28, "-" );
						break;					
					case '*':
						insert_list( 29, "*" );
						break;					
					case '/':
						insert_list( 30, "/" );
						break;					
					case ':':
						insert_list( 31, ":" );
						break;
					case '?':
						insert_list( 32, "?" );
						break;
					case '<':
						if( *(p+1) == '=' )
						{
							insert_list( 36, "<=" );
							p++;
						}
						else
							insert_list( 33, "<" );
						break;		
					case '>':
						if( *(p+1) == '=' )
						{
							insert_list( 38, ">=" );
							p++;
						}
						else
							insert_list( 37, ">" );
						break;							
					case '=':
						insert_list( 39, "=" );
						break;
					case ';':
						insert_list( 40, ";" );
						break;
					case '(':
						insert_list( 41, "(" );
						break;
					case ')':
						insert_list( 42, ")" );
						break;
					case '{':
						insert_list( 43, "{" );
						break;
					case '}':
						insert_list( 44, "}" );
						break;
					case '[':
						insert_list( 45, "[" );
						break;
					case ']':
						insert_list( 46, "]" );
						break;
					default:
						return ;
						break;
				}
				p++;	
			}
		}
	}
	
}
int main()
{
	freopen( "in.txt", "r", stdin );
	freopen( "out.txt", "w", stdout );
	//對關鍵字表 進行qsort() 使用二分查詢關鍵字 就26個單詞懶得寫了,效率上也過多提高 
//	qsort(keyword,26,sizeof(keyword[0]),cmp);
//	for( int i = 0; i < 26; i++ )
//		printf( "%s\n", keyword[i] );
	init_list();
	next_node = head;
	//詞法分析函式 
	scaner();
	//遍歷連結串列 
	for( Node t = head->next; t != NULL; t = t->next )
		printf( "(%d, %s)\n", t->num, t->token );
	return 0;
}

相關文章