Related articles |
---|
request for simple example: parse tree generation kendrac@ee.ubc.ca (1996-10-03) |
Re: request for simple example: parse tree generation mac@coos.dartmouth.edu (1996-10-06) |
Re: request for simple example: parse tree generation belinfan@cs.utwente.nl (1996-10-10) |
Re: request for simple example: parse tree generation pjj@cs.man.ac.uk (1996-10-10) |
From: | mac@coos.dartmouth.edu (Alex Colvin) |
Newsgroups: | comp.compilers |
Date: | 6 Oct 1996 00:55:23 -0400 |
Organization: | Dartmouth College, Hanover, NH, USA |
References: | 96-10-013 |
Keywords: | yacc |
>I'm looking for a *simple* example that shows how to generate
>an explicit parse tree using yacc. Since the tool is used a lot,
>I thought there would be some sort of "standard" way of doing this.
>Kendra
>[Yeah, I wanted to put some examples of that in the book, but ran out of
>time and pages. It's pretty straightforward, generate subtrees each time
>you reduce a rule and splice them together as you go, ending up with one
>big tree when the parse is done. -John]
as in the following fragment...
ParseTree(opcode,...) constructs a tree node
Here the stack is used both to pass tree nodes (as in product_expr)
and opcodes (as in product_op).
product_expr
: product_expr product_op cast_expr
{ $$ = ParseTree($2,$1,$3); }
| cast_expr { $$ = $1; }
;
product_op /* miltiplicative_expr */
: '*' { $$ = opMULT; }
| '/' { $$ = opDIV; }
| '%' { $$ = opREM; }
| tkMOD { $$ = opMOD; }
;
conditional_expr /* AC: could be called simple_expr */
: infix_expr { $$ = $1; }
| infix_expr '?' expr ':' conditional_expr
{ $$ = ParseTree(opCONDEXP,$1,$3,$5); }
;
assignment_expr
: conditional_expr { $$ = $1; }
| unary_expr assignment_operator assignment_expr
{ $$ = ParseTree($2,$1,$3); }
;
single_expr /* no commas */
: assignment_expr { $$ = $1; }
;
--
Alex Colvin
alex.colvin@dartmouth.edu
--
Return to the
comp.compilers page.
Search the
comp.compilers archives again.