⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 program1.cpp

📁 能够完成正则文法所描述的Pascal语言子集单词符号的词法分析
💻 CPP
字号:
// Program1.cpp : Defines the entry point for the console application.
//
/* 程序名称:词法分析程序设计*/
/*
完成以下正则文法所描述的Pascal语言子集单词符号的词法分析程序。
    <标识符>→字母︱ <标识符>字母︱ <标识符>数字
    <无符号整数>→数字︱ <无符号整数>数字
    <单字符分界符> →+ ︱- ︱* ︱; ︱(︱) 
    <双字符分界符>→<大于>=︱<小于>=︱<小于>>︱<冒号>=︱<斜竖>*
    <小于>→<                    
    <等于>→=
    <大于>→> 
    <冒号> →: 
    <斜竖> →/
该语言的保留字 :program   begin  end  if  then  else  for  do  while  and  or  not
*/

#include "stdafx.h"
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include <math.h>

struct TOKEN{	
	char *lexeme;
	int num;
};

TOKEN tokentab[] ={
	{"PROGRAM",1},{"BEGIN",2},{"END",3},
	{"IF",4},{"THEN",5},{"ELSE",6},{"FOR",7},
	{"DO",8},{"WHILE",9},{"AND",10},{"OR",11},
	{"NOT",12}
};//保留字及编号

/*其他字符及编号
+ 21   - 22   * 23   ;24   ( 25   ) 26   > 27   < 28   = 29   /  30
>= 31  <= 32  <> 33  := 34  /* 35 
标示符 37   数字38
*/

char token[20];
static FILE *fp,*fp2;
extern int lookup(char* tab)
{
	for(int i=0; i < 11; i++)
	{
		if(strcmp(tokentab[i].lexeme,tab) == 0)		return tokentab[i].num;
	}
	return 0;
}

extern void out(int id ,char* tab)
{
	printf("( %2d,%10s )\n",id,tab);
}
extern void outfile(int id ,char* tab)
{
	fprintf(fp2,"[%d,%s]",id,tab);
}

extern report_error()
{
	printf("(  0,      error!)\n");
	//outfile(  0,"error!");
}

char Getchar()
{
	char ch;
	ch = fgetc(fp);
	return toupper(ch);
}

void scanner()
{
	char ch;
	int i,c;
	ch = Getchar();
	while(!feof(fp))
	{
	    if(isalpha(ch))//判断字母
	    {
		    token[0] = ch;
		    ch = Getchar();
		    i = 1;
  		    while(isalnum(ch))
		    {
		    	token[i] = ch; i++;
		    	ch = Getchar();
		    }
		    token[i]='\0';
		    fseek(fp,-1,1);
		    c=lookup(token);
		    if(c == 0) 
		    {
		    	c=37;
		    	out(c,token);
				outfile(c,token);
		    }
			else 
			{
				out(c,token);
			    outfile(c,token);
			}
 			 
		}
		else
		    if(isdigit(ch))
		    {
			    token[0] = ch;
			    ch = Getchar();
			    i = 1;
			    while(isdigit(ch))
			    {
				    token[i] = ch;
				    i++;
				    ch = Getchar();
			    }
		    	token[i] = '\0';
		    	fseek(fp,-1,1);
		    	c=38;
		    	out(c,token);
				outfile(c,token);
	    	}
	    	else
				//if(!isspace(ch)) break;
				//else
	    	switch(ch)
	    	{
 				case'\n': ;break;
 				case' ': ;break;
                case'+':c=21;out(c,"+");outfile(c,"+");break;
	    		case'-':c=22;out(c,"-");outfile(c,"-");break;
	    		case'*':c=23;out(c,"*");outfile(c,"*");break;
	    		case';':c=24;out(c,";");outfile(c,";");break;
	    		case'(':c=25;out(c,"(");outfile(c,"(");break;
	    		case')':c=26;out(c,")");outfile(c,")");break;
				case'=':c=29;out(c,"=");outfile(c,"=");break;
	    		case'>':ch=Getchar();
					if(ch == '=') 
					{ 
						c=31;out(c,">=");
					    outfile(c,">=");
					}
		    		else
			    	{
				    	fseek(fp,-1,1);
    					c=27;
	    			}
		    		out(c,">");
					outfile(c,">");
			    	break;
    	    	case'<':ch=Getchar();
	    	    	if(ch == '=')  c=32;
			    	else
	    			{
						if(ch == '>') { c=33;out(c,"<=");outfile(c,"<=");}
			    		else
				    	{
					        fseek(fp,-1,1);
			    		    c=28;
			    		}
			    		out(c,"<");
						outfile(c,"<");
		    		break;
	    		case':':ch=Getchar();
	    		    if(ch == '=')  c=34;
	    		    out(c,":=");
					outfile(c,":=");
	    		    break;
				case'/':ch=Getchar();
	    		             if(ch =='*') 
							 {
							     bool m=true;
				                 ch=Getchar();
					             while(m)
								 {
									 if(ch!=EOF)
									 {
						             if(ch == '*')
									 {
							             ch=Getchar();
                                         if(ch =='/') m=false;
										 else	m=true;
									 }
									 else	
									 {
										 m=true;
									     ch=Getchar();
									 }
									 }
									 else 
									 {
										 report_error();
										 system("PAUSE");
									 }
								 }
							 }
							 else						
							 {
								 fseek(fp,-1,1);
								 c=30;   
								 out(c,"/");
						         outfile(c,"/");
							 }
			    		
							 break;
	    		default:report_error();
	    			break;
		        }	    	    
		    }
		ch = Getchar();
	}
	return;
}

int main(int argc, char* argv[])
{

	fp=fopen("test.txt", "r");
	fp2=fopen("out.txt","w");
	scanner();
	fclose(fp);
	fclose(fp2);
	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -