How do operators work?

I want to know how operators work inside a computer?

And there are many operators who use a computer to perform many tasks, such as "+", to add two numbers, and also to concatenate two lines in java .. so how does it distinguish these types of tasks?

+4
source share
7 answers

What do you really ask: "how do compilers work?" - operators - this is one type of token that the compiler should analyze, and is not fundamentally different from other types of tokens, similar to methods - in fact, in languages ​​that allow you to define your own operators, the definition usually looks like the definition of a method.

The only difference is that operators can have infix syntax and require priority rules, but it just means that the analysis rules are a bit more complicated than for other tokens. In fact, this last link may be the most direct and specific answer to your question.

+3
source

The compiler knows the data type of the operands. If he sees an expression such as 2+3 , he will determine that both operands (i.e. 2 and 3 ) are integers, so the compiler will generate executable code that does the integer addition. If the expression "Hey, " + "Bob!" , the compiler sees that the operands are strings, and if the language supports adding strings (for example, in Java), it will generate code that performs string concatenation.

There are also languages ​​where this is determined when the program starts. In some cases, this must be done at run time, because the compiler will not have enough information at compile time to decide what this + operator should do.

+1
source

When using language parsers, the source code is converted to compiled code or internal representation, which is then executed.

0
source

The syntax of computer languages ​​is carefully designed so that the compiler / interpreter can decide what use of the operator code means.

By definition, correctly written syntax code will never be ambiguous.

When the compiler says “syntax error”, it tells you that the code is ambiguous.

0
source

Operators work as functions with parameters. Or, in modern object-oriented languages, for example, methods on an object with a parameter. You can see it loudly if you look at the method overload methods in C ++, for example. So the numerical + operator - in the form of pseudocode - is something like this:

 function integer.+(b: integer) { return ADD self, b; } 

The rest is syntactic sugar.

Have you ever had an HP calculator with Postfix notifications? Think about it in these terms. Thus, the compiler converts a + b to (+ ab) or an object oriented to. + (B).

The compiler deduces from the two operands the type of operation (addition, concatenation), compatibility of evidence types (adding an integer to float ok, concat integer and string depends on the language).

It then converts the code into an internal representation and into machine code.

0
source

It distinguishes tasks based on (get) which operator you use. When the code is parsed, the parser / compiler finds the operators and decides what to do with things around it. If + is used for both addition and concatenation, it will depend on the arguments provided to it. The language decides what to do when it is mixed, for example, if "4" + 5 is equal to "45" or 9.

In addition, operators have priority. That is, some operators are evaluated before others. Similar to the order of operations you study in math. For example, the operators + and - usually have a lower priority than the operators * and / (multiplication and division) (that is, the operations of multiplication will be performed before the operations of addition). That is why 4 * 5 + 2 will be 22 instead of 28. You can find the operator priority table here. This is for the C language, but it should be similar for most other languages.

There is also associativity of operators. This is (basically) the order in which operators of the same type are evaluated. Most of the ones you're used to will remain associative. For instance:

  4 * 5 * 6 * 7 

it is left associative, which means the following:

  ((4 * 5) * 6) * 7 

if there are two or more operators with the same priority "in the line" (as in the example), then they will be evaluated from left to right. That is, first 4 * 5, then 20 * 6, then 120 * 7. An example of the correct associative operator is the exponent operator:

  4 ^ 5 ^ 6 ^ 7 

It is getting

  4 ^ (5 ^ (6 ^ 7)) 

They are evaluated from right to left. If the exponent operator remained associative, this would give the wrong answer.

Recall associativity as "which side the brackets continue." they are left for the left associative and right for the right associative.

That same link includes associativity for each operator. Again, this is for the C language, but it should be nearly identical for most other languages ​​that you will use.

0
source

A string containing operators such as "a + b * (cd)" is an expression. The compiler parses the expression and creates an intermediate structure called an abstract syntax string:

  + / \ a * / \ b + / \ cd 

Using a syntax tree, an expression is evaluated. Leaf types (a, b, c, d) determine which operation is needed. This information is used to evaluate the expression (for the interpreter) or to generate machine code:

 get c ; get variable c in a register add d ; add variable d mul b ; multiply with variable b add a ; add variable a store restult ; store the restult 

Implicit operations, such as type conversions, are sometimes added. And most compilers add some optimizations.

0
source

All Articles