Example of building an AST in Bison

eyeris <drewpvogel@gmail.com>
Fri, 03 Aug 2007 15:22:34 -0700

          From comp.compilers

Related articles
Example of building an AST in Bison drewpvogel@gmail.com (eyeris) (2007-08-03)
| List of all articles for this month |
From: eyeris <drewpvogel@gmail.com>
Newsgroups: comp.compilers
Date: Fri, 03 Aug 2007 15:22:34 -0700
Organization: Compilers Central
Keywords: AST, question, comment
Posted-Date: 07 Aug 2007 09:43:31 EDT

Can anyone here point me to an example Bison input file that shows how
to construct an AST? I've read a few papers on ASTs and I understand
their function and how to build one by hand, but I can't get my head
around how to handle some of my Bison rules. e.g.


item: ITEMDECL itemparts ;
itemparts: | itemparts itempart;
itempart: TEXT | KEYWORD;


In the action for the "item" rule, what would $2 represent? In my AST
it would be a series of sibling nodes, but how does Bison represent
them in C code? They can't be an array because TEXT and KEYWORD could
return different types, which would produce invalid C code. I fear I'm
missing something simple.
[In a LALR parser, you build the tree from the bottom up, e.g.:


  itempart: TEXT { $$ = node(TEXTPART,$1,NULL); }


  itemparts: itemparts itempart { $$ = node(PARTLIST, $1, $2); }


  item: ITEMDECL itemparts { $$ = node(ITEM, $1, $2); }


where node(type,l,r) allocates a new node.


-John]


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.