Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Identity expression, factor, and term?

I am learning context-free grammar, and I don't understand how to identity expression, factor and term in a programming language like C or C++.

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

What is a term? What is an expression? and What is a factor in an actual piece of code?

We can have

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

Thank you very much.

like image 595
CppLearner Avatar asked Nov 08 '11 18:11

CppLearner


1 Answers

The "factor", "term" and "expression" concepts come from math and don't really have to do with programming languages.

Factors are things you multiply:

1*2*(3+4)

Terms are things you add:

1 + 2 + (3*4)

And expressions are for the whole result

1 + 3 * 7

In context-free language parsing you use these distinctions to organize the precedences between operators. So an expression is made of a sum of terms and a term is made of a product of factors and a factor is either a number or a parenthesized subexpression.

like image 54
hugomg Avatar answered Oct 04 '22 01:10

hugomg