Expression of personality, factor and term?

I study context-free grammar, and I don’t understand how to express an expression, factor, or term in a programming language such as C or C ++.

Suppose we have an assignment operator, id: = E , where E is any arithmetic expression.

What is a term? What is an expression? and what is the factor in the actual part of the code?

We can have

int i = 3, j = 14 int i = 3 + j * 14; 

Thank you very much.

+4
source share
3 answers

The concepts of "factor", "term" and "expression" come from mathematics and are not related to programming languages.

Factors are what you multiply:

 1*2*(3+4) 

Conditions are the things you add:

 1 + 2 + (3*4) 

And expressions for the whole result

 1 + 3 * 7 

In context-sensitive parsing, you use these differences to prioritize between statements. Thus, an expression is expressed in the sum of terms, and the term consists of a product of factors, and the factor is either a number or a subexpression in parentheses.

+12
source

Your homework probably also has a grammar specification for a (subset) of a programming language such as C or C ++, something like:

Programs> Expression | Definition | Declaration
Expression-> Expression + Term
Expression-> Expression - Term
Expression-> Expression * Factor
...
...
...
etc.

Then 3 + j * 14 is an expression, 3 is a member (everything related to a + is an expression or term according to the above grammar) j and 14 are factors Note that the above grammar is a very crude imitation of The grammar of a real programming language may look.

+1
source

So, suppose we have such a grammar:

Program β†’ (Definitions | lambda)
Definitions-> Definitions Definitions

Definitions-> "int" Definition ";" | "int" Definition, Definition ";"
Definition β†’ Name "=" Expression

Expression-> Term "+" Expression
Expression-> Expression "-" Term
Expression-> Expression "*" Factor
Deadline β†’ "3" | "fourteen"
Factor β†’ "3" | "fourteen"
Expression β†’ "3" | "fourteen"

Note that my trailing characters are enclosed in quotation marks, and I omit the part where Name is defined as a combination of letters and numbers and underscores starting with a letter or underscore :)

So in your example:

Line 1 int i = 3, j = 14;
Line 2 int i = 3 + j * 14;

i and j are Names. 3, 14 (line 1) and 3 + j * 14 (line 2) are expressions. Then, in line 2, 3 is the term, j * 14 is an expression, j is a factor and 14 is a factor :)

+1
source

Source: https://habr.com/ru/post/1380259/


All Articles