



免费预览已结束,剩余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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 记账实操-货代公司账务处理
- 设备机械维修合同范本5篇范文
- 璀璨未来酒店设计方案:解析市场趋势与行业洞察
- 【高中语文】《客至》课件+统编版高二语文选择性必修下册
- 2024-2025学年下学期高一生物人教版期末必刷常考题之种群基因组成的变化与物种的形成
- 森林动物题目大全及答案
- 赛车比赛位置题目及答案
- 3 2 导数与函数的单调性 极值和最值-高考数学真题分类 十年高考
- 2023-2024学年江苏省盐城市高二下学期6月期末考试数学试题(解析版)
- 2023-2024学年河北省廊坊市六校高二下学期期末质量检测联考数学试卷(解析版)
- 跟着音乐去旅行智慧树知到期末考试答案章节答案2024年浙江旅游职业学院
- 老年外科患者围手术期营养支持中国专家共识(2024版)
- 企业员工保密协议书范本
- 2023年6月上海高考英语卷试题真题答案解析(含作文范文+听力原文)
- 征集和招录人员政治考核表
- 生态环境保护与可持续发展智慧树知到期末考试答案章节答案2024年浙江农林大学
- MH-T 5003-2016 民用运输机场航站楼离港系统工程设计规范
- 专题24 生物的进化-备战2024年中考《生物》复习全考点
- 康复治疗技术专业《临床疾病概要1》课程标准
- 人教版四年级数学上册全册电子教案
- 人口与贫困问题
评论
0/150
提交评论