Re: Writing a C Compiler: lvalues

Tom St Denis <tom@iahu.ca>
Sun, 9 May 2010 10:25:38 -0700 (PDT)

          From comp.compilers

Related articles
Writing a C Compiler: lvalues andre.nho@gmail.com (=?ISO-8859-1?Q?Andr=E9_Wagner?=) (2010-05-08)
Re: Writing a C Compiler: lvalues ben.usenet@bsb.me.uk (Ben Bacarisse) (2010-05-09)
Re: Writing a C Compiler: lvalues bartc@freeuk.com (bart.c) (2010-05-09)
Re: Writing a C Compiler: lvalues tom@iahu.ca (Tom St Denis) (2010-05-09)
Re: Writing a C Compiler: lvalues kst-u@mib.org (Keith Thompson) (2010-05-09)
Re: Writing a C Compiler: lvalues esosman@ieee.org (Eric Sosman) (2010-05-09)
Re: Writing a C Compiler: lvalues stargazer3p14@gmail.com (Stargazer) (2010-05-10)
Re: Writing a C Compiler: lvalues marc@lithia.nl (Marc van Lieshout) (2010-05-16)
Re: Writing a C Compiler: lvalues esosman@ieee.org (Eric Sosman) (2010-05-17)
Re: Writing a C Compiler: lvalues kst-u@mib.org (Keith Thompson) (2010-05-17)
[7 later articles]
| List of all articles for this month |

From: Tom St Denis <tom@iahu.ca>
Newsgroups: comp.lang.c,comp.compilers
Date: Sun, 9 May 2010 10:25:38 -0700 (PDT)
Organization: Compilers Central
References: 10-05-036
Keywords: C, design
Posted-Date: 09 May 2010 16:07:08 EDT

On May 8, 9:34 am, Andri Wagner <andre....@gmail.com> wrote:


> What I'm trying to say is: the compiler yields different assembly code
> for when 'x' is a lvalue and when 'x' is not a lvalue.
>
> This gets more confusing when I have expressions such as 'x++'. This
> is simple, since 'x' is obviously a lvalue in this case. In the case
> of the compiler, I can parse 'x' and see that the lookahead points to
> '++', so it's a lvalue.
>
> But what about '(x)++'? In this case, the compiler evaluates the
> subexpression '(x)', and this expression results the value of 'x', not
> the address. Now I have a '++' ahead, so how can I know the address of
> 'x' since all that I have is a value?


++ requires an object that an address can be taken of attached to
either the right or left which forms part of a larger expression.


so it's really


(object)++


could be, for instance


(*(ptr + a))++


For all it matters.


I guess it depends on how you wrote your parser, but basically when
you encounter ++ it must either be before or after an expression whose
address is computable.


> All documentation that I found about lvalues were too vague, and
> directed to the programmer, and not to the compiler writer. Are there
> any specific rules for determining if the result of a expression is a
> lvalue?


Read the BNF grammar for C. The full BNF form is in appendix A32 of K&R
C 2nd edition. Page 238 describes how to look at both post and prefix
expressions.


BTW I don't claim to be a compiler theory expert so that's about all
the help you're gonna get from me :-)


Tom


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.