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




                   COMPILER
                  ENGINEERING
           LAB # 1: INTRODUCTION & LEXICAL
                        ANALYSIS
WHAT IS A COMPILER?

 • It is a program that reads a program written in one
   language - the source language – and translates it
   into an equivalent program in another language –
   the target language-

 • An important part of this translation process is that
   the compiler reports to its user the presence of
   errors in the source program.




                   Department of Computer Science -
25-29/2/12                                                 2
                      Compiler Engineering Lab
COMPILER THEORY


             Source                                        target
                              Compiler
             program                                      program


                                error
                              messages




                       Department of Computer Science -
25-29/2/12                                                          3
                          Compiler Engineering Lab
COMPILER ENVIRONTMENT TOOLS

 • Many     software    tools that
   manipulate source program first
   perform some analysis .

 • Some examples of such tools
   include

             Department of Computer Science -
25-29/2/12                                      4
                Compiler Engineering Lab
1- STRUCTURE EDITOR

 • It takes as input a sequence of commands to build a
   source program
 • performs the text creation and modification function
   of a text editor
 • Analyze program text, putting and appropriate
   hierarchical structure on the source program
     • Checks that the input is correctly formed
     • Can supply Keywords automatically
     • Can jump from a begin or left parenthesis to its
       matching end or right parenthesis
                      Department of Computer Science -
25-29/2/12                                                5
                         Compiler Engineering Lab
2- PRETTY PRINTERS

 • Analyze the program and prints it in
   such a way that the structure of the
   program becomes clearly visible.




                Department of Computer Science -
25-29/2/12                                         6
                   Compiler Engineering Lab
3- STATIC CHECKERS

 • Reads a program
 • Analyze it
 • Discover potential bugs without
   running the program
 • Catch logical errors


                Department of Computer Science -
25-29/2/12                                         7
                   Compiler Engineering Lab
4 - INTERPRETERS

 • Performs the operations implied by
   the source program.

 • What is the difference between a
   Compiler and an Interpreter ?



               Department of Computer Science -
25-29/2/12                                        8
                  Compiler Engineering Lab
COMPILER PHASES



                Department of Computer Science -
25-29/2/12                                         9
                   Compiler Engineering Lab
PARTS OF COMPILATION

 1.          Analysis                      2. Synthesis
     The analysis part                        The synthesis part
      breaks up the                             constructs         the
      source program                            desired         target
      into   consistent                         program from the
                                                intermediate
      pieces                                    representation.
     and creates an
      intermediate
      representation of
      the       source
      program.
                        Department of Computer Science -
25-29/2/12                                                          10
                           Compiler Engineering Lab
PROCESSING ENDS OF A COMPILER

 1. Front-End                          2. Back-End
    Consists of phases that              Includes those portions of
    depend primarily on the              the compiler that depend
    source language and                  on the target machine ,
    largely independent of               and do not depend on the
    the     target   machine             source language (code
    (lexical – syntactic –               optimization    ,    code
    symbol table – semantic              generation)
    – intermediate code )


                    Department of Computer Science -
25-29/2/12                                                       11
                       Compiler Engineering Lab
COMPLIER PHASES

         Source
        Program
                                          Lexical                 “Scanning”

   Compiler          Analysis             Syntax (Hierarchical)    “Parsing”
       Front End
                                         Contextual               “Semantic
                                                                   Analysis”
                                         Intermediate
       Back End                          Code
                     Synthesis                                Phases are
                                         Object              important to
                                         Code                 simplify the
       Machine                                                compiler’s
      Language                                                  structure

                     Department of Computer Science -
25-29/2/12                                                                     12
                        Compiler Engineering Lab
COMPLIER PHASES INTERACTION
          (VIA DATA STRUCTURE)
         Source
        Program                                            Text

                                        Lexical
                                                          Tokens
   Compiler        Analysis             Syntax
                                                         Abstract
       Front End                                       (Syntax Tree)
                                       Contextual
                                                      Decorated AST
                                       Intermediate   + Symbol Table
       Back End                        Code
                   Synthesis                           Intermediate
                                                           Code
                                       Object
                                       Code           Object Code
       Machine
      Language
                   Department of Computer Science -
25-29/2/12                                                         13
                      Compiler Engineering Lab
