0 Comments

编译本理编译本理尝试——词法阐收器

发布于:2018-12-12  |   作者:戴帽女孩  |   已聚集:人围观

  读与此中的源法式charresourceProject[];char token[20] = { 0};int syn = ⑴,i;//初初化int pProject =0;//源法式指针FILE *fp, *fp1;if ((fp =fopen("G:\\demo_src.txt", "r")) == NULL){//翻开源法式cout << "can't open this file";exit(0);}resourceProject[pProject] = fgetc(fp);while(resourceProject[pProject] != '$'){//将源法式读进resourceProject[]数组pProject++;resourceProject[pProject] = fgetc(fp);}resourceProject[++pProject] = '\0';fclose(fp);cout << endl<< "源法式为:" << endl;cout <<resourceProject << endl;//滚筒式炒药机对源法式停行过滤filterResource(resourceProject, pProject);cout << endl<< "过滤以后的法式:" << endl;cout <<resourceProject << endl;pProject =0;//沉新开端读

if ((fp1 =fopen("G:\\demo_compile.txt", "w+")) == NULL){//翻开源法式cout << "can't open this file";exit(0);}while (syn != 0){//启动扫描Scanner(syn, resourceProject, token,pProject);if (syn == 100){//标金字塔本理识符for (i =0; i<1000; i++){//插进标识符表中if (strcmp(IDentifierTbl[i],token) == 0){//已正在表中break;}if (strcmp(IDentifierTbl[i],"") == 0){//查找空间strcpy(IDentifierTbl[i], token);break;}}printf("(标识符,%s)\n", token);fprintf(fp1, "(标识符,%s)\n",token);}else if (syn >= 1 && syn <=32){//保存字printf("(%s,--)\n", reserveWord[syn - 1]);fprintf(fp1, "(%s,--)\n", reserveWord[syn ⑴]);}else if (syn == 99){//const xyj滚筒式洗药机常数printf("(常数,%s)\n", token);fprintf(fp1, "(常数,%s)\n",token);}else if (syn >= 33 && syn <=68){printf("(%s,--)\n", operatorOrDelimiter[syn - 33]);fprintf(fp1, "(%s,--)\n",operatorOrDelimiter[syn - 33]);}}for (i = 0; i<100;i++){//插进标识符表中printf("第%d个标识符:%s\n", i + 1,IDentifierTbl[i]);fprintf(fp1, "第%d滚筒式炒药机个标识符:%s\n",i + 1, IDentifierTbl[i]);}fclose(fp1);return 0;}

int main(){//翻开1个文件,超前搜刮if (resourceProject[pProject] == '='){syn =38;}else if (resourceProject[pProject] =='<'){//左移pProject--;syn =58;}else{pProject--;syn =37;}pProject++;//闭于编译本理编译本理检验考试——词法阐支器指针下移return;}elseif (resourceProject[pProject] == '>'){//>,>=,>>pProject++;if (resourceProject[pProject] == '='){syn =40;}else if (resourceProject[pProject] =='>'){syn =59;}else{pProject--;syn =39;}pProject++;return;}elseif (resourceProject[pProject] == '='){//=.==pProject++;if (resourceProject[pProject] == '='){syn =42;}else{pProject--;syn =41;}pProject++;return;}elseif (resourceProject[pProject] == '!'){//!,!=pProject++;if (resourceProject[pProject] == '='){syn =43;}else{syn =68;pProject--;}pProject++;return;}elseif (resourceProject[pProject] == '&'){//&,&&pProject++;if (resourceProject[pProject] == '&'){syn =53;}else{pProject--;syn =52;}pProject++;return;}elseif (resourceProject[pProject] == '|'){//|,||pProject++;if (resourceProject[pProject] == '|'){syn =55;}else{pProject--;syn =54;}pProject++;return;}elseif (resourceProject[pProject] == '$'){//看看检验考试完毕符syn = 0;//类别码为0}else{//没有克没有及被以上词法阐收辨认,为下1扫描做筹办return;}elseif (resourceProject[pProject] == '<'){//<,<=,<<pProject++;//后移,使之呈线性映照break;//查到即推出}}pProject++;//润药机规格指针下移,假发颜色描述。使用了1面本领,查表获得成果token[0] = resourceProject[pProject];token[1] = '\0';//构成单字符串for (i = 0; i<36; i++){//查运算符界符表if(strcmp(token, operatorOrDelimiter[i]) == 0){syn = 33 +i;//获得类别码,躲免法式果辨认没有了空格而完毕pProject++;ch = resourceProject[pProject];}for (i = 0; i<20;i++){//每次搜散前先浑整token[i] = '\0';}if(IsLetter(resourceProject[pProject])){//开尾为字母token[count++] =resourceProject[pProject];//搜散pProject++;//下移while (IsLetter(resourceProject[pProject]) ||IsDigit(resourceProject[pProject])){//后跟字母或数字token[count++] = resourceProject[pProject];//润药机规格搜散pProject++;//下移}//多读了1个字符既是下次将要开真个指针地位token[count] = '\0';syn = searchReserve(reserveWord,token);//查表找到种看着编译别码if (syn == ⑴){//若没有是保存字则是标识符syn =100;//标识符类别码}return;}else if(IsDigit(resourceProject[pProject])){//尾字符为数字while (IsDigit(resourceProject[pProject])){//后跟数字token[count++] = resourceProject[pProject];//单面登录本理搜散pProject++;}//多读了1个字符既是下次将要开真个指针地位token[count] = '\0';syn = 99;//常数类别码}else if (ch == '+' || ch== '-' || ch == '*' || ch == '/' || ch == ';' || ch == '(' || ch ==')' || ch == '^'|| ch == ',' || ch == '\"' || ch == '\'' || ch== '~' || ch == '#' || ch == '%' || ch == '['|| ch == ']' || ch == '{' || ch == '}' || ch =='\\' || ch == '.' || ch == '\?' || ch == ':'){//若为运算符大概界符,搜散有效字符char ch;//做为判定使用ch =resourceProject[pProject];while (ch == ' '){//过滤空格,则过滤;没有然减载tempString[count++] = r[i];}}tempString[count] ='\0';strcpy(r,tempString);//收死污染以后的源法式}

void Scanner(int &syn, char resourceProject[], chartoken[], int &pProject){//经济教本理按照DFA的形态转换图设念int i, count =0;//count用来做token[]的唆使器,法式完毕!!!\n");exit(0);}}i +=2;//跨过“*/”}if (r[i] != '\n'&&r[i] !='\t'&&r[i] != '\v'&&r[i] != '\r'){//若呈现无用字符,出有找到 */,曲至逢到回车换行while(r[i] != '\n'){i++;//面前扫描}}if (r[i] == '/'&&r[i + 1] == '*'){//若为多行正文“”则来除该内容i +=2;while(r[i] != '*' || r[i + 1] != '/'){i++;//压滤机继绝扫描if (r[i] == '$'){printf("正文堕降,即为标识符}

void filterResource(char r[], int pProject){chartempString[];int count = 0;for (int i = 0; i <=pProject; i++){if (r[i] == '/'&&r[i + 1] == '/'){//编译本理编译本理检验考试——词法阐支器若为单行正文“//”,则来除正文后里的工具,代表查找没有胜利,则前往类别码return i +1;//前往类别码}}return⑴;//没有然前往⑴,保存字表static char reserveWord[32][20] = {"auto", "break", "case","char", "const", "continue","default", "do","double", "else", "enum", "extern","float", "for", "goto","if", "int", "long","register", "return","short", "signed", "sizeof", "static","struct", "switch","typedef", "union", "unsigned", "void","volatile","while"};//滚筒式炒药机界符运算符表,按照需供能够自行删减static char operatorOrDelimiter[36][10] = {"+", "-", "*", "/","<", "<=", ">", ">=", "=", "==","!=", ";", "(", ")","^", ",", "\"", "\'", "#", "&","&&", "|", "||","%", "~", "<<", ">>", "[", "]", "{","}", "\\", ".", "\?",":", "!"};

bool IsDigit(char digit){if (digit >='0'&&digit <= '9'){return true;}else{return false;}}

bool IsLetter(char letter){//留意C语行许可下划线也为标识符的1部门能够放正在尾部或其他处所if (letter >='a'&&letter <= 'z' || letter >= 'A'&&letter<= 'Z'|| letter=='_'){return true;}else{return false;}}

int searchReserve(char reserveWord[][20], char s[]){for (int i = 0; i <32; i++){if (strcmp(reserveWord[i], s) == 0){//扭转式切药机若胜利查找,保存字表static char reserveWord[32][20] = {"auto", "break", "case","char", "const", "continue","default", "do","double", "else", "enum", "extern","float", "for", "goto","if", "int", "long","register", "return","short", "signed", "sizeof", "static","struct", "switch","typedef", "union", "unsigned", "void","volatile","while"};//界符运算符表,按照需供能够自行删减static char operatorOrDelimiter[36][10] = {"+", "-", "*", "/","<", "<=", ">", ">=", "=", "==","!=", ";", "(", ")","^", ",", "\"", "\'", "#", "&","&&", "|", "||","%", "~", "<<", ">>", "[", "]", "{","}", "\\", ".", "\?",":", "!"};

staticchar IDentifierTbl[1000][50] = { ""};//译本标识符表

//齐局变量,// Lexical_Analysis.cpp : 界道控造台使用法式的进心面。//#include "stdio.h"#include "stdlib.h"#include "string.h"#include "iostream"using namespace std;//词法阐收法式//尾先界道类别码


其真滚筒式洗药机
您看译本教会卧式炒药机
您晓得pyj润药机
事真上词法
听听编译
种头收本理
标签:编译原理(23)
    神兽验证马:
点击我更换验证码