Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.compilers > #2973

Re: Using a C struct to represent a node in a parse tree ... how many fields in the struct?

From Johann Klammer <klammerj@NOSPAM.a1.net>
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-08 20:44 +0200
Organization Aioe.org NNTP Server
Message-ID <22-04-004@comp.compilers> (permalink)
References <22-04-002@comp.compilers>

Show all headers | View raw


On 04/08/2022 01:17 PM, Roger L Costello wrote:
> 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]
>
doesn't happen.
e.g. if/else is always 1 or two nodes. if they're deeper nested
it ends up in subnodes. as long as you use flex/yacc you'll just have to look
what's the highest number of nonterminals in your ruleset. and that's the number
of fields in your struct, possibly plus an identifying enum and usually some
union for the terminal symbols(as they won't have subnodes)
if you're overly anal you can prolly dynamic allocate those pointers.

> struct node
> {
> 	int 	        nodetype ;
>	node            *fields[0];
> } ;

Back to comp.compilers | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

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