]> ocean-lang.org Git - ocean/blobdiff - csrc/parsergen.mdc
parsergen: move the 'follow' declaration
[ocean] / csrc / parsergen.mdc
index 86aa299fff058d07a9e562a21d006f9ccb2c1cf6..9156c63041121d0ef1f0e34bc7b45932967128b8 100644 (file)
@@ -561,6 +561,7 @@ Now we have all the bits we need to parse a full production.
        abort:
                while (tk.num != TK_newline && tk.num != TK_eof)
                        tk = token_next(state);
+               free(p.body);
                return err;
        }
 
@@ -695,7 +696,7 @@ used as a terminal anywhere that a terminal is expected.
                                        s->name.len, s->name.txt);
                        }
                        if (errs) {
-                               free(g);
+                               free(g); // FIXME free content
                                g = NULL;
                        }
                }
@@ -704,7 +705,7 @@ used as a terminal anywhere that a terminal is expected.
                fprintf(stderr, "Error at line %d: %s\n",
                        tk.line, err);
                token_close(state);
-               free(g);
+               free(g); // FIXME free content
                return NULL;
        }
 
@@ -1071,6 +1072,9 @@ and we find the set of possible "first" symbols after there.  This is
 done using `add_first` above and only needs to be done once as the
 "first" sets are now stable and will not change.
 
+###### grammar fields
+       struct symset *follow;
+
 ###### follow code
 
        for (p = 0; p < g->production_count; p++) {
@@ -1120,9 +1124,6 @@ combine these two functions into a single loop.
 We now just need to create and initialise the `follow` list to get a
 complete function.
 
-###### grammar fields
-       struct symset *follow;
-
 ###### functions
        static void build_follow(struct grammar *g)
        {
@@ -2072,8 +2073,6 @@ The go to table is stored in a simple array of `sym` and corresponding
                }
        }
 
-###### functions
-
        static void gen_states(FILE *f, struct grammar *g)
        {
                int i;
@@ -2661,6 +2660,35 @@ table.
                        return -1;
        }
 
+### Memory allocation
+
+The `scanner` returns tokens in a local variable - we want them in allocated
+memory so they can live in the `asn_stack`.  Similarly the `asn` produced by
+a reduce is in a large buffer.  Both of these require some allocation and
+copying, hence `memdup` and `tok_copy`.
+
+###### parser includes
+       #include <memory.h>
+
+###### parser functions
+
+       void *memdup(void *m, int len)
+       {
+               void *ret;
+               if (len == 0)
+                       return NULL;
+               ret = malloc(len);
+               memcpy(ret, m, len);
+               return ret;
+       }
+
+       static struct token *tok_copy(struct token tk)
+       {
+               struct token *new = malloc(sizeof(*new));
+               *new = tk;
+               return new;
+       }
+
 ### The state stack.
 
 The core data structure for the parser is the stack.  This tracks all
@@ -2833,35 +2861,6 @@ before we `shift` the nonterminal in.
                return indents;
        }
 
-### Memory allocation
-
-The `scanner` returns tokens in a local variable - we want them in allocated
-memory so they can live in the `asn_stack`.  Similarly the `asn` produced by
-a reduce is in a large buffer.  Both of these require some allocation and
-copying, hence `memdup` and `tokcopy`.
-
-###### parser includes
-       #include <memory.h>
-
-###### parser functions
-
-       void *memdup(void *m, int len)
-       {
-               void *ret;
-               if (len == 0)
-                       return NULL;
-               ret = malloc(len);
-               memcpy(ret, m, len);
-               return ret;
-       }
-
-       static struct token *tok_copy(struct token tk)
-       {
-               struct token *new = malloc(sizeof(*new));
-               *new = tk;
-               return new;
-       }
-
 ### The heart of the parser.
 
 Now we have the parser.  For each token we might shift it, trigger a