Path: csiph.com!weretis.net!feeder6.news.weretis.net!news.misty.com!news.iecc.com!.POSTED.news.iecc.com!nerds-end From: Roger L Costello Newsgroups: comp.compilers Subject: Using a C struct to represent a node in a parse tree ... how many fields in the struct? Date: Fri, 8 Apr 2022 11:17:01 +0000 Organization: Compilers Central Lines: 29 Sender: news@iecc.com Approved: comp.compilers@iecc.com Message-ID: <22-04-002@comp.compilers> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 8bit Injection-Info: gal.iecc.com; posting-host="news.iecc.com:2001:470:1f07:1126:0:676f:7373:6970"; logging-data="77925"; mail-complaints-to="abuse@iecc.com" Keywords: parse, question, comment Posted-Date: 08 Apr 2022 11:50:38 EDT X-submission-address: compilers@iecc.com X-moderator-address: compilers-request@iecc.com X-FAQ-and-archives: http://compilers.iecc.com Content-Language: en-US Xref: csiph.com comp.compilers:2972 Hi Folks, The compiler book [1] that I am reading says this: The most obvious way to represent the information gained from lexical and syntax analysis is as a tree along the lines of the parse tree. In C this is suitably handled using a simple struct for each node: struct node { int nodetype ; struct node *field1 ; struct node *field2 ; struct node *field3 ; struct node *field4 ; struct node *field5 ; } ; But, but, but, ..what if a node requires more than 5 fields; how is that handled? /Roger [1] Introduction to Compiling Techniques, A First Course Using ANSI C, LEX and YACC by J. P. Bennett, page 47. [Use a bigger struct. You know what kind of nodes your parser creates, so define a struct that does what you want. Personally, I prefer to define a struct for each node type and a general node that is a union, or perhaps a union of pointers. -John]