diff --git a/main.py b/main.py index e116cfa..c439082 100644 --- a/main.py +++ b/main.py @@ -95,12 +95,15 @@ def print_unit_rules(rules): """) for element in rule[1]: elem_code = get_code(element) - print(f""" nbCharParsed = parse_{elem_code}(word, pos + totalCharParsed); - if (!nbCharParsed) {{ + if elem_code != "": + print(f""" nbCharParsed = parse_{elem_code}(word, pos + totalCharParsed); + if (nbCharParsed == -1) {{ printf("Fail {elem_code} in {code}{rules_dict[left]}\\n"); - return 0; + return -1; }} totalCharParsed += nbCharParsed;""") + else: + print(f" printf(\"Epsilon! -> Success\\n\");") print(f""" printf("Success {code}{rules_dict[left]}\\n"); return totalCharParsed; @@ -116,11 +119,11 @@ def print_global_rules(): for i in range(1, value + 1): print(f""" nbCharParsed = parse_{code}{i}(word, pos); - if (nbCharParsed) {{ + if (nbCharParsed != -1) {{ return nbCharParsed; }}""") print(""" - return 0; + return -1; }""") @@ -131,12 +134,16 @@ def get_code(s): def print_terminal_rules(): for t in terminals_set: code = get_code(t) + l = len(t) print(f"""int parse_{code}(char* word, int pos) {{ - if (strcomp(word[pos], "{t}") == 0) {{ + char substr[{l+1}]; + substr[0] = '\\0'; + strncat(substr, &word[pos], {l}); + if (strcmp(substr, "{t}") == 0) {{ print_with_indent(pos, "{code}\\n"); - return 1; + return {l}; }} else {{ - return 0; + return -1; }} }}""")