1. University of Dammam
Girls’ College of Science
Department of Computer Science
Compiler Engineering Lab
COMPILER
ENGINEERING
LAB # 3 : TRANSLATION FOR SIMPLE EXPRESSION
2. TRANSLATION FOR SIMPLE
EXPRESSION
Department of Computer Science -
3-7/3/12 2
Compiler Engineering Lab
3. FIRST..,
DISCOVER MISTAKES
USING THE LEXICAL
ANALYZER
Department of Computer Science -
3-7/3/12 3
Compiler Engineering Lab
4. ERROR DISCOVERY
• Example(s):
• When the lexeme > BSIZE
• When the lexeme doesn’t match any
regular definition
• Dealing with Errors:
• Unget character when the lexeme
• Print Error Message and exit(0)
Department of Computer Science -
3-7/3/12 4
Compiler Engineering Lab
5. A SYNTAX-DIRECTED
TRANSLATOR
• We shall construct a compiler that
translates infix expression into
postfix form.
• A notation in which the operators
appear after there operands
Department of Computer Science -
3-7/3/12 5
Compiler Engineering Lab
6. A SYNTAX-DIRECTED
TRANSLATOR
9–5+2
This will be translated from
infix to postfix form as:
95–2+
Department of Computer Science -
3-7/3/12 6
Compiler Engineering Lab
7. ABSTRACT SYNTAX
TREE FOR 9-5+2
+
- 2
9 5
Department of Computer Science -
3-7/3/12 7
Compiler Engineering Lab
8. A SYNTAX-DIRECTED
TRANSLATOR
• Now, we construct a syntax-directed
translator that translates arithmetic
expressions into postfix form.
• To keep the initial program manageably
small, we start off with expressions
consisting of digits separated by ( + , - )
signs .
Department of Computer Science -
3-7/3/12 8
Compiler Engineering Lab
9. A SYNTAX-DIRECTED
TRANSLATOR
design C program to translate an infix
expression into postfix form
Write the program inside the main
function starting with reading one
character then call the functions you
need .
Department of Computer Science -
3-7/3/12 9
Compiler Engineering Lab
10. A SYNTAX-DIRECTED
TRANSLATOR
• Read a character from the user call it
lookahead .
• Declare lookahead as a global variable
to be used by all functions you need.
Department of Computer Science -
3-7/3/12 10
Compiler Engineering Lab
11. A SYNTAX-DIRECTED
TRANSLATOR
• You will need three functions each will
perform certain operations for the translation
.
• All these function will be void (no return)
• No variables will be passed through these
function
• Only the global variable lookahead
Department of Computer Science -
3-7/3/12 11
Compiler Engineering Lab
12. ERROR ( )
• Use this function for syntax errors
• It will print this sentence :
( SYNTAX ERROR ! )
when errors appears on program.
Department of Computer Science -
3-7/3/12 12
Compiler Engineering Lab
13. TERM ( )
• Check if lookahead character is a digit
• If it is a digit put the character on the
screen and read another character.
• If lookahead is not digit it will be a syntax
error .
Department of Computer Science -
3-7/3/12 13
Compiler Engineering Lab
14. EXPR ( )
• Use Expr function to check for operators..
• Before start checking operators inside expr(
) check if that character is digit use term ( )
function
Department of Computer Science -
3-7/3/12 14
Compiler Engineering Lab
15. BACK TO EXPR( ) FUNCTION
• After reading one character after digit check if that
character is +
• If true : read another character and check if that is
digit, using term ( ) function that will perform
previous steps of putting digit character on screen
and read another , put ( + ) on screen
• If the character is not (+) : check if it is (-) , do the
same as previous.
Department of Computer Science -
3-7/3/12 15
Compiler Engineering Lab
16. A SYNTAX-DIRECTED
TRANSLATOR
• Find way to let program to handle
sequence of operations using while
statement .
• If the operator was not matched ( + ,
- ) call error() then break.
Department of Computer Science -
3-7/3/12 16
Compiler Engineering Lab
17. QUESTIONS?
Thank you for listening
Department of Computer Science -
3-7/3/12 17
Compiler Engineering Lab