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

📄 word_parser.txt

📁 一个C语言编译器
💻 TXT
📖 第 1 页 / 共 2 页
字号:
				printf("</NUMBER>%s<NUMBER>\n",records);
				return token_type;
			}
        
    	   
			else if(curr_ch=='+')
			{
				next_ch=getc(res);
				if(next_ch=='+')
				{
					printf("line %d:</RELOP>++<RELOP>\n",lineno);
					token_type = SELF_ADD;
					return SELF_ADD ;
				}
				
				else
				{
					ungetc(next_ch,res);
					printf("line %d:</RELOP>+<RELOP>\n",lineno);
					token_type= '+';
					return '+';
				}
			}
			
			else if(curr_ch=='-')
			{
				next_ch=getc(res);
				if(next_ch=='-')
				{
					printf("line %d:</RELOP>--<RELOP>\n",lineno);
					token_type=SELF_SUB;
					return SELF_SUB;
				}
				
				else
				{
					ungetc(next_ch,res);
					printf("line %d:</RELOP>-<RELOP>\n",lineno);
					token_type='-';
					return '-';
				}
			}
            
			else if(curr_ch=='*')
			{				
					printf("line %d:</RELOP>*<RELOP>\n",lineno);
					token_type='*';
					return '*';				
			}
			
			else if(curr_ch=='&')
			{
				next_ch=getc(res);
				if(next_ch== '&')
				{
					printf("line %d:</LOGIC_OP>&&<LOGIC_OP>\n",lineno);
					token_type=AND;
					return AND;
				}
				else
				{
					ungetc(next_ch,res);
					printf("line %d:</RELOP>&<RELOP>\n",lineno);
					token_type='&';
					return '&';
				}
			}
			
			else if(curr_ch=='!')
			{
				next_ch=getc(res);
				if(next_ch=='=')
				{
					printf("line %d:</LOGIC_OP>!=<LOGIC_OP>\n",lineno);
					token_type=UNEQUAL;
					return UNEQUAL;
				}
				else
				{
					ungetc(next_ch,res);
					printf("line %d:</LOGIC_OP>!<LOGIC_OP>\n",lineno);
					token_type='!';
					return '!';
				}

			}
			
			else if(curr_ch=='|')
			{
				next_ch=getc(res);
				if(next_ch=='|')
				{
					printf("line %d:</LOGIC_OP>!<LOGIC_OP>\n",lineno);
					token_type=OR;
					return OR;
				}
				ungetc(curr_ch,res);
				error("illegar character");
			}
			
			else if(curr_ch == '=')
			{
				next_ch=getc(res);
				if(next_ch == '=')
				{
					printf("line %d:</LOGIC_OP>==<LOGIC_OP>\n",lineno);
					token_type=EQUIV;
					return EQUIV;
				}
				else
				{
					ungetc(next_ch,res);
					printf("line %d:</RELOP>=<RELOP>\n",lineno);
					token_type='=';
					return '=';
				}
			}
			
			else if(curr_ch=='>')
			{
				next_ch=getc(res);
				if(next_ch=='=')
				{
					printf("line %d:</LOGIC_OP> >= <LOGIC_OP>\n",lineno);
					token_type=BIG_EQUAL;
					return BIG_EQUAL;
				}
				else
				{
					ungetc(next_ch,res);
					printf("line %d:</LOGIC_OP> > <LOGIC_OP>\n",lineno);
					token_type='>';
					return '>';
				}
			}
			
			else if(curr_ch=='<')
			{
				next_ch=getc(res);
				if(next_ch=='=')
				{
					printf("line %d:</LOGIC_OP> <= <LOGIC_OP>\n",lineno);
					token_type=SMA_EQUAL;
					return SMA_EQUAL;
				}
				else
				{
					ungetc(next_ch,res);
					printf("line %d:</LOGIC_OP> < <LOGIC_OP>\n",lineno);
					token_type='<';
					return '<';
				}
			}
        
			else if(curr_ch == '%')
			{
				next_ch=getc(res);
				if(next_ch=='=')
				{
					printf("line %d:</RELOP>%=<RELOP>\n",lineno);
					token_type=REMAIN;
					return REMAIN;
				}
				else
				{
					ungetc(next_ch,res);
					printf("line %d:</RELOP>%<RELOP>\n",lineno);
					token_type='%';
					return '%';;
				}
			}
     
			else if(curr_ch==',')
			{
				printf("line %d:</LOGIC_OP> ,<LOGIC_OP>\n",lineno);
				token_type = ',';
				return ',';
			}
			
			else if(curr_ch==';')
			{
				printf("line %d:</DEPARTOR> ; <DEPARTOR>\n",lineno);
				token_type=curr_ch;
				return token_type;
			}    
			
			else if(curr_ch=='(')
			{
				if(!is_full(pair))
				{
					push('(',&pair);
				}
				else
					error("stack full");

				token_type=curr_ch;
				return token_type;
			}
			else if(curr_ch==')')
			{
				if(get_top(&pair)=='(')
					pop(&pair);
				else
					error("match character missing '('");
				
				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)	;			
				
				records[j]='\0';
				if(curr_ch=EOF)
					ungetc(curr_ch,res);

				k=insert(records,STRING);					
				printf("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"); 				
				
                token_type=curr_ch;
				return token_type;
			}
			
			else if(curr_ch=='[')
			{
				if(get_top(&pair)=='[')
					pop(&pair);
				else
					error("match character missing '['");
				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[0]=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");
				
				token_type=curr_ch;
				return token_type;
			}
			
			else if(curr_ch==')')
			{
				if(get_top(&pair))
					pop(&pair);
				else
					error("match character missing '{' ");				
				
                token_type =')';
				return token_type;
			}
    }
}
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)
{	
	if(token_val >= MAX_ID)
	{
		error(" symtable too full");
		return 0;
	}
	else
	{
		
		strcpy(sym_list[token_val].lexpre ,q);
		sym_list[token_val].field=token;
		token_val++;
		return (token_val-1);
	}
}




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->pair_list[note->top] = c;
	note->top++;
	}
}
void pop(struct stack *note)
{
	if(is_empty(note))
		error("stack empty");
	else
		note->top--;
}
void error(char *s)
{
fprintf(err,"LINE %d : %s\n",lineno,s);			
}

void recover()
{
}

⌨️ 快捷键说明

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