小型编译器之C-词法分析器 可以作为一般小型编译器的模板

源代码在线查看: util.c

软件大小: 28 K
上传用户: zdh103
关键词: 编译器 分析器 模板
下载地址: 免注册下载 普通下载 VIP

相关代码

				/****************************************************/
				/* File: util.c                                     */
				/* Utility function implementation                  */
				/* for the TINY compiler                            */
				/* Compiler Construction: Principles and Practice   */
				/* Kenneth C. Louden                                */
				/****************************************************/
				
				#include "globals.h"
				#include "util.h"
				
				
				void printToken( TokenType token, const char* tokenString )
				{ switch (token)
				  { case IF:
				    case RETURN:
				    case ELSE:
				    case VOID:
				    case INT:
				    case WHILE:
				
				      fprintf(listing,
				         "reserved word: %s\n",tokenString);
				      break;
				    case ASSIGN: fprintf(listing,"!=\n"); break;
				    case LT: fprintf(listing,"					case EQ: fprintf(listing,"=\n"); break;
					case EQQ: fprintf(listing,"==\n"); break;
					case LTT: fprintf(listing,"				    case RT: fprintf(listing,">\n"); break;
					case RTT: fprintf(listing,">=\n"); break;
				    case LPAREN: fprintf(listing,"(\n"); break;
				    case RPAREN: fprintf(listing,")\n"); break;
					case LPARENZ: fprintf(listing,"[\n"); break;
				    case RPARENZ: fprintf(listing,"]\n"); break;
					case LPAREND: fprintf(listing,"{\n"); break;
				    case RPAREND: fprintf(listing,"}\n"); break;
				    case SEMI: fprintf(listing,";\n"); break;
				    case PLUS: fprintf(listing,"+\n"); break;
				    case MINUS: fprintf(listing,"-\n"); break;
				    case TIMES: fprintf(listing,"*\n"); break;
				    case OVER: fprintf(listing,"/\n"); break;
				    case ENDFILE: fprintf(listing,"EOF\n"); break;
					case COMMA: fprintf(listing,",\n"); break;
				    case NUM:
				      fprintf(listing,
				          "NUM, val= %s\n",tokenString);
				      break;
				    case ID:
				      fprintf(listing,
				          "ID, name= %s\n",tokenString);
				      break;
				    case ERROR:
				      fprintf(listing,
				          "ERROR: %s\n",tokenString);
				      break;
				    default: /* should never happen */
				      fprintf(listing,"Unknown token: %d\n",token);
				  }
				}
				
							

相关资源