summaryrefslogtreecommitdiff
path: root/src/grammar.c
diff options
context:
space:
mode:
authorJSDurand <mmemmew@gmail.com>2022-01-04 20:24:07 +0800
committerJSDurand <mmemmew@gmail.com>2022-01-04 20:24:07 +0800
commit949888ad5d0cd0f9f9a87f9938a632b3e32df051 (patch)
treeae9155484a3a5b4124c6d3fae14ada9d718c8579 /src/grammar.c
parent55dc897da6e81f2a26cfc7e66ac942824773498b (diff)
Fix problems
Now some problems are fixed. It can now read grammar correctly (hopefully) from a file, in the BNF format. And strings (terminals) are handled fine as well. So glad that there are no leak problems now.
Diffstat (limited to 'src/grammar.c')
-rw-r--r--src/grammar.c38
1 files changed, 19 insertions, 19 deletions
diff --git a/src/grammar.c b/src/grammar.c
index 118a10e..4052510 100644
--- a/src/grammar.c
+++ b/src/grammar.c
@@ -68,6 +68,12 @@ destroy_rule_group_free_first(void *rule_grp)
}
static void
+print_sep()
+{
+ printf(", ");
+}
+
+static void
print_rule_group(void *rule_grp)
{
Rule_group *rg = (Rule_group *) rule_grp;
@@ -75,7 +81,7 @@ print_rule_group(void *rule_grp)
for (int i = 0; i < list_length(rg->rights); i++) {
List *str = (List *) list_nth(rg->rights, i);
printf("Rule %u => ", rg->left);
- map_list(str, print_tnt);
+ map_list_between(str, print_tnt, print_sep);
printf("\n");
}
}
@@ -130,7 +136,7 @@ build_grammar(Grammar *g, List *rules, List *names)
an error. */
for (int i = 0; i < rule_len; i++) {
if (((Rule *) list_nth(rules, i))->left >= len) {
- fleprintf("%s:%d:%d, Rule contains weird non-terminal\n", i);
+ fleprintf("%d, Rule contains weird non-terminal\n", i);
return 1;
}
}
@@ -138,12 +144,12 @@ build_grammar(Grammar *g, List *rules, List *names)
List *rule_grps = new_list();
if (rule_grps == NULL) {
- fleprintf0("%s:%d, Cannot create list\n");
+ fleprintf0("Cannot create list\n");
return 1;
}
if (list_assure_size(rule_grps, len)) {
- fleprintf0("%s:%d, Cannot assure size of rule groups\n");
+ fleprintf0("Cannot assure size of rule groups\n");
return 1;
}
@@ -153,7 +159,7 @@ build_grammar(Grammar *g, List *rules, List *names)
/* Initialize the list of rule groups */
for (int i = 0; i < len; i++) {
if ((temp_ls = new_list()) == NULL) {
- fleprintf("%s:%d:%d, Cannot create list\n", i);
+ fleprintf("%d, Cannot create list\n", i);
map_list(rule_grps, destroy_rule_group_no_free);
destroy_list(rule_grps, 0);
@@ -163,7 +169,7 @@ build_grammar(Grammar *g, List *rules, List *names)
temp_pointer = MYALLOC(Rule_group, 1);
if (temp_pointer == NULL) {
- fleprintf0("%s:%d, Cannot malloc\n");
+ fleprintf0("Cannot malloc\n");
map_list(rule_grps, destroy_rule_group_no_free);
destroy_list(rule_grps, 0);
destroy_list(temp_ls, 0);
@@ -176,7 +182,7 @@ build_grammar(Grammar *g, List *rules, List *names)
int result = add_to_list(rule_grps, temp_pointer);
if (result) {
- fleprintf("%s:%d:%d, Cannot add to list\n", i);
+ fleprintf("%d, Cannot add to list\n", i);
map_list(rule_grps, destroy_rule_group_no_free);
destroy_list(rule_grps, 0);
@@ -196,7 +202,7 @@ build_grammar(Grammar *g, List *rules, List *names)
r->right);
if (result) {
- fleprintf("%s:%d:%d, Cannot add to list\n", i);
+ fleprintf("%d, Cannot add to list\n", i);
for (int j = 0; j < list_length(rule_grps); j++) {
Rule_group *rg = (Rule_group *) list_nth(rule_grps, j);
@@ -223,9 +229,9 @@ print_tnt(void *element)
TNT *tnt = (TNT*) element;
if (tnt->type)
- printf("NT %u, ", tnt->data.nt);
+ printf("NT %u", tnt->data.nt);
else
- printf("T %lu, ", tnt->data.t);
+ printf("T %lu", tnt->data.t);
}
void
@@ -269,7 +275,6 @@ find_in_cpa_list(NUM *string, NUM size, List *list)
return (foundp) ? index : -1;
}
-/* a local function pointer */
void
print_name(void *element)
{
@@ -281,7 +286,7 @@ print_name(void *element)
int result = encode(*(array->array+i), string);
if (result) {
- fleprintf("%s:%d:%llu, fail to encode\n", i);
+ fleprintf("%llu, fail to encode\n", i);
str_set_length(string, 5);
continue;
}
@@ -296,13 +301,8 @@ print_name(void *element)
destroy_str(string, 1);
}
-/* a local function pointer */
-void
-print_sep()
-{
- printf(", ");
-}
-
+/* REVIEW: Print the names of non-terminals out, instead of printing
+ the numbers? */
void
print_grammar(Grammar *g)
{