Chapter 16.2 Flashcards
"Translation Software"
interpreter
an interpreter examines source code one statement at a time
executes code line by line before producing a completely translated version of it.
also produces intermediate code but executes per line.
how does an interpreter execute a program
an interpreter examines and
checks each line for errors,
if no error is found the line is executed
if an error is found this is reported and the interpreter halts,
interpretation has to be repeated every time the program is run
interpretation is repeated for every iteration in loops
compiler
takes the whole code in as input and translates high-level language programs into object code. produces an executable file
has front-end analysis and back-end analysis stages.
assembler
translates assembly language into machine language
compilation stages
lexical analysis
syntax analysis
(intermediate) code generation
code optimisation
lexical analysis
first stage
converts a sequence of characters into a sequence of tokens
removes comments and whitespace
tokenises code (changes each line into a series of tokens) and categorizes lexemes (e.g. keywords and identifiers) by checking against a predefined set of rules
a symbol table (keeps track of variables and their meanings) and a keyword table is created (see respective pages) to create a sequence of tokens
symbol table
records the names and attributes of an identifier
identifier name, data type, role (variable, constant, procedure etc.)
identifier value or location
keyword table
used to recognize certain keywords in a language
- the reserved words used
- the operators used
- their matching tokens
syntax analysis
second stage
uses parsing algorithms to interpret the meaning of a sequence of tokens
- constructs parse tree
- check syntax or grammar through syntax diagram
- produce error report - if errors are found, each token is output but code generation is not run
syntax diagram
breaks down the grammar and syntax rules of a variable or constant
rectangle = variable/identifier
rectangle with repeated arrow going around = can be repeated as many times
circle = a value which cannot further be broken down
code generation
converts the intermediate code produced by previous stages into an executable form
code optimisation
produces an efficient object program, aims to use the minimum amount of resources
to have fewer instructions, occupy less space in memory, reduce execution time.
BNF notation
another way to represent syntax/grammar rules. a set of recursive rules
<> = surrounds each variable
(letters or numbers by themselves do NOT have <>)
::= means “is defined as”
|= separates individual options. “or”
Reverse Polish Notation (RPN)
a postfix notation which never requires brackets and has no rules of precedence
why is RPN used to carry out the evaluation of expressions?
RPN provides an unambiguous method of representing an expression,
reads from left to right,
without needing brackets,
without precedence rules.