词法分析器.zip

  • 一一一10!
    了解作者
  • C/C++
    开发工具
  • 55KB
    文件大小
  • zip
    文件格式
  • 0
    收藏次数
  • 1 积分
    下载积分
  • 0
    下载次数
  • 2021-04-15 17:36
    上传日期
C-语言的词法分析器,能够逐行对C-进行编译,进行词法分析
词法分析器.zip
  • 词法分析器
  • scan.cpp
    6.4KB
  • cmi.cminus
    329B
  • scan.h
    660B
  • unit.cpp
    5.1KB
  • C-.dev
    1.4KB
  • scan.o
    4.7KB
  • Makefile.win
    1.2KB
  • C-.exe
    136.3KB
  • C-.layout
    80B
  • main.o
    1.6KB
  • main.cpp
    1.2KB
  • unit.o
    5.7KB
  • unit.h
    1KB
  • globals.h
    2.9KB
内容介绍
#include <stdio.h> #include <stdlib.h> #include <stdio.h> #include <ctype.h> #include <string.h> #include "globals.h" #include "unit.h" #include "scan.h" extern 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,INUNEQ, INCOMMENTL,INCOMMENT,INCOMMENTR,COMPAREL,COMPARER ,INNUM, INID, DONE } StateType; /* lexeme of identifier or reserved word */ char tokenString[MAXTOKENLEN + 1]; /* BUFLEN = length of the input buffer for source code lines */ #define BUFLEN 256 #define EOF_FILE -1 static char lineBuf[BUFLEN]; /* 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 behav ior 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, "%d: %s", lineno, lineBuf); bufsize = strlen(lineBuf); linepos = 0; return lineBuf[linepos++]; } else { EOF_flag = TRUE; return EOF; } } else return lineBuf[linepos++]; } /* 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; } reservedWords[MAXRESERVED] = { { "if", IF }, { "else", ELSE }, { "int", INT }, { "return", RETURN }, { "void", VOID }, { "while", WHILE } }; /* lookup an identifier to see if it is a reserved word */ /* uses linear search */ static TokensType reservedLookup(char * s) { int i; for (i = 0; i<MAXRESERVED; i++) if (!strcmp(s,reservedWords[i].str)) return reservedWords[i].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 == '!') state = INUNEQ; else if ((c == ' ') || (c == '\t') || (c == '\n')) save = FALSE; else if (c == '/') { save = FALSE; state = INCOMMENTL; } else if (c == '<') state = COMPAREL; else if (c == '>') state = COMPARER; else { state = DONE; switch (c) { case EOF: save = FALSE; currentToken = ENDFILE; break; //case '=': // currentToken = EQ; // break; case '+': currentToken = PLUS; break; case '-': currentToken = MINUS; break; case '*': currentToken = TIMES; break; //case '/': // currentToken = OVER; // break; case '(': currentToken = LPARENS; break; case ')': currentToken = RPARENS; break; case '[': currentToken = LPARENM; break; case ']': currentToken = RPARENM; break; case '{': currentToken = LPARENL; break; case '}': currentToken = RPARENL; break; case ';': currentToken = SEMI; break; case ',': currentToken = COMMA; break; default: currentToken = ERROR; break; } } break; case INCOMMENTL: state = DONE; if (c == '*') { save = FALSE; state = INCOMMENT; break; } else currentToken = OVER; break; case INCOMMENT: save = FALSE; if (c == EOF) { state = DONE; currentToken = ENDFILE; } else if (c == '*') state = INCOMMENTR; break; case INCOMMENTR: save = FALSE; if (c == EOF) { state = DONE; currentToken = ENDFILE; } else if (c == '/') state = START; break; case COMPAREL: state = DONE; if (c == '=') currentToken = LE; else {/* backup in the input */ ungetNextChar(); save = FALSE; state = DONE; currentToken = LT; } break; case COMPARER: state = DONE; if (c == '=') currentToken = ME; else {/* backup in the input */ ungetNextChar(); save = FALSE; state = DONE; currentToken = MT; } break; case INASSIGN: state = DONE; if (c == '=') currentToken = EQ; else {/* backup in the input */ ungetNextChar(); save = FALSE; currentToken = ASSIGN; } break; case INUNEQ: state = DONE; if (c == '=') currentToken = UNEQ; 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= %d\n", state); state = DONE; currentToken = ERROR; break; } if ((save) && (tokenStringIndex <= MAXTOKENLEN)) tokenString[tokenStringIndex++] = (char)c; if (state == DONE) { tokenString[tokenStringIndex] = '\0'; if (currentToken == ID) currentToken = reservedLookup(tokenString); } } if (TraceScan) { fprintf(listing, "%d: ", lineno); printToken(currentToken, tokenString); } return currentToken; } /* end getToken */
评论
    相关推荐
    • 词法分析器
      基于c语言C的一个词法分析器,能实现对关键字,标识符,常数,界符以及运算符的识别,很适合初学者
    • 词法分析器
      这是一个用MFC编写的词法分析器,适用于编译原理中对词法的分析过程,包括源代码等。
    • 词法分析器
      设计并实现一个词法分析器,实现对指定位置的类C语言源程序文本文件的读取,并能够对该源程序中的所有单词进行分类,指出其所属类型,实现简单的词法分析操作。
    • 词法分析器
      词法分析器,基本的C语言词法分析。。。。。。。。。。。。。。。。。。
    • 词法分析器
      手撸纯C词法分析器一枚,可以试下多种语言的词法分析功能。 只要改一下keywd表就行了。主要是一个自动机。。。。。。。 可以实现跨平台,gcc vc均有测试。大家可以自己接语法树生成处理。
    • 词法分析器
      词法分析器可实现编译原理中词法的功能 包括程序段中关键字、数据类型、数值型变量、字符串的有效识别和分类,并有详细的注释,代码可读性很强。
    • 词法分析器
      待分析的简单的词法 (1)注释: 以“//”开头到该行尾部为注释 (2)关键字(共6个): int real if then else while (3)标识符(ID)和整型常数(NUM): ...符和关键字,词法分析阶段通常被忽略。
    • 词法分析器.rar
      实现标准C语言词法分析器。 单词种别编码要求: 基本字、运算符、界符:一符一种 标识符:统一为一种 常量:按类型编码
    • 词法分析器.zip
      可以对PL/0的程序进行词法分析归类,并实现文件的输出
    • parse_c.zip
      C语言语法检查器。