diff --git a/LR1Generator.cpp b/LR1Generator.cpp index efa5436..603eb80 100644 --- a/LR1Generator.cpp +++ b/LR1Generator.cpp @@ -18,39 +18,43 @@ using std::endl; int main() { - clock_t start,end;//定义clock_t变量 - start = clock(); //开始时间 + try { + clock_t start, end;//定义clock_t变量 + start = clock(); //开始时间 - const GrammarResourcePool *pool; + const GrammarResourcePool *pool; - const AnalyseTableGenerator *atg; + const AnalyseTableGenerator *atg; - LR1Generator generator; + LR1Generator generator; - generator.getProductions(); + generator.getProductions(); - generator.run(); + generator.run(); - generator.output(pool, atg); + generator.output(pool, atg); - //输出时间 - end = clock(); //结束时间 - double times = double(end-start)/CLOCKS_PER_SEC; - wcout<<"LR1Generator Run time = "<< times <<"s MicroSeconds" << " = " << times * 1000 <<"ms" << endl; + //输出时间 + end = clock(); //结束时间 + double times = double(end - start) / CLOCKS_PER_SEC; + wcout << "LR1Generator Run time = " << times << "s MicroSeconds" << " = " << times * 1000 << "ms" << endl; - start = clock(); //开始时间 + start = clock(); //开始时间 - SyntaxParser syntaxParser(pool, atg); + SyntaxParser syntaxParser(pool, atg); - syntaxParser.getToken(); + syntaxParser.getToken(); - syntaxParser.parse(); + syntaxParser.parse(); - //输出时间 - end = clock(); //结束时间 - times = double(end-start)/CLOCKS_PER_SEC; - wcout<<"SyntaxParser Run time = "< "struct" "ID" "{" member_list "}" more_struct_type +@struct_type -> "STRUCT" "ID" "OPENING_BRACE" member_list "CLOSING_BRACE" more_struct_type more_struct_type -> "EOF" more_struct_type -> 蔚 -member_list -> type_spec declarators ";" member_list +member_list -> type_spec declarators "SEMICOLON" member_list member_list -> 蔚 type_spec -> base_type_spec type_spec -> @struct_type base_type_spec -> floating_pt_type base_type_spec -> integer_type -base_type_spec -> "char" -base_type_spec -> "boolean" -floating_pt_type -> "float" -floating_pt_type -> "double" -floating_pt_type -> "long" "double" +base_type_spec -> "CHAR" +base_type_spec -> "BOOLEAN" +floating_pt_type -> "FLOAT" +floating_pt_type -> "DOUBLE" +floating_pt_type -> "LONG" "DOUBLE" integer_type -> signed_int integer_type -> unsigned_int -signed_int -> "short" -signed_int -> "int16" -signed_int -> "long" -signed_int -> "int32" -signed_int -> "long" "long" -signed_int -> "int64" -signed_int -> "int8" +signed_int -> "SHORT" +signed_int -> "INT16" +signed_int -> "LONG" +signed_int -> "INT32" +signed_int -> "LONG" "LONG" +signed_int -> "INT64" +signed_int -> "INT8" -unsigned_int -> "unsigned" "short" -unsigned_int -> "unsigned" "long" -unsigned_int -> "unsigned" "long" "long" -unsigned_int -> "uint16" -unsigned_int -> "uint32" -unsigned_int -> "uint64" -unsigned_int -> "uint8" +unsigned_int -> "UNSIGNED" "SHORT" +unsigned_int -> "UNSIGNED" "LONG" +unsigned_int -> "UNSIGNED" "LONG" "LONG" +unsigned_int -> "UINT16" +unsigned_int -> "UINT32" +unsigned_int -> "UINT64" +unsigned_int -> "UINT8" declarators -> declarator more_declarators -more_declarators -> "," declarator more_declarators +more_declarators -> "COMMA" declarator more_declarators more_declarators -> 蔚 declarator -> "ID" more_declarator more_declarator -> exp_list more_declarator -> 蔚 -exp_list -> "[" or_expr more_or_expr "]" -more_or_expr -> "," or_expr more_or_expr +exp_list -> "LEFT_BRACKET" or_expr more_or_expr "RIGHT_BRACKET" +more_or_expr -> "COMMA" or_expr more_or_expr more_or_expr -> 蔚 or_expr -> xor_expr more_xor_expr -more_xor_expr -> "|" xor_expr more_xor_expr +more_xor_expr -> "DELIMITER" xor_expr more_xor_expr more_xor_expr -> 蔚 xor_expr -> and_expr more_and_expr -more_and_expr -> "^" and_expr more_and_expr +more_and_expr -> "INSERT" and_expr more_and_expr more_and_expr -> 蔚 and_expr -> shift_expr more_shift_expr -more_shift_expr -> "&" shift_expr more_shift_expr +more_shift_expr -> "AND" shift_expr more_shift_expr more_shift_expr -> 蔚 shift_expr -> add_expr more_add_expr more_add_expr -> shift_sign add_expr more_add_expr -shift_sign -> ">>" -shift_sign -> "<<" +shift_sign -> "RIGHT_SHIFT" +shift_sign -> "LEFT_SHIFT" more_add_expr -> 蔚 add_expr -> multi_expr more_multi_expr more_multi_expr -> multi_sign multi_expr more_multi_expr -multi_sign -> "+" -multi_sign -> "-" +multi_sign -> "PLUS" +multi_sign -> "SUB" more_multi_expr -> 蔚 multi_expr -> unary_expr more_unary_expr more_unary_expr -> unary_sign unary_expr more_unary_expr -unary_sign -> "*" -unary_sign -> "/" -unary_sign -> "%" +unary_sign -> "MULT" +unary_sign -> "SLASH" +unary_sign -> "PERCENT" more_unary_expr -> 蔚 unary_expr -> unary_sign_2 unary_declare -unary_sign_2 -> "-" -unary_sign_2 -> "+" -unary_sign_2 -> "~" +unary_sign_2 -> "SUB" +unary_sign_2 -> "PLUS" +unary_sign_2 -> "TILDE" unary_sign_2 -> 蔚 unary_declare -> "INTEGER" unary_declare -> "STRING" -unary_declare -> BOOLEAN +unary_declare -> BOOLEAN_VALUE -BOOLEAN -> "TRUE" -BOOLEAN -> "FALSE" \ No newline at end of file +BOOLEAN_VALUE -> "TRUE" +BOOLEAN_VALUE -> "FALSE" \ No newline at end of file diff --git a/src/GrammarResourcePool.cpp b/src/GrammarResourcePool.cpp index 61b3398..237eff3 100644 --- a/src/GrammarResourcePool.cpp +++ b/src/GrammarResourcePool.cpp @@ -86,7 +86,7 @@ const std::set *GrammarResourcePool::FOLLOW(int symbol) { if(it != follows.end()) { return it->second; } else { - throw std::runtime_error("symbol NOT Found"); + throw std::runtime_error("Symbol_Index" + std::to_string(symbol) + "NOT Found"); } } diff --git a/src/SyntaxParser.cpp b/src/SyntaxParser.cpp index d5520cf..0ec11e1 100644 --- a/src/SyntaxParser.cpp +++ b/src/SyntaxParser.cpp @@ -112,13 +112,7 @@ void SyntaxParser::getToken() { auto token_info = get_token_info(tokens[i]); int symbol_index; - if(token_info.first == L"ID" - || token_info.first == L"EOF" - || token_info.first == L"INTEGER" - || token_info.first == L"STRING") - symbol_index = pool->getSymbolIndex(token_info.first); - else - symbol_index = pool->getSymbolIndex(token_info.second); + symbol_index = pool->getSymbolIndex(token_info.first); tokens_queue.push(symbol_index); _line_index++;