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

📄 parse.c

📁 一个C语言编译器
💻 C
📖 第 1 页 / 共 2 页
字号:
				*(records+j)='\0';
				insert(records,token_type);
				fprintf(out_parse,"line %d:</NUMBER>%s<NUMBER>\n",lineno,records);
				return token_type;
			}


			else if(curr_ch=='+')
			{
				next_ch=getc(res);
				if(next_ch=='+')
				{
					fprintf(out_parse,"line %d:</RELOP>++<RELOP>\n",lineno);
					token_type = SELF_ADD;
					return SELF_ADD ;
				}

				else
				{
					ungetc(next_ch,res);
					fprintf(out_parse,"line %d:</RELOP>+<RELOP>\n",lineno);
					token_type= '+';
					return '+';
				}
			}

			else if(curr_ch=='-')
			{
				next_ch=getc(res);
				if(next_ch=='-')
				{
					fprintf(out_parse,"line %d:</RELOP>--<RELOP>\n",lineno);
					token_type=SELF_SUB;
					return SELF_SUB;
				}

				else
				{
					ungetc(next_ch,res);
					fprintf(out_parse,"line %d:</RELOP>-<RELOP>\n",lineno);
					token_type='-';
					return '-';
				}
			}

			else if(curr_ch=='*')
			{				
				fprintf(out_parse,"line %d:</RELOP>*<RELOP>\n",lineno);
				token_type='*';
				return '*';				
			}

			else if(curr_ch=='&')
			{
				next_ch=getc(res);
				if(next_ch== '&')
				{
					fprintf(out_parse,"line %d:</LOGIC_OP>&&<LOGIC_OP>\n",lineno);
					token_type=AND;
					return AND;
				}
				else
				{
					ungetc(next_ch,res);
					fprintf(out_parse,"line %d:</RELOP>&<RELOP>\n",lineno);
					token_type='&';
					return '&';
				}
			}

			else if(curr_ch=='!')
			{
				next_ch=getc(res);
				if(next_ch=='=')
				{
					fprintf(out_parse,"line %d:</LOGIC_OP>!=<LOGIC_OP>\n",lineno);
					token_type=UNEQUAL;
					return UNEQUAL;
				}
				else
				{
					ungetc(next_ch,res);
					fprintf(out_parse,"line %d:</LOGIC_OP>!<LOGIC_OP>\n",lineno);
					token_type='!';
					return '!';
				}

			}

			else if(curr_ch=='|')
			{
				next_ch=getc(res);
				if(next_ch=='|')
				{
					fprintf(out_parse,"line %d:</LOGIC_OP>!<LOGIC_OP>\n",lineno);
					token_type=OR;
					return OR;
				}
				ungetc(curr_ch,res);
				error("illegar character");
				return PARSE_ERROR;
			}

			else if(curr_ch == '=')
			{
				next_ch=getc(res);
				if(next_ch == '=')
				{
					fprintf(out_parse,"line %d:</LOGIC_OP>==<LOGIC_OP>\n",lineno);
					token_type=EQUIV;
					return EQUIV;
				}
				else
				{
					ungetc(next_ch,res);
					fprintf(out_parse,"line %d:</RELOP>=<RELOP>\n",lineno);
					token_type='=';
					return '=';
				}
			}

			else if(curr_ch=='>')
			{
				next_ch=getc(res);
				if(next_ch=='=')
				{
					fprintf(out_parse,"line %d:</LOGIC_OP> >= <LOGIC_OP>\n",lineno);
					token_type=BIG_EQUAL;
					return BIG_EQUAL;
				}
				else
				{
					ungetc(next_ch,res);
					fprintf(out_parse,"line %d:</LOGIC_OP> > <LOGIC_OP>\n",lineno);
					token_type='>';
					return '>';
				}
			}

			else if(curr_ch=='<')
			{
				next_ch=getc(res);
				if(next_ch=='=')
				{
					fprintf(out_parse,"line %d:</LOGIC_OP> <= <LOGIC_OP>\n",lineno);
					token_type=SMA_EQUAL;
					return SMA_EQUAL;
				}
				else
				{
					ungetc(next_ch,res);
					fprintf(out_parse,"line %d:</LOGIC_OP> < <LOGIC_OP>\n",lineno);
					token_type='<';
					return '<';
				}
			}

			else if(curr_ch == '%')
			{
				next_ch=getc(res);
				if(next_ch=='=')
				{
					fprintf(out_parse,"line %d:</RELOP>%=<RELOP>\n",lineno);
					token_type=REMAIN;
					return REMAIN;
				}
				else
				{
					ungetc(next_ch,res);
					fprintf(out_parse,"line %d:</RELOP>%<RELOP>\n",lineno);
					token_type='%';
					return '%';
				}
			}

			else if(curr_ch==',')
			{
				fprintf(out_parse,"line %d:</LOGIC_OP> ,<LOGIC_OP>\n",lineno);
				token_type = ',';
				return ',';
			}

			else if(curr_ch==';')
			{
				fprintf(out_parse,"line %d:</DEPARTOR> ; <DEPARTOR>\n",lineno);
				token_type=';';
				return ';';
			}    

			else if(curr_ch=='(')
			{
				if(!is_full(&pair))
				{
					push('(',&pair);
				}
				else
					error("stack full");
			    fprintf(out_parse,"line :%d</PAIR_MATCH>%c<PAIR_MATCH>\n",lineno,curr_ch);  		     
				token_type=curr_ch;
				return token_type;
			}
			else if(curr_ch==')')
			{
				if(get_top(&pair)=='(')
					pop(&pair);
				else				
				{
					printf("--pair top:%c--\n",get_top(&pair));
					error("match character missing '(' ");
				}
				 fprintf(out_parse,"line :%d</PAIR_MATCH>%c<PAIR_MATCH>\n",lineno,curr_ch);     
				token_type=curr_ch;
				return token_type;
			}

			else if(curr_ch=='\"')
			{
				j=k=0  ;
				do
				{
					curr_ch=getc(res);
					if(curr_ch=='\n')
						lineno++;						
					records[j++]=curr_ch;
					
				} while(curr_ch!='\"'&& curr_ch != EOF&&j< ID_SIZE);			

				records[j-1]='\0';
				if(curr_ch==EOF)
					ungetc(curr_ch,res);
				if(j>=ID_SIZE)
                    error("too long string");    	     
				k=insert(records,STRING);					
				fprintf(out_parse,"line %d:</STRING>%s<STRING>\n",lineno,records);
				    
				token_type=STRING;
				return STRING; 				
			}

			else if(curr_ch=='[')
			{
				if(!is_full(&pair))
				{
					push('[',&pair);
				}
				else
					error("stack full"); 				
			    fprintf(out_parse,"line :%d</PAIR_MATCH>%c<PAIR_MATCH>\n",lineno,curr_ch); 	
				token_type=curr_ch;
				return token_type;
			}

			else if(curr_ch==']')
			{
				if(get_top(&pair)=='[')
					pop(&pair);
				else
					error("match character missing '['");
				 fprintf(out_parse,"line :%d</PAIR_MATCH>%c<PAIR_MATCH>\n",lineno,curr_ch); 
				token_type=curr_ch;
				return token_type;
			}

			else if( curr_ch=='\'')
			{
				curr_ch=getc(res);
				if(curr_ch!='\\')
				{
					if( curr_ch=getc(res)!= '\'')
					{
						error("unmatch character ' ");
						ungetc(curr_ch,res);
					}
					insert("\'",SINGLE_CHAR);
					token_type=SINGLE_CHAR;
					return token_type;
				}
				else
				{
					curr_ch=getc(res);
					if(curr_ch=='t')
						insert("\t",SINGLE_CHAR);
					else if(curr_ch=='n')
						insert("\n",SINGLE_CHAR);
					else if(curr_ch=='b')
						insert("\b",SINGLE_CHAR);
					else if(curr_ch=='r')
						insert("\r",SINGLE_CHAR);
					else if(curr_ch=='f')
						insert("\f",SINGLE_CHAR);
					else if(curr_ch=='\\')
						insert("\\",SINGLE_CHAR);
					else if(curr_ch=='\'')
						insert("\'",SINGLE_CHAR);
					else if(curr_ch=='\"')
						insert("\"",SINGLE_CHAR);
					else
					{ 
						*records=curr_ch;
						*(records+1)='\0';
						insert(records,SINGLE_CHAR);
						error("illegar character in ASCII");
					}
					token_type=SINGLE_CHAR;
					return token_type;
				}
			}

			else if(curr_ch=='{')
			{
				if(!is_full(&pair))
				{
					push('{',&pair);
				}
				else
					error("stack full");
			    fprintf(out_parse,"line :%d</PAIR_MATCH>%c<PAIR_MATCH>\n",lineno,curr_ch); 	
				token_type=curr_ch;
				return token_type;
			}

			else if(curr_ch=='}')
			{
				if(get_top(&pair)=='{')
					pop(&pair);
				else					
				{
					printf("--pair top:%c --\n",get_top(&pair));
					error("match character missing '{' ");
				}    				
			   fprintf(out_parse,"line :%d</PAIR_MATCH>%c<PAIR_MATCH>\n",lineno,curr_ch); 
				token_type ='}';
				return token_type;
			}
			else
			{
				strcpy(records,"unrecognized character ");
				records[strlen(records)]=curr_ch;
				records[strlen(records)+1]='\0';
				error(records);
				return PARSE_ERROR; 
			}			
		}
	}
	int lookup(char *q)
	{
		int i;

		for(i=1;i<=KEY_MOUNT;i++)
		{
			if(!strcmp( q,symtable[i].lexpre))
				return i;
		}
		return 0;
	}
	int insert(char *q,int token)
	{

		struct symbol *temp;
		sym_list_curr->next=temp=(struct symbol*)malloc(sizeof(struct symbol));		
		strcpy(temp->lexpre ,q);
		temp->field=token;
		temp->next=NULL;
		if(token_val==0)
			sym_list_head->next=temp;
		token_val++;
		sym_list_curr=temp;
		return token_val;

	}




	void test(struct stack *note)
	{
		char c=33;
		printf("\npush\n");
		printf("the stack top:%d\n",note->top);
		while((!is_full(note))&& c<=126)
		{
			    push(c,note);
				printf("%c",get_top(note));
				c++;
		}
		printf("\nthe stack top:%d\n",note->top);
		printf("\npop out\n");
		printf("the stack top:%d\n",note->top);
		while(!is_empty(note))
		{
		 	printf("%c",pop(note));
		}
		printf("\nthe stack top:%d\n",note->top);
		printf("\ntest over\n");
	}
	int is_full(struct stack *note)
	{
		if(note->top>=100)
			return 1;
		else
			return 0;
	}
	int is_empty(struct stack *note)
	{
		if(note->top<=0)
			return 1;
		else
			return 0;
	} 
	int get_top(struct stack *note)
	{
		return note->pair_list[note->top];
	}       
	void push(char c,struct stack *note)
	{
		if(is_full(note))
			error("stack full");
		else
		{			
			note->top++;
			note->pair_list[note->top] = c;
		}
	}
	int pop(struct stack *note)
	{
		if(is_empty(note))
			error("stack empty");
		else
		{
			note->top--;         
			return get_top(note);
		}    

	}
	void error(char *s)
	{
		fprintf(err,"LINE %d : %s\n",lineno,s);			
	}

	void recover()
	{
	}

⌨️ 快捷键说明

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