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

Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...RKavithamani
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 

Recently uploaded (20)

Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 

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