编译原理

您所在的位置:网站首页 编译原理语法分析的方法 编译原理

编译原理

2023-06-09 15:06| 来源: 网络整理| 查看: 265

第1关:使用C/C++语言编写PL/0编译程序的语法分析程序 1、任务描述

基于第二章的词法分析程序,使用C/C++语言编写PL/0编译程序的语法分析程序。

2、编程要求

完成上述编程任务,将C/C++语言源程序复制粘贴到右侧代码编辑器,点击“评测”按钮,运行程序,系统会自动进行结果对比。

3、测试说明

平台会对你编写的代码进行测试:

测试输入:

const a = 10; var b, c; procedure p; if a = 'a' && ch = 'A' && ch = '0' && ch = 8) printf("(标识符长度超长,%s,行号:%d)\n", word, lineNumber); else wordList[wordIndex++] = word; word[0] = '\0'; } } // 非法字符(串) else { Connect(word, ch); printf("(非法字符(串),%s,行号:%d)\n", word, lineNumber); charIndex++; word[0] = '\0'; } } // 判断数字串是否合法,合法则保存到单词表中 void ProcessNumber(char ch) { // 数字 if (IsDigit(ch)) { Connect(word, charList[charIndex]); charIndex++; ProcessNumber(charList[charIndex]); } // 无符号整数 else if (!IsChar(ch)) { if (StrLength(word) >= 6) printf("(无符号整数越界,%s,行号:%d)\n", word, lineNumber); else wordList[wordIndex++] = word; // 保存到单词表 wordList 中 word[0] = '\0'; } // 非法字符(串) else { while (charList[charIndex] != ' ' && IsBoundary(charList[charIndex]) != 1 && charList[charIndex] != '\0') { Connect(word, charList[charIndex]); charIndex++; } printf("(非法字符(串),%s,行号:%d)\n", word, lineNumber); word[0] = '\0'; } } // 变量名声明判断 int id() { // 拷贝当前读取的单词到word变量中 int i = 0; for (i = 0; i < wordList[wordIndex].length(); i++) word[i] = wordList[wordIndex][i]; word[i] = '\0'; // 判断该单词不是保留字且不为空 if (!IsReservedWord(word) && word[0] != '.') { wordIndex++; } else PrintError(1); } // 子程序 void subroutine() { // 当前单词表匹配 const —— 常量定义 if (!wordList[wordIndex].compare("const")) { wordIndex++; handleConstant(); // 匹配 "," 说明存在多个变量定义 while (!wordList[wordIndex].compare(",")) { wordIndex++; handleConstant(); // 处理判断下一个常量 } // 匹配 ";" 说明变量声明结束 if (!wordList[wordIndex].compare(";")) { wordIndex++; // 匹配换行符,进入下一行的识别 if (!wordList[wordIndex].compare("\n")) { wordIndex++; lineNumber++; // 函数加一 annotation(); } subroutine(); } // 否则出错 else { PrintError(0); if (!wordList[wordIndex].compare("\n")) { wordIndex++; lineNumber++; annotation(); } subroutine(); } if (!wordList[wordIndex].compare("\n")) { wordIndex++; lineNumber++; annotation(); } } // 当前单词表匹配 var else if (!wordList[wordIndex].compare("var")) { wordIndex++; handerVariable(); // 读下一个单词,变量 语法判断 // 匹配 "," 说明有多个变量 while (!wordList[wordIndex].compare(",")) { wordIndex++; handerVariable(); } // 匹配 ";" 说明变量声明结束 if (!wordList[wordIndex].compare(";")) { wordIndex++; while (!wordList[wordIndex].compare("\n")) { wordIndex++; lineNumber++; } annotation(); subroutine(); } // 否则出错 else { PrintError(0); if (!wordList[wordIndex].compare("\n")) { wordIndex++; lineNumber++; annotation(); } subroutine(); } } // 当前单词表匹配 procedure else if (!wordList[wordIndex].compare("procedure")) { wordIndex++; id(); // 匹配 ";" 说明变量声明结束 if (!wordList[wordIndex].compare(";")) { wordIndex++; subroutine(); if (!wordList[wordIndex].compare(";")) { annotation(); while (!wordList[wordIndex].compare("\n")) { wordIndex++; lineNumber++; annotation(); } wordIndex++; } else { PrintError(1); } } // 否则出错 else { PrintError(0); subroutine(); } while (!wordList[wordIndex].compare("procedure")) { wordIndex++; id(); if (!wordList[wordIndex].compare(";")) { wordIndex++; subroutine(); if (!wordList[wordIndex].compare(";")) wordIndex++; else PrintError(1); } else { PrintError(0); subroutine(); } } } // 当前单词表匹配 换行符 if (!wordList[wordIndex].compare("\n")) { wordIndex++; lineNumber++; } sentence(); // 迭代判断 } // 常量定义 void handleConstant() { // 标识符处理判断 identifier(); // 匹配等号 = if (!wordList[wordIndex].compare("=")) { wordIndex++; number(); // 读入下一个数字进行判断 } // 打印错误 else PrintError(0); } // 变量定义 void handerVariable() { id(); } // 语句分析 void sentence() { // 匹配 if 语句 if (!wordList[wordIndex].compare("if")) { wordIndex++; condition(); // 读下一个单词,条件 语法判断 // 条件判断后,匹配 then 语句 if (!wordList[wordIndex].compare("then")) { wordIndex++; // 如果换行,行数加一 if (!wordList[wordIndex].compare("\n")) { wordIndex++; lineNumber++; } sentence(); // 读下一个单词,语句 语法判断 } // 不匹配,报错 else { PrintError(0); sentence(); } if (!wordList[wordIndex].compare("\n")) { wordIndex++; lineNumber++; } } // 匹配 while 语句 else if (!wordList[wordIndex].compare("while")) { wordIndex++; condition(); // 读下一个单词,条件 语法判断 // 条件判断后,匹配 do 语句 if (!wordList[wordIndex].compare("do")) { wordIndex++; if (!wordList[wordIndex].compare("\n")) { wordIndex++; lineNumber++; } sentence(); // 读下一个单词,语句 语法判断 } // 不匹配,报错 else { PrintError(1); if (!wordList[wordIndex].compare("\n")) { wordIndex++; lineNumber++; } sentence(); } } // 匹配 call 语句 else if (!wordList[wordIndex].compare("call")) { wordIndex++; id(); } // 匹配 read 语句 else if (!wordList[wordIndex].compare("read")) { wordIndex++; if (!wordList[wordIndex].compare("(")) { wordIndex++; id(); // 匹配 "," 说明有多个变量 while (!wordList[wordIndex].compare(",")) { wordIndex++; id(); } // "(" 匹配 ")" ,结束判断 if (!wordList[wordIndex].compare(")")) wordIndex++; else PrintError(1); } else if (!wordList[wordIndex].compare("\n")) { wordIndex++; lineNumber++; } else PrintError(0); } // 匹配 write 语句 else if (!wordList[wordIndex].compare("write")) { wordIndex++; if (!wordList[wordIndex].compare("(")) { wordIndex++; expression(); // 读下一个单词 表达式语法判断 // 匹配 "," 说明有多个表达式 while (!wordList[wordIndex].compare(",")) { wordIndex++; expression(); } // "(" 匹配 ")" ,结束判断 if (!wordList[wordIndex].compare(")")) wordIndex++; else if (!wordList[wordIndex].compare("\n")) { wordIndex++; lineNumber++; } else PrintError(0); } else { PrintError(1); expression(); while (!wordList[wordIndex].compare(",")) { wordIndex++; expression(); } if (!wordList[wordIndex].compare(")")) wordIndex++; else if (!wordList[wordIndex].compare("\n")) { wordIndex++; lineNumber++; } else PrintError(0); } } // 匹配 begin 语句 else if (!wordList[wordIndex].compare("begin")) { wordIndex++; if (!wordList[wordIndex].compare("\n")) { wordIndex++; lineNumber++; } sentence(); // 读下一个单词 语句语法判断 if (!wordList[wordIndex].compare(";")) { wordIndex++; annotation(); if (!wordList[wordIndex].compare("\n")) { wordIndex++; lineNumber++; } sentence(); } else { PrintError(0); sentence(); } while (!wordList[wordIndex].compare(";")) { wordIndex++; annotation(); if (!wordList[wordIndex].compare("\n")) { wordIndex++; lineNumber++; } sentence(); } // 匹配 end 语句,说明语句判断结束 if (!wordList[wordIndex].compare("end")) { wordIndex++; if (!wordList[wordIndex].compare("\n")) { wordIndex++; lineNumber++; } } else { PrintError(0); return; } if (!wordList[wordIndex].compare("\n")) { wordIndex++; lineNumber++; } } // 匹配 换行符 else if (!wordList[wordIndex].compare("\n")) { wordIndex++; lineNumber++; } // 匹配 注解 else if (!wordList[wordIndex].compare("//") || !wordList[wordIndex].compare("/*")) { annotation(); subroutine(); } else { int tt = 0; for (tt = 0; tt < wordList[wordIndex].length(); tt++) word[tt] = wordList[wordIndex][tt]; word[tt] = '\0'; if (!IsReservedWord(word) && word[0] != '.') { id(); if (!wordList[wordIndex].compare(":=")) wordIndex++; expression(); } } } // 注释 void annotation() { // 匹配 "\n" 行数加一 if (!wordList[wordIndex].compare("\n")) { wordIndex++; lineNumber++; } // 匹配 "//" 为单行注解 if (!wordList[wordIndex].compare("//")) { wordIndex++; wordIndex++; while (!wordList[wordIndex].compare("\n")) { wordIndex++; lineNumber++; } annotation(); } // 匹配 "/*" 为多行注解 else if (!wordList[wordIndex].compare("/*")) { wordIndex++; while (wordList[wordIndex].compare("*/")) { if (!wordList[wordIndex].compare("\n")) { wordIndex++; lineNumber++; } else { wordIndex++; } } wordIndex++; } // 一直换行则行数一直加1 while (!wordList[wordIndex].compare("\n")) { wordIndex++; lineNumber++; } } // 条件分析 void condition() { expression(); // 先进行 表达式 判断 if (!wordList[wordIndex].compare("=") || !wordList[wordIndex].compare("#") || !wordList[wordIndex].compare("=")) { wordIndex++; expression(); } else if (!wordList[wordIndex].compare("odd")) { wordIndex++; expression(); } else PrintError(0); } // 表达式 void expression() { term(); // 先进行 项 判断 if (!wordList[wordIndex].compare("+") || !wordList[wordIndex].compare("-")) { wordIndex++; term(); while (!wordList[wordIndex].compare("+") || !wordList[wordIndex].compare("-")) { wordIndex++; term(); } } } // 项 void term() { factor();// 先进行 因子 判断 while (!wordList[wordIndex].compare("*") || !wordList[wordIndex].compare("/")) { wordIndex++; factor(); } } // 因子 void factor() { if (!wordList[wordIndex].compare("(")) { wordIndex++; expression(); if (!wordList[wordIndex].compare(")")) { wordIndex++; } else PrintError(0); } else if (!wordList[wordIndex].compare("\n")) { wordIndex++; lineNumber++; } else wordIndex++; } // 数字 void number() { wordIndex++; } // 标识符 void identifier() { wordIndex++; } int main() { // 程序字符串输入 charList[0] = '\0'; scanf("%[^\.]", charList); // 读取输入流中的字符,直到遇到第一个句点('.')为止 charList[StrLength(charList)] = '.'; // 在程序字符串结尾添加'.'作为标识符 // 进行词法分析 LexicalAnalysis(); // 进行语法分析 charIndex = 0; wordIndex = 0; lineNumber = 1; GrammaticalAnalysis(); return 0; }



【本文地址】


今日新闻


推荐新闻


    CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3