调整语法;

This commit is contained in:
Saturneric 2021-05-01 00:05:03 +08:00
parent a5d808cabc
commit 1714e6f560
4 changed files with 66 additions and 68 deletions

View File

@ -18,39 +18,43 @@ using std::endl;
int main() { int main() {
clock_t start,end;//定义clock_t变量 try {
start = clock(); //开始时间 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(); //结束时间 end = clock(); //结束时间
double times = double(end-start)/CLOCKS_PER_SEC; double times = double(end - start) / CLOCKS_PER_SEC;
wcout<<"LR1Generator Run time = "<< times <<"s MicroSeconds" << " = " << times * 1000 <<"ms" << endl; 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(); //结束时间 end = clock(); //结束时间
times = double(end-start)/CLOCKS_PER_SEC; times = double(end - start) / CLOCKS_PER_SEC;
wcout<<"SyntaxParser Run time = "<<times<<"s MicroSeconds " << " = " << times * 1000 <<"ms" << endl; wcout << "SyntaxParser Run time = " << times << "s MicroSeconds " << " = " << times * 1000 << "ms" << endl;
} catch(std::runtime_error &e) {
std::wcout << "Runtime Error: " << e.what() << endl;
}
return 0; return 0;
} }

View File

@ -1,89 +1,89 @@
@struct_type -> "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 -> "EOF"
more_struct_type -> ε more_struct_type -> ε
member_list -> type_spec declarators ";" member_list member_list -> type_spec declarators "SEMICOLON" member_list
member_list -> ε member_list -> ε
type_spec -> base_type_spec type_spec -> base_type_spec
type_spec -> @struct_type type_spec -> @struct_type
base_type_spec -> floating_pt_type base_type_spec -> floating_pt_type
base_type_spec -> integer_type base_type_spec -> integer_type
base_type_spec -> "char" base_type_spec -> "CHAR"
base_type_spec -> "boolean" base_type_spec -> "BOOLEAN"
floating_pt_type -> "float" floating_pt_type -> "FLOAT"
floating_pt_type -> "double" floating_pt_type -> "DOUBLE"
floating_pt_type -> "long" "double" floating_pt_type -> "LONG" "DOUBLE"
integer_type -> signed_int integer_type -> signed_int
integer_type -> unsigned_int integer_type -> unsigned_int
signed_int -> "short" signed_int -> "SHORT"
signed_int -> "int16" signed_int -> "INT16"
signed_int -> "long" signed_int -> "LONG"
signed_int -> "int32" signed_int -> "INT32"
signed_int -> "long" "long" signed_int -> "LONG" "LONG"
signed_int -> "int64" signed_int -> "INT64"
signed_int -> "int8" signed_int -> "INT8"
unsigned_int -> "unsigned" "short" unsigned_int -> "UNSIGNED" "SHORT"
unsigned_int -> "unsigned" "long" unsigned_int -> "UNSIGNED" "LONG"
unsigned_int -> "unsigned" "long" "long" unsigned_int -> "UNSIGNED" "LONG" "LONG"
unsigned_int -> "uint16" unsigned_int -> "UINT16"
unsigned_int -> "uint32" unsigned_int -> "UINT32"
unsigned_int -> "uint64" unsigned_int -> "UINT64"
unsigned_int -> "uint8" unsigned_int -> "UINT8"
declarators -> declarator more_declarators declarators -> declarator more_declarators
more_declarators -> "," declarator more_declarators more_declarators -> "COMMA" declarator more_declarators
more_declarators -> ε more_declarators -> ε
declarator -> "ID" more_declarator declarator -> "ID" more_declarator
more_declarator -> exp_list more_declarator -> exp_list
more_declarator -> ε more_declarator -> ε
exp_list -> "[" or_expr more_or_expr "]" exp_list -> "LEFT_BRACKET" or_expr more_or_expr "RIGHT_BRACKET"
more_or_expr -> "," or_expr more_or_expr more_or_expr -> "COMMA" or_expr more_or_expr
more_or_expr -> ε more_or_expr -> ε
or_expr -> xor_expr more_xor_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 -> ε more_xor_expr -> ε
xor_expr -> and_expr more_and_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 -> ε more_and_expr -> ε
and_expr -> shift_expr more_shift_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 -> ε more_shift_expr -> ε
shift_expr -> add_expr more_add_expr shift_expr -> add_expr more_add_expr
more_add_expr -> shift_sign add_expr more_add_expr more_add_expr -> shift_sign add_expr more_add_expr
shift_sign -> ">>" shift_sign -> "RIGHT_SHIFT"
shift_sign -> "<<" shift_sign -> "LEFT_SHIFT"
more_add_expr -> ε more_add_expr -> ε
add_expr -> multi_expr more_multi_expr add_expr -> multi_expr more_multi_expr
more_multi_expr -> multi_sign multi_expr more_multi_expr more_multi_expr -> multi_sign multi_expr more_multi_expr
multi_sign -> "+" multi_sign -> "PLUS"
multi_sign -> "-" multi_sign -> "SUB"
more_multi_expr -> ε more_multi_expr -> ε
multi_expr -> unary_expr more_unary_expr multi_expr -> unary_expr more_unary_expr
more_unary_expr -> unary_sign unary_expr more_unary_expr more_unary_expr -> unary_sign unary_expr more_unary_expr
unary_sign -> "*" unary_sign -> "MULT"
unary_sign -> "/" unary_sign -> "SLASH"
unary_sign -> "%" unary_sign -> "PERCENT"
more_unary_expr -> ε more_unary_expr -> ε
unary_expr -> unary_sign_2 unary_declare unary_expr -> unary_sign_2 unary_declare
unary_sign_2 -> "-" unary_sign_2 -> "SUB"
unary_sign_2 -> "+" unary_sign_2 -> "PLUS"
unary_sign_2 -> "~" unary_sign_2 -> "TILDE"
unary_sign_2 -> ε unary_sign_2 -> ε
unary_declare -> "INTEGER" unary_declare -> "INTEGER"
unary_declare -> "STRING" unary_declare -> "STRING"
unary_declare -> BOOLEAN unary_declare -> BOOLEAN_VALUE
BOOLEAN -> "TRUE" BOOLEAN_VALUE -> "TRUE"
BOOLEAN -> "FALSE" BOOLEAN_VALUE -> "FALSE"

View File

@ -86,7 +86,7 @@ const std::set<int> *GrammarResourcePool::FOLLOW(int symbol) {
if(it != follows.end()) { if(it != follows.end()) {
return it->second; return it->second;
} else { } else {
throw std::runtime_error("symbol NOT Found"); throw std::runtime_error("Symbol_Index" + std::to_string(symbol) + "NOT Found");
} }
} }

View File

@ -112,13 +112,7 @@ void SyntaxParser::getToken() {
auto token_info = get_token_info(tokens[i]); auto token_info = get_token_info(tokens[i]);
int symbol_index; int symbol_index;
if(token_info.first == L"ID" symbol_index = pool->getSymbolIndex(token_info.first);
|| 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);
tokens_queue.push(symbol_index); tokens_queue.push(symbol_index);
_line_index++; _line_index++;