admin 管理员组

文章数量: 887021


2023年12月19日发(作者:important battery message怎么处理)

// ine("成功"); foreach(FourFormula f in list) { ine(f); } ine("---finish---"); } } ///

/// 函数 /// private void functionParser() { Word word = xtWord(); //返回类型 if(!rnType(word)) { addError(word, "返回值错误"); } word = xtWord(); //函数名 if(!isIdentifier(word)) { addError(word, "函数名错误"); } //(参数){ word = xtWord(); if(m != _m) { addError(word, "(缺失"); tWord(); } arguments(); word = xtWord(); if(m != _m) { addError(word, ")缺失"); tWord(); } word = xtWord(); if(m != _curly_m) { addError(word, "{缺失"); tWord(); } //语句串 statementSequence(); //} word = xtWord(); if(m != _curly_m) { addError(word, "}缺失"); tWord(); } } /// /// 参数 /// private void arguments() { Word word = xtWord(); if(!(word)) { if(isIdentifier(word)) addBeforeError(word, "参数类型缺失"); else { if(rd().typeNum == m) addBeforeError(word, ",多余"); tWord(); return; }

} } word = xtWord(); if(!isIdentifier(word)) { addBeforeError(word, "参数标识符错误"); if(m == _m) { tWord(); return; } } //逗号 word = xtWord(); if(m == m) { arguments(); } else if(m == _m) { tWord(); return; } } ///

/// 判断是否是标识符 /// /// /// private bool isIdentifier(Word word) { return m == fierTypeNumber; } /// /// 语串 /// private void statementSequence() { Word word = xtWord(); while(m != _curly_m && m != eNumber) { statement(word); word = xtWord(); } tWord(); } /// /// 语句 /// /// private void statement(Word word) { if((word)) { defineStatement(); } else if(isIdentifier(word)) { assignStatement(); } else if(ndition(word)) { ifCondition(); } /*else if(oop(word)) { forLoop(); }*/ else if(eLoop(word)) { whileLoop(); } else { addError(word, "多余");

if(m != _m) { addBeforeError(word, ")缺失"); tWord(); } }

} else if(m == _m) { boolExpressStatement(falseList,trueList); } else{ addBeforeError(word, "布尔表达式错误"); tWord(); //throw new Exception();

}

} private bool isExpress() { int left = 1; int right = 0; int words = 1; Word word = xtWord(); while(essSymbol(word)) { if(m == _m) { left++; } else if(m == _m) { right++; } word = xtWord(); words++; } tWord(words); return left==right; } ///

/// 关系表达式或布尔值 /// /// /// private void boolRelation(List trueList, List falseList) { Word word = expressStatement(); Word word2 = null; Word op = xtWord(); if(areOperator(op)) { word2 = expressStatement(); if(word2 == null) { return ; } } else{ tWord(); op = new Word(); }

FourFormula f = addFourFormula(null, word, word2, op); (f); f = addFourFormula(); (f);

}

#endregion ///

/// 判断是否是数值变量类型 /// /// /// private bool isValue(Word word) { return m == TypeNumber || m == fierTypeNumber || m == peNumber || m == TypeNumber; } #region 添加四元式 List list = new List(); /// /// 保存四元式 /// private FourFormula addFourFormula(Word result, Word arg1, Word arg2, Word op) { FourFormula fourFormula = new FourFormula(); = result; 1 = arg1; 2 = arg2; e = op; w = ; (fourFormula); return fourFormula; } private FourFormula addFourFormula() { FourFormula fourFormula = new FourFormula(); w = ; (fourFormula); return fourFormula; } #endregion int idNameCount = 0; /// /// 创建临时变量 /// /// public Word createTempVar() { Word word = new Word(); = "$T" + idNameCount++; m =lTypeNumber; return word; }

#region 输出错误 bool success = true;//语法解析结果 ///

/// 输出在特定之前的错误 /// /// /// private void addBeforeError(Word word, string v) { success = false; ine("error:" + tionInfo() + "t在该字之前" + v); } /// /// 输出错误 ///

/// /// private void addError(Word word, string v) { success = false; ine("error:" + tionInfo() + "t" + v); } #endregion }}using System;using c;using ;using ;using ;namespace CLanguage { ///

/// 四元式 /// class FourFormula { public int codeRow; public Word result; public Word arg1; public Word operate; public Word arg2; public override string ToString() { string s = "(" + codeRow + ") "; //赋值 if(m>0) { s = s + + " = " + ; if(operate != null) { s +=" "+ ; } if(arg2 != null) { s += " " + ; } } else if(operate == null) {//直接跳转 s += "goto (" + + ")"; } else {//条件跳转 s = s + "if " + ; if(!=null) { s += " " + + " " + ; } s += " goto (" + + ")"; } return s; } }}using System;using c;using ;

public Word Scanner() { Word myWord;

read(); readValidChar(); //标识符 if(er(character)) { myWord=readWord(); }//数值 else if(t(character)) { myWord = readDigit(); }//字符常量 else if(character == ''') { myWord = readConstChar(); }//字符串 else if(character == '"') { myWord = readConstString(); }/*//结束符 else if(character == endChar) { = "" + endChar; m = endTypeNumber; }*///空值 else if(character == nullChar) { myWord = new Word(); = row; = col; = "Ending"; m = endTypeNumber; }//其他字符 else { myWord = readOtherChar(); } return myWord; } ///

/// 标识符 /// /// private Word readWord() { Word myWord = new Word(); = row; = col; while(er(character) || t(character)) { += character; read(); } retract(); m = eNumber(); return myWord; } /// /// 其他字符 /// /// private Word readOtherChar() { Word myWord = new Word(); = row; = col; string s = "" + character; for(int i = 0; i < 2; i++) { read(); if(character == nullChar) { break; }

} s += character; } foreach(string op in operatorSort) { if(With(op)) { retract( - ); s = op; break; } } = s; m = eNumber(); return myWord; } ///

