



免费预览已结束,剩余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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 高一英语学案:预习导航Themeparks-SectionⅡ
- 2024年铜陵市中医医院招聘真题
- 2024年黔西市市属事业单位考试真题
- 2024年邳州农村商业银行招聘真题
- 趣味课堂-创意无限卡通模板
- 2024年江苏师范大学科文学院招聘专职辅导员真题
- 2024年广安市前锋区定向选聘社区工作者真题
- 技术入股合作协议书(2025年版)
- 人教初中地理八下八年级地理期末试题1
- 物品采购合同范本药品
- 【16G101】钢筋平法图集三维速查2020 88P
- 《践行社会主义核心价值观》主题班会教案
- 消防应急组织架构图
- TJSTJXH 6-2022 城市轨道交通工程盾构管片预制及拼装技术标准
- 信息技术2.0微能力:小学五年级道德与法治上(第三单元)守望相助-中小学作业设计大赛获奖优秀作品-《义务教育道德与法治课程标准(2022年版)》
- 新教材人教版高中化学选择性必修三全册知识点梳理
- 基于嵌入式系统的无线传感器网络的应用研究
- Q∕SY 08124.23-2017 石油企业现场安全检查规范 第23部分:汽车装卸车栈台
- 有机化学 第十三章 有机含氮化合物
- 青岛版小学二年级数学下册《解决问题(信息窗3)》参考课件(共12张PPT)
- 中华护理学会科研课题申请书
评论
0/150
提交评论