0

What part of the compiler calculates arithmetic operations?

EX-

int x = 5;
int y = 4;
print(x+y); 

Part of calculates print function (Lexical,syntax ,Semantics)

Im trying compiler

rici
  • 234,347
  • 28
  • 237
  • 341
  • 3
    Computing constant expressions at compile time is called "constant folding". Figuring out what the value of a non-constant variable might be is called "symbolic execution". But if I were you, I'd start simple, without trying to do advanced techniques. – rici Apr 25 '20 at 20:22
  • Thank you rici.What advice would you give progress order.Actually it s my midterm project.First I did lexer and now I am working on parsing.... – Murat Yetkin Aslan Apr 25 '20 at 22:10
  • It’s hard to answer this question because there are two ways this could play out. First, the compiler itself could, as @rici noted, recognize that the expression is a constant, evaluate it, and get 9. This could in principle be done during parsing or semantic analysis, but would likely be in a later optimization pass. Second, the compiler could just generate code that evaluates the expression, but then that’s in code generation. Is there more context to this question? – templatetypedef Apr 25 '20 at 23:55
  • As i said this is my midterm project.Professor wants simple programming language who doing basic arithmetic operations.I did lexer and now workin on parser.But Arithmetic operations stuck in my mind. How can i calculate arithmetic operation and how can i show result of this in result of compiler? – Murat Yetkin Aslan Apr 26 '20 at 08:24
  • If it's only a simple programming language then the compiler probably doesn't calculate any arithmetic expressions, it just generates target machine code (or bytecode) that does the evaluation, like: (pseudo-bytecode) `load.var x`, `load.var y`, `add` – Ghost4Man Apr 26 '20 at 10:51

0 Answers0