Program Construction Flashcards
What is an assembler
A simple translation program
Converts low level assembly language into machine code (binary)
What is a compiler
Convert high level language (source code) into machine code (object code)
What is an interpreter
Runs a high level language program without converting it into a binary executable
Takes a line of the high level program (source code), translates it and runs it, then runs the next line etc.
Advantages of an assembler
Just as efficient as machine code
No extra unnecessary lines of code generated
Can be used for programs which require maximum efficiency/performance
Advantages of a compiler
Although compilation may take mins, it produces an executable that will be fast to run over and over again
Executable program can be given to the end user without them needing to have a compiler
Very difficult for the end user to edit the code as it is in binary format
Shows a list of errors in the program which must be corrected before compilation (can also be disadvantage)
Disadvantages of compiler
Compiling can be slow and tedious when developing a program (as each time program needs to be ran it has to be recompiled)
A compiler targets a particular platform and cpu instruction set e.g. programs compiled for android ARM processors won’t work on Intel x86 chips etc.
Commonly used for a number of languages e.g. python, javascript
Advantages of an interpreter
No delay in waiting for compilation, program can begin to run immediately (ideal for program development)
Should run on any computer as long as there is an interpreter available for the language
Disadvantages of an interpreter
Program will run much more slowly as each line of code needs to be translated each time before it can run
Needs an interpreter to run it
End user has access to source code (can be stolen)
Only finds errors when it gets to a line with an error in, so program could have errors thst are never discovered until experienced by the end user
What is the ideal solution for compiler and interpreter
Assuming both exist for the language
Use an interpreter during development phase
When fully worked and tested, compile it for distribution to end-user
What is intermediate code
Similar to machine code but not processor specific
Potentially allows code to be created to run on multiple architectures
Advantage of intermediate code
Code runs very quickly as the conversion from intermediate code is much more straightforward than from high-level language
What are the stages of compilation (compiler)
Source code -> lexical analysis -> syntax analysis -> semantic analysis -> code generation -> optimisation -> object code (machine code)
Lexical analysis - code generation stored in symbol table
What does lexical analysis entale
Removes all comments and spaces + unnecessary characters
Tokenisation - converts source code into tokens which refer to entries in the symbol table
Add entries to the symbol table for variables, procedure names etc.
Does some basic error checking (e.g. flag up error if variable name was too long)
By the end of Lexical Analysis the source code program has been turned into a set of tokens which relate to entries in symbol table.
What are the two tables for lexical analysis
Reserved word table
User identifier table
Look at examples
What does the syntax analysis stage entale
Checks that the structure of the program is correct, that it follows grammar rules of the language
does this using a meta language
at this stage a list of syntax errors could be produced and compilation may stop