Back 

Translation questions and answers

Questions

Q1. Why do programs that people write need to be translated into object code?
Q2. What are the three main steps in translating some source code?
Q3. Describe what happens in the lexical analysis stage of translation.
Q4. What is meant by the ‘syntax’ of a program instruction?
Q5. Describe what happens in the syntax analysis stage of translation.
Q6. Why does the code generated during translation need to be optimised?
Q7. Show how the reverse Polish notation AB+CD*+ is calculated using the stack.
Q8. What is a linker program used for?
Q9. What does a loader program do?
Q10. What is meant by ‘relative addressing’? 

Answers

Q1. CPUs do not understand keywords and mnemonics. They only understand 1s and 0s. Therefore any source code must be translated into object code before it can be run.
Q2. The main steps in translation are lexical analysis, syntax analysis, code optimisation and code generation.
Q3. In lexical analysis, the source code is looked at. Any unnecessary parts of it are removed. This would include comments and spaces. Keywords and symbols are then replaced with ‘tokens’. A token is a generic description of the type of symbol. For example, if you had a constant called 'months_in_year', this would be replaced by the token CONSTANT. If you had variables called 'speed' and 'distance' then these would be replaced with the token VARIABLE. If you had a keyword 'IF', this would get replaced with the selection token for 'IF'. If you had a maths operator such as ‘>’, this would get replaced with the token OPERATOR. By replacing symbols, variables, keywords and so on with generic tokens, you can turn a program into a set of 'patterns' of instructions. It is then a relatively easy job for the compiler in the syntax stage to check each pattern against the allowable ones. Tokens replace a keyword like IF, FOR, PRINT, THEN and so on, a symbol that has got a fixed meaning, such as +, *, numeric constants and user-defined variable names.
Q4. The syntax of an instruction refers to the rules for the construction and use of that instruction.
Q5. Following lexical analysis, the syntax of the program is checked. The first job is to take the long stream of tokens from the lexical analysis stage and to split them up into phrases. Each phrase can then be checked against the rules of the programming language (the syntax of the language).
Q6. When code is generated, it may not result in the most efficient code that could be generated. For example, code may be generated like X = Y + 0, which would run more efficiently if it were just X = Y (because adding 0 to Y won’t make any difference to X). The code must be optimised! When the compiler has generated the object code, it runs routines, which do just this. It tries to make the code as fast and as efficient as possible by finding and removing unnecessary code.
Q7.


Q8. A linker is a program that allows a user to link library programs or separate modules of code into their own programs.
Q9. A loader is a piece of software, which decides exactly where to put object code in RAM, ready for it to be run.
Q10. In relative addressing, all data items and all references in the code are made relative to a base address.

Back