SlideShare a Scribd company logo
University of Dammam
Girls’ College of Science
Department of Computer Science
Compiler Engineering Lab




                   COMPILER
                  ENGINEERING
      LAB # 3 : TRANSLATION FOR SIMPLE EXPRESSION
TRANSLATION FOR SIMPLE
                EXPRESSION



                 Department of Computer Science -
3-7/3/12                                            2
                    Compiler Engineering Lab
FIRST..,
           DISCOVER MISTAKES
           USING THE LEXICAL
               ANALYZER



               Department of Computer Science -
3-7/3/12                                          3
                  Compiler Engineering Lab
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
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
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
ABSTRACT SYNTAX
             TREE FOR 9-5+2

                                      +

                   -                              2

           9                  5
               Department of Computer Science -
3-7/3/12                                              7
                  Compiler Engineering Lab
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
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
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
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
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
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
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
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
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
QUESTIONS?

 Thank you for listening 




                   Department of Computer Science -
3-7/3/12                                              17
                      Compiler Engineering Lab

More Related Content

PPTX
Compiler Engineering Lab#2
PPTX
4 compiler lab - Syntax Ana
PPTX
Compiler Engineering Lab#1
PDF
Cd lab manual
PPTX
7 compiler lab
PDF
Different phases of a compiler
KEY
Unit 1 cd
PDF
Phases of the Compiler - Systems Programming
Compiler Engineering Lab#2
4 compiler lab - Syntax Ana
Compiler Engineering Lab#1
Cd lab manual
7 compiler lab
Different phases of a compiler
Unit 1 cd
Phases of the Compiler - Systems Programming

What's hot (20)

PDF
Compiler unit 1
PPT
Unit 5 cspc
PPT
Compiler1
PDF
Cs6660 compiler design
PDF
Lecture2 general structure of a compiler
PPT
Compiler Construction
PPTX
Compiler Design
DOCX
Compiler Design Material
PPTX
Error detection recovery
PPTX
Intermediate code- generation
PPT
Lexical analyzer
PPT
what is compiler and five phases of compiler
PDF
Compilers Design
PPT
Cpcs302 1
PPT
Compiler Design
PPSX
Spr ch-05-compilers
PPT
Compiler Design Unit 1
PPTX
Compiler Chapter 1
PPT
Compiler Design Tutorial
PDF
Compiler design
Compiler unit 1
Unit 5 cspc
Compiler1
Cs6660 compiler design
Lecture2 general structure of a compiler
Compiler Construction
Compiler Design
Compiler Design Material
Error detection recovery
Intermediate code- generation
Lexical analyzer
what is compiler and five phases of compiler
Compilers Design
Cpcs302 1
Compiler Design
Spr ch-05-compilers
Compiler Design Unit 1
Compiler Chapter 1
Compiler Design Tutorial
Compiler design
Ad

Viewers also liked (6)

PPT
The Power of Regular Expression: use in notepad++
PPTX
Lexical analyzer
PPTX
Regular expression (compiler)
PDF
Regular language and Regular expression
PPT
Nfa vs dfa
The Power of Regular Expression: use in notepad++
Lexical analyzer
Regular expression (compiler)
Regular language and Regular expression
Nfa vs dfa
Ad

Similar to Compiler Engineering Lab#3 (20)

PPTX
6 compiler lab - Flex
PDF
TSR_CLASS CD-UNIT 4.pdf ewqhqhqhewhwiqhe
PDF
Directive-based approach to Heterogeneous Computing
PPTX
Compiler Engineering Lab#5 : Symbol Table, Flex Tool
PDF
F1270089476650
PDF
Apache Big Data Europe 2016
PDF
Micro-Benchmarking Considered Harmful
PPT
01Intro__to_compile_construction____.ppt
PPSX
Summer training in matlab
PDF
STATIC TESTING TOOLS ARCHITECTURE IN OPENMP FOR EXASCALE SYSTEMS
PDF
Efficient and Advanced Omniscient Debugging for xDSMLs (SLE 2015)
PDF
Concurrent Matrix Multiplication on Multi-core Processors
PPT
Ch07 assembly language marwadi university.ppt
PPTX
SequenceL intro slideshare
PPTX
SequenceL Auto-Parallelizing Toolset Intro slideshare
PDF
Architectures for parallel
PDF
Dsp lab manual 15 11-2016
PDF
Understand and Harness the Capabilities of Intel® Xeon Phi™ Processors
6 compiler lab - Flex
TSR_CLASS CD-UNIT 4.pdf ewqhqhqhewhwiqhe
Directive-based approach to Heterogeneous Computing
Compiler Engineering Lab#5 : Symbol Table, Flex Tool
F1270089476650
Apache Big Data Europe 2016
Micro-Benchmarking Considered Harmful
01Intro__to_compile_construction____.ppt
Summer training in matlab
STATIC TESTING TOOLS ARCHITECTURE IN OPENMP FOR EXASCALE SYSTEMS
Efficient and Advanced Omniscient Debugging for xDSMLs (SLE 2015)
Concurrent Matrix Multiplication on Multi-core Processors
Ch07 assembly language marwadi university.ppt
SequenceL intro slideshare
SequenceL Auto-Parallelizing Toolset Intro slideshare
Architectures for parallel
Dsp lab manual 15 11-2016
Understand and Harness the Capabilities of Intel® Xeon Phi™ Processors

Recently uploaded (20)

PDF
Complications of Minimal Access Surgery at WLH
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PPTX
Cell Types and Its function , kingdom of life
PDF
Sports Quiz easy sports quiz sports quiz
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
Classroom Observation Tools for Teachers
PPTX
Cell Structure & Organelles in detailed.
PPTX
GDM (1) (1).pptx small presentation for students
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
Computing-Curriculum for Schools in Ghana
PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
master seminar digital applications in india
PDF
Anesthesia in Laparoscopic Surgery in India
Complications of Minimal Access Surgery at WLH
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Cell Types and Its function , kingdom of life
Sports Quiz easy sports quiz sports quiz
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
Classroom Observation Tools for Teachers
Cell Structure & Organelles in detailed.
GDM (1) (1).pptx small presentation for students
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Computing-Curriculum for Schools in Ghana
O7-L3 Supply Chain Operations - ICLT Program
human mycosis Human fungal infections are called human mycosis..pptx
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
FourierSeries-QuestionsWithAnswers(Part-A).pdf
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Microbial disease of the cardiovascular and lymphatic systems
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
master seminar digital applications in india
Anesthesia in Laparoscopic Surgery in India

Compiler Engineering Lab#3

  • 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

Editor's Notes