Lecture 3 Flashcards
(42 cards)
Layered View of Computer
(inside )
Bare Machine.
Macro Instruction Interpreter.
Operating System.
Compiler level, such as C++, FORTRAN, Operating System Command Interpreter (CMD).
Then Virtual Compiler.
(outside)
Computer & Languages
Primitive Operations.
Processor circuits provide a realisation of a set of primitive operations, or machine instructions for arithmetic and logic
operations.
Computer & Languages
Macroinstructions
Some of machine instructions are called macroinstructions, because they are put together with a set of instructions of even lower level called microinstructions.
Computer & Languages
Set of instructions
Machine Language of a computer is its set of instructions. In the absence of any other supporting software, its own machine language is the only language a computer can understand.
A computer could be designed and built with particular high-level languages as its machine languages, but it would be very complex, expensive, difficult or impossible to use other high-level languages.
Computer and Languages Continued
On top of the machine language, is the Operating System.
Large Collection of programs that supplies higher-primitive than those of the machine language, e.g primitives for device and file system management, I/O operations, text/program editor and etc.
Rested on the operating system are various language
implementations.
* A particular language implementation and the operating system form
a virtual computer of that language, e.g., the operating system and C compiler provide a virtual C computer.
Language Implementation Methods
Compilation
Programs are translated into machine language, includes Just-In-Time systems.
Use: large scientific or commercial applications.
Language Implementation Methods
Pure Interpretation
Programs are interpreted by another program known as an interpreter.
Use: Small programs or when efficiency is not a big issue, web programs.
Language Implementation Methods
Hybrid Implementation Systems
A compromise between compilers and pure interpreters.
Use: Small and Medium systems when efficiency is not the first priority.
Compilation
Translates high-level program (source language) into machine code (machine language).
Slow translation, fast execution.
Compilation process
Lexical Analysis:
Converts characters in the source program into lexical units.
Syntax Analysis:
Transforms lexical units into parse trees which represent the syntactic structure of program.
Semantics Analysis:
Generate intermediate code.
Code Generation:
Machine code is generated.
Work is done by a compiler.
Compiler
Computer Program that translates a program in a source language into a equivalent program in a target language.
Compiler can give error messages.
Compiler Source Language
Source language is a high-level language. The target language is a low level language.
- A machine or assembly language.
- A language executable by a virtual machine.
Structure of a Compiler
- Source Program -
Lexical Analyser
Semantic Analyser
Code optimiser
Code generations
(all have a symbol table)
- Target Programs -
Structure of a compiler details
The source program is read by the first phase. (Lexical Analyser)
The target program is output by the final phase (code gen.)
Phases interact with a data structure called a symbol table.
Lexical Analysis
Reads the source program text ( one character at a time ) and returns sequence of tokens to send to next phase.
Lexical Analysis Tokens
Tokens are symbolic names for lexical elements of the source language, in a Pascal compiler.
- IF might be the token for the Paschal keyword if.
- IDENT stands for any identifier (e,g. foo, n12)
Each token is associated with a pattern
- IF is an associated with the pattern “i followed by f”
-IDENT is associated with the pattern “ a sequence of letters, digits and ‘_’ that begin with a letter.”
scanner matches patterns against sequences of input chars
Symbol Table
Is a data structure containing all identifiers (with attributes) of a source program.
Symbol Table Attributes
For variables, typical attributes include type size, scope.
For methods, procedures or functions:
- Number of arguments and their types and passing mechanisims.
- the return type (if any)
The symbol table provides access to identifier attributes throughout the compilation process.
Syntax Analysis (Parsing)
Syntax analyser (parser) analyses the syntactic structure of the source program.
Input to a parser is the sequence of output tokens from the lexical analyser.
Attempts to apply rules that define syntax of language on the sequence of tokens.
Syntax Analysis (Parsing) rules
Suppose that part of the source language is described by the rules:
<assign> -> <ident> := <exp>
<exp> -> <ident> | <ident> + <ident>
The lexical analyser, on reading the input fragment x: = y + z, returns the following token sequence:
IDENT(x) ASSIGN IDENT(y) PLUS IDENT(z)
</ident></ident></ident></exp></exp></ident></assign>
Parse Trees
The parser uses these rules to derive the sequence of tokens; such a derivation would relate to the parse trees on the left:
check slides!
Parser Abstract Syntax Trees
Represent the source program syntax, but is simpler that the corresponding parse tree.
Semantic Analysis
Checks whether the program is syntactically correct, but not that it is completely valid or semantically correct.
Semantic Analysis Expression example
Expression such as y + z is syntactically valid.
But if y is declared as a float, and z as a string, it is
semantically meaningless.
An example, a procedure call, such as doit (12, x, y, z) , is syntactically ok, but semantically meaningless if doit is declared to take 3 arguments.
Semantic analyser determines if the source program is semantically valid.