【编译原理】词法分析器
来源:互联网 发布:淘宝节点考试在哪里 编辑:程序博客网 时间:2024/04/29 20:18
#include <stdio.h>#include <iostream>#include <string.h>using namespace std;// char *keyword[6] = {"begin", "if", "then", "while", "do", "end"};// char *opword[16] = {":", ":=", "+", "-", "*", "/", "<", "<=", "<>", ">", ">=", "=", ";", "(", ")", "#"};bool showline = true;int syn;char token[100];int num;int row;bool huiche = false;int t2, t1;int STATE;int inSTATE;bool inzhushi = false;void clearpos(){ memset(token, 0, sizeof(token)); t1 = 0; t2 = 1; num = 0;}bool isvar(char *s){ int l = strlen(s); if (!isalpha(s[0])) return false; for (int i = 0; i < l; i++) { if (isdigit(s[i]) || isalpha(s[i])) continue; else return false; } return true;}bool isint(char *s){ for (int i = 0; i < strlen(s); i++) { if (!isdigit(s[i])) return false; } return true;}int getnum(char *s){ if (STATE == 2) return 11; if (strcmp(s, "begin") == 0)return 1; else if (strcmp(s, "if") == 0)return 2; else if (strcmp(s, "then") == 0)return 3; else if (strcmp(s, "while") == 0)return 4; else if (strcmp(s, "do") == 0)return 5; else if (strcmp(s, "end") == 0)return 6; else if (isvar(s)) return 10; else if (strcmp(s, "+") == 0)return 13; else if (strcmp(s, "-") == 0)return 14; else if (strcmp(s, "*") == 0)return 15; else if (strcmp(s, "/") == 0)return 16; else if (strcmp(s, ":") == 0)return 17; else if (strcmp(s, ":=") == 0)return 18; else if (strcmp(s, "<") == 0)return 20; else if (strcmp(s, "<>") == 0)return 21; else if (strcmp(s, "<=") == 0)return 22; else if (strcmp(s, ">") == 0)return 23; else if (strcmp(s, ">=") == 0)return 24; else if (strcmp(s, "=") == 0)return 25; else if (strcmp(s, ";") == 0)return 26; else if (strcmp(s, "(") == 0)return 27; else if (strcmp(s, ")") == 0)return 28; else if (strcmp(s, "#") == 0)return 0; else if (s[0] == '/' && s[1] == '*')return -2; else return -1;}void output(){ if (huiche && showline) { huiche = false; printf("\n"); printf("%d: ", row); } syn = getnum(token); if (syn == -1) { printf("(error!,%s)\n", token); return; } else if (syn == -2) { inzhushi = true; inSTATE = -2; int l = strlen(token); if (token[l - 1] == '/' && token[l - 2] == '*' && l >= 4) { inzhushi = false; STATE = 0; } return; } else if (STATE == 2) { if (isint(token)) printf("(%d,%s)", 11, token); else printf("(error!,%s)\n", token); } else { printf("(%d,%s)", syn, token); } if (showline == false) printf("\n");}int main(){ freopen("C:\\Users\\Sky\\Desktop\\1.in", "r", stdin); char ch; row = 1; clearpos(); STATE = 0; num = 0; t1 = 0; if (showline) printf("%d: ", row); while (scanf("%c", &ch),ch!='#') { if (inzhushi) { if (ch == '\n' && showline) { row++; printf("\n"); printf("%d: ", row); } if (inSTATE == -2)//注释起始状态 { clearpos(); if (ch == '*') inSTATE = 0; } else if (inSTATE == -1)//有*状态1 { if (ch == '*') inSTATE = 0; else inSTATE = -2; } else if (inSTATE == 0)//有*状态2 { if (ch == '*') inSTATE = 0; else if (ch == '/' ) { inzhushi = false; STATE = 0; } else inSTATE = -1; } continue; } if (STATE == 0)//空状态 { if (isalpha(ch)) { STATE = 1; token[t1++] = ch; } else if (isdigit(ch)) { STATE = 2; // num = ch - '0'; token[t1++] = ch; } else if (ch == '\n' || ch == ' ') { STATE = 0; if (ch == '\n') { huiche = true; row++; } } else { STATE = 3; token[t1++] = ch; } } else if (STATE == 1)//变量,关键字状态 { if (isalpha(ch)) { token[t1++] = ch; } else if (isdigit(ch)) { token[t1++] = ch; } else if (ch == '\n' || ch == ' ') { output(); clearpos(); STATE = 0; if (ch == '\n') { huiche = true; row++; } } else { output(); clearpos(); STATE = 3; token[t1++] = ch; } } else if (STATE == 2)//数字状态 { if (isalpha(ch)) { token[t1++] = ch; } else if (isdigit(ch)) { // num *= 10; // num += ch - '0'; token[t1++] = ch; } else if (ch == '\n' || ch == ' ') { output(); clearpos(); STATE = 0; if (ch == '\n') { huiche = true; row++; } } else { output(); clearpos(); STATE = 3; token[t1++] = ch; } } else if (STATE == 3)//符号集状态 { if (isalpha(ch)) { output(); clearpos(); STATE = 1; token[t1++] = ch; } else if (isdigit(ch)) { output(); clearpos(); STATE = 2; // num = ch - '0'; token[t1++] = ch; } else if (ch == '\n' || ch == ' ') { output(); clearpos(); STATE = 0; if (ch == '\n') { huiche = true; row++; } } else { token[t1++] = ch; } } } return 0;}// begin// /*sdas/*d*/// x2231:=2;// if x>9// then x:=2*x+1/3;// end #
0 0
- 编译原理---词法分析器
- 编译原理词法分析器
- 编译原理 词法分析器
- 【编译原理】词法分析器
- 编译原理词法分析器
- 编译原理-词法分析器
- 编译原理词法分析器
- 编译原理词法分析器
- 编译原理--词法分析器
- 编译原理—词法分析器
- 编译原理----Lex词法分析器
- 词法分析器-2-编译原理
- 【编译原理】Lex词法分析器
- 编译原理__词法分析器
- 编译原理(词法分析器)
- 编译原理词法分析器c++源程序
- 编译原理课程设计之词法分析器
- 编译原理->词法分析器的分析
- 语言的最高境界
- Fragment 系列一<基础知识详解>
- 回家过会疯掉岁更丰富考核结果符合国
- SET IDENTITY_INSERT 学习心得
- ACM概率题
- 【编译原理】词法分析器
- LIBSVM在python下的使用
- 心态
- sphinx php 扩展安装
- 额try服饰风格的的发生
- [leetcode] Word Break II
- Android研究之游戏开发主角与地图的滚动
- Linux内核剖析 之 内存寻址(三)
- 的符合国家规范的发生的