COMPILER PHASES

                    LEXICAL ANALYZER

                     SYNTAX ANALYZER

         ERROR     SYMANTIC ANALYZER                  Symbol Table
        HANDLING
                                                        Manager
                    INTERMEDIATE CODE
                        GENERATOR

                       CODE OPTIMIZER

                      CODE GENERATOR



                   Department of Computer Science -
25-29/2/12                                                           14
                      Compiler Engineering Lab
COMPILER CONSTRUCTION TOOLS

 • Compiler can be written like any program
 •A    programmer       can     use    software
   development tools like :
     • Debugger
     • Version manager
     • Profilers
 • More    specialized   tools have    been
   developed for helping implementing various
   phases of a compiler

                   Department of Computer Science -
25-29/2/12                                            15
                      Compiler Engineering Lab
1- SCANNER GENERATORS

 • Generate lexical analyzer from a
   specification based on regular expression.




                  Department of Computer Science -
25-29/2/12                                           16
                     Compiler Engineering Lab
2- PARSER GENERATORS

 • Produces syntax analyzers from input that is
   based on a context – free grammar.
 • In early compilers ,syntax analysis consumed
   a large fraction of running time and large
   fraction of intellectual effort of writing
   compilers.
 • Using parser generator gives ability to
   implement this phase in few days.


                  Department of Computer Science -
25-29/2/12                                           17
                     Compiler Engineering Lab
3- SYNTAX–DIRECTED
              TRANSLATOR ENGINE

 • Produce collection of routines that walk
   the parser tree generating the intermediate
   code




                Department of Computer Science -
25-29/2/12                                         18
                   Compiler Engineering Lab
4 - AUTOMATIC CODE
                   GENERATOR

 • Takes a collection of rules that define the
   translation of each operation of the
   intermediate language into the machine
   language for the target machine




                Department of Computer Science -
25-29/2/12                                         19
                   Compiler Engineering Lab
5 - DATA FLOW ENGINE

 • Much of information needed to perform
   good code optimization involves “ data_
   flow analysis”,

 • The gathering of information about how
   values are transmitted from one part of a
   program to each other part

                  Department of Computer Science -
25-29/2/12                                           20
                     Compiler Engineering Lab
LEXICAL ANALYSIS
              FIRST PHASE OF A COMPILER




                  Department of Computer Science -
25-29/2/12                                           21
                     Compiler Engineering Lab
INSERTING A LEXICAL ANALYZER
BETWEEN THE INPUT AND THE PARSER


          Read
        character


                    Lexical
Input                                     Parser
                    Analyzer
        push back              pass Token and
        character                its attribute
LEXICAL ANALYZER MECHANISM

• Read the characters from the input
• Group them into lexemes
• Pass the tokens formed by the lexemes together
  with their attribute values to the later stages
• In some situations the lexical analyzer has to
  read some more characters ahead before it can
  decide on the token to be returned to the parser
• the extra character has to be pushed back onto
  the input, because it can be the beginning of the
  next lexeme.
IMPLEMENTING THE INTERACTION



 Read character
 using getchar( )
                    Lexan()    pass Token and
                                 its attribute
                    Lexical
   push back
                    Analyzer
  character F
 ungetc(F,stdin)
LEX   …

• A particular tool , that has been widely used to
  specify lexical analyzers for a variety of
  languages
• Using such tool will allow us to show how the
  specification   of     patterns  using    regular
  expressions can be combined with action
REGULAR EXPRESSION PATTERNS
          FOR TOKENS
Regular expression   Token    Attribute-value
       ws              -               -

        If             if              -

       then          then              -

       else           else             -

        Id             id    Pointer to table entry

       Num           num     Pointer to table entry

        <            relop            LT

        <=           relop            LE

        =            relop            EQ

        <>           relop            NE

        >            relop            GT

        >=           relop            GE
LEX SPECIFICATION

• A Lex program consists of three parts:
 1. Declarations
 2. Translation rules
 3. Auxiliary procedure
1- DECLARATIONS SECTION

Includes declarations of :
variables, manifest constants
and regular definitions

Manifest constant..
Is an identifier that is declared to represent
  a constant
DEFINITION OF MANIFEST CONSTANT USED
      BY THE TRANSLATION RULES




     LT , LE, EQ , NE , GT , GE , IF ,
      THEN , ELSE , ID , NUMBER ,
               RELOP, AROP
