Translation Software
COMPUTER SCIENCE 9618 PAPER 3
Translation Software
Stages In The Compilation Of A Program
Lexical Analysis
Syntax Analysis
Code Generation
Optimization
Stage 1 : Lexical Analysis
Unnecessary Characters ( White Space, Comments are
removed)
Source Program is converted to tokens
Tokenization
Tokens : Any variable, consonant,
keyword, symbol, FOR, Count, Print, =, <-Total Number Of Tokens Are : 12
Keyword Table
Reserved word ( FOR TO PRINT)
Operators
Their matching tokens
Symbol Table
Identifiers (Variable,
Consonant) name used
The data type
role ( Variable,
Consonant, array,
procedure )
Value / Location
A Keyword Table is a predefined list of reserved words in a
programming language, such as if, while, and int, used by the compiler
to recognize language constructs. The Symbol Table, on the other
hand, stores information about user-defined symbols like variables
and functions, including their data types, scope, and memory
locations.
Exam Style Question
Question : How the contents of the keyword and symbol
tables are used to translate the source code program ?
Keywords/operators are looked up (in the keyword table)
Keywords/operators are represented by the tokens
Identifiers are looked up in (the symbol table)
Identifiers are converted to locations/addresses
Used to create a sequence of tokens (for the program)
Question : Additional tasks completed at the lexical analysis
stage that does not involve the use of a keyword or a symbol
table:
White space removed
Redundant characters are removed
Removal of comments
Stage 2 : Syntax Analysis
Constructing Parse Tree
Checking the table of tokens to ensure that the rules /
syntax / grammar of the language are obeyed
Producing an error report
The process of working out if a given statement is valid
according to the given BNF production rules is called parsing
The grammatical rules or syntax for a programming language
need to be set out clearly so a programmer can write code
that obeys the rules, and a compiler can be built to check
that a program obeys these rules.
The rules can be shown graphically in a SYNTAX DIAGRAM or
using a meta language such as Backus-Naur Form (BNF)
notation.
Question : Outline the purpose of syntax analysis?
It checks that the code matches the grammar of the
language
Syntax errors are reported
A parse tree is produced.
Backus Naur Form
Is a mathematical way of defining the syntax
is defined by
LHS ::= RHS
note : |
represents OR
<DIGIT> ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
<Number> ::= <DIGIT><DIGIT>
<Number> ::= <DIGIT> | <DIGIT><Number>
<Letter> ::= A | B | C | D | E | F | G | H | I | J | K ...
A word consists of two letters
<word> ::= <Letter><Letter>
A word consists of any number of letters
<word> ::= <Letter> | <Letter><word>
A digit could be 1 2 3 4 5
<Digit> ::= 0 | 1 | 2 | 3 | 4 | 5
Operator could be + - / *
<operator> ::= + | - | / | *
Letter could be A B C D E F
<Letter> ::= A | B | C | D | E | F
Variable could be a single letter or letter followed
by a single digit.
<Variable> ::= <Letter> | <Letter><Digit>
Assignment Statement
Number = N1 + N2;
<AssignmentStatement> ::= <Variable> = <Variable><operator><Variable> ;
Syntax Diagram
<DIGIT> ::= 1
1
<DIGIT> ::= 1 | 2 | 3 | 4 | 5
AssignmentStatement : : = <variable> = <variable><operator> <variable> ;
<word> : : = <Letter> | <Letter> <word>
<word> : : = <Letter> | <Letter> <digit>
35 is not a variable
:= is not an operator
9 is not a digit
== | > | <
<Digit> <Digit>
<Letter> | <Letter><Variable>
<Variable><Operator><number> |
<Variable> <Operator> <Variable>
Stage 3 : Code Generation
The code generation stage produces an object code.
The program should have correct syntax.
The object code is in machine-readable form.
Stage 4 : Code Optimization
Why code Optimization is necessary ?
Optimization means that the code will have fewer
instructions.
Optimized code occupies less space in memory.
Fewer instructions reduce the execution time of the
program
Code Optimization
LDD 234
ADD 235
ADD 236
STO 233
Benefits of optimizing the code
Code has fewer instructions/occupies less space in
memory.
Time taken to execute the whole program decreases.
Reverse Polish Notation
Is a method of representing an arithmetic expression
Infix Form : A - B
Reverse Polish Notation : AB-
Case 1 : Using RPN to evaluate the expression using stacks
M = 10
P=4
Q=2
R=1
2
2
10
5
1
4
4
6
6
5
5
5
5
5
5
25
Note : Always Solve Left to Right Whenever operator enters
the stack, use the operator on the two values at the top.
3
2
2
6
30
30
30
30
5
24
24
24
24
24
2
8
5
5
3
8
8
8
24
24
19
Case 2 : Infix To RPN Conversion
Note : Always use Bodmas rule and left to right is the way
you are suppose to convert
1) A + B
A B +
2) A - B * C
A-BC*
ABC*-
3) ( P + Q ) + ( R - P )
PQ+ + RPPQ+ RP- +
4) ( a - b ) * ( a + c ) / 7
ab - * ac+ / 7
ab - ac + * / 7
ab - ac + * 7 /
5) P + Q - R / S
P + Q - RS/
PQ+ - RS /
PQ + RS / -
6) - A / B * 4 / ( C - D )
A - / B * 4 / CDA- / B * 4 / CDA-B/ * 4 / CDA-B/4* / CDA-B/4*CD-/
Case 3 : RPN to Infix Conversion
Note : Use stack concept to solve question try to imagine
stacks and the working done in case 2 and always start from
left to right
1) A B A-B
B
A
2) PQ + M * RP - P + Q M * RP -(P+Q) * M RP -(P + Q) * M R-P ((P + Q) * M ) - (R-P)
3) x w z + y - *
x (w + z) y - *
x (w + z - y) *
x * (w + z - y)
4) b a * c d a + + (b * a ) c d a + + (b * a ) c (d + a) + (b * a) (c + d + a) ( b * a ) - (c + d + a)
Case 4 : Theoretical Questions
Question : Explain how RPN is used by an interpreter to
evaluate expression ?
Expressions are always evaluated left to right
Each operator uses the two previous values on the stack
(except unary minus)
If element is a number, push that number onto the stack
If element is an operator, then pop the first two numbers
from the stack
Perform that operation on those numbers
Push the result back onto the stack
End once the last item in the expression has been dealt
with.
Question : Advantages of using RPN for the evaluation of an
expression ?
No need for rules of precedence (BODMAS)
No need for brackets
In RPN evaluation of operators is always left to right
Question : Identify with reasons, a data structure that could be
used to evaluate an expression in RPN ?
STRUCTURE: Stack
Reason: Two operands are popped from the stack in the
reverse order to how they were pushed.
Question : Explain why Reverse Polish Notation (RPN) is used
to carry out the evaluation of expression ?
Reverse Polish Notation provides a clear method of
representing an expression
reading from left to right
without the need to use brackets
with no need for rules of precedence / BODMAS
Translation Software
Question 1
Sir Taha Ali
Papersdock
+92 318 2248934
Question 2
Sir Taha Ali
Papersdock
+92 318 2248934
Sir Taha Ali
Papersdock
+92 318 2248934
Sir Taha Ali
Papersdock
+92 318 2248934
Question 3
Sir Taha Ali
Papersdock
+92 318 2248934
Question 4
Sir Taha Ali
Papersdock
+92 318 2248934
Sir Taha Ali
Papersdock
+92 318 2248934
Question 5
Sir Taha Ali
Papersdock
+92 318 2248934
Question 6
Sir Taha Ali
Papersdock
+92 318 2248934
Sir Taha Ali
Papersdock
+92 318 2248934
Sir Taha Ali
Papersdock
+92 318 2248934
Question 7
Sir Taha Ali
Papersdock
+92 318 2248934
Sir Taha Ali
Papersdock
+92 318 2248934
Question 8
Sir Taha Ali
Papersdock
+92 318 2248934
Sir Taha Ali
Papersdock
+92 318 2248934
Sir Taha Ali
Papersdock
+92 318 2248934
Question 9
Sir Taha Ali
Papersdock
+92 318 2248934
Sir Taha Ali
Papersdock
+92 318 2248934
Sir Taha Ali
Papersdock
+92 318 2248934
Question 10
Sir Taha Ali
Papersdock
+92 318 2248934
Sir Taha Ali
Papersdock
+92 318 2248934
Sir Taha Ali
Papersdock
+92 318 2248934
Question 11
Sir Taha Ali
Papersdock
+92 318 2248934
Sir Taha Ali
Papersdock
+92 318 2248934
Question 12
Sir Taha Ali
Papersdock
+92 318 2248934
Sir Taha Ali
Papersdock
+92 318 2248934
Question 13
Sir Taha Ali
Papersdock
+92 318 2248934
Sir Taha Ali
Papersdock
+92 318 2248934
Question 14
Sir Taha Ali
Papersdock
+92 318 2248934
Sir Taha Ali
Papersdock
+92 318 2248934
Question 15
Sir Taha Ali
Papersdock
+92 318 2248934
Sir Taha Ali
Papersdock
+92 318 2248934
Sir Taha Ali
Papersdock
+92 318 2248934
Question 16
Sir Taha Ali
Papersdock
+92 318 2248934
Sir Taha Ali
Papersdock
+92 318 2248934
Sir Taha Ali
Papersdock
+92 318 2248934
Question 17
Sir Taha Ali
Papersdock
+92 318 2248934
Sir Taha Ali
Papersdock
+92 318 2248934
Question 18
Sir Taha Ali
Papersdock
+92 318 2248934
Sir Taha Ali
Papersdock
+92 318 2248934
Question 19
Sir Taha Ali
Papersdock
+92 318 2248934
Question 20
Sir Taha Ali
Papersdock
+92 318 2248934
Sir Taha Ali
Papersdock
+92 318 2248934
Sir Taha Ali
Papersdock
+92 318 2248934
Question 21
Sir Taha Ali
Papersdock
+92 318 2248934
Question 22
Question 23
Sir Taha Ali
Papersdock
+92 318 2248934
Question 24
Sir Taha Ali
Papersdock
+92 318 2248934
Sir Taha Ali
Papersdock
+92 318 2248934
Sir Taha Ali
Papersdock
+92 318 2248934
Question 25
Sir Taha Ali
Papersdock
+92 318 2248934
Sir Taha Ali
Papersdock
+92 318 2248934
Sir Taha Ali
Papersdock
+92 318 2248934
Question 26
Sir Taha Ali
Papersdock
+92 318 2248934
Question 27
Sir Taha Ali
Papersdock
+92 318 2248934
Question 28
Sir Taha Ali
Papersdock
+92 318 2248934
Question 29
Sir Taha Ali
Papersdock
+92 318 2248934
Answer
Answer 1
Answer 2
Sir Taha Ali
Papersdock
+92 318 2248934
Answer 3
Sir Taha Ali
Papersdock
+92 318 2248934
Answer 4
Answer 5
Sir Taha Ali
Papersdock
+92 318 2248934
Answer 6
Sir Taha Ali
Papersdock
+92 318 2248934
Answer 7
Sir Taha Ali
Papersdock
+92 318 2248934
Answer 8
Sir Taha Ali
Papersdock
+92 318 2248934
Answer 9
Sir Taha Ali
Papersdock
+92 318 2248934
Answer 10
Answer 11
Sir Taha Ali
Papersdock
+92 318 2248934
Sir Taha Ali
Papersdock
+92 318 2248934
Answer 12
Sir Taha Ali
Papersdock
+92 318 2248934
Answer 13
Sir Taha Ali
Papersdock
+92 318 2248934
Answer 14
Sir Taha Ali
Papersdock
+92 318 2248934
Answer 15
Sir Taha Ali
Papersdock
+92 318 2248934
Answer 16
Sir Taha Ali
Papersdock
+92 318 2248934
Answer 17
Sir Taha Ali
Papersdock
+92 318 2248934
Answer 18
Sir Taha Ali
Papersdock
+92 318 2248934
Answer 19
Answer 20
Sir Taha Ali
Papersdock
+92 318 2248934
Answer 21
Answer 22
Sir Taha Ali
Papersdock
+92 318 2248934
Answer 23
Sir Taha Ali
Papersdock
+92 318 2248934
Answer 24
Sir Taha Ali
Papersdock
+92 318 2248934
Answer 25
Sir Taha Ali
Papersdock
+92 318 2248934
Answer 26
Sir Taha Ali
Papersdock
+92 318 2248934
Answer 27
Sir Taha Ali
Papersdock
+92 318 2248934
Answer 28
Sir Taha Ali
Papersdock
+92 318 2248934
Answer 29
Sir Taha Ali
Papersdock
+92 318 2248934