



免费预览已结束,剩余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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 行业劳动竞赛活动方案策划
- 社会责任与经济效益平衡-洞察及研究
- 智能环境监测与调控-洞察及研究
- 数据局部性隐私泄露风险分析-洞察及研究
- 绿色零售政策对企业的影响分析-洞察及研究
- 造价咨询实施方案模板
- 西藏安全员培训考试题型及答案解析
- 建邺区亲子活动方案策划书
- 2025年光伏组件高效封装技术发展与市场前景报告
- 2025年光伏组件生产设备升级改造与绿色制造技术展望报告
- 房地产销售岗位简历模版大全
- 智能楼宇管理员试卷及答案
- 创新成果转化高效推进承诺书6篇范文
- 自卸车安全教育培训课件
- 冶金行业事故回放课件
- 5年(2021-2025)高考1年模拟地理真题分类汇编专题03 地球上的水(解析版)(浙江专用)
- 电气检修班组安全培训课件
- 2024版中国高血压防治指南(完整版)
- 不再举报协议书6篇
- 空管知识培训材料课件
- 四合一检测仪使用课件
评论
0/150
提交评论