REGULAR DEFINITIONS



delim [ tn]
Ws        {delim}+
letter [A-Za-z]
digit    [0-9]
id        {letter}({letter}|{digit})*
number
 {digit}+(.{digit}+)?(E[+-]?{digit}+)?
2-TRANSLATION RULES

are statements of the form
 P1 {action1}
 P2 {action2}
 ……………..
 Pn {action n}
• where each p is a regular expression and each
  {action} is a program fragment describing what
  action the lexical analyzer shoud take when
  pattern p matches a lexeme
2- TRANSLATION RULES

Ws            no action and no return
if            return (IF)
then          return (THEN)
else          return (ELSE)
“<“           val =LT
              return (RELOP)
and similarly to other relation operations
Id            val = install_id( )
              return(ID)
Number
              val= install_num( )
              return(NUM)
3-AUXILIARY PROCEDURES


• Holds whatever auxiliary procedures are needed
  by the action
• a lexical analyzer created by lex behaves in
  concert with a parser in the following manner:
  when activated by the parser the lexical analyzer
  begins reading its remaining input ,one character
  at a time ,until it has found the longest prefix of
  the input that is matched by one of the regular
  expressions P then it execute action
CON..

• Typically action will return control to the parser, if it
  does not the lexical analyzer proceeds to find
  more lexemes until an action causes control to
  return to the parser
• The lexical analyzer returns a single quantity to
  the parser ,the token..
• to pass an attribute value with information about
  the lexeme we can set a global variable called val
AUXILIARY PROCEDURES

• install_id ( )
  Procedure to install the lexeme
• install_num ( )
  similar procedure to install a lexeme
  that is a number
WRITING A LEXICAL ANALYZER


• Write a lexical analyzer Using C++
  language.
• Write it as a function called from inside
  main( )
• Call that function Lexan
• Lexan function returns the value of
  Token
THE LEXICAL ANALYZER WILL DO..

• Read character from the user

• If the character is a blank (Space) or a (tab)
  (written ‘t’) no token is returned to the parser,
  exit the function

• If the character is (new line) written (‘n’) the line
  numbers will be incremented ,no token is
  returned

• If the character is one Digit .. Tokenval
MORE THAN ONE DIGIT ..

• Allow user to enter sequence of characters
• While the user entering digits after first digit the
  analyzer allows him to enter more digits
• Each time the analyzer compute the Tokenval
• If the next character is not digit push back the
  character
• Each time print the result from each part to see
  the output
TOKENVAL..

• First digit
   Tokenval= t –’0’
• Next digit
 Tokenval = tokenval * 10 + t - ’0’
READING CHARACTER FROM THE USER

#include <stdio.h>
int getchar( );
• Gets character from stdin.
• getchar is a macro that returns the next
  character on the named input stream stdin.
• On success , getchar returns the character read,
  after converting it to an int without sign extension
  using the ASCII code.
PUSHING BACK CHARACTERS

#include <stdio.h>
ungetc (c,stdin)
• Pushes a character back into input stream.
• ungetc pushes the character c back onto the
  named input stream, which must be open for
  reading. This character will be returned on the
  next call to getchar for that stream. One
  character can be pushed back in all situations.
• On success, ungetc returns the character pushed
  back.
TEST CHARACTER IF (DIGIT) OR NOT

#include <ctype.h>
isdigit(t)
• Tests for decimal-digit character.
• isdigit is a macro that classifies ASCII-coded
  integer values by table lookup
• isdigit returns nonzero if c is a digit.
QUESTIONS?

 Thank you for listening 




                   Department of Computer Science -
25-29/2/12                                            43
                      Compiler Engineering Lab

More Related Content

What's hot

Different phases of a compiler
Different phases of a compilerDifferent phases of a compiler
Different phases of a compilerSumit Sinha
 
Cs6660 compiler design
Cs6660 compiler designCs6660 compiler design
Cs6660 compiler designhari2010
 
Compiler Engineering Lab#2
Compiler Engineering Lab#2Compiler Engineering Lab#2
Compiler Engineering Lab#2MashaelQ
 
Principles of compiler design
Principles of compiler designPrinciples of compiler design
Principles of compiler designDHARANI BABU
 
Compiler Construction
Compiler ConstructionCompiler Construction
Compiler ConstructionSarmad Ali
 
