diff --git a/1.txt b/1.txt new file mode 100644 index 0000000..4e21609 --- /dev/null +++ b/1.txt @@ -0,0 +1,2 @@ +const x=8,y=7;var a,b,c;begin a=2*x;;b=a+x+y;;c=a+3*b;end +const x=8,y=7;var a,b,c;begin a=b+x;;if a>0 then begin c=y -1;;a=a+2;end else begin c=a+y;end;while a>0 do a=a-1;end diff --git "a/CPlab2_\350\257\215\346\263\225\345\210\206\346\236\220/\350\257\215\346\263\225\345\210\206\346\236\220.cpp" b/CPlab2_LexicalAnalysis/LexicalAnalysis.cpp similarity index 75% rename from "CPlab2_\350\257\215\346\263\225\345\210\206\346\236\220/\350\257\215\346\263\225\345\210\206\346\236\220.cpp" rename to CPlab2_LexicalAnalysis/LexicalAnalysis.cpp index f4360e7..c8d9bb4 100644 --- "a/CPlab2_\350\257\215\346\263\225\345\210\206\346\236\220/\350\257\215\346\263\225\345\210\206\346\236\220.cpp" +++ b/CPlab2_LexicalAnalysis/LexicalAnalysis.cpp @@ -7,13 +7,13 @@ using namespace std; #define ID 15 #define NUM 16 -//存放处理后的字符串 + char tempstr[255] = {}; -//空格标志 + bool temp = false; -//临时数组 + char word[255] = {}; -//keyword关键字 + string keyword[9] = { "Const", "Var", "if", "else", "then", "while", "do", "begin", "end" @@ -23,20 +23,21 @@ int keyword_num[9] = { 1,2,3,4,5, 6,7,8,9 }; -//部分运算符,定界符等 -char symbol[9] = { '+','-','*','/',';','(',')','{','}'}; -//对应的种码值 + +char symbol[9] = { '+', '-', '*', '/', ';', '(' ,')' ,'{' ,'}'}; + int symbol_num[9] = { 21,22,23,24,25,26,27,28,29 }; -//判断是否为字母 +bool err_flag = false; + bool IsLetter(char ch) { - if ((ch >= 'a'&&ch <= 'z') || (ch >= 'A'&&ch <= 'Z')) + if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) return true; return false; } -//判断是否为数字 + bool IsDigit(char ch) { if (ch >= '0'&&ch <= '9') @@ -44,7 +45,7 @@ bool IsDigit(char ch) return false; } -//判断是否为定界符等 + int IsSymbol(char ch) { for (int i = 0; i<9; i++) @@ -55,7 +56,7 @@ int IsSymbol(char ch) return -1; } -//判断是否为关键字 + int IsKeyword(string str) { for (int i = 0; i<26; i++) @@ -65,15 +66,15 @@ int IsKeyword(string str) return i; } } - //不是关键字即为ID + return ID; } -//空格处理 + void HandleSpace(char a[]) { int j = 0; - memset(word, 0, 255);//需要清空,不然可能残留上次的字符串 + memset(word, 0, 255); temp = false; for (int i = 0; i < strlen(a); i++) { @@ -94,7 +95,7 @@ void HandleSpace(char a[]) } } -//处理"//"注释 + void prePro() { int j = 0; @@ -119,27 +120,27 @@ void Scanner(char *str); int main() { - char instr[255] = {}; //接收输入字符串 - bool flag = false; //多行注释标志,false为未处于注释区域 - string Token;//存放字符串 - char *str = NULL;//存放每行的字符串 - char delims[] = " ";//分割标志 + char instr[255] = {}; + bool flag = false; + string Token; + char *str = NULL; + char delims[] = " "; freopen("test.cpp", "r", stdin); freopen("result.txt", "w", stdout); printf("词法分析结果如下:\n"); printf("******************************\n"); - while ((gets_s(instr)) != NULL) + while ((gets(instr)) != NULL) { HandleSpace(instr); prePro(); - str = strtok(tempstr, delims);//分割字符串 + str = strtok(tempstr, delims); while (str != NULL) { - //头文件,宏定义 + if (*(str) == '#') { printf("#\n"); @@ -147,17 +148,20 @@ int main() } Scanner(str); - + if (err_flag) { + break; + } str = strtok(NULL, delims); } + if(err_flag) break; } return 0; } void Scanner(char *str) { - bool flag = false; //多行注释标志,false为未处于注释区域 - string Token;//存放字符串 + bool flag = false; + string Token; int type = 0; for (int i = 0; i", Token.c_str()); + Token = ""; + err_flag = true; + break; + } i++; } + if (err_flag) break; type = NUM; printf("< %s , %d >\n", Token.c_str(), type); Token = ""; + } //<,<=,<> diff --git a/CPlab2_LexicalAnalysis/LexicalAnalysis.py b/CPlab2_LexicalAnalysis/LexicalAnalysis.py new file mode 100644 index 0000000..bd239b5 --- /dev/null +++ b/CPlab2_LexicalAnalysis/LexicalAnalysis.py @@ -0,0 +1,94 @@ +def scan(string): + KEY_WORDS = ["Const", "Var", "if", "else", "then","while", "do", "begin", "end"] + SYMBOLS = [',', '+', '-', '*', '/', ';', '(' ,')' ,'{' ,'}', '==', '<=', '>=', '<>', '<', '>', '='] + word_map = {key: i+1 for i, key in enumerate(KEY_WORDS)} + word_map.update({key: i+21 for i, key in enumerate(SYMBOLS)}) + def process(string): + word = '' + if ' ' in string: + for s in string.split(' '): + word += s + else: + word = string + if '//' in word: + return word.split('//')[0] + return word + + def have_keywords(word): + for keyword in KEY_WORDS: + if keyword in word: + return keyword + return None + + def have_symbol(word): + for sy in SYMBOLS: + if sy in word: + return sy + return None + + def is_alpha(ch): + return (ch >= 'a' and ch <= 'z') or (ch >= 'A' and ch <= 'Z') + + def is_digit(ch): + return ch >= '0' and ch <= '9' + + # def is_in_symbols(c): + # return c in SYMBOLS + + # def is_keywords(c): + # return c in KEY_WORDS + + + queue = [process(string)] + if '/*' in queue[0] or '*/' in queue[0]: + return + stop = False + while not stop: + changed = False + tempQueue = [] + for item in queue: + key = have_keywords(item) + sy = have_symbol(item) + if key and key != item: + changed = True + item1, item2 = item.split(key) + if item1: + tempQueue.append(item1) + tempQueue.append(key) + if item2: + tempQueue.append(item2) + continue + elif sy and sy != item: + changed = True + sp = item.split(sy) + for i, it in enumerate(sp): + if it: + tempQueue.append(it) + if i != len(sp) - 1: + tempQueue.append(sy) + + else: + tempQueue.append(item) + + if item == queue[-1] and not changed: + stop = True + queue = tempQueue.copy() + for item in queue: + if item in word_map: + print("<", item, ",", word_map[item],">") + else: + if item and is_alpha(item[0]): + print("<", item, ",", 15,">") + if item and is_digit(item[0]): + print("<", item, ",", 16,">") + +def main(): + with open('./test.cpp', 'r') as r_stream: + for line in r_stream: + if line: + if '\n' in line: + scan(line[0:-1]) + else: + scan(line) + +main() \ No newline at end of file diff --git a/CPlab2_LexicalAnalysis/a.out b/CPlab2_LexicalAnalysis/a.out new file mode 100755 index 0000000..98526f8 Binary files /dev/null and b/CPlab2_LexicalAnalysis/a.out differ diff --git a/CPlab2_LexicalAnalysis/result.txt b/CPlab2_LexicalAnalysis/result.txt new file mode 100644 index 0000000..d183536 --- /dev/null +++ b/CPlab2_LexicalAnalysis/result.txt @@ -0,0 +1,26 @@ +词法分析结果如下: +****************************** +# +< int , 15 > +< main , 15 > +< ( , 26 > +< ) , 27 > +< { , 28 > +< Const , 1 > +< const_a , 15 > +< = , 35 > +< 10 , 16 > +< ; , 25 > +< Var , 2 > +< var_b , 15 > +< = , 35 > +< 15 , 16 > +< ; , 25 > +< if , 3 > +< ( , 26 > +< const_a , 15 > +< > , 33 > +< 5 , 16 > +< ) , 27 > +< { , 28 > + \ No newline at end of file diff --git a/CPlab2_LexicalAnalysis/test.cpp b/CPlab2_LexicalAnalysis/test.cpp new file mode 100644 index 0000000..bde4f5e --- /dev/null +++ b/CPlab2_LexicalAnalysis/test.cpp @@ -0,0 +1,19 @@ +Const x=8,y=7; +Var a,b,c; +begin + a=b+x; + + if a>0 + then + begin + c=y -; + a=a+2; + end + else + begi + c=a+y; + end + + while a>0 + do a=a-1; +end \ No newline at end of file diff --git a/CPlab2_LexicalAnalysis/test.py b/CPlab2_LexicalAnalysis/test.py new file mode 100644 index 0000000..b79b3da --- /dev/null +++ b/CPlab2_LexicalAnalysis/test.py @@ -0,0 +1,3 @@ +a = 'adsd;' +if 'adsd;' in a: + print('vfs') \ No newline at end of file diff --git "a/CPlab2_\350\257\215\346\263\225\345\210\206\346\236\220/result.txt" "b/CPlab2_\350\257\215\346\263\225\345\210\206\346\236\220/result.txt" deleted file mode 100644 index 3d3d1ab..0000000 --- "a/CPlab2_\350\257\215\346\263\225\345\210\206\346\236\220/result.txt" +++ /dev/null @@ -1,66 +0,0 @@ -词法分析结果如下: -****************************** -# -< int , 15 > -< main , 15 > -< ( , 26 > -< ) , 27 > -< { , 28 > -< Const , 1 > -< const_a , 15 > -< = , 35 > -< 10 , 16 > -< ; , 25 > -< Var , 2 > -< var_b , 15 > -< = , 35 > -< 15 , 16 > -< ; , 25 > -< if , 3 > -< ( , 26 > -< const_a , 15 > -< > , 33 > -< 5 , 16 > -< ) , 27 > -< { , 28 > -< var_b , 15 > -< = , 35 > -< var_b , 15 > -< + , 21 > -< 1 , 16 > -< ; , 25 > -< } , 29 > -< else , 4 > -< var_b , 15 > -< = , 35 > -< var_b , 15 > -< - , 22 > -< 4 , 16 > -< ; , 25 > -< do , 7 > -< { , 28 > -< test , 15 > -< * , 23 > -< / , 24 > -< } , 29 > -< while , 6 > -< ( , 26 > -< var_b , 15 > -< > , 33 > -< 5 , 16 > -< ) , 27 > -< ; , 25 > -< a , 15 > -< b , 15 > -< c , 15 > -< = , 35 > -< 0 , 16 > -< ; , 25 > -< abc , 15 > -< = , 35 > -< 0 , 16 > -< ; , 25 > -< return , 15 > -< 0 , 16 > -< ; , 25 > -< } , 29 > diff --git "a/CPlab2_\350\257\215\346\263\225\345\210\206\346\236\220/test.cpp" "b/CPlab2_\350\257\215\346\263\225\345\210\206\346\236\220/test.cpp" deleted file mode 100644 index 0f721da..0000000 --- "a/CPlab2_\350\257\215\346\263\225\345\210\206\346\236\220/test.cpp" +++ /dev/null @@ -1,22 +0,0 @@ -#include -int main() -{ - //test - Const const_a = 10; - Var var_b = 15; - - if (const_a > 5){ - var_b = var_b + 1; - } - else - var_b = var_b - 4; - - do { - /* test */ - } while(var_b > 5); - - a, b, c = 0; - abc = 0; - - return 0; -} \ No newline at end of file diff --git "a/CPlab3_\351\200\222\345\275\222\344\270\213\351\231\215\350\257\255\346\263\225\345\210\206\346\236\220/\351\200\222\345\275\222\344\270\213\351\231\215\350\257\255\346\263\225\345\210\206\346\236\220.cpp" b/CPlab3_RecursiveDescent/RecursiveDescent.cpp similarity index 93% rename from "CPlab3_\351\200\222\345\275\222\344\270\213\351\231\215\350\257\255\346\263\225\345\210\206\346\236\220/\351\200\222\345\275\222\344\270\213\351\231\215\350\257\255\346\263\225\345\210\206\346\236\220.cpp" rename to CPlab3_RecursiveDescent/RecursiveDescent.cpp index d45b311..0073a4f 100644 --- "a/CPlab3_\351\200\222\345\275\222\344\270\213\351\231\215\350\257\255\346\263\225\345\210\206\346\236\220/\351\200\222\345\275\222\344\270\213\351\231\215\350\257\255\346\263\225\345\210\206\346\236\220.cpp" +++ b/CPlab3_RecursiveDescent/RecursiveDescent.cpp @@ -72,7 +72,7 @@ int main() printf("词法分析结果如下:\n"); printf("******************************\n"); - while ((gets_s(instr)) != NULL) + while ((gets(instr)) != NULL) { HandleSpace(instr); prePro(); @@ -692,4 +692,4 @@ void prePro() tempstr[j++] = word[i]; } } -} \ No newline at end of file +} diff --git a/CPlab3_RecursiveDescent/RecursiveDescent.py b/CPlab3_RecursiveDescent/RecursiveDescent.py new file mode 100644 index 0000000..1287577 --- /dev/null +++ b/CPlab3_RecursiveDescent/RecursiveDescent.py @@ -0,0 +1,91 @@ +import re +import collections + +# Tokens +NUM = r'(?P\d+)' +PLUS = r'(?P\+)' +MINUS = r'(?P-)' +TIMES = r'(?P\*)' +DIVIDE = r'(?P/)' +LPAREN = r'(?P\()' +RPAREN = r'(?P\))' +WS = r'(?P\s+)' + +patern = re.compile('|'.join([NUM, PLUS, MINUS, TIMES, DIVIDE, LPAREN, RPAREN, WS])) + +Token = collections.namedtuple('Token', ['type', 'value']) + +def generate_tokens(text): + scanner = patern.scanner(text) + for m in iter(scanner.match, None): + tok = Token(m.lastgroup, m.group()) + if tok.type != 'WS': + yield tok + +class ExpressionParser: + + def parse(self, text): + self.tokens = generate_tokens(text) + self.tok = None + self.nexttok = None + self._advance() + return self.expr() + + def _advance(self): + self.tok, self.nexttok = self.nexttok, next(self.tokens, None) + + def _accept(self, toktype): + if self.nexttok and self.nexttok.type == toktype: + self._advance() + return True + else: + return False + + def _expect(self, toktype): + if not self._accept(toktype): + raise SyntaxError('Expected ' + toktype) + + # Grammer rules + # expression ::= term { ('+'|'-') term }* + # term ::= factor { '*'|'/' factor }* + # factor ::= NUM | (expr) + def expr(self): + "expression ::= term { ('+'|'-') term }*" + exprval = self.term() + while self._accept('PLUS') or self._accept('MINUX'): + op = self.tok.type + right = self.term() + if op == 'PLUS': + exprval += right + elif op == 'MINUS': + exprval -= right + return exprval + + def term(self): + "term ::= factor { '*'|'/' factor }*" + termval = self.factor() + while self._accept('TIMES') or self._accept('DIVIDE'): + op = self.tok.type + right = self.factor() + if op == 'TIMES': + termval *= right + elif op == 'DIVIDE': + termval /= right + return termval + + def factor(self): + "factor ::= NUM | (expr)" + if self._accept('NUM'): + return int(self.tok.value) + elif self._accept('LPAREN'): + exprval = self.expr() + self._expect('RPAREN') + return exprval + else: + raise SyntaxError('Expected NUMBER oe LPAREN') + + + +if __name__ == '__main__': + e = ExpressionParser() + print(e.parse('(3 + 4) * 5 + 6')) \ No newline at end of file diff --git a/CPlab3_RecursiveDescent/a.out b/CPlab3_RecursiveDescent/a.out new file mode 100755 index 0000000..3fa1baa Binary files /dev/null and b/CPlab3_RecursiveDescent/a.out differ diff --git "a/CPlab3_\351\200\222\345\275\222\344\270\213\351\231\215\350\257\255\346\263\225\345\210\206\346\236\220/result.txt" b/CPlab3_RecursiveDescent/result.txt similarity index 94% rename from "CPlab3_\351\200\222\345\275\222\344\270\213\351\231\215\350\257\255\346\263\225\345\210\206\346\236\220/result.txt" rename to CPlab3_RecursiveDescent/result.txt index 2464a51..704af5a 100644 --- "a/CPlab3_\351\200\222\345\275\222\344\270\213\351\231\215\350\257\255\346\263\225\345\210\206\346\236\220/result.txt" +++ b/CPlab3_RecursiveDescent/result.txt @@ -1,78 +1,78 @@ -词法分析结果如下: -****************************** - 1: < Const , 1 > - 2: < x , 15 > - 3: < = , 35 > - 4: < 8 , 16 > - 5: < , , 40 > - 6: < y , 15 > - 7: < = , 35 > - 8: < 7 , 16 > - 9: < ; , 25 > -10: < Var , 2 > -11: < a , 15 > -12: < , , 40 > -13: < b , 15 > -14: < , , 40 > -15: < c , 15 > -16: < ; , 25 > -17: < begin , 8 > -18: < a , 15 > -19: < = , 35 > -20: < b , 15 > -21: < + , 21 > -22: < x , 15 > -23: < ; , 25 > -24: < if , 3 > -25: < a , 15 > -26: < > , 33 > -27: < 0 , 16 > -28: < then , 5 > -29: < begin , 8 > -30: < c , 15 > -31: < = , 35 > -32: < y , 15 > -33: < - , 22 > -34: < 1 , 16 > -35: < a , 15 > -36: < = , 35 > -37: < a , 15 > -38: < + , 21 > -39: < 2 , 16 > -40: < ; , 25 > -41: < end , 9 > -42: < else , 4 > -43: < begin , 8 > -44: < c , 15 > -45: < = , 35 > -46: < a , 15 > -47: < + , 21 > -48: < y , 15 > -49: < ; , 25 > -50: < end , 9 > -51: < while , 6 > -52: < a , 15 > -53: < > , 33 > -54: < 0 , 16 > -55: < do , 7 > -56: < a , 15 > -57: < = , 35 > -58: < a , 15 > -59: < - , 22 > -60: < 1 , 16 > -61: < ; , 25 > -62: < end , 9 > - - -语法分析结果如下: -******************************** -Const define success! -Const define success! -Const description success! -Var define success! -Var define success! -Var define success! -Var description success! -******************************** -Parse Success! Congratulations! -******************************** +词法分析结果如下: +****************************** + 1: < Const , 1 > + 2: < x , 15 > + 3: < = , 35 > + 4: < 8 , 16 > + 5: < , , 40 > + 6: < y , 15 > + 7: < = , 35 > + 8: < 7 , 16 > + 9: < ; , 25 > +10: < Var , 2 > +11: < a , 15 > +12: < , , 40 > +13: < b , 15 > +14: < , , 40 > +15: < c , 15 > +16: < ; , 25 > +17: < begin , 8 > +18: < a , 15 > +19: < = , 35 > +20: < b , 15 > +21: < + , 21 > +22: < x , 15 > +23: < ; , 25 > +24: < if , 3 > +25: < a , 15 > +26: < > , 33 > +27: < 0 , 16 > +28: < then , 5 > +29: < begin , 8 > +30: < c , 15 > +31: < = , 35 > +32: < y , 15 > +33: < - , 22 > +34: < 1 , 16 > +35: < a , 15 > +36: < = , 35 > +37: < a , 15 > +38: < + , 21 > +39: < 2 , 16 > +40: < ; , 25 > +41: < end , 9 > +42: < else , 4 > +43: < begin , 8 > +44: < c , 15 > +45: < = , 35 > +46: < a , 15 > +47: < + , 21 > +48: < y , 15 > +49: < ; , 25 > +50: < end , 9 > +51: < while , 6 > +52: < a , 15 > +53: < > , 33 > +54: < 0 , 16 > +55: < do , 7 > +56: < a , 15 > +57: < = , 35 > +58: < a , 15 > +59: < - , 22 > +60: < 1 , 16 > +61: < ; , 25 > +62: < end , 9 > + + +语法分析结果如下: +******************************** +Const define success! +Const define success! +Const description success! +Var define success! +Var define success! +Var define success! +Var description success! +******************************** +Parse Success! Congratulations! +******************************** diff --git "a/CPlab3_\351\200\222\345\275\222\344\270\213\351\231\215\350\257\255\346\263\225\345\210\206\346\236\220/test.cpp" b/CPlab3_RecursiveDescent/test.cpp similarity index 100% rename from "CPlab3_\351\200\222\345\275\222\344\270\213\351\231\215\350\257\255\346\263\225\345\210\206\346\236\220/test.cpp" rename to CPlab3_RecursiveDescent/test.cpp diff --git "a/CPlab3_\351\200\222\345\275\222\344\270\213\351\231\215\350\257\255\346\263\225\345\210\206\346\236\220_2/\350\257\255\346\263\225\345\210\206\346\236\220.cpp" b/CPlab3_RecursiveDescent_2/SyntaxAnalysis.cpp similarity index 100% rename from "CPlab3_\351\200\222\345\275\222\344\270\213\351\231\215\350\257\255\346\263\225\345\210\206\346\236\220_2/\350\257\255\346\263\225\345\210\206\346\236\220.cpp" rename to CPlab3_RecursiveDescent_2/SyntaxAnalysis.cpp diff --git "a/CPlab3_\351\200\222\345\275\222\344\270\213\351\231\215\350\257\255\346\263\225\345\210\206\346\236\220_2/result.txt" b/CPlab3_RecursiveDescent_2/result.txt similarity index 100% rename from "CPlab3_\351\200\222\345\275\222\344\270\213\351\231\215\350\257\255\346\263\225\345\210\206\346\236\220_2/result.txt" rename to CPlab3_RecursiveDescent_2/result.txt diff --git "a/CPlab6_\344\270\255\351\227\264\344\273\243\347\240\201\347\224\237\346\210\220/\344\270\255\351\227\264\344\273\243\347\240\201\347\224\237\346\210\220.cpp" b/CPlab6_IntermediateCodeGen/IntermediateCodeGen.cpp similarity index 100% rename from "CPlab6_\344\270\255\351\227\264\344\273\243\347\240\201\347\224\237\346\210\220/\344\270\255\351\227\264\344\273\243\347\240\201\347\224\237\346\210\220.cpp" rename to CPlab6_IntermediateCodeGen/IntermediateCodeGen.cpp diff --git "a/CPlab6_\344\270\255\351\227\264\344\273\243\347\240\201\347\224\237\346\210\220/result.txt" b/CPlab6_IntermediateCodeGen/result.txt similarity index 100% rename from "CPlab6_\344\270\255\351\227\264\344\273\243\347\240\201\347\224\237\346\210\220/result.txt" rename to CPlab6_IntermediateCodeGen/result.txt diff --git a/ConsoleApplication1.cpp b/ConsoleApplication1.cpp new file mode 100644 index 0000000..5be67ec Binary files /dev/null and b/ConsoleApplication1.cpp differ