编译原理实验,词法分析器及判错

配套课本忘了盒盒盒

在这里插入代码片
#include 
#include 
char str1[80],token[20];
//声明保留字符串 方便比对
char *rwtab[6] = {"begin","if","then","while","do","end"};
//syn为token对应的码 tar为switch对应的输出类型keyword
int syn,tar ;
//num为token索引
int num = 0;
//k为str1索引
int k=0;
//ch为k当前对应的str1的字符
char ch ;

int kk = 0;

void Irparser();
void yucu();
void statement();
void expression();
void term();
void factor();
void scanner();

int main()
{
    int count=0;
    printf("\nplease input string\n") ;
    do{
        scanf("%c",&str1[count++]);
    }while(str1[count-1]!='#');

	scanner();
    Irparser();
    return 0;
}

void scanner(){
    for (int j = 0; j < 8; ++j)
        token[j]= NULL;
    
    while (1){
        if(str1[k++] != ' '){
            ch = str1[k-1];
            break;
        }
    };

    token[0] = ch;
    if(ch>='0'&&ch<='9'){
        num = 0 ;
        while(ch>='0'&&ch<='9'){
            token[num++] = ch;
            ch = str1[k++];
        }
        k--;
        syn = 11;
    } else if ((ch>='A'&&ch<='Z')||(ch>='a'&&ch<='z')){
        num = 0;
        while((ch>='A'&&ch<='Z')||(ch>='a'&&ch<='z')||(ch>='0'&&ch<='9')){
            token[num++] = ch;
            ch = str1[k++];
        }

        token[num++] = '\0';
        k--;
        syn = 10;

        for (int i = 0; i < 6; ++i) {
            if(strcmp(token,rwtab[i])==0){
                syn = i+1;
                break;
            }
        }

    } else {
        switch(ch){
            case '<':
                ch = str1[k++];
                if(ch == '>'){
                    syn = 21;
                    token[1] = ch;
                }else if(ch == '='){
                    syn =  22;
                    token[1] = ch;
                }else{
                    syn = 20;
                    k--;
                }
                break;
            case '>':
                ch = str1[k++];
                if(ch == '='){
                    syn = 24;
                    token[1] = ch;
                } else{
                    syn = 23;
                    k--;
                }
                break;
            case ':':
                ch = str1 [k++];
                if(ch == '='){
                    syn = 18;
                    token[1] = ch;
                } else{
                    syn = 17;
                    k--;
                }
                break;
            case '+':
                ch = str1 [k];
                syn = 13;
                
                break;
            case '-':
                ch = str1 [k];
                syn = 14;
                
                break;
            case '*':
                ch = str1 [k];
                syn = 15;
                
                break;
            case '/':
                ch = str1 [k];
                syn = 16;
                
                break;
            case '=':
                ch = str1 [k];
                syn = 25;
                
                break;
            case ';':
                ch = str1 [k];
                syn = 26;
                
                break;
            case '(':
                ch = str1 [k];
                syn = 27;
                
                break;
            case ')':
                ch = str1 [k];
                syn = 28;
                
                break;
            case '#':
                ch = str1 [k];
                syn = 0;
                
                break;
            default:
                syn = -1;
        }
    }
}

void Irparser(){
	//递归下降分析 
	if(syn == 1)
	{
		scanner();
		yucu();
		if(syn == 6)
		{
			scanner();
			if(syn == 0 && kk == 0)
			{
				printf("success!");
			}	
		}
		else
		{
			if(kk!= 1)
			printf("error,lost 'end'");
			kk = 1;
		}
	}
	else
	{
		printf("error,lost 'begin'");
		kk = 1;
	}
	
}

void yucu(){
	//语句串分析 
	statement();
	while(syn == 26)
	{
		scanner();
		statement();
	}
}

void statement(){
	//标识符
	if(syn == 10)
	{
		scanner();
		if(syn == 18)
		{
			scanner();
			expression();
		}
		else
		{
			printf("error,':='");
			kk = 1;
		}
	 }
	 else
	 {
	 	printf("error,语句错误");
	 	kk = 1;
	 }
}

void expression(){
	//分析函数 
	term();
	while(syn == 13 || syn == 14)
	{
		scanner();
		term();
	}
}

void term(){
	//函数分析子函数
	factor();
	while(syn == 15 || syn == 16)
	{
		scanner();
		factor();
	 } 
}

void factor(){
	if(syn == 10 || syn ==11)
		scanner();
	else if (syn == 27)
	{
		scanner();
		expression();
		if (syn == 28)
		scanner();
		else
		{
			printf("')'error");
			kk = 1;
		}
	}
	else{
		printf("error,表达式错误");
		kk = 1; 
	}
}

你可能感兴趣的:(编译原理实验,词法分析器及判错)