Phases of the Compiler - Systems Programming
Phases of the Compiler - Systems ProgrammingPhases of the Compiler - Systems Programming
Phases of the Compiler - Systems ProgrammingMukesh Tekwani
 
Error detection recovery
Error detection recoveryError detection recovery
Error detection recoveryTech_MX
 
Compiler Construction
Compiler ConstructionCompiler Construction
Compiler ConstructionAhmed Raza
 
Compiler Design
Compiler DesignCompiler Design
Compiler DesignMir Majid
 
Lecture2 general structure of a compiler
Lecture2 general structure of a compilerLecture2 general structure of a compiler
Lecture2 general structure of a compilerMahesh Kumar Chelimilla
 
Chapter One
Chapter OneChapter One
Chapter Onebolovv
 
Chapter 1 1
Chapter 1 1Chapter 1 1
Chapter 1 1bolovv
 
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 compileradilmehmood93
 
Compiler Design(Nanthu)
Compiler Design(Nanthu)Compiler Design(Nanthu)
Compiler Design(Nanthu)guest91cc85
 

What's hot (20)

Different phases of a compiler
Different phases of a compilerDifferent phases of a compiler
Different phases of a compiler
 
Cs6660 compiler design
Cs6660 compiler designCs6660 compiler design
Cs6660 compiler design
 
Compiler Engineering Lab#2
Compiler Engineering Lab#2Compiler Engineering Lab#2
Compiler Engineering Lab#2
 
Analysis of the source program
Analysis of the source programAnalysis of the source program
Analysis of the source program
 
Principles of compiler design
Principles of compiler designPrinciples of compiler design
Principles of compiler design
 
Compiler Construction
Compiler ConstructionCompiler Construction
Compiler Construction
 
Compiler Design
Compiler DesignCompiler Design
Compiler Design
 
Phases of the Compiler - Systems Programming
Phases of the Compiler - Systems ProgrammingPhases of the Compiler - Systems Programming
Phases of the Compiler - Systems Programming
 
Error detection recovery
Error detection recoveryError detection recovery
Error detection recovery
 
Compiler1
Compiler1Compiler1
Compiler1
 
Compiler Construction
Compiler ConstructionCompiler Construction
Compiler Construction
 
Unit 1 cd
Unit 1 cdUnit 1 cd
Unit 1 cd
 
Compiler Design
Compiler DesignCompiler Design
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 unit 1
Compiler unit 1Compiler unit 1
Compiler unit 1
 
Chapter One
Chapter OneChapter One
Chapter One
 
Chapter 1 1
Chapter 1 1Chapter 1 1
Chapter 1 1
 
Compiler Design Material
Compiler Design MaterialCompiler Design Material
Compiler Design Material
 
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
 
Compiler Design(Nanthu)
Compiler Design(Nanthu)Compiler Design(Nanthu)
Compiler Design(Nanthu)
 

Viewers also liked

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++Anjesh Tuladhar
 
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
 
Compiler Design Lab File
Compiler Design Lab FileCompiler Design Lab File
Compiler Design Lab FileKandarp Tiwari
 
Regular language and Regular expression
Regular language and Regular expressionRegular language and Regular expression
Regular language and Regular expressionAnimesh Chaturvedi
 

Viewers also liked (9)

Lexical analyzer
Lexical analyzerLexical analyzer
Lexical analyzer
 
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
 
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
 
Compiler Design Lab File
Compiler Design Lab FileCompiler Design Lab File
Compiler Design Lab File
 
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
 
Phases of Compiler
Phases of CompilerPhases of Compiler
Phases of Compiler
 
Nfa vs dfa
Nfa vs dfaNfa vs dfa
Nfa vs dfa
 

Similar to Compiler Engineering Lab#1

Similar to Compiler Engineering Lab#1 (20)

Ch 1.pptx
Ch 1.pptxCh 1.pptx
Ch 1.pptx
 
Overall 23 11_2007_hdp
Overall 23 11_2007_hdpOverall 23 11_2007_hdp
Overall 23 11_2007_hdp
 
Compiler design
Compiler designCompiler design
Compiler design
 
12109 microprocessor & programming
12109 microprocessor & programming12109 microprocessor & programming
12109 microprocessor & programming
 
microprocessor & programming
 microprocessor & programming microprocessor & programming
microprocessor & programming
 
