3.8 Program Construction Flashcards
a
Translators
convert one language to another, usually high level to machine code (e.g compilers, interpreters)
Assemblers
Translates assembly code into machine code, 1:1 relationship so relatively simple translation process and faster execution due to this close relationship. Comments get removed and symbolic references get replaced for addresses Non portbale (assembly is artichtecture specific, like interpreters)
Compiler
Translates code into executeable form but does not execute the program. Takes a while to compile however faster execution as its precompiled. If a syntax error occurs the the program halts and the error is reported. If its a semantic error then it waits until the end to produce an error message. Produces in the form of an excurtable file so its easy to transfer, also in unredable format so secure. Compillers produce machine code specific to computer architecture.
Interpreters
Translates and execute source code line by line. Uses bytecode to** speed up translation process** . Runtime errors crash the program, synatx makes it halt and report the error. Its easier debugging since errors are reported as they occur.
Lexical Analysis
- Whitespaces and comments are removed
- Tokenisation occurs which converst part of code into tokens
- A symbol table is created to keep track of identfiers
Syntax Analysis
- Tokens are identified to see if they match the syntax of the language used and are in the correct order. BNF would be used to define grammatical rules. If an error is found, program is halted and error is reported.
Semantic Analysis
Compiler checks meaning of the program
Type Checking - using incorrect data types (e.g int age = “hello”)
Scope resolutions - checke to see if global/local variables used correctly
Logic Error - cheks that varaibles are intiallised, for infinite loops and unreachable code.
For semantic errors, the code continues and error are displayed at the end.
Code Generation
Object code is created - Executable file
Code Optimisation
Idenitfy redudundant code and rectify it.