SlideShare a Scribd company logo
1 of 17
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

What's hot

What's hot (20)

Compiler unit 1
Compiler unit 1Compiler unit 1
Compiler unit 1
 
Unit 5 cspc
Unit 5 cspcUnit 5 cspc
Unit 5 cspc
 
Compiler1
Compiler1Compiler1
Compiler1
 
Cs6660 compiler design
Cs6660 compiler designCs6660 compiler design
Cs6660 compiler design
 
Lecture2 general structure of a compiler
Lecture2 general structure of a compilerLecture2 general structure of a compiler
Lecture2 general structure of a compiler
 
Compiler Construction
Compiler ConstructionCompiler Construction
Compiler Construction
 
Compiler Design
Compiler DesignCompiler Design
Compiler Design
 
Compiler Design Material
Compiler Design MaterialCompiler Design Material
Compiler Design Material
 
Error detection recovery
Error detection recoveryError detection recovery
Error detection recovery
 
Intermediate code- generation
Intermediate code- generationIntermediate code- generation
Intermediate code- generation
 
Lexical analyzer
Lexical analyzerLexical analyzer
Lexical analyzer
 
what is compiler and five phases of compiler
what is compiler and five phases of compilerwhat is compiler and five phases of compiler
what is compiler and five phases of compiler
 
Compilers Design
Compilers DesignCompilers Design
Compilers Design
 
Cpcs302 1
Cpcs302  1Cpcs302  1
Cpcs302 1
 
Compiler Design
Compiler DesignCompiler Design
Compiler Design
 
Spr ch-05-compilers
Spr ch-05-compilersSpr ch-05-compilers
Spr ch-05-compilers
 
Compiler Design Unit 1
Compiler Design Unit 1Compiler Design Unit 1
Compiler Design Unit 1
 
Compiler Chapter 1
Compiler Chapter 1Compiler Chapter 1
Compiler Chapter 1
 
Compiler Design Tutorial
Compiler Design Tutorial Compiler Design Tutorial
Compiler Design Tutorial
 
Compiler design
Compiler designCompiler design
Compiler design
 

Viewers also liked

Viewers also liked (6)

The Power of Regular Expression: use in notepad++
The Power of Regular Expression: use in notepad++The Power of Regular Expression: use in notepad++
The Power of Regular Expression: use in notepad++
 
abc
abcabc
abc
 
Lexical analyzer
Lexical analyzerLexical analyzer
Lexical analyzer
 
Regular expression (compiler)
Regular expression (compiler)Regular expression (compiler)
Regular expression (compiler)
 
Regular language and Regular expression
Regular language and Regular expressionRegular language and Regular expression
Regular language and Regular expression
 
Nfa vs dfa
Nfa vs dfaNfa vs dfa
Nfa vs dfa
 

Similar to Compiler Engineering Lab#3

6 compiler lab - Flex
6 compiler lab - Flex6 compiler lab - Flex
6 compiler lab - FlexMashaelQ
 
Directive-based approach to Heterogeneous Computing
Directive-based approach to Heterogeneous ComputingDirective-based approach to Heterogeneous Computing
Directive-based approach to Heterogeneous ComputingRuymán Reyes
 
Compiler Engineering Lab#5 : Symbol Table, Flex Tool
Compiler Engineering Lab#5 : Symbol Table, Flex ToolCompiler Engineering Lab#5 : Symbol Table, Flex Tool
Compiler Engineering Lab#5 : Symbol Table, Flex ToolMashaelQ
 
F1270089476650
F1270089476650F1270089476650
F1270089476650Anil Kumar
 
Apache Big Data Europe 2016
Apache Big Data Europe 2016Apache Big Data Europe 2016
Apache Big Data Europe 2016Tim Ellison
 
Micro-Benchmarking Considered Harmful
Micro-Benchmarking Considered HarmfulMicro-Benchmarking Considered Harmful
Micro-Benchmarking Considered HarmfulThomas Wuerthinger
 
Summer training in matlab
Summer training in matlabSummer training in matlab
Summer training in matlabArshit Rai
 
Efficient and Advanced Omniscient Debugging for xDSMLs (SLE 2015)
Efficient and Advanced Omniscient Debugging for xDSMLs (SLE 2015)Efficient and Advanced Omniscient Debugging for xDSMLs (SLE 2015)
Efficient and Advanced Omniscient Debugging for xDSMLs (SLE 2015)Benoit Combemale
 
Concurrent Matrix Multiplication on Multi-core Processors
Concurrent Matrix Multiplication on Multi-core ProcessorsConcurrent Matrix Multiplication on Multi-core Processors
Concurrent Matrix Multiplication on Multi-core ProcessorsCSCJournals
 
SequenceL Auto-Parallelizing Toolset Intro slideshare
SequenceL Auto-Parallelizing Toolset Intro slideshareSequenceL Auto-Parallelizing Toolset Intro slideshare
SequenceL Auto-Parallelizing Toolset Intro slideshareDoug Norton
 
