Demo entry 6796987

ll

   

Submitted by anonymous on May 19, 2019 at 17:46
Language: C++. Code size: 10.6 kB.

#include<iostream>
#include<cstdio>
#include<string.h>
#include<stdlib.h>
using namespace std;

char d_token[50] = "";
char token_ad[32] = "";
char ch;
int c_pos = 0;//递归下降程序的扫描位置

char KeyWord[10][20] = { "return", "int", "break", "double", "if", "else", "for", "do", "while", "then" };
char token[50];
int syn;//类别编码
int n;//输入长度
char str[300];
char gtr[150];
int c;//当前正要读入位置
int v_id = 0;//表示当前第多少个二元组
int v_count = 0;

typedef struct {
	int syn_id;
	char tk[50];
}V;

V v[100], out_v;

void S();
void E();
void E1();
void T();
void T1();
void F();
void A();
void M();
void V1();
void SS();
void EE();



void advance()
{
	strcpy(token_ad, v[c_pos].tk);
	syn = v[c_pos].syn_id;
	c_pos++;
}

void SS()
{
	if (syn == 1)
	{
		advance();
		if (syn == 21)//=的类别码
		{
			advance();
			if (syn == 3)
			{
				advance();
			}
		}
	}
	else if (syn == 104)//if的类别码
	{
		advance();
		EE();
		SS();
		if (syn == 105)//else的类别码
		{
			advance();
			SS();
		}
	}
}

void EE()
{
	if (syn == 1)
	{
		advance();
		if (syn == 23)
		{
			advance();
			if (syn == 3)
			{
				advance();
			}
		}
		else if (syn == 18)
		{
			advance();
			if (syn == 3)
			{
				advance();
			}
			else
			{
				printf("error!");
			}
		}
		else
		{
			printf("error!");
		}
	}
}

void S()
{
	V1();
	if (syn == 21)  //=号的识别码
	{
		advance();
		E();
	}
	else
	{
		printf("error!S() \n");
		exit(0);
	}
}

void E()
{
	T();
	E1();
}

void E1()
{
	if (syn == 4 || syn == 7)//+的识别码为4,-的识别码为7
	{
		A();
		T();
		E1();
	}
	else if (syn != 15 && syn != 55)// )与#的识别码分别为15和55
	{
		printf("error!E1() \n");
		exit(0);
	}
}

void T()
{
	F();
	T1();
}

void T1()
{
	if (syn == 10 || syn == 27)//*和/的识别码分别为10和27
	{
		M();
		F();
		T1();
	}
	else if (syn != 15 && syn != 4 && syn != 7 && syn != 55)
	{
		printf("error!T1() \n");
		exit(0);
	}
}

void F()
{
	if (syn == 14)//(
	{
		advance();
		E();
		if (syn == 15)//)
		{
			advance();
		}
		else
		{
			printf("error! F() \n");
			exit(0);
		}
	}
	else if (syn == 1)
	{
		advance();
	}
	else
	{
		printf("error! F() \n");
		exit(0);
	}
}

void A()
{
	if (syn == 4 || syn == 7)
	{
		advance();
	}
	else
	{
		printf("error!\n");
		exit(0);
	}
}

void M()
{
	if (syn == 10 || syn == 27)
	{
		advance();
	}
	else
	{
		printf("error! M->*|/ \n");
	}
}

void V1()
{
	if (syn == 1)
	{
		advance();
	}
	else
	{
		printf("error! V->i\n");
		exit(0);
	}
}




void v_out(int o_id, char *o_tk)
{
	out_v.syn_id = o_id;
	strcpy(out_v.tk, o_tk);
	printf("( %d, %s )\n", out_v.syn_id, out_v.tk);
	v[v_count].syn_id = out_v.syn_id;
	strcpy(v[v_count].tk, out_v.tk);
	v_count++;
	//return out_v;
}

