views:

128

answers:

7

I want to know how the operators work internally in computer?

And There are many operators that computer use for performing many tasks like "+" is used for adding two numbers as well as concatenate two string in java.. so, how does it differentiate these type of tasks?

+1  A: 

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

There are also languages where this is decided when the program is run. In some cases it has to be done a run-time, since the compiler won't have enough information at compile time to decide what this "+" operator is supposed to do.

Thomas Padron-McCarthy
A: 

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

gregor
A: 

The syntax of computer languages is carefully designed so that the compiler/interpreter can decide which use of an operator is meant by the code.

By definition, properly written syntactic code will never be ambiguous.

When the compiler says 'syntax error' it's telling you that the code is ambiguous.

pavium
Well, a syntax error does not necessarily mean ambiguous code.
Thomas Padron-McCarthy
But ambiguous code probably would result in a syntax error.
pavium
A: 

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

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

The rest is syntactical sugar.

Did you ever have an HP calculator with postfix notation? Think of it in these terms. So the compiler will translate a+b to (+ a b) or object oriented to a.+(b).

The compiler derives from the two operands the type of the operation (addition, concatination), proofs type compatibility (add integer to float ok, concat integer and string depends on the language).

It then converts the code to the internal representation and to machine code.

Ralph Rickenbach
+3  A: 

What you're really asking is "how do compilers work?" - operators are one kind of token a compiler has to parse, and not fundamentally different from other kinds of tokens like methods - in fact, in languages that allow you to define your own operators, the definition usually looks like a method definition.

The only difference is that operators can have an infix syntax and require precedence rules - but that just means that the parsing 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.

Michael Borgwardt
A: 

It differentiates the tasks based on (get this) what operator you use. When the code is parsed, the parser/compiler finds the operators and decides what to do with the things around it. If + is used for both addition and concatenation, it would depend on the arguments supplied to it. The language decides what to do when it's a mix, for example if, "4" + 5 equals "45" or 9.

Additionally, operators have precedence. That is, certain operators are evaluated sooner than others. Sort of like the order of operations you learn in math. For example, the + and - operators typically have a lower precedence than the * and / (multiplication and division) operators (that is, multiplication operations will happen before addition operations). This is why 4 * 5 + 2 will equal 22 instead of 40. You can find a table of operator precedence here. This is for the C language, but it should be similar for most other languages.

There is also operator associativity. This is (basically) the order which operators of the same type are evaluated. Most of the ones you are used to will be left associative. For example:

4 * 5 * 6 * 7

this is left associative, meaning this:

((4 * 5) * 6) * 7

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

 4 ^ 5 ^ 6 ^ 7

This becomes

 4 ^ (5 ^ (6 ^ 7))

These get evaluated from right to left. If the exponent operator were left associative, this would produce the wrong answer.

Think of the associativity as "what side the brackets go on." they're on the left for left associative, and on the right for right associative.

That same link includes the associativity for each operator. Again, it's for the C language, but it should be almost identical for most other languages you will use.

Carson Myers
A: 

A string containing operators like "a+b*(c-d)" is an expression. The compiler parses the expression and creates an intermediate structure called an abstract syntax string:

          +
         / \
        a   *
           / \
          b   +
             / \
            c   d

With help of The syntax tree the expression is evaluated. The types of the leaves (a,b,c,d) dictate which operation is needed. This information is used to evalate the expression (for an interpretor) or create the 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

Sometimes implicit operations are added, like type conversions. And most compilers add some optimizations.

Gamecat