CS112_38_slides-expressions

CS112_38_slides-expressions - Expression trees Yinglin Wang...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
CS112 Fundamentals of Programming Abstractions Expression trees Yinglin Wang CS Dept., SJTU CS112 Fundamentals of Programming Abstractions Expression Trees We will now be turning our attention to trees that represent expressions of the type we see in programming languages. Our goal is to gain an understanding of how languages are implemented, in preparation for assignment #6. Expression Trees Languages ----------------- Parsing Interpreters
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CS112 Fundamentals of Programming Abstractions Compilers A compiler translates one language into another. The target language is the low level "machine language" of the particular processor being used. The translated program is stored as a separate file called an executable, so that it can be run later. After compiling the program once, we can run it as many times as we like without recompiling because the program is already saved in its executable format. A compiler is a translator . The multiplication and addition don't take place at compile time. They take place at run time. CS112 Fundamentals of Programming Abstractions Interpreters An interpreter analyzes one statement at a time, and carries out the necessary actions immediately. If the program returns to the same line again, or if it is run again later, the analysis is repeated. Behind the scenes, the interpreter is doing something like this. ans = 3 * (6 + 5) y = ans + 1 The job of an interpreter is to evaluate expressions "on the spot", that is, immediately after determining the meaning. Enter(varTable, "ans", Multiply(3, Add(6, 5))) Enter(varTable, "y", Add(Lookup(varTable, "ans"), 1))
Background image of page 2
CS112 Fundamentals of Programming Abstractions interpreters This is the simple interpreter discussed in the text. It reads in expressions and valuates them. One of the operators it handles is the assignment operator. CS112 Fundamentals of Programming Abstractions Interpreters The job of the interpreter is to read a command (an expression in the case above), evaluate it, print the result, and repeat the process. Compare that with the job of a compiler.
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CS112 Fundamentals of Programming Abstractions Interpreting/Compiling 1. Lexical analysis (divide the line into tokens) 2. Parse the line (analyze the structure of the expression)
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 14

CS112_38_slides-expressions - Expression trees Yinglin Wang...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online