bug-gnu-utils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: parsing getline var argument


From: Aharon Robbins
Subject: Re: parsing getline var argument
Date: Mon, 13 Aug 2007 23:09:49 +0300

> Date: Sun, 12 Aug 2007 20:17:35 -0400 (EDT)
> From: "Joel E. Denny" <address@hidden>
> Subject: parsing getline var argument
> To: address@hidden
>
> I am trying to understand GNU AWK's parser as it relates to an extension I 
> am working on for Bison.  However, I don't use AWK often, so some of my 
> questions may be naive.
>
> I checked out GNU AWK from CVS and built it.  The most recent ChangeLog 
> entry at the time was Sat Aug 11 22:48:11 2007.

That's what's current.

> I then ran:
>
>   % ./gawk '{getline $4; print $4;}' > stdout
>   line 1
>   line 2
>   % cat stdout
>   line2
>
> Recent releases of GAWK behave in the same way, but this behavior differs 
> from /usr/bin/awk under Solaris 10:
>
>   % /usr/bin/awk '{getline $4; print $4;}' > stdout
>   line 1
>   line 2
>   % od -c stdout
>   0000000  \n
>   0000001
>
> What is the correct behavior?  Is `getline $4;' a GNU extension?  Or is 
> the behavior undefined here?

Gawk is correct. The argument to getline is an lvalue, which receives
the contents of the line just read in.  Using a field is a very ill-defined
kind of thing to do, but Solaris awk output is broken.  That is a very OLD
awk anyway, and you should compare the Solaris /usr/xpg4/bin/awk, which
happens to get it right. So does mawk.

> If CVS GAWK is correct, then how should it parse the following expression?
>
>   getline $4*0;
>
> In Makefile, I added -DGAWKDEBUG to CFLAGS.  I then rebuilt GAWK and ran:
>
>   % ./gawk --parsedebug 'getline $4*0;'
>
> >From the parser trace, I see that GAWK parses the expression as:
>
>   (getline $4)*0;

This is correct. Getline returns a value (1, 0, -1).
>
> For example:
>
>   % ./gawk '{ print getline $4; print getline $4*0; }' > stdout
>   line 1
>   line 2
>   line 3
>   % cat stdout
>   1
>   0

This is correct.

> However:
>
>   % /usr/bin/awk '{ print getline $4; print getline $4*0; }' > stdout
>   line 1
>   line 2
>   line 3
>   % cat stdout
>   1
>   10
>
> What behavior is correct?

The solaris /usr/bin/awk is wrong. As mentioned, don't use it.

> Thanks.

You're welcome,

Arnold




reply via email to

[Prev in Thread] Current Thread [Next in Thread]