Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ primary expressions - Is it primary expression or not?

  1. Why are they called "primary"? In the order of evaluence they are the first?

  2. C++03 standard defines the expression in chapter 5 (Note 1):

    An expression is a sequence of operators and operands that specifies a computation.

    Then the 5.1 "Primary expressions" defines the list of primary expressions:

    (1) primary-expression:

    literal
    
    this
    
    ( expression )
    
    id-expression
    

    My main question is in connection the third point:

    ( expression )
    

    So, according to the standard, every expression with brackets are primary expressions and they are calculated firstly. It looks logical, and gives an exact explanation of the behavior of brackets in C++ expressions (precedence).

    So this means that for example

    (variable + 10)
    

    is a primary expression.

    var = (variable + 10) * 3
    

    and according to my theory, it looks logic, BUT from other sources I know

    (variable + 10)
    

    is NOT a primary expression, but WHY? I don't understand, however the standard defines the (expression) as a primary expression.

Please, help me because I can't. Thank you very much, and sorry for my bad English. Hi.

like image 468
user2148758 Avatar asked Jun 23 '13 09:06

user2148758


People also ask

What is a primary expression in C?

Primary expressions are the building blocks of more complex expressions. They may be constants, identifiers, a Generic selection, or an expression in parentheses.

What does primary expression mean?

Primary expressions are the building blocks of more complex expressions. They may be literals, names, and names qualified by the scope-resolution operator ( :: ). A primary expression may have any of the following forms: primary-expression. literal.

What is primary expression error in C++?

The “expected primary expression before int” error means that you are trying to declare a variable of int data type in the wrong location. It mostly happens when you forget to terminate the previous statement and proceed with declaring another variable.

What is primary expression in Java?

Primary expressions include most of the simplest kinds of expressions, from which all others are constructed: literals, class literals, field accesses, method invocations, and array accesses. A parenthesized expression is also treated syntactically as a primary expression.

What are primary expressions in C?

Primary expressions are the building blocks of more complex expressions. They may be constants, identifiers, a Generic selection, or an expression in parentheses. Syntax. primary-expression: identifier constant string-literal (expression) generic-selection. expression: assignment-expression

What is a primary expression in JavaScript?

Thank you. Primary expressions are the building blocks of more complex expressions. They may be literals, names, and names qualified by the scope-resolution operator ( :: ). A primary expression may have any of the following forms: A literal is a constant primary expression.

What is a primary-expression?

The term primary-expression is an element of the language grammar. They could equally have been called foobar-expression , it's just a name that doesn't have any deeper meaning. A primary-expression is not necessarily atomic, evaluated first, top-level, more important than other expressions , or anything like that .

What is the difference between primary and postfix expressions?

Primary expressions − It is an operand which can be a name, a constant or any parenthesized expression. Example − c = a+ (5*b); Postfix expressions − In a postfix expression, the operator will be after the operand. Example − ab+


2 Answers

C++ expressions can be complex, which is to say they can be made up of nested expressions, combined through the use of operators, and those nested expressions may in turn be complex.

If you decompose a complex expression into ever smaller units, at some point you'll be left with units that are atomic in the sense that they cannot be decomposed further. Those are primary expressions; they include identifiers, literals, the keyword this, and lambda expressions.

However, it is true that there is one non-atomic construct that the C++ Standard defines as primary: Expressions enclosed in round brackets (aka parentheses). So the (variable + 10) example you give is a primary expression (and so are the sub-expressions variable (which is an identifier), and 10 (which is a literal).

I believe the Standard lists them as primary expressions because they play the some role as truly atomic expressions when it comes to the order of evaluation: Anything within the brackets must be evaluated before the value of the backeted expressions can enter into evaluations with other expressions: In (5+10)*a, the value of 5+10 must be evaluated before it can enter into the evaluation of *a. [Note that this does not mean 5+10 is evaluated before the expression a is evaluated. It only means that 5+10 must be evaluated before the multiplication itself can be evaluated.]

So, bracketed sub-expressions, in this sense, act as if they were atomic.

And I guess this is why the Standard doesn't use the term "atomic expressions" for this concept. They act as if they were atomic, but at least the bracketed variety is not actually atomic. "Primary" seems, to me, to be a good choice of words.

like image 135
jogojapan Avatar answered Oct 03 '22 14:10

jogojapan


The term primary-expression is an element of the language grammar. They could equally have been called foobar-expression , it's just a name that doesn't have any deeper meaning.

A primary-expression is not necessarily atomic, evaluated first, top-level, more important than other expressions , or anything like that . And all expressions are "building blocks" because any expression can be added to to form a larger expression.

The definition was already given in the question so I won't repeat it here.

(variable + 10) is a primary-expression, your "other sources" are wrong. Here is another example of primary-expression:

([]{
int n, t1 = 0, t2 = 1, nextTerm = 0;

cout << "Enter the number of terms: ";
cin >> n;

cout << "Fibonacci Series: ";

for (int i = 1; i <= n; ++i)
{
    // Prints the first two terms.
    if(i == 1)
    {
        cout << " " << t1;
        continue;
    }
    if(i == 2)
    {
        cout << t2 << " ";
        continue;
    }
    nextTerm = t1 + t2;
    t1 = t2;
    t2 = nextTerm;
    
    cout << nextTerm << " ";
}
return 0;  
}())

(this calls a lambda function and parenthesizes the result; code borrowed from : here).


Also, the question flirts with a common misconception about precedence and order of evaluation. The standard doesn't mention "precedence" at all. A precedence table is a way of presenting the rules of the the language grammar in a way that is easier to read.

It refers to the way that operands are grouped with operators, not the order in which subexpressions are executed. In the case of f() + ([]{int n, t1 = 0, t2 = 1, nextTerm = 0; cout << "Enter the number of terms: ";.... etc. etc. , the f() may or may not be called before the lambda is called. The parentheses around the lambda do not cause it to be evaluated first.

like image 20
M.M Avatar answered Oct 03 '22 14:10

M.M