From: NeilBrown Date: Fri, 5 Mar 2021 09:16:22 +0000 (+1100) Subject: parsergen: move "Memory allocation" section. X-Git-Url: https://ocean-lang.org/code/?p=ocean;a=commitdiff_plain;h=bb840f939996d9d64acdf6198e1ca18c7059ea15 parsergen: move "Memory allocation" section. This movement isn't really significant yet, but it will help a bit later, and I want to get it out of the way. Signed-off-by: NeilBrown --- diff --git a/csrc/parsergen.mdc b/csrc/parsergen.mdc index f2bbe01..36c7464 100644 --- a/csrc/parsergen.mdc +++ b/csrc/parsergen.mdc @@ -2662,6 +2662,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 + +###### 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 @@ -2834,35 +2863,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 - -###### 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