SequenceL intro slideshare
SequenceL intro slideshareSequenceL intro slideshare
SequenceL intro slideshareDoug Norton
 
Understand and Harness the Capabilities of Intel® Xeon Phi™ Processors
Understand and Harness the Capabilities of Intel® Xeon Phi™ ProcessorsUnderstand and Harness the Capabilities of Intel® Xeon Phi™ Processors
Understand and Harness the Capabilities of Intel® Xeon Phi™ ProcessorsIntel® Software
 
LabVIEW Introduction
LabVIEW IntroductionLabVIEW Introduction
LabVIEW IntroductionSalim Khan
 

Similar to Compiler Engineering Lab#3 (20)

6 compiler lab - Flex
6 compiler lab - Flex6 compiler lab - Flex
6 compiler lab - Flex
 
Directive-based approach to Heterogeneous Computing
Directive-based approach to Heterogeneous ComputingDirective-based approach to Heterogeneous Computing
Directive-based approach to Heterogeneous Computing
 
Compiler Engineering Lab#5 : Symbol Table, Flex Tool
Compiler Engineering Lab#5 : Symbol Table, Flex ToolCompiler Engineering Lab#5 : Symbol Table, Flex Tool
Compiler Engineering Lab#5 : Symbol Table, Flex Tool
 
F1270089476650
F1270089476650F1270089476650
F1270089476650
 
Apache Big Data Europe 2016
Apache Big Data Europe 2016Apache Big Data Europe 2016
Apache Big Data Europe 2016
 
Micro-Benchmarking Considered Harmful
Micro-Benchmarking Considered HarmfulMicro-Benchmarking Considered Harmful
Micro-Benchmarking Considered Harmful
 
lect 5
lect 5lect 5
lect 5
 
Summer training in matlab
Summer training in matlabSummer training in matlab
Summer training in matlab
 
Efficient and Advanced Omniscient Debugging for xDSMLs (SLE 2015)
Efficient and Advanced Omniscient Debugging for xDSMLs (SLE 2015)Efficient and Advanced Omniscient Debugging for xDSMLs (SLE 2015)
Efficient and Advanced Omniscient Debugging for xDSMLs (SLE 2015)
 
Concurrent Matrix Multiplication on Multi-core Processors
Concurrent Matrix Multiplication on Multi-core ProcessorsConcurrent Matrix Multiplication on Multi-core Processors
Concurrent Matrix Multiplication on Multi-core Processors
 
Matopt
MatoptMatopt
Matopt
 
SequenceL Auto-Parallelizing Toolset Intro slideshare
SequenceL Auto-Parallelizing Toolset Intro slideshareSequenceL Auto-Parallelizing Toolset Intro slideshare
SequenceL Auto-Parallelizing Toolset Intro slideshare
 
SequenceL intro slideshare
SequenceL intro slideshareSequenceL intro slideshare
SequenceL intro slideshare
 
Architectures for parallel
Architectures for parallelArchitectures for parallel
Architectures for parallel
 
Dsp lab manual 15 11-2016
Dsp lab manual 15 11-2016Dsp lab manual 15 11-2016
Dsp lab manual 15 11-2016
 
Tutorial
TutorialTutorial
Tutorial
 
Understand and Harness the Capabilities of Intel® Xeon Phi™ Processors
Understand and Harness the Capabilities of Intel® Xeon Phi™ ProcessorsUnderstand and Harness the Capabilities of Intel® Xeon Phi™ Processors
Understand and Harness the Capabilities of Intel® Xeon Phi™ Processors
 
Mkl mic lab_0
Mkl mic lab_0Mkl mic lab_0
Mkl mic lab_0
 
Matlab lecture
Matlab lectureMatlab lecture
Matlab lecture
 
LabVIEW Introduction
LabVIEW IntroductionLabVIEW Introduction
LabVIEW Introduction
 

Recently uploaded

GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSJoshuaGantuangco2
 
Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)cama23
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)lakshayb543
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17Celine George
 
Food processing presentation for bsc agriculture hons
Food processing presentation for bsc agriculture honsFood processing presentation for bsc agriculture hons
Food processing presentation for bsc agriculture honsManeerUddin
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parentsnavabharathschool99
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptxmary850239
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfPatidar M
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfVanessa Camilleri
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 
Integumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptIntegumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptshraddhaparab530
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxlancelewisportillo
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfJemuel Francisco
 

Recently uploaded (20)

GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
 
Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17
 
Food processing presentation for bsc agriculture hons
Food processing presentation for bsc agriculture honsFood processing presentation for bsc agriculture hons
Food processing presentation for bsc agriculture hons
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptxYOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parents
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdf
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdf
 
Raw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptxRaw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptx
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 
Integumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptIntegumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.ppt
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
 

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

  1. 8345555