lin-club  

[Haifux] Re: Never say Yuck about yacc

Shlomi Fish
Mon, 15 Jul 2002 22:33:53 -0700

On Mon, 15 Jul 2002, Eli Billauer wrote:

> Hello all,
>
> After playing around a bit with the Perl-byacc thing, there are a few
> notes that might come handy to whoever wants to do something useful with
> it. (Am I still the only one who does homework between lectures?)
>
> And before pumping you with the technical details, I must say that it
> was quite a pleasant experience. I've never taken a compiler course (I'm
> an Elec. Eng. remember?) so I feel I've really learned something.
>
> First of all, I warmly suggest to change the yyerror function to this
> one:
>
> sub yyerror
> {
>     die $_[0]." at line ". $lexer->line .":\n".$lexer->buffer . "\n";
> }
>
> This one gives you a chance of understanding what happened. I didn't try
> too hard to make a pointer on where, exactly, in the line, the parser
> got stuck, neither resume parsing after an error. If someone knows how
> this is done, (in the Perl version!) kindly enlight me.
>
> Another thing that got me stuck, is the list-like recursion, such as
> (yacc!):
>
> chunk:   { $$ = ""; }
>         |        item chunk { $$ = $1.$2; }
> ;
>
> Now, note the first line, representing that "chunk" could be nothing. It
> also says that nothing should be nothing (that is, $$ = "")! It took me
> some time to find out that the $$="" is necessary, otherwise we get
> previous garbage in $$, and I mean GARBAGE.
>
> Now, I guess that the compiler freaks among us will notice, that I made
> a "right recursion", as opposed to the glorious "left recursion", which
> would be saying "chunk item { $$ = $1.$2; }". (Right recursion forces
> the parser to read all the chunk into the stack before starting to
> chew).
>
> The reason I stayed with right recursion is that left recursion didn't
> work :(. I got empty strings. Anyone got any idea of why?
>

I think Yacc imposes some limitations on its grammar and does not accept
left-recursive grammars. Better check the Dragon Book to be sure.

Regards,

        Shlomi Fish

> That's all.
>
>     Eli
>
>
> --------------------------------------------------------------------------
> Haifa Linux Club Mailing List (http://linuxclub.il.eu.org)
> To unsub send an empty message to [EMAIL PROTECTED]
>
>



----------------------------------------------------------------------
Shlomi Fish        [EMAIL PROTECTED]
Home Page:         http://t2.technion.ac.il/~shlomif/
Home E-mail:       [EMAIL PROTECTED]

He who re-invents the wheel, understands much better how a wheel works.


--------------------------------------------------------------------------
Haifa Linux Club Mailing List (http://linuxclub.il.eu.org)
To unsub send an empty message to [EMAIL PROTECTED]