compiler engineering lab#3

17
LAB # 3 : TRANSLATION FOR SIMPLE EXPRESSION COMPILER ENGINEERING University of Dammam Girls’ College of Science Department of Computer Science Compiler Engineering Lab

Upload: mashaelq

Post on 12-May-2015

1.215 views

Category:

Education


0 download

DESCRIPTION

Writing a Simple Lexical Analyzer: Translation for Simple Expression

TRANSCRIPT

Page 1: Compiler Engineering Lab#3

L A B # 3 : T R A N S L AT I O N F O R S I M P L E E X P R E S S I O N

COMPILER ENGINEERING

University of DammamGirls’ College of ScienceDepartment of Computer Science Compiler Engineering Lab

Page 2: Compiler Engineering Lab#3

Department of Computer Science - Compiler Engineering Lab

2

TRANSLATION FOR SIMPLE EXPRESSION

3-7/3/12

Page 3: Compiler Engineering Lab#3

Department of Computer Science - Compiler Engineering Lab

3

FIRST..,DISCOVER MISTAKES

USING THE LEXICAL ANALYZER

3-7/3/12

Page 4: Compiler Engineering Lab#3

Department of Computer Science - Compiler Engineering Lab

4

•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)

ERROR DISCOVERY

3-7/3/12

Page 5: Compiler Engineering Lab#3

Department of Computer Science - 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

3-7/3/12

Page 6: Compiler Engineering Lab#3

Department of Computer Science - Compiler Engineering Lab

6

9 – 5 + 2 This will be translated from infix to postfix form as:

9 5 – 2 +

A SYNTAX-DIRECTED TRANSLATOR

3-7/3/12

Page 7: Compiler Engineering Lab#3

Department of Computer Science - Compiler Engineering Lab

ABSTRACT SYNTAX TREE FOR 9-5+2

9

-

5

+

2

3-7/3/12 7

Page 8: Compiler Engineering Lab#3

Department of Computer Science - Compiler Engineering Lab

8

• 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 .

A SYNTAX-DIRECTED TRANSLATOR

3-7/3/12

Page 9: Compiler Engineering Lab#3

Department of Computer Science - Compiler Engineering Lab

9

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 .

A SYNTAX-DIRECTED TRANSLATOR

3-7/3/12

Page 10: Compiler Engineering Lab#3

Department of Computer Science - Compiler Engineering Lab

10

• Read a character from the user call it lookahead .

• Declare lookahead as a global variable to be used by all functions you need.

A SYNTAX-DIRECTED TRANSLATOR

3-7/3/12

Page 11: Compiler Engineering Lab#3

Department of Computer Science - Compiler Engineering Lab

11

• 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

A SYNTAX-DIRECTED TRANSLATOR

3-7/3/12

Page 12: Compiler Engineering Lab#3

Department of Computer Science - Compiler Engineering Lab

12

ERROR ( )

• Use this function for syntax errors • It will print this sentence :

( SYNTAX ERROR ! )

when errors appears on program.

3-7/3/12

Page 13: Compiler Engineering Lab#3

Department of Computer Science - 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 .

3-7/3/12

Page 14: Compiler Engineering Lab#3

Department of Computer Science - 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

3-7/3/12

Page 15: Compiler Engineering Lab#3

Department of Computer Science - 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.

3-7/3/12

Page 16: Compiler Engineering Lab#3

Department of Computer Science - Compiler Engineering Lab

16

• Find way to let program to handle sequence of operations using while statement .

• If the operator was not matched ( + , - ) call error() then break.

A SYNTAX-DIRECTED TRANSLATOR

3-7/3/12

Page 17: Compiler Engineering Lab#3

Department of Computer Science - Compiler Engineering Lab

17

QUESTIONS?

Thank you for listening

3-7/3/12