How do people that write compilers call it? I know that int a, b, c;
is called an expression. But things like this: int a = 2
? or a++
, a=c
etc I don't have any idea. And where do I find these terms? C standard or buy a book about compiler construction? I hope this is clear. Thanks in advance.
-
1Isn't `int a, b, c;` a declaration? – K. Brafford Aug 12 '12 at 19:28
1 Answers
int a, b, c;
This is not an expression. This is a declaration.
int a = 2;
This is also a declaration. The 2
part is the initializer.
a ++
This is a "post-increment" expression. Note that outside of the C world this could be just a statement.
a = c
This is an assignment expression (which again, outside of C, this can be a statement).
The exact definition of these terms and categorization of each syntax for C can be found in the C standard (Chapter 6: Language), which you can find download/purchase information from Where do I find the current C or C++ standard documents?.
Note that these are only true for C (and most of its derivatives, e.g. C++, Javascript, etc.). These concepts may not be meaningful in other languages (e.g. some language doesn't distinguish between statements and expressions).
-
-
2
-
"post-increament" <-- typo. And `a = c;` is a statement; `a = c` is expression. I have a bit of doubt they can be called otherwise. – nhahtdh Aug 12 '12 at 19:31
-
@cnicutar - it was when I wrote a compiler in grad school. But, I just read that changed with newer versions of C... so I guess the answer is "depends". – Hogan Aug 12 '12 at 19:32
-
And an operation that ends with a `,` independent if e.g, is assignment(a=0), pos-increment(a++), (a | x) etc is there a name? – Jack Aug 12 '12 at 19:36
-
@nhahtdh: Thanks. Fixed the type. Since OP doesn't have the `;` I reordered it to say they are expressions in C. – kennytm Aug 12 '12 at 19:39
-
@Jack: If you can read BNF form, it would help you understand why `a = c` is called an expression and `a = c;` is called a statement: http://www.cs.man.ac.uk/~pjj/bnf/c_syntax.bnf . About this `a = 0, a++, a | x`, it is called an expression, consists of other expressions, comma-separated. – nhahtdh Aug 12 '12 at 19:41
-
@tuğrulbüyükışık: `new` is not part of C syntax, or at least in the link in my comment above. – nhahtdh Aug 12 '12 at 19:42
-
-
-
-
I don't want to sound like an ass, but people who really write (new /fresh) compilers do not care what you call these _expressions_. It is all about set of inputs that can be parsed as a regular expression/transition graphs. http://en.wikipedia.org/wiki/Automata_theory http://en.wikipedia.org/wiki/Programming_language_theory http://people.cs.uchicago.edu/~blume/classes/aut2008/proglang/text/offline.pdf – Abhinav Aug 12 '12 at 20:16
-
@KennyTM, no in C prefix and postfix operators result in plain values, not in lvalues. You must be mixing it up with C++. – Jens Gustedt Aug 12 '12 at 20:45
-
1@Abhinav Regular expressions occur in the lexer, if at all. Parsing deals with CFLs. The terms 'regular expression' and 'transition graph' do not appear in either of your citations. And of course compiler writers care what they are called, as they have to name productions after them. – user207421 Aug 13 '12 at 00:55