[Haifux] Never say Yuck about yacc

2002-07-15 Thread Eli Billauer

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?

That's all.

Eli


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





Re: [Haifux] Never say Yuck about yacc

2002-07-15 Thread Muli Ben-Yehuda

On Mon, Jul 15, 2002 at 05:10:55PM +0200, 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?)

Oh, I do lots of homewor, just not necessarily related to any past or
present lecture's material ;)

If you want to dive deeper into compiler theory, I've got a copy of
the dragon book here you're welcome to borrow. 
-- 
http://vipe.technion.ac.il/~mulix/
http://syscalltrack.sf.net/



msg01236/pgp0.pgp
Description: PGP signature