int Go()
{
	if (c == n)
		return -1;
	memset(token, 0, sizeof(token));
	ch = str[c++];
	while (ch == ' ')
	{
		ch = str[c++];
	}
	if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
	{
		int m = 0;
		token[m++] = ch;
		ch = str[c++];
		while ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9'))
		{
			token[m++] = ch;
			ch = str[c++];
		}
		if (m >= 32)
		{
			printf("Error! 用户定义的标识符不能超过32个!\n");
			return -1;
		}
		token[m] = '\0';
		c--;
		//标致符可能只有一个字符
		syn = 1; //标志符的识别码
		for (int i = 0; i < 10; i++)
		{
			if (strcmp(token, KeyWord[i]) == 0)
			{
				syn = 100 + i; //保留字的识别码
				v_out(syn, token);
				return syn;
			}
		}
		v_out(syn, token);
		//printf("( %d, %s )\n", syn, token);
		return syn;//当不为保留字时,则为一般标志符
	}
	else if ((ch >= '0' && ch <= '9'))
	{
		int digit = 0;
		int m = 0;
		digit += ch - '0';
		token[m++] = ch;
		ch = str[c++];
		while (ch >= '0' && ch <= '9')
		{
			token[m++] = ch;
			digit = 10 * digit + ch - '0';
			ch = str[c++];
		}
		token[m] = '\0';
		if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
		{
			printf("Error! 数字后不能接字母!\n");
			return -1;
		}
		c--;
		syn = 3;//无符号整数的识别码是3
		v_out(syn, token);
		//printf("( %d, %d )\n", syn, digit);
		return syn;
	}
	else//其余的情况都是特定符号
	{
		switch (ch)
		{
		case '+':
			syn = 4;//+的类别码为4
			ch = str[c++];
			if (ch == '+')
			{
				syn = 5; //++的类别码是5
				strcpy(token, "++");
				v_out(syn, token);
				//printf("( %d, ++ )\n", syn);
				return syn;
			}
			else if (ch = '=')
			{
				syn = 6;//+=的类别码为6
				strcpy(token, "+=");
				v_out(syn, token);
				//printf("( %d, += )\n", syn);
				return syn;
			}
			c--;
			strcpy(token, "+");
			v_out(syn, token);
			//printf("( %d, + )\n", syn);
			return syn;
			break;
		case '-':
			syn = 7;//-的类别码为7
			ch = str[c++];
			if (ch == '-')
			{
				syn = 8;//--的类别码是8
				strcpy(token, "--");
				v_out(syn, token);
				//printf("( %d, -- )\n", syn);
				return syn;
			}
			else if (ch == '=')
			{
				syn = 9;
				strcpy(token, "-=");
				v_out(syn, token);
				//printf("( %d, -= )\n", syn);
				return syn;
			}
			c--;
			strcpy(token, "-");
			v_out(syn, token);
			//printf("( %d, - )\n", syn);
			return syn;
			break;
		case '*':
			syn = 10;//*的类别码为10
			ch = str[c++];
			if (ch == '=')
			{
				syn = 11;//*=的类别码为11
				strcpy(token, "*=");
				v_out(syn, token);
				//printf("( %d, *= )\n", syn);
				return syn;
			}
			c--;
			strcpy(token, "*");
			v_out(syn, token);
			//printf("( %d, * )\n", syn);
			return syn;
			break;
		case ';':
			syn = 12;//;的类别码为12
			strcpy(token, ";");
			v_out(syn, token);
			//printf("( %d, ; )\n", syn);
			return syn;
			break;
		case ',':
			syn = 13;//,的类别码为13
			strcpy(token, ",");
			v_out(syn, token);
			return syn;
			break;
		case '(':
			syn = 14;//(的类别码为14
			strcpy(token, "(");
			v_out(syn, token);
			//printf("( %d, ( )\n", syn);
			return syn;
			break;
		case ')':
			syn = 15;//)的类别码为15
			strcpy(token, ")");
			v_out(syn, token);
			//printf("( %d, ) )\n", syn);
			return syn;
			break;
		case '{':
			syn = 16;
			strcpy(token, "{");
			v_out(syn, token);
			//printf("( %d, { )\n", syn);
			return syn;
			break;
		case '}':
			syn = 17;
			strcpy(token, "}");
			v_out(syn, token);
			//printf("( %d, } )\n", syn);
			return syn;
			break;
		case '<':
			syn = 18;
			ch = str[c++];
			if (ch == '=')
			{
				syn = 19; // <=的类别码为19
				strcpy(token, "<=");
				v_out(syn, token);
				//printf("( %d, <= )\n", syn);
				return syn;
			}
			else if (ch == '<')
			{
				syn = 20;//<< 的类别码为20
				strcpy(token, "<<");
				v_out(syn, token);
				//printf("( %d, << )\n", syn);
				return syn;
			}
			else if (ch == '>')
			{
				syn = 26;//<>的类别码为26
				strcpy(token, "<>");
				v_out(syn, token);
				//printf("( %d, <> )\n", syn);
				return syn;
			}
			c--;
			strcpy(token, "<");
			v_out(syn, token);
			//printf("( %d, < )\n", syn);
			return syn;
			break;
		case '=':
			syn = 21;//=的类别码为21
			ch = str[c++];
			if (ch == '=')
			{
				syn = 22;//==的类别码为22
				strcpy(token, "==");
				v_out(syn, token);
				//printf("( %d, == )\n", syn);
				return syn;
			}
			c--;
			strcpy(token, "=");
			v_out(syn, token);
			//printf("( %d, = )\n", syn);
			return syn;
			break;
		case '>':
			syn = 23;//>的类别码为23
			ch = str[c++];
			if (ch == '=')
			{
				syn = 24;//>=的类别码为24
				strcpy(token, ">=");
				v_out(syn, token);
				return syn;
			}
			else if (ch == '>')
			{
				syn = 25;//>>的类别码为25
				strcpy(token, ">>");
				v_out(syn, token);
				return syn;
			}
			c--;
			strcpy(token, ">");
			v_out(syn, token);
			//printf("( %d, > )\n", syn);
			return syn;
			break;
		case '/':
			syn = 27;// /的类别码为27
			ch = str[c++];
			if (ch == '=')
			{
				syn = 29;// /=的类别码为28
				strcpy(token, "/=");
				v_out(syn, token);
				return syn;
			}
			else if (ch == '*') // /**/认为只能读一行
			{
				//int t = c - 2;
				//ch = str[c++];
				token[0] = '/';
				token[1] = '*';
				int m;
				for (m = 2; c < n; m++)
				{
					token[m] = str[c];
					if (str[c] == '*' && str[c + 1] == '/')
						break;
					c++;
				}
				if (c < n - 1)
				{
					syn = 36;//
							 //token[m] == *
					token[m + 1] = '/';
					token[m + 2] = '\0';
					syn = 36; // /**/的类别码为36
					c = c + 2;
					v_out(syn, token);
					//printf("( %d, %s )\n", syn, token);
					return syn;
				}
				else //if (c == n)
				{
					printf("Error! /**/注释符号不完整!\n");
					return -1;
				}
				syn = 30; // /*的类别码为30,实际这个不能被打印
				strcpy(token, "/*");
				v_out(syn, token);
				//printf("( %d, /* )\n", syn);
				return syn;
			}
			else if (ch == '/')
			{
				int m = 2;
				token[0] = '/';
				token[1] = '/';
				while (str[c] != ' ') //当出现//时后面的字符都是注释 c!=n,到空格为止
				{
					token[m++] = str[c++];
				}
				token[m] = '\0';
				syn = 35;// //注释的识别码为35
				v_out(syn, token);
				//printf("( %d, %s )\n", syn, token);
				return syn;
			}
			c--;
			strcpy(token, "/");
			v_out(syn, token);
			//printf("( %d, / )\n", syn);
			return syn;
			break;
		case '!':
			syn = 31;
			ch = str[c++];
			if (ch == '=')
			{
				syn = 32;
				strcpy(token, "!=");
				v_out(syn, token);
				return syn;
			}
			c--;
			strcpy(token, "!");
			v_out(syn, token);
			return syn;
			break;
		case '&':
			ch = str[c++];
			if (ch == '&')
			{
				syn = 33;//&&的标识符为33
				strcpy(token, "&&");
				v_out(syn, token);
				return syn;
			}
			//当仅有一个&时,不是标识符,而是认为输入错误
			printf("Error! 单独的 & 不是合法的符号!\n");
			return -1; // 出现错误时,不再进行后面字符的判断
		case '|':
			ch = str[c++];
			if (ch == '|')
			{
				syn = 34;//||的标识符为34
				strcpy(token, "||");
				v_out(syn, token);
				//printf("( %d, || )\n", syn);
				return syn;
			}
			printf("Error! 单独的 | 不是合法的标识符!\n");
			return -1;
		case '#':
			syn = 55;
			strcpy(token, "#");
			v_out(syn, token);
			//printf("( %d, # )\n", syn);
			return syn;
			break;
		default:
			syn = -1;
			printf("Error! 错误的输入!\n");
			return syn;
			break;
		}
	}
}

int main()
{
	freopen("D:\\test_1.txt", "r", stdin);
	while (gets_s(gtr) != NULL)
	{
		strcat(str, gtr);
	}
	printf("词法分析的结果是: \n");
	n = strlen(str);//strlen不包括 0
	c = 0;
	int res = 0;
	do {
		res = Go();
	} while (res != -1);
	//gets_s(d_token);
	for (int i = 0; i < v_count; i++)
	{
		strcat(d_token, v[i].tk);
	}
	printf("\n递归下降程序的分析结果是: \n");
	advance();
	S();
	if (ch == '#')
		printf("Success!\n\n");
	else
		printf("error!\n\n");
}

This snippet took 0.03 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).