Chapter 2 Program language translation.pptx
Chapter 2 Program language translation.pptxChapter 2 Program language translation.pptx
Chapter 2 Program language translation.pptx
 
SS UI Lecture 1
SS UI Lecture 1SS UI Lecture 1
SS UI Lecture 1
 
Ss ui lecture 1
Ss ui lecture 1Ss ui lecture 1
Ss ui lecture 1
 
Compiler gate question key
Compiler gate question keyCompiler gate question key
Compiler gate question key
 
System software
System softwareSystem software
System software
 
Chap01-Intro.ppt
Chap01-Intro.pptChap01-Intro.ppt
Chap01-Intro.ppt
 
1 introduction to compiler
1 introduction to compiler1 introduction to compiler
1 introduction to compiler
 
unit1pdf__2021_12_14_12_37_34.pdf
unit1pdf__2021_12_14_12_37_34.pdfunit1pdf__2021_12_14_12_37_34.pdf
unit1pdf__2021_12_14_12_37_34.pdf
 
Ch1 (1).ppt
Ch1 (1).pptCh1 (1).ppt
Ch1 (1).ppt
 
Compiler Design Introduction
Compiler Design IntroductionCompiler Design Introduction
Compiler Design Introduction
 
LANGUAGE TRANSLATOR
LANGUAGE TRANSLATORLANGUAGE TRANSLATOR
LANGUAGE TRANSLATOR
 
Compiler Construction
Compiler ConstructionCompiler Construction
Compiler Construction
 
Embedded system-Introduction to development cycle and development tool
Embedded system-Introduction to development cycle and development  toolEmbedded system-Introduction to development cycle and development  tool
Embedded system-Introduction to development cycle and development tool
 
Introduction to C Programming
Introduction to C ProgrammingIntroduction to C Programming
Introduction to C Programming
 
01 overview
01 overview01 overview
01 overview
 

Recently uploaded

18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
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
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
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
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfakmcokerachita
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
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
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
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
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...M56BOOKSTORE PRODUCT/SERVICE
 

Recently uploaded (20)

18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
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
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
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 ...
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdf
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
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
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
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
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
 

