免费预览已结束,剩余1页可下载查看
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
/*/* File: scan.c */* The scanner implementation for the TINY compiler */* Compiler Construction: Principles and Practice */* Kenneth C. Louden */*/#include #include #include #include #include globals.h#include util.h#include scan.hextern FILE * source; /* source code text file */extern FILE * listing; /* listing output text file */extern FILE * code; /* code text file for TM simulator */extern int lineno; /* source line number for listing */extern int EchoSource ;extern int TraceScan ;/* states in scanner DFA */typedef enum START,INASSIGN,INCOMMENT,INNUM,INID,DONE StateType;/* lexeme of identifier or reserved word */char tokenStringMAXTOKENLEN+1;/* BUFLEN = length of the input buffer for source code lines */#define BUFLEN 256#define EOF_FILE -1static char lineBufBUFLEN; /* holds the current line */static int linepos = 0; /* current position in LineBuf */static int bufsize = 0; /* current size of buffer string */static int EOF_flag = FALSE; /* corrects ungetNextChar behavior on EOF */* getNextChar fetches the next non-blank character from lineBuf, reading in a new line if lineBuf is exhausted */static int getNextChar(void) if (!(linepos bufsize) lineno+; if (fgets(lineBuf,BUFLEN-1,source) if (EchoSource) fprintf(listing,%4d: %s,lineno,lineBuf); bufsize = strlen(lineBuf); linepos = 0; return lineBuflinepos+; else EOF_flag = TRUE; return EOF; else return lineBuflinepos+;/* ungetNextChar backtracks one character in lineBuf */static void ungetNextChar(void) if (!EOF_flag) linepos- ;/* lookup table of reserved words */static struct char* str; TokensType tok; reservedWordsMAXRESERVED = if,IF,then,THEN,else,ELSE,end,END, repeat,REPEAT,until,UNTIL,read,READ, write,WRITE;/* lookup an identifier to see if it is a reserved word */* uses linear search */static TokensType reservedLookup (char * s) int i; for (i=0;iMAXRESERVED;i+) if (!strcmp(s,reservedWordsi.str) return reservedWordsi.tok; return ID;/*/* the primary function of the scanner */*/* function getToken returns the * next token in source file */TokensType getToken(void) /* index for storing into tokenString */ int tokenStringIndex = 0; /* holds current token to be returned */ TokensType currentToken; /* current state - always begins at START */ StateType state = START; /* flag to indicate save to tokenString */ int save; while (state != DONE) int c = getNextChar(); save = TRUE; switch (state) case START: if (isdigit(c) state = INNUM; else if (isalpha(c) state = INID; else if (c = :) state = INASSIGN; else if (c = ) | (c = t) | (c = n) save = FALSE; else if (c = ) save = FALSE; state = INCOMMENT; else state = DONE; switch (c) case EOF_FILE: save = FALSE; currentToken = ENDFILE; break; case =: currentToken = EQ; break; case : currentToken = LT; break; case +: currentToken = PLUS; break; case -: currentToken = MINUS; break; case *: currentToken = TIMES; break; case /: currentToken = OVER; break; case (: currentToken = LPAREN; break; case ): currentToken = RPAREN; break; case ;: currentToken = SEMI; break; default: currentToken = ERROR; break; break; case INCOMMENT: save = FALSE; if (c = EOF) state = DONE; currentToken = ENDFILE; else if (c = ) state = START; break; case INASSIGN: state = DONE; if (c = =) currentToken = ASSIGN; else /* backup in the input */ ungetNextChar(); save = FALSE; currentToken = ERROR; break; case INNUM: if (!isdigit(c) /* backup in the input */ ungetNextChar(); save = FALSE; state = DONE; currentToken = NUM; break; case INID: if (!isalpha(c) /* backup in the input */ ungetNextChar(); save = FALSE; state = DONE; currentToken = ID; break; case DONE: default: /* should never happen */ fprintf(listing,Scanner Bug: state= %dn,state); state = DONE; currentToken = ERROR; break; if (save) & (tokenStringIndex = MAXTOKENLEN) tokenStringtokenStringIndex+ = (char) c; if (state = DONE) tokenStringtokenString
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 广州家庭住宅租赁合同范例
- 临翔区商标转让合同范例
- 库房加固合同范例
- 2024年郑州客运上岗证急救知识
- 2024年吉林客车驾驶员考试试题加解析
- 广西南宁市2024-2025学年七年级上学期段考历史试题
- 玻璃厂采光井施工合同
- 教育设施建造师聘用合同模板
- 机械设备采购投标资料范本一
- 城市综合体商铺租赁条款
- 安徽省建设工程造价咨询服务项目及收费标准
- 建筑工程关键施工技术工艺及工程项目实施的重点难点和解决方案
- 泌尿系统梗阻病人的护理.ppt
- (完整版)初中数学中考考试大纲
- 柴油机的振动与平衡-文档资料
- 广东省河流水功能二级区划成果表
- 纯燃高炉煤气锅炉吸热特点及运行
- 酒驾私了协议书——范本
- 森林施工组织设计(完整版)
- 304不锈钢冷轧剥片缺陷分析及控制
- 立体停车库详解
评论
0/150
提交评论