X-Git-Url: https://ocean-lang.org/code/?p=ocean;a=blobdiff_plain;f=csrc%2Fparsergen.mdc;h=36c7464d58703859c2b59ab881ab7d595d3a0242;hp=f2bbe0151d75977dd2b2ae9f16031f4c171a5949;hb=bb840f939996d9d64acdf6198e1ca18c7059ea15;hpb=5f8aaec6eb5315fff28c9c55c15620f43d483f97 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