Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.compilers > #2975
| From | "marb...@yahoo.co.uk" <marblypup@yahoo.co.uk> |
|---|---|
| Newsgroups | comp.compilers |
| Subject | Re: Using a C struct to represent a node in a parse tree ... how many fields in the struct? |
| Date | 2022-04-09 05:03 -0700 |
| Organization | Compilers Central |
| Message-ID | <22-04-008@comp.compilers> (permalink) |
| References | <22-04-002@comp.compilers> <22-04-006@comp.compilers> |
Hmm! The last time I wrote a (simple) compiler, it never occurred to me to use any of those methods! Here's my code (with the 'payload' removed from the struct and new_node):
I guess Johann would say I'm being extremely anal :-D
struct tree {
enum token_type type;
struct tree *child, *sibling;
};
static struct tree *new_node(int type) {
struct tree *res=malloc_or_bomb(sizeof *res);
res->type=type;
res->child=res->sibling=NULL;
return res;
}
static struct tree *add_child(struct tree *parent, struct tree *child) {
/* Add child to end of parent's list of children. */
struct tree **p;
for (p=&parent->child; NULL!=*p; p=&(*p)->sibling)
;
*p=child;
return child;
}
static struct tree *add_first_child(struct tree *parent, struct tree *child) {
/* Add child to start of parent's list of children. */
child->sibling=parent->child;
parent->child=child;
return child;
}
(I think I pinched the child/sibling technique for storing trees with arbitrary numbers of children from an Infocom game!)
[The child/sibling approach is the way Lisp lists have worked since the last 1950s.
For reasons related to the architecture of the IBM 709, they're usually spelled CAR and CDR. -John]
Back to comp.compilers | Previous | Next — Previous in thread | Next in thread | Find similar
Using a C struct to represent a node in a parse tree ... how many fields in the struct? Roger L Costello <costello@mitre.org> - 2022-04-08 11:17 +0000
Re: Using a C struct to represent a node in a parse tree ... how many fields in the struct? Johann Klammer <klammerj@NOSPAM.a1.net> - 2022-04-08 20:44 +0200
Re: Using a C struct to represent a node in a parse tree ... how many fields in the struct? Kaz Kylheku <480-992-1380@kylheku.com> - 2022-04-08 19:49 +0000
Re: Using a C struct to represent a node in a parse tree ... how many fields in the struct? "marb...@yahoo.co.uk" <marblypup@yahoo.co.uk> - 2022-04-09 05:03 -0700
Re: Using a C struct to represent a node in a parse tree ... how many fields in the struct? luser droog <luser.droog@gmail.com> - 2022-04-27 21:22 -0700
csiph-web