词法分析源码.doc_第1页
词法分析源码.doc_第2页
词法分析源码.doc_第3页
词法分析源码.doc_第4页
免费预览已结束,剩余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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论