/// 识别数字常量 /// /// private Word readDigit() { Word myWord = new Word(); = row; = col; while(t(character)) { += character; read(); } if(character == '.') { += character; read(); while(t(character)) { += character; read(); } } retract(); m = numberTypeNumber; return myWord; } /// /// 识别字符常量 /// /// private Word readConstChar() { Word myWord = new Word(); = row; = col; // = "" + character; read(); //读取直到'''结束 while(character != ''') { += character; read(); //读到空字符或结束字符 if(character == nullChar /*|| character == endChar*/|| rol(character)) { /* if(character == endChar) { +=endChar; } */ m = errorTypeNumber; return myWord; } } // += character; Match r = (, "^(([0-7]{1,3}|x[0-9a-fA-F]+|[abfnrtv'"?])|[^])$"); //转义字符模式匹配

//转义字符模式匹配 if(!s) { m = errorTypeNumber; return myWord; } m = charTypeNumber; return myWord; } ///

/// 识别常量字符串 /// /// private Word readConstString() { Word myWord = new Word(); = row; = col; // = "" + character; read(); while(character != '"') { += character; read(); //读到空字符或结束字符 if(character == nullChar || rol(character)) { // += "0"; m = errorTypeNumber; return myWord; } } // += character; //转义字符模式匹配 if(!isLegalString()) { m = errorTypeNumber; return myWord; } m = stringTypeNumber; return myWord; } /// /// 合法字符串书写 /// /// /// private bool isLegalString(string word) { int i = 0; while(i < ) { if(word[i] == '') { if(++i == ) return false; foreach(char c in translateChar) { if(c == word[i]) { goto aLabel; } } return false; } aLabel: i++; } return true; }

row--; col = input[row].Length + col; checkCol(); } } }}using System;using c;using ;using tion;using ;using ;namespace CLanguage { class KeyWords { ///

/// 关键字表 /// public static string[] keyWordTable = { "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"}; public static Word 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 KeyWords() { Type t = typeof(KeyWords); FieldInfo[] fields = lds(); Word word; foreach(FieldInfo f in fields) if((typeof(Word))) { word = new Word(); = r(); m = getTypeNumber(); ue(t, word); } } /// /// 获取关键字编码 /// /// public static int getTypeNumber(string s) { for(int i = 0; i < ; i++) { if(keyWordTable[i] == s) { return i+1; } } return fierTypeNumber; }

} public static bool isPrefType(Word w) { return m == m || m == m; } public static bool isType(Word w) { return m == m || m == m || m == m || m == m || m == m;

} public static bool isReturnType(Word w) { return isType(w) || m == m; } internal static bool isIfCondition(Word word) { return m == m; } internal static bool isForLoop(Word word) { return m == m; } internal static bool isWhileLoop(Word word) { return m == m; } internal static bool isDoLoop(Word word) { return m == m; } internal static bool isKeyWord(Word word) { return m > 0 && m <= ; }

} class Symbols { public static string[] operatorTable = { "{","}","[","]","(",")","->",".", "++","--", "!","&&","||", "~","&","|","^", "+","-","*","/","%", "<<",">>", "<",">",">=","<=", "==","!=","?",":",",",";", "=","+=","-=","*=","/=","%=", "&=","^=","|=","<<=",">>=" }; public static Word left_curly_bracket, right_curly_bracket, left_square_bracket, right_square_bracket, left_bracket, right_bracket, arrow, point, two_add, two_sub, logic_not, logic_and, logic_or, bitwise_not, bitwise_and, bitwise_or, bitwise_xor, add, sub, mul, except, remain, left_move, right_move, less, greater, greater_equal, less_equal, equal, not_equal, question_mark, colon, comma, semicolon, assign, add_assign, sub_assign, mul_assign, except_assign, remain_assign, and_assign, xor_assign, or_assign, left_move_assign, right_move_assign;

and_assign, xor_assign, or_assign, left_move_assign, right_move_assign; internal static bool isSemicolo(Word word) { return word .typeNum == m; } static Symbols() { Type t = typeof(Symbols); FieldInfo[] fields = lds(); Word word; int index = 0; foreach(FieldInfo f in fields) if((typeof(Word))) { word = new Word(); = operatorTable[index++]; m = getTypeNumber(); ue(t, word); } } public static int getTypeNumber(string s) { int start = 100; for(int i = 0; i < ; i++) { if(operatorTable[i] == s) { return start + i; } } return ne; } internal static bool isArithmeticOperator(Word w) { return m == m || m == m || m == m || m == m || m == m; } internal static bool isCompareOperator(Word w) { return m == m || m == m || m == less_m || m == greater_m || m == m || m == not_m; } internal static bool isExpressSymbol(Word w) { return m == m || m == m || m == m || m == m|| m == m

|| m == left_m || m == right_m || m == fierTypeNumber|| m == TypeNumber; } }}using System;using c;using ;using ;using ;namespace CLanguage { ///

/// 读词缓存流 /// class WordStream { WordPicker wordPicker; List words = new List(); int index = -1;


本文标签: 错误 类型 常量 字符 识别