knowledge-database (beta)

Current group: comp.lang.objective-c

Using a yacc parser in Objc

Using a yacc parser in Objc  
jsiehler
 Re: Using a yacc parser in Objc  
jsiehler
 Re: Using a yacc parser in Objc  
jsiehler
 Re: Using a yacc parser in Objc  
Michael Ash
From:jsiehler
Subject:Using a yacc parser in Objc
Date:13 Jan 2005 09:54:43 -0800
I have an Objective C class called 'Expr' with several subclasses, and
a yacc parser with reduction rules like

exp: exp '+' exp { $$ = [[BinaryOpExpr alloc] initAs: PLUSOP of:$1
and:$3];}

to build up a complicated Expr from a formula that has been input.

When yyparse() finishes, I would like it to return the completed Expr
to the calling program.

I tried modifying the yyparse header to
int yyparse (Expr *result)
and amending the final reduction action to include a line like
result = yyvsp[-1]
which does make result point to the completed object (and within
yyparse() I can do things like have result print or evaluate itself),
but upon return to the calling program, result is apparently pointing
to garbage, as if all the instances created during parsing get trashed
when yyparse() completes.

What's the right way to get my completed Expr out of yyparse() and back
to the calling program?

Thanks.
js
From:jsiehler
Subject:Re: Using a yacc parser in Objc
Date:13 Jan 2005 10:40:44 -0800
Ach, what a dumb mistake I made. Got it figured out now.
From:jsiehler
Subject:Re: Using a yacc parser in Objc
Date:13 Jan 2005 12:49:29 -0800
>I'm sure you mean Expr **result here.
Yes, that was my stupid mistake.

Your alternative is cleaner and you're precisely right about it needing
less maintenance since I have to edit the result if I should change the
grammer and re-yacc. Thanks.
From:Michael Ash
Subject:Re: Using a yacc parser in Objc
Date:Thu, 13 Jan 2005 12:43:17 -0600
jsiehler wrote:
> I have an Objective C class called 'Expr' with several subclasses, and
> a yacc parser with reduction rules like
>
> exp: exp '+' exp { $$ = [[BinaryOpExpr alloc] initAs: PLUSOP of:$1
> and:$3];}
>
> to build up a complicated Expr from a formula that has been input.
>
> When yyparse() finishes, I would like it to return the completed Expr
> to the calling program.
>
> I tried modifying the yyparse header to
> int yyparse (Expr *result)

I'm sure you mean Expr **result here. You want to "return" an Expr *, so
you need to pass a pointer to *that*.

> and amending the final reduction action to include a line like
> result = yyvsp[-1]

This would then become *result = yyvsp[-1].

> What's the right way to get my completed Expr out of yyparse() and back
> to the calling program?

My technique was to use a global variable to get the final result. In the
extra declarations section of my .y file, I put:

id parsedTree;

And then in the top-level parse statement:

program : method_list
{ parsedTree = $1; $$ = $1; }
;

This might be easier to maintain than your solution, and it seems a little
cleaner. But then again, this is yacc, and nothing is going to be very
clean.
   

Copyright © 2006 knowledge-database   -   All rights reserved