Compiler Engineering Lab#1

  • 1. University of Dammam Girls’ College of Science Department of Computer Science Compiler Engineering Lab COMPILER ENGINEERING LAB # 1: INTRODUCTION & LEXICAL ANALYSIS
  • 2. WHAT IS A COMPILER? • It is a program that reads a program written in one language - the source language – and translates it into an equivalent program in another language – the target language- • An important part of this translation process is that the compiler reports to its user the presence of errors in the source program. Department of Computer Science - 25-29/2/12 2 Compiler Engineering Lab
  • 3. COMPILER THEORY Source target Compiler program program error messages Department of Computer Science - 25-29/2/12 3 Compiler Engineering Lab
  • 4. COMPILER ENVIRONTMENT TOOLS • Many software tools that manipulate source program first perform some analysis . • Some examples of such tools include Department of Computer Science - 25-29/2/12 4 Compiler Engineering Lab
  • 5. 1- STRUCTURE EDITOR • It takes as input a sequence of commands to build a source program • performs the text creation and modification function of a text editor • Analyze program text, putting and appropriate hierarchical structure on the source program • Checks that the input is correctly formed • Can supply Keywords automatically • Can jump from a begin or left parenthesis to its matching end or right parenthesis Department of Computer Science - 25-29/2/12 5 Compiler Engineering Lab
  • 6. 2- PRETTY PRINTERS • Analyze the program and prints it in such a way that the structure of the program becomes clearly visible. Department of Computer Science - 25-29/2/12 6 Compiler Engineering Lab
  • 7. 3- STATIC CHECKERS • Reads a program • Analyze it • Discover potential bugs without running the program • Catch logical errors Department of Computer Science - 25-29/2/12 7 Compiler Engineering Lab
  • 8. 4 - INTERPRETERS • Performs the operations implied by the source program. • What is the difference between a Compiler and an Interpreter ? Department of Computer Science - 25-29/2/12 8 Compiler Engineering Lab
  • 9. COMPILER PHASES Department of Computer Science - 25-29/2/12 9 Compiler Engineering Lab
  • 10. PARTS OF COMPILATION 1. Analysis 2. Synthesis  The analysis part The synthesis part breaks up the constructs the source program desired target into consistent program from the intermediate pieces representation.  and creates an intermediate representation of the source program. Department of Computer Science - 25-29/2/12 10 Compiler Engineering Lab
  • 11. PROCESSING ENDS OF A COMPILER 1. Front-End 2. Back-End Consists of phases that Includes those portions of depend primarily on the the compiler that depend source language and on the target machine , largely independent of and do not depend on the the target machine source language (code (lexical – syntactic – optimization , code symbol table – semantic generation) – intermediate code ) Department of Computer Science - 25-29/2/12 11 Compiler Engineering Lab
  • 12. COMPLIER PHASES Source Program Lexical “Scanning” Compiler Analysis Syntax (Hierarchical) “Parsing” Front End Contextual “Semantic Analysis” Intermediate Back End Code Synthesis Phases are Object important to Code simplify the Machine compiler’s Language structure Department of Computer Science - 25-29/2/12 12 Compiler Engineering Lab
  • 13. COMPLIER PHASES INTERACTION (VIA DATA STRUCTURE) Source Program Text Lexical Tokens Compiler Analysis Syntax Abstract Front End (Syntax Tree) Contextual Decorated AST Intermediate + Symbol Table Back End Code Synthesis Intermediate Code Object Code Object Code Machine Language Department of Computer Science - 25-29/2/12 13 Compiler Engineering Lab
  • 14. COMPILER PHASES LEXICAL ANALYZER SYNTAX ANALYZER ERROR SYMANTIC ANALYZER Symbol Table HANDLING Manager INTERMEDIATE CODE GENERATOR CODE OPTIMIZER CODE GENERATOR Department of Computer Science - 25-29/2/12 14 Compiler Engineering Lab
  • 15. COMPILER CONSTRUCTION TOOLS • Compiler can be written like any program •A programmer can use software development tools like : • Debugger • Version manager • Profilers • More specialized tools have been developed for helping implementing various phases of a compiler Department of Computer Science - 25-29/2/12 15 Compiler Engineering Lab
  • 16. 1- SCANNER GENERATORS • Generate lexical analyzer from a specification based on regular expression. Department of Computer Science - 25-29/2/12 16 Compiler Engineering Lab
  • 17. 2- PARSER GENERATORS • Produces syntax analyzers from input that is based on a context – free grammar. • In early compilers ,syntax analysis consumed a large fraction of running time and large fraction of intellectual effort of writing compilers. • Using parser generator gives ability to implement this phase in few days. Department of Computer Science - 25-29/2/12 17 Compiler Engineering Lab
  • 18. 3- SYNTAX–DIRECTED TRANSLATOR ENGINE • Produce collection of routines that walk the parser tree generating the intermediate code Department of Computer Science - 25-29/2/12 18 Compiler Engineering Lab
  • 19. 4 - AUTOMATIC CODE GENERATOR • Takes a collection of rules that define the translation of each operation of the intermediate language into the machine language for the target machine Department of Computer Science - 25-29/2/12 19 Compiler Engineering Lab
  • 20. 5 - DATA FLOW ENGINE • Much of information needed to perform good code optimization involves “ data_ flow analysis”, • The gathering of information about how values are transmitted from one part of a program to each other part Department of Computer Science - 25-29/2/12 20 Compiler Engineering Lab
  • 21. LEXICAL ANALYSIS FIRST PHASE OF A COMPILER Department of Computer Science - 25-29/2/12 21 Compiler Engineering Lab
  • 22. INSERTING A LEXICAL ANALYZER BETWEEN THE INPUT AND THE PARSER Read character Lexical Input Parser Analyzer push back pass Token and character its attribute
  • 23. LEXICAL ANALYZER MECHANISM • Read the characters from the input • Group them into lexemes • Pass the tokens formed by the lexemes together with their attribute values to the later stages • In some situations the lexical analyzer has to read some more characters ahead before it can decide on the token to be returned to the parser • the extra character has to be pushed back onto the input, because it can be the beginning of the next lexeme.
  • 24. IMPLEMENTING THE INTERACTION Read character using getchar( ) Lexan() pass Token and its attribute Lexical push back Analyzer character F ungetc(F,stdin)
  • 25. LEX … • A particular tool , that has been widely used to specify lexical analyzers for a variety of languages • Using such tool will allow us to show how the specification of patterns using regular expressions can be combined with action
  • 26. REGULAR EXPRESSION PATTERNS FOR TOKENS Regular expression Token Attribute-value ws - - If if - then then - else else - Id id Pointer to table entry Num num Pointer to table entry < relop LT <= relop LE = relop EQ <> relop NE > relop GT >= relop GE
  • 27. LEX SPECIFICATION • A Lex program consists of three parts: 1. Declarations 2. Translation rules 3. Auxiliary procedure
  • 28. 1- DECLARATIONS SECTION Includes declarations of : variables, manifest constants and regular definitions Manifest constant.. Is an identifier that is declared to represent a constant
  • 29. DEFINITION OF MANIFEST CONSTANT USED BY THE TRANSLATION RULES LT , LE, EQ , NE , GT , GE , IF , THEN , ELSE , ID , NUMBER , RELOP, AROP
  • 30. REGULAR DEFINITIONS delim [ tn] Ws {delim}+ letter [A-Za-z] digit [0-9] id {letter}({letter}|{digit})* number {digit}+(.{digit}+)?(E[+-]?{digit}+)?
  • 31. 2-TRANSLATION RULES are statements of the form P1 {action1} P2 {action2} …………….. Pn {action n} • where each p is a regular expression and each {action} is a program fragment describing what action the lexical analyzer shoud take when pattern p matches a lexeme
  • 32. 2- TRANSLATION RULES Ws no action and no return if return (IF) then return (THEN) else return (ELSE) “<“ val =LT return (RELOP) and similarly to other relation operations Id val = install_id( ) return(ID) Number val= install_num( ) return(NUM)
  • 33. 3-AUXILIARY PROCEDURES • Holds whatever auxiliary procedures are needed by the action • a lexical analyzer created by lex behaves in concert with a parser in the following manner: when activated by the parser the lexical analyzer begins reading its remaining input ,one character at a time ,until it has found the longest prefix of the input that is matched by one of the regular expressions P then it execute action
  • 34. CON.. • Typically action will return control to the parser, if it does not the lexical analyzer proceeds to find more lexemes until an action causes control to return to the parser • The lexical analyzer returns a single quantity to the parser ,the token.. • to pass an attribute value with information about the lexeme we can set a global variable called val
  • 35. AUXILIARY PROCEDURES • install_id ( ) Procedure to install the lexeme • install_num ( ) similar procedure to install a lexeme that is a number
  • 36. WRITING A LEXICAL ANALYZER • Write a lexical analyzer Using C++ language. • Write it as a function called from inside main( ) • Call that function Lexan • Lexan function returns the value of Token
  • 37. THE LEXICAL ANALYZER WILL DO.. • Read character from the user • If the character is a blank (Space) or a (tab) (written ‘t’) no token is returned to the parser, exit the function • If the character is (new line) written (‘n’) the line numbers will be incremented ,no token is returned • If the character is one Digit .. Tokenval
  • 38. MORE THAN ONE DIGIT .. • Allow user to enter sequence of characters • While the user entering digits after first digit the analyzer allows him to enter more digits • Each time the analyzer compute the Tokenval • If the next character is not digit push back the character • Each time print the result from each part to see the output
  • 39. TOKENVAL.. • First digit Tokenval= t –’0’ • Next digit Tokenval = tokenval * 10 + t - ’0’
  • 40. READING CHARACTER FROM THE USER #include <stdio.h> int getchar( ); • Gets character from stdin. • getchar is a macro that returns the next character on the named input stream stdin. • On success , getchar returns the character read, after converting it to an int without sign extension using the ASCII code.
  • 41. PUSHING BACK CHARACTERS #include <stdio.h> ungetc (c,stdin) • Pushes a character back into input stream. • ungetc pushes the character c back onto the named input stream, which must be open for reading. This character will be returned on the next call to getchar for that stream. One character can be pushed back in all situations. • On success, ungetc returns the character pushed back.
  • 42. TEST CHARACTER IF (DIGIT) OR NOT #include <ctype.h> isdigit(t) • Tests for decimal-digit character. • isdigit is a macro that classifies ASCII-coded integer values by table lookup • isdigit returns nonzero if c is a digit.
  • 43. QUESTIONS? Thank you for listening  Department of Computer Science - 25-29/2/12 43 Compiler Engineering Lab