CSE 428: Lecture notes 4.1
The role of parse trees in the implementation of programming languages
Parse-trees are are used as an internal representation of the source program
by the interpreter or the compiler. In very schematic terms, we can represent
the various phases of the implementation as follows
Interpreter case
_________ ________ __________ _____________
| | | | | | | |
source ->| scanner |-->| parser |- parse ->| static |-->| interpreter |-> results
|_________| |________| tree | analyzer | |_____________|
|__________| ^
|
data
Compiler case
_________ ________ __________ __________ _________
| | | | | | | | | |
source ->| scanner |-->| parser |- parse ->| static |-->| compiler |-> compiled ->| machine |-> results
|_________| |________| tree | analyzer | |__________| code |_________|
|__________| ^
|
data
-
Scanner: takes the source file (sequence of keyboard characters) and generates
the sequence of tokens
-
Parser: checks the syntactical correctness and generates the parse-tree
-
Static analyzer: check the static correctness and possibly makes some optimizations.
The optimization can also be a separate phase.
The static analyzer might enrich the parse tree with some information,
typically informations about the type of the identifiers.
Actually, real implementations are often a combination of compilation and
interpretation: the code gets compiled into an intermediate language (something
in between the source langauge and the machine language) and then interpreted.