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]
struct node
{
int nodetype ;
node *fields[0];
} ;
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?
On 2022-04-08, Roger L Costello <cost...@mitre.org> 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?The fields are all nodes so you can easily have a linked list:
struct node
{
int nodetype;
struct node *car;
struct node *cdr;
}
The Lisp family of languages handle all syntax using nothing but binary
cells like this, including nodes with arbitrary numbers of arguments.
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 300 |
Nodes: | 16 (2 / 14) |
Uptime: | 59:48:42 |
Calls: | 6,712 |
Files: | 12,243 |
Messages: | 5,355,695 |