SlideShare ist ein Scribd-Unternehmen logo
1 von 54
Compiler Design
      CodeReplugd
COMPILERS
   A compiler is a program takes a program written in a source language
    and translates it into an equivalent program in a target language.



           source program                        COMPILER                       target program
    ( Normally a program written                                              ( Normally the equivalent
    in a high-level programming                                               program in machine code –
    language)                                                                 relocatable object file)

                                                error messages
Other Applications
    In addition to the development of a compiler, the techniques used in compiler design can be applicable to many
     problems in computer science.
       Techniques used in a lexical analyzer can be used in text editors, information retrieval system, and pattern
         recognition programs.
       Techniques used in a parser can be used in a query processing system such as SQL.
       Many software having a complex front-end may need techniques used in compiler design.
            A symbolic equation solver which takes an equation as input. That program should parse the given input
              equation.
       Most of the techniques used in compiler design can be used in Natural Language Processing (NLP) systems.
   An interpreter reads an executable source program written in a
    high-level programming language as well as data for this program,
    and it runs the program against the data to produce some results.
   One example is the Unix shell interpreter, which runs operating
    system commands interactively
Cousins of compiler
    Preprocessor(Macro processing, file inclusion, Rational preprocessor, Language extensions)
   Assembler
   Linkers-A linker takes several object les and libraries as input and produces one executable object file. It
    retrieves from the input files (and puts them together in the executable object file) the code of all the referenced
    functions/procedures and it resolves all external references to real addresses. The libraries include the operating
    system libraries, the language-specic libraries, and, maybe, user-created libraries.

   Loaders
   Debuggers-used to determine execution errors in a compiled program
   Profiler- Collects Statistics on the behavior of the object program during execution
   Project Managers-used to coordinate and merge the source code produced by different members of the
    team
What is the Challenge in compiler Design?
Many variations:
   many programming languages (eg, FORTRAN, C++, Java)
   many programming paradigms (eg, object-oriented, functional, logic)
   many computer architectures (eg, MIPS, SPARC, Intel, alpha)
   many operating systems (eg, Linux, Solaris, Windows)

Qualities of a compiler (in order of importance):
   the compiler itself must be bug-free
   it must generate correct machine code
   the generated machine code must run fast
   the compiler itself must run fast (compilation time must be proportional to program size)
   the compiler must be portable (ie, modular, supporting separate compilation)
   it must print good diagnostics and error messages
   the generated code must work well with existing debuggers
   must have consistent and predictable optimization.

Building a compiler requires knowledge of
   programming languages (parameter passing, variable scoping, memory allocation, etc)
   theory (automata, context-free languages, etc)
   algorithms and data structures (hash tables, graph algorithms, dynamic programming etc)
   computer architecture (assembly programming)
Front end : machine independent
phases/Language dependent
     Lexical analysis
     Syntax analysis
     Semantic analysis
     Intermediate code generation
     Some code optimization
Back end : machine dependent
phases/Language Independent
     Final code generation
    Machine-dependent
    optimizations
 Analysis:
      Lexical analysis
      Syntax analysis
      Semantic analysis
 Synthesis:
      Intermediate code generation
       code optimization
      Final code generation
      Storage a
position := initial + rate * 60


                                                                         intermediate code generator

            lexical analyzer


                                                                            temp1 := inttoreal (60)
          id1 := id2 + id3 * 60                                             temp2 := id3 * temp1
                                                                            temp3 := id2 + temp2
                                                                            id1   := temp3

            syntax analyzer


                                              The Phases of a Compiler
                 :=                                                              code optimizer
    id1
                       +

                 id2
                                  *                                          temp1 := id3 * 60.0
                                                                             id1   := id2 + temp1
    id3

    60


          semantic analyzer
                                                                                 code generator




            :=                                                                 MOVF    id3, R2
                                                                               MULF    #60.0, R2
                                                                               MOVF    id2, R1
    id1                 +                                                      ADDF    R2,    R1
                                                                               MOVF    R1,   id1

                 id2              *

                       id3        inttoreal
                                         60
Lexical Analyzer
• Lexical Analyzer reads the source program character by character
  to produce tokens.
• Normally a lexical analyzer doesn’t return a list of tokens at one
  shot, it returns a token when the parser asks a token from it.




source                     token
            Lexical
program                                  Parser
            Analyzer   get next token




                                  Notes | CodeReplugd
Token
• Token represents a set of strings described by a pattern.
    – Identifier represents a set of strings which start with a letter continues with letters and digits
    – The actual string (newval) is called as lexeme.
    – Tokens: identifier, number, addop, delimeter, …
• Since a token can represent more than one lexeme, additional information should
  be held for that specific lexeme. This additional information is called as the
  attribute of the token.
• For simplicity, a token may have a single attribute which holds the required
  information for that token.
    – For identifiers, this attribute a pointer to the symbol table, and the symbol table holds the
      actual attributes for that token.
• Some attributes:
    – <id,attr>                   where attr is pointer to the symbol table
    – <assgop,_>      no attribute is needed (if there is only one assignment operator)
    – <num,val>                   where val is the actual value of the number.
• Token type and its attribute uniquely identifies a lexeme.
• Regular expressions are widely used to specify patterns.

                                          Notes | CodeReplugd
Terminology of Languages
• Alphabet : a finite set of symbols (ASCII characters)
• String :
   –   Finite sequence of symbols on an alphabet
   –   Sentence and word are also used in terms of string
   –   ε is the empty string
   –   |s| is the length of string s.
• Language: sets of strings over some fixed alphabet
   –   ∅ the empty set is a language.
   –   {ε} the set containing empty string is a language
   –   The set of well-formed C programs is a language
   –   The set of all possible identifiers is a language.
• Operators on Strings:
   – Concatenation: xy represents the concatenation of strings x and y. s ε = s   εs=
     s
   – sn = s s s .. s ( n times) s0 = ε
                                     Notes | CodeReplugd
Operations on Languages
• Concatenation:
   – L1L2 = { s1s2 | s1 ∈ L1 and s2 ∈ L2 }


• Union
   – L1 ∪ L2 = { s | s ∈ L1 or s ∈ L2 }


• Exponentiation:
   – L0 = {ε}      L1 = L        L2 = LL


• Kleene Closure

   – L* =

• Positive Closure

   – L+ =


                                       Notes | CodeReplugd
Example
• L1 = {a,b,c,d}       L2 = {1,2}


• L1L2 = {a1,a2,b1,b2,c1,c2,d1,d2}

• L1 ∪ L2 = {a,b,c,d,1,2}

• L13 = all strings with length three (using a,b,c,d}


• L1* = all strings using letters a,b,c,d and empty string

• L1+ = doesn’t include the empty string

                             Notes | CodeReplugd
Regular Expressions
• We use regular expressions to describe tokens of a programming
  language.
• A regular expression is built up of simpler regular expressions
  (using defining rules)
• Each regular expression denotes a language.
• A language denoted by a regular expression is called as a regular
  set.




                            Notes | CodeReplugd
Regular Expressions (Rules)
Regular expressions over alphabet Σ

      Reg. Expr           Language it denotes
	

   ε	

 	

      	

   {ε}
      a∈ Σ	

       	

   {a}
      (r1) | (r2)         L(r1) ∪ L(r2)
      (r1) (r2)           L(r1) L(r2)
      (r)*                (L(r))*
      (r)                 L(r)

• (r)+ = (r)(r)*
• (r)? = (r) | ε

                              Notes | CodeReplugd
Regular Expressions (cont.)
• We may remove parentheses by using precedence rules.
   – *                        highest
   – concatenation            next
   – |                                     lowest
• ab*|c means           (a(b)*)|(c)

• Ex:
   –   Σ = {0,1}
   –   0|1 => {0,1}
   –   (0|1)(0|1) => {00,01,10,11}
   –   0* => {ε ,0,00,000,0000,....}
   – (0|1)* => all strings with 0 and 1, including the empty string




                                       Notes | CodeReplugd
Regular Definitions
• To write regular expression for some languages can be difficult,
  because their regular expressions can be quite complex. In those
  cases, we may use regular definitions.
• We can give names to regular expressions, and we can use these
  names as symbols to define other regular expressions.

• A regular definition is a sequence of the definitions of the form:
  d1 → r1              where di is a distinct name and
   d2 → r2                       ri is a regular expression over symbols
   in
      .                               Σ∪{d1,d2,...,di-1}
   dn → rn
                            basic symbols          previously defined names
                             Notes | CodeReplugd
Regular Definitions (cont.)
• Ex: Identifiers in Pascal
                      letter → A | B | ... | Z | a | b | ... | z
                      digit → 0 | 1 | ... | 9
                      id → letter (letter | digit ) *
    – If we try to write the regular expression representing identifiers without using regular
      definitions, that regular expression will be complex.
                      (A|...|Z|a|...|z) ( (A|...|Z|a|...|z) | (0|...|9) ) *


• Ex: Unsigned numbers in Pascal
                      digit → 0 | 1 | ... | 9
                      digits → digit +
                      opt-fraction → ( . digits ) ?
                      opt-exponent → ( E (+|-)? digits ) ?
                      unsigned-num → digits opt-fraction opt-exponent




                                         Notes | CodeReplugd
Finite Automata
• A recognizer for a language is a program that takes a string x, and answers “yes”
  if x is a sentence of that language, and “no” otherwise.
• We call the recognizer of the tokens as a finite automaton.
• A finite automaton can be: deterministic(DFA) or non-deterministic (NFA)
• This means that we may use a deterministic or non-deterministic automaton as a
  lexical analyzer.
• Both deterministic and non-deterministic finite automaton recognize regular sets.
• Which one?
    – deterministic – faster recognizer, but it may take more space
    – non-deterministic – slower, but it may take less space
    – Deterministic automatons are widely used lexical analyzers.
• First, we define regular expressions for tokens; Then we convert them into a
  DFA to get a lexical analyzer for our tokens.
    – Algorithm1: Regular Expression    NFA  DFA (two steps: first to NFA, then to
      DFA)
    – Algorithm2: Regular Expression  DFA (directly convert a regular expression into a
      DFA)
                                        Notes | CodeReplugd
Non-Deterministic Finite Automaton (NFA)
• A non-deterministic finite automaton (NFA) is a mathematical
  model that consists of:
   –   S - a set of states
   –   Σ - a set of input symbols (alphabet)
   –   move – a transition function move to map state-symbol pairs to sets of states.
   –   s0 - a start (initial) state
   –   F – a set of accepting states (final states)


• ε- transitions are allowed in NFAs. In other words, we can move
  from one state to another one without consuming any symbol.
• A NFA accepts a string x, if and only if there is a path from the
  starting state to one of accepting states such that edge labels along
  this path spell out x.


                                       Notes | CodeReplugd
NFA (Example)
            a                                           0 is the start state s0
                                                        {2} is the set of final states F
                 a               b                      Σ = {a,b}
            0            1                   2
 start                                                  S = {0,1,2}
            b                                           Transition Function:          a      b
                                                                                 0 {0,1}    {0}
         Transition graph of the NFA                                             1      _   {2}
                                                                                 2      _     _
The language recognized by this NFA is (a|b) * a b




                                       Notes | CodeReplugd
Deterministic Finite Automaton (DFA)

• A Deterministic Finite Automaton (DFA) is a special form of a NFA.
  • no state has ε- transition
  • for each symbol a and state s, there is at most one labeled edge a leaving s.
    i.e. transition function is from pair of state-symbol to state (not set of states)



                           a
           b       a
                                           The language recognized by
               a           b
       0               1          2
                                           this DFA is also (a|b) * a b
                       b




                                 Notes | CodeReplugd
Implementing a DFA
• Le us assume that the end of a string is marked with a special
  symbol (say eos). The algorithm for recognition will be as follows:
  (an efficient implementation)
   s    s0                 { start from the initial state }
   c  nextchar                       { get the next character from the input string }
   while (c != eos) do    { do until the en dof the string }
       begin
                     s  move(s,c) { transition function }
                     c  nextchar
       end
   if (s in F) then       { if s is an accepting state }
       return “yes”
   else
       return “no”


                                   Notes | CodeReplugd
Implementing a NFA

    S  ε-closure({s0})                 { set all of states can be accessible from s0 by ε-
         transitions }
    c  nextchar
    while (c != eos) {
         begin
             s  ε-closure(move(S,c)) { set of all states can be accessible from a state in S
                       c  nextchar                 by a transition on c }
         end
    if (S∩F != Φ) then                { if S contains an accepting state }
         return “yes”
    else
         return “no”


•   This algorithm is not efficient.

                                    Notes | CodeReplugd
Converting A Regular Expression into A NFA
             (Thomson’s Construction)
• This is one way to convert a regular expression into a NFA.
• There can be other ways (much efficient) for the conversion.
• Thomson’s Construction is simple and systematic method.
  It guarantees that the resulting NFA will have exactly one final state,
  and one start state.
• Construction starts from simplest parts (alphabet symbols).
  To create a NFA for a complex regular expression, NFAs of its
  sub-expressions are combined to create its NFA,




                             Notes | CodeReplugd
Thomson’s Construction (cont.)
                                                                    ε

• To recognize an empty string ε                                i       f


                                                                    a
• To recognize a symbol a in the alphabet Σ                     i       f



• If N(r1) and N(r2) are NFAs for regular expressions r1 and
r2
    • For regular expression r1 | r2

         ε        N(r1)   ε

     i       ε                f               NFA for r1 | r2
                          ε
                  N(r2)




                                  Notes | CodeReplugd
Thomson’s Construction (cont.)

• For regular expression r1 r2

          i       N(r1)         N(r2)   f           Final state of N(r2) become final state of N(r1r2)


              NFA for r1 r2


• For regular expression r*
                            ε

              ε                             ε
      i                    N(r)                 f

                            ε

                      NFA for r*

                                                Notes | CodeReplugd
Thomson’s Construction (Example - (a|b) * a )

         a                                             a
a:                                             ε               ε
                         (a | b)
                                               ε               ε
        b                                              b
b:
                               ε

                               a         ε
                     ε
                 ε
     (a|b)   *       ε                   ε
                                                   ε

                               b
                                ε



                               ε
                               a         ε
                     ε
                 ε                                 ε       a
     (a|b) * a       ε
                               b
                                         ε


                               ε



                         Notes | CodeReplugd
Converting a NFA into a DFA (subset construction)
     put ε-closure({s0}) as an unmarked state into the set of DFA (DS)
     while (there is one unmarked S1 in DS) do
                                                      ε-closure({s0}) is the set of all states can be accessible
        begin                                         from s0 by ε-transition.
                      mark S1
                      for each input symbol a dofrom a state s in S there is a transition on
                                                 set of states to which
                                                  a                    1
                         begin
                           S2  ε-closure(move(S1,a))
                           if (S2 is not in DS) then
                                add S2 into DS as an unmarked state
                           transfunc[S1,a]  S2
                         end
                     end

•   a state S in DS is an accepting state of DFA if a state in S is an accepting state of NFA
•   the start state of DFA is ε-closure({s0})


                                              Notes | CodeReplugd
Converting a NFA into a DFA (Example)
                                  2          3
                                       a         ε
                  0       1   ε
                      ε                              6       7       8
                                                         ε       a
                              ε                  ε
                                  4          5
                                       b

                                       ε

S0 = ε-closure({0}) = {0,1,2,4,7}       S0 into DS as an unmarked state
                    ⇓ mark S0
ε-closure(move(S0,a)) = ε-closure({3,8}) = {1,2,3,4,6,7,8} = S1       S1 into DS
ε-closure(move(S0,b)) = ε-closure({5}) = {1,2,4,5,6,7} = S2           S2 into DS
          transfunc[S0,a]  S1          transfunc[S0,b]  S2
                     ⇓ mark S1
ε-closure(move(S1,a)) = ε-closure({3,8}) = {1,2,3,4,6,7,8} = S1
ε-closure(move(S1,b)) = ε-closure({5}) = {1,2,4,5,6,7} = S2
          transfunc[S1,a]  S1          transfunc[S1,b]  S2
                     ⇓ mark S2
ε-closure(move(S2,a)) = ε-closure({3,8}) = {1,2,3,4,6,7,8} = S1
ε-closure(move(S2,b)) = ε-closure({5}) = {1,2,4,5,6,7} = S2
          transfunc[S2,a]  S1          transfunc[S2,b]  S2
                                      Notes | CodeReplugd
Converting a NFA into a DFA (Example – cont.)

S0 is the start state of DFA since 0 is a member of S0={0,1,2,4,7}
S1 is an accepting state of DFA since 8 is a member of S1 = {1,2,3,4,6,7,8}


                                          a

                                      S1

                             a

                      S0             b        a

                                 b


                                      S2

                                      b




                                     Notes | CodeReplugd
Converting Regular Expressions Directly to DFAs
• We may convert a regular expression into a DFA (without creating a
  NFA first).
• First we augment the given regular expression by concatenating it
  with a special symbol #.
        r        (r)# augmented regular expression
• Then, we create a syntax tree for this augmented regular expression.
• In this syntax tree, all alphabet symbols (plus # and the empty
  string) in the augmented regular expression will be on the leaves,
  and all inner nodes will be the operators in that augmented regular
  expression.
• Then each alphabet symbol (plus #) will be numbered (position
  numbers).


                            Notes | CodeReplugd
Regular Expression                          DFA (cont.)

(a|b) * a           (a|b) * a #       augmented regular expression


                 •            Syntax tree of (a|b) * a #
            •         #
                      4
        *        a
                 3            • each symbol is numbered (positions)
        |                     • each symbol is at a leave
    a        b
    1        2                • inner nodes are operators




                                   Notes | CodeReplugd
followpos
Then we define the function followpos for the positions (positions
assigned to leaves).

       followpos(i) -- is the set of positions which can follow
                        the position i in the strings generated by
                        the augmented regular expression.

For example,    ( a | b) * a #
                   1 2    3 4

       followpos(1) = {1,2,3}
       followpos(2) = {1,2,3}              followpos is just defined for leaves,
                                           it is not defined for inner nodes.
       followpos(3) = {4}
       followpos(4) = {}

                             Notes | CodeReplugd
firstpos, lastpos, nullable
• To evaluate followpos, we need three more functions to be defined
  for the nodes (not just for leaves) of the syntax tree.

• firstpos(n) -- the set of the positions of the first symbols of strings
                           generated by the sub-expression rooted by
  n.

• lastpos(n) -- the set of the positions of the last symbols of strings
                          generated by the sub-expression rooted by
  n.

• nullable(n) -- true if the empty string is a member of strings
                                 generated by the sub-expression
  rooted by n                             false otherwise
                             Notes | CodeReplugd
How to evaluate firstpos, lastpos, nullable
           n       nullable(n)               firstpos(n)                    lastpos(n)
leaf labeled ε           true                       Φ                             Φ


leaf labeled            false                      {i}                            {i}
with position i

      |           nullable(c1) or    firstpos(c1) ∪ firstpos(c2)     lastpos(c1) ∪ lastpos(c2)
 c1        c2     nullable(c2)

      •           nullable(c1) and   if (nullable(c1))               if (nullable(c2))
 c1        c2     nullable(c2)         firstpos(c1) ∪ firstpos(c2)     lastpos(c1) ∪ lastpos(c2)
      *                  true        else firstpos(c1)
                                     firstpos(c1)                    else lastpos(c2)
                                                                     lastpos(c1)
      c1




                                       Notes | CodeReplugd
How to evaluate followpos
•   Two-rules define the function followpos:

1. If n is concatenation-node with left child c1 and right child c2,
   and i is a position in lastpos(c1), then all positions in firstpos(c2)
   are in followpos(i).
2. If n is a star-node, and i is a position in lastpos(n), then all
   positions in firstpos(n) are in followpos(i).

•   If firstpos and lastpos have been computed for each node,
    followpos of each position can be computed by making one depth-
    first traversal of the syntax tree.



                              Notes | CodeReplugd
Example -- ( a | b) * a #

                               green – firstpos
            {1,2,3} • {4}
                               blue – lastpos
       {1,2,3} {3} {4}# {4}
              •
                        4
     {1,2}*{1,2}{3}a{3}
                   3
                              Then we can calculate followpos
     {1,2} | {1,2}

  {1} a {1} {2}b {2}          followpos(1) = {1,2,3}
      1        2
                              followpos(2) = {1,2,3}
                              followpos(3) = {4}
                              followpos(4) = {}


• After we calculate follow positions, we are ready to create DFA
  for the regular expression.
                               Notes | CodeReplugd
Algorithm (RE                      DFA)
•   Create the syntax tree of (r) #
•   Calculate the functions: followpos, firstpos, lastpos, nullable
•   Put firstpos(root) into the states of DFA as an unmarked state.
•   while (there is an unmarked state S in the states of DFA) do
     – mark S
     – for each input symbol a do
         • let s1,...,sn are positions in S and symbols in those positions are a
         • S’  followpos(s1) ∪ ... ∪ followpos(sn)
         • move(S,a)  S’
         • if (S’ is not empty and not in the states of DFA)
              – put S’ into the states of DFA as an unmarked state.


• the start state of DFA is firstpos(root)
• the accepting states of DFA are all states containing the position of #

                                    Notes | CodeReplugd
Example -- ( a | b) * a #
                                                  1        2           3 4


followpos(1)={1,2,3}      followpos(2)={1,2,3}                 followpos(3)={4}
    followpos(4)={}

S1=firstpos(root)={1,2,3}
	

 	

  ⇓ mark S1
a: followpos(1) ∪ followpos(3)={1,2,3,4}=S2                        move(S1,a)=S2
b: followpos(2)={1,2,3}=S1                                                 move(S1,b)=S1
	

 	

  ⇓ mark S2
a: followpos(1) ∪ followpos(3)={1,2,3,4}=S2                        move(S2,a)=S2
b: followpos(2)={1,2,3}=S1                                                 move(S2,b)=S1
                                            b                           a
                                                       a
                                             S1                   S2

                                                           b
start state: S1
accepting states: {S2}           Notes | CodeReplugd
Example -- ( a | ε) b c* #
                                                 1       2   3   4
followpos(1)={2}      followpos(2)={3,4}      followpos(3)={3,4}
followpos(4)={}

S1=firstpos(root)={1,2}
	

       ⇓ mark S1
a: followpos(1)={2}=S2               move(S1,a)=S2
b: followpos(2)={3,4}=S3 move(S1,b)=S3
	

       ⇓ mark S2
b: followpos(2)={3,4}=S3             move(S2,b)=S3
                                                                          S2
	

       ⇓ mark S3                                                   a
                                                                          b
c: followpos(3)={3,4}=S3 move(S3,c)=S3                           S1
                                                                      b
                                                                          S3   c


start state: S1
                                   Notes | CodeReplugd
Minimizing Number of States of a DFA
• partition the set of states into two groups:
   – G1 : set of accepting states
   – G2 : set of non-accepting states


• For each new group G
   – partition G into subgroups such that states s1 and s2 are in the same group iff
      for all input symbols a, states s1 and s2 have transitions to states in the same group.


• Start state of the minimized DFA is the group containing
  the start state of the original DFA.
• Accepting states of the minimized DFA are the groups containing
  the accepting states of the original DFA.


                                     Notes | CodeReplugd
Minimizing DFA - Example
              a
                               G1 = {2}
              2
      a                        G2 = {1,3}
  1           b        a
          b
              3                G2 cannot be partitioned because
                                          move(1,a)=2   move(1,b)=3
              b
                                          move(3,a)=2   move(2,b)=3


So, the minimized DFA (with minimum states)

                   b                  a

                  {1,3}    a    {2}

                           b




                                 Notes | CodeReplugd
Minimizing DFA – Another Example
                a
            2
       a            a
                                        Groups:            {1,2,3}         {4}
   1         b              4
                    a
       b
                                        {1,2}                        {3}          a      b
            3           b       no more partitioning                             1->2   1->3
                                                                                 2->2   2->3
            b                                                                    3->4   3->3



So, the minimized DFA                               b

                                                   {3}
                                 a        b
                                {1,2}                a      b

                                          a        {4}




                                     Notes | CodeReplugd
Some Other Issues in Lexical Analyzer
• The lexical analyzer has to recognize the longest possible string.
   – Ex: identifier newval -- n             ne     new       newv     newva       newval


• What is the end of a token? Is there any character which marks the
  end of a token?
   – It is normally not defined.
   – If the number of characters in a token is fixed, in that case no problem: + -
   – But <                 < or <> (in Pascal)
   – The end of an identifier : the characters cannot be in an identifier can mark the end of
     token.
   – We may need a lookhead
       • In Prolog:             p :- X is 1.            p :- X is
         1.5.                                            The dot followed by a white space character
         can mark the end of a number.                        But if that is not the case, the dot must
         be treated as a part of the number.



                                       Notes | CodeReplugd
Some Other Issues in Lexical Analyzer (cont.)
• Skipping comments
   – Normally we don’t return a comment as a token.
   – We skip a comment, and return the next token (which is not a comment) to the
     parser.
   – So, the comments are only processed by the lexical analyzer, and the don’t
     complicate the syntax of the language.


• Symbol table interface
   – symbol table holds information about tokens (at least lexeme of identifiers)
   – how to implement the symbol table, and what kind of operations.
       • hash table – open addressing, chaining
       • putting into the hash table, finding the position of a token from its lexeme.



• Positions of the tokens in the file (for the error handling).

                                       Notes | CodeReplugd

Weitere ähnliche Inhalte

Was ist angesagt?

Introduction to bitcoin
Introduction to bitcoinIntroduction to bitcoin
Introduction to bitcoinWolf McNally
 
Instruction Set Architecture (ISA)
Instruction Set Architecture (ISA)Instruction Set Architecture (ISA)
Instruction Set Architecture (ISA)Gaditek
 
Data structure & its types
Data structure & its typesData structure & its types
Data structure & its typesRameesha Sadaqat
 
Introduction to Cryptocurrency (Bitcoin)
Introduction to Cryptocurrency (Bitcoin)Introduction to Cryptocurrency (Bitcoin)
Introduction to Cryptocurrency (Bitcoin)Kashif Khans
 
Block cipher modes of operation
Block cipher modes of operation Block cipher modes of operation
Block cipher modes of operation harshit chavda
 
Error detection & correction codes
Error detection & correction codesError detection & correction codes
Error detection & correction codesRevathi Subramaniam
 
Synchronous vs Asynchronous Programming
Synchronous vs Asynchronous ProgrammingSynchronous vs Asynchronous Programming
Synchronous vs Asynchronous Programmingjeetendra mandal
 
heap Sort Algorithm
heap  Sort Algorithmheap  Sort Algorithm
heap Sort AlgorithmLemia Algmri
 
Bubble Sort Algorithm Presentation
Bubble Sort Algorithm Presentation Bubble Sort Algorithm Presentation
Bubble Sort Algorithm Presentation AhmedAlbutty
 
Input output organization
Input output organizationInput output organization
Input output organizationabdulugc
 
Transaction processing ppt
Transaction processing pptTransaction processing ppt
Transaction processing pptJaved Khan
 
cryptography ppt free download
cryptography ppt free downloadcryptography ppt free download
cryptography ppt free downloadTwinkal Harsora
 

Was ist angesagt? (20)

Introduction to bitcoin
Introduction to bitcoinIntroduction to bitcoin
Introduction to bitcoin
 
process control block
process control blockprocess control block
process control block
 
Instruction Set Architecture (ISA)
Instruction Set Architecture (ISA)Instruction Set Architecture (ISA)
Instruction Set Architecture (ISA)
 
Data structure & its types
Data structure & its typesData structure & its types
Data structure & its types
 
Introduction to Cryptocurrency (Bitcoin)
Introduction to Cryptocurrency (Bitcoin)Introduction to Cryptocurrency (Bitcoin)
Introduction to Cryptocurrency (Bitcoin)
 
Transaction Processing in DBMS.pptx
Transaction Processing in DBMS.pptxTransaction Processing in DBMS.pptx
Transaction Processing in DBMS.pptx
 
The Dark Web
The Dark WebThe Dark Web
The Dark Web
 
Block cipher modes of operation
Block cipher modes of operation Block cipher modes of operation
Block cipher modes of operation
 
Operating System lab
Operating System labOperating System lab
Operating System lab
 
Error detection & correction codes
Error detection & correction codesError detection & correction codes
Error detection & correction codes
 
Synchronous vs Asynchronous Programming
Synchronous vs Asynchronous ProgrammingSynchronous vs Asynchronous Programming
Synchronous vs Asynchronous Programming
 
Bit coin
Bit coinBit coin
Bit coin
 
Linked list
Linked listLinked list
Linked list
 
heap Sort Algorithm
heap  Sort Algorithmheap  Sort Algorithm
heap Sort Algorithm
 
Bitcoin
BitcoinBitcoin
Bitcoin
 
Bubble Sort Algorithm Presentation
Bubble Sort Algorithm Presentation Bubble Sort Algorithm Presentation
Bubble Sort Algorithm Presentation
 
Input output organization
Input output organizationInput output organization
Input output organization
 
Transaction processing ppt
Transaction processing pptTransaction processing ppt
Transaction processing ppt
 
Bitcoin
BitcoinBitcoin
Bitcoin
 
cryptography ppt free download
cryptography ppt free downloadcryptography ppt free download
cryptography ppt free download
 

Andere mochten auch

Compiler Design
Compiler DesignCompiler Design
Compiler DesignMir Majid
 
Data Locality
Data LocalityData Locality
Data LocalitySyam Lal
 
Compiler design syntax analysis
Compiler design syntax analysisCompiler design syntax analysis
Compiler design syntax analysisRicha Sharma
 
Compiler Engineering Lab#3
Compiler Engineering Lab#3Compiler Engineering Lab#3
Compiler Engineering Lab#3MashaelQ
 
Compiler Engineering Lab#1
Compiler Engineering Lab#1Compiler Engineering Lab#1
Compiler Engineering Lab#1MashaelQ
 
Cs6660 compiler design may june 2016 Answer Key
Cs6660 compiler design may june 2016 Answer KeyCs6660 compiler design may june 2016 Answer Key
Cs6660 compiler design may june 2016 Answer Keyappasami
 
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
 
Cs6660 compiler design november december 2016 Answer key
Cs6660 compiler design november december 2016 Answer keyCs6660 compiler design november december 2016 Answer key
Cs6660 compiler design november december 2016 Answer keyappasami
 
Compiler design tutorial
Compiler design tutorialCompiler design tutorial
Compiler design tutorialVarsha Shukla
 

Andere mochten auch (20)

Compiler Design
Compiler DesignCompiler Design
Compiler Design
 
Compiler unit 5
Compiler  unit 5Compiler  unit 5
Compiler unit 5
 
Compiler unit 4
Compiler unit 4Compiler unit 4
Compiler unit 4
 
Lexical analyzer
Lexical analyzerLexical analyzer
Lexical analyzer
 
Data Locality
Data LocalityData Locality
Data Locality
 
Compiler design
Compiler designCompiler design
Compiler design
 
Lexical analyzer
Lexical analyzerLexical analyzer
Lexical analyzer
 
Role-of-lexical-analysis
Role-of-lexical-analysisRole-of-lexical-analysis
Role-of-lexical-analysis
 
Specification-of-tokens
Specification-of-tokensSpecification-of-tokens
Specification-of-tokens
 
Compiler design syntax analysis
Compiler design syntax analysisCompiler design syntax analysis
Compiler design syntax analysis
 
Compiler Engineering Lab#3
Compiler Engineering Lab#3Compiler Engineering Lab#3
Compiler Engineering Lab#3
 
Compiler Engineering Lab#1
Compiler Engineering Lab#1Compiler Engineering Lab#1
Compiler Engineering Lab#1
 
Cs6660 compiler design may june 2016 Answer Key
Cs6660 compiler design may june 2016 Answer KeyCs6660 compiler design may june 2016 Answer Key
Cs6660 compiler design may june 2016 Answer Key
 
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
 
Bottomupparser
BottomupparserBottomupparser
Bottomupparser
 
Bottomupparser
BottomupparserBottomupparser
Bottomupparser
 
Cd2 [autosaved]
Cd2 [autosaved]Cd2 [autosaved]
Cd2 [autosaved]
 
Cs6660 compiler design november december 2016 Answer key
Cs6660 compiler design november december 2016 Answer keyCs6660 compiler design november december 2016 Answer key
Cs6660 compiler design november december 2016 Answer key
 
Compiler design tutorial
Compiler design tutorialCompiler design tutorial
Compiler design tutorial
 

Ähnlich wie Compiler Design: Lexical Analyzer Basics

Ähnlich wie Compiler Design: Lexical Analyzer Basics (20)

1 cc
1 cc1 cc
1 cc
 
Chapter 1 1
Chapter 1 1Chapter 1 1
Chapter 1 1
 
Unit1.ppt
Unit1.pptUnit1.ppt
Unit1.ppt
 
Compiler1
Compiler1Compiler1
Compiler1
 
phases of a compiler
 phases of a compiler phases of a compiler
phases of a compiler
 
CD U1-5.pptx
CD U1-5.pptxCD U1-5.pptx
CD U1-5.pptx
 
Compiler Construction
Compiler ConstructionCompiler Construction
Compiler Construction
 
Introduction to compiler
Introduction to compilerIntroduction to compiler
Introduction to compiler
 
Cpcs302 1
Cpcs302  1Cpcs302  1
Cpcs302 1
 
Presentation1
Presentation1Presentation1
Presentation1
 
Presentation1
Presentation1Presentation1
Presentation1
 
Concept of compiler in details
Concept of compiler in detailsConcept of compiler in details
Concept of compiler in details
 
Lecture 01 introduction to compiler
Lecture 01 introduction to compilerLecture 01 introduction to compiler
Lecture 01 introduction to compiler
 
Compiler design
Compiler designCompiler design
Compiler design
 
Cd econtent link1
Cd econtent link1Cd econtent link1
Cd econtent link1
 
Code Analysis-run time error prediction
Code Analysis-run time error predictionCode Analysis-run time error prediction
Code Analysis-run time error prediction
 
Techniques & applications of Compiler
Techniques & applications of CompilerTechniques & applications of Compiler
Techniques & applications of Compiler
 
Compilers Design
Compilers DesignCompilers Design
Compilers Design
 
SS & CD Module 3
SS & CD Module 3 SS & CD Module 3
SS & CD Module 3
 
Module 2
Module 2 Module 2
Module 2
 

Kürzlich hochgeladen

MS4 level being good citizen -imperative- (1) (1).pdf
MS4 level   being good citizen -imperative- (1) (1).pdfMS4 level   being good citizen -imperative- (1) (1).pdf
MS4 level being good citizen -imperative- (1) (1).pdfMr Bounab Samir
 
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptxDecoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptxDhatriParmar
 
Narcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfNarcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfPrerana Jadhav
 
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptxDIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptxMichelleTuguinay1
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptxmary850239
 
Tree View Decoration Attribute in the Odoo 17
Tree View Decoration Attribute in the Odoo 17Tree View Decoration Attribute in the Odoo 17
Tree View Decoration Attribute in the Odoo 17Celine George
 
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...Association for Project Management
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Projectjordimapav
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management SystemChristalin Nelson
 
Indexing Structures in Database Management system.pdf
Indexing Structures in Database Management system.pdfIndexing Structures in Database Management system.pdf
Indexing Structures in Database Management system.pdfChristalin Nelson
 
Grade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptxGrade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptxkarenfajardo43
 
Oppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmOppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmStan Meyer
 
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
 
Mythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITWMythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITWQuiz Club NITW
 
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...DhatriParmar
 
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
 

Kürzlich hochgeladen (20)

MS4 level being good citizen -imperative- (1) (1).pdf
MS4 level   being good citizen -imperative- (1) (1).pdfMS4 level   being good citizen -imperative- (1) (1).pdf
MS4 level being good citizen -imperative- (1) (1).pdf
 
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptxDecoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
 
Narcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfNarcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdf
 
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptxDIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx
 
Tree View Decoration Attribute in the Odoo 17
Tree View Decoration Attribute in the Odoo 17Tree View Decoration Attribute in the Odoo 17
Tree View Decoration Attribute in the Odoo 17
 
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
 
prashanth updated resume 2024 for Teaching Profession
prashanth updated resume 2024 for Teaching Professionprashanth updated resume 2024 for Teaching Profession
prashanth updated resume 2024 for Teaching Profession
 
Paradigm shift in nursing research by RS MEHTA
Paradigm shift in nursing research by RS MEHTAParadigm shift in nursing research by RS MEHTA
Paradigm shift in nursing research by RS MEHTA
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Project
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management System
 
Indexing Structures in Database Management system.pdf
Indexing Structures in Database Management system.pdfIndexing Structures in Database Management system.pdf
Indexing Structures in Database Management system.pdf
 
Mattingly "AI & Prompt Design: Large Language Models"
Mattingly "AI & Prompt Design: Large Language Models"Mattingly "AI & Prompt Design: Large Language Models"
Mattingly "AI & Prompt Design: Large Language Models"
 
Grade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptxGrade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptx
 
Oppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmOppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and Film
 
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
 
Mythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITWMythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITW
 
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptxINCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
 
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
 
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
 

Compiler Design: Lexical Analyzer Basics

  • 1. Compiler Design CodeReplugd
  • 2. COMPILERS  A compiler is a program takes a program written in a source language and translates it into an equivalent program in a target language. source program COMPILER target program ( Normally a program written ( Normally the equivalent in a high-level programming program in machine code – language) relocatable object file) error messages Other Applications  In addition to the development of a compiler, the techniques used in compiler design can be applicable to many problems in computer science.  Techniques used in a lexical analyzer can be used in text editors, information retrieval system, and pattern recognition programs.  Techniques used in a parser can be used in a query processing system such as SQL.  Many software having a complex front-end may need techniques used in compiler design.  A symbolic equation solver which takes an equation as input. That program should parse the given input equation.  Most of the techniques used in compiler design can be used in Natural Language Processing (NLP) systems.
  • 3. An interpreter reads an executable source program written in a high-level programming language as well as data for this program, and it runs the program against the data to produce some results.  One example is the Unix shell interpreter, which runs operating system commands interactively
  • 4. Cousins of compiler  Preprocessor(Macro processing, file inclusion, Rational preprocessor, Language extensions)  Assembler  Linkers-A linker takes several object les and libraries as input and produces one executable object file. It retrieves from the input files (and puts them together in the executable object file) the code of all the referenced functions/procedures and it resolves all external references to real addresses. The libraries include the operating system libraries, the language-specic libraries, and, maybe, user-created libraries.  Loaders  Debuggers-used to determine execution errors in a compiled program  Profiler- Collects Statistics on the behavior of the object program during execution  Project Managers-used to coordinate and merge the source code produced by different members of the team
  • 5. What is the Challenge in compiler Design? Many variations:  many programming languages (eg, FORTRAN, C++, Java)  many programming paradigms (eg, object-oriented, functional, logic)  many computer architectures (eg, MIPS, SPARC, Intel, alpha)  many operating systems (eg, Linux, Solaris, Windows) Qualities of a compiler (in order of importance):  the compiler itself must be bug-free  it must generate correct machine code  the generated machine code must run fast  the compiler itself must run fast (compilation time must be proportional to program size)  the compiler must be portable (ie, modular, supporting separate compilation)  it must print good diagnostics and error messages  the generated code must work well with existing debuggers  must have consistent and predictable optimization. Building a compiler requires knowledge of  programming languages (parameter passing, variable scoping, memory allocation, etc)  theory (automata, context-free languages, etc)  algorithms and data structures (hash tables, graph algorithms, dynamic programming etc)  computer architecture (assembly programming)
  • 6.
  • 7.
  • 8. Front end : machine independent phases/Language dependent Lexical analysis Syntax analysis Semantic analysis Intermediate code generation Some code optimization Back end : machine dependent phases/Language Independent Final code generation Machine-dependent optimizations Analysis: Lexical analysis Syntax analysis Semantic analysis Synthesis: Intermediate code generation code optimization Final code generation Storage a
  • 9.
  • 10. position := initial + rate * 60 intermediate code generator lexical analyzer temp1 := inttoreal (60) id1 := id2 + id3 * 60 temp2 := id3 * temp1 temp3 := id2 + temp2 id1 := temp3 syntax analyzer The Phases of a Compiler := code optimizer id1 + id2 * temp1 := id3 * 60.0 id1 := id2 + temp1 id3 60 semantic analyzer code generator := MOVF id3, R2 MULF #60.0, R2 MOVF id2, R1 id1 + ADDF R2, R1 MOVF R1, id1 id2 * id3 inttoreal 60
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17. Lexical Analyzer • Lexical Analyzer reads the source program character by character to produce tokens. • Normally a lexical analyzer doesn’t return a list of tokens at one shot, it returns a token when the parser asks a token from it. source token Lexical program Parser Analyzer get next token Notes | CodeReplugd
  • 18. Token • Token represents a set of strings described by a pattern. – Identifier represents a set of strings which start with a letter continues with letters and digits – The actual string (newval) is called as lexeme. – Tokens: identifier, number, addop, delimeter, … • Since a token can represent more than one lexeme, additional information should be held for that specific lexeme. This additional information is called as the attribute of the token. • For simplicity, a token may have a single attribute which holds the required information for that token. – For identifiers, this attribute a pointer to the symbol table, and the symbol table holds the actual attributes for that token. • Some attributes: – <id,attr> where attr is pointer to the symbol table – <assgop,_> no attribute is needed (if there is only one assignment operator) – <num,val> where val is the actual value of the number. • Token type and its attribute uniquely identifies a lexeme. • Regular expressions are widely used to specify patterns. Notes | CodeReplugd
  • 19. Terminology of Languages • Alphabet : a finite set of symbols (ASCII characters) • String : – Finite sequence of symbols on an alphabet – Sentence and word are also used in terms of string – ε is the empty string – |s| is the length of string s. • Language: sets of strings over some fixed alphabet – ∅ the empty set is a language. – {ε} the set containing empty string is a language – The set of well-formed C programs is a language – The set of all possible identifiers is a language. • Operators on Strings: – Concatenation: xy represents the concatenation of strings x and y. s ε = s εs= s – sn = s s s .. s ( n times) s0 = ε Notes | CodeReplugd
  • 20. Operations on Languages • Concatenation: – L1L2 = { s1s2 | s1 ∈ L1 and s2 ∈ L2 } • Union – L1 ∪ L2 = { s | s ∈ L1 or s ∈ L2 } • Exponentiation: – L0 = {ε} L1 = L L2 = LL • Kleene Closure – L* = • Positive Closure – L+ = Notes | CodeReplugd
  • 21. Example • L1 = {a,b,c,d} L2 = {1,2} • L1L2 = {a1,a2,b1,b2,c1,c2,d1,d2} • L1 ∪ L2 = {a,b,c,d,1,2} • L13 = all strings with length three (using a,b,c,d} • L1* = all strings using letters a,b,c,d and empty string • L1+ = doesn’t include the empty string Notes | CodeReplugd
  • 22. Regular Expressions • We use regular expressions to describe tokens of a programming language. • A regular expression is built up of simpler regular expressions (using defining rules) • Each regular expression denotes a language. • A language denoted by a regular expression is called as a regular set. Notes | CodeReplugd
  • 23. Regular Expressions (Rules) Regular expressions over alphabet Σ Reg. Expr Language it denotes ε {ε} a∈ Σ {a} (r1) | (r2) L(r1) ∪ L(r2) (r1) (r2) L(r1) L(r2) (r)* (L(r))* (r) L(r) • (r)+ = (r)(r)* • (r)? = (r) | ε Notes | CodeReplugd
  • 24. Regular Expressions (cont.) • We may remove parentheses by using precedence rules. – * highest – concatenation next – | lowest • ab*|c means (a(b)*)|(c) • Ex: – Σ = {0,1} – 0|1 => {0,1} – (0|1)(0|1) => {00,01,10,11} – 0* => {ε ,0,00,000,0000,....} – (0|1)* => all strings with 0 and 1, including the empty string Notes | CodeReplugd
  • 25. Regular Definitions • To write regular expression for some languages can be difficult, because their regular expressions can be quite complex. In those cases, we may use regular definitions. • We can give names to regular expressions, and we can use these names as symbols to define other regular expressions. • A regular definition is a sequence of the definitions of the form: d1 → r1 where di is a distinct name and d2 → r2 ri is a regular expression over symbols in . Σ∪{d1,d2,...,di-1} dn → rn basic symbols previously defined names Notes | CodeReplugd
  • 26. Regular Definitions (cont.) • Ex: Identifiers in Pascal letter → A | B | ... | Z | a | b | ... | z digit → 0 | 1 | ... | 9 id → letter (letter | digit ) * – If we try to write the regular expression representing identifiers without using regular definitions, that regular expression will be complex. (A|...|Z|a|...|z) ( (A|...|Z|a|...|z) | (0|...|9) ) * • Ex: Unsigned numbers in Pascal digit → 0 | 1 | ... | 9 digits → digit + opt-fraction → ( . digits ) ? opt-exponent → ( E (+|-)? digits ) ? unsigned-num → digits opt-fraction opt-exponent Notes | CodeReplugd
  • 27. Finite Automata • A recognizer for a language is a program that takes a string x, and answers “yes” if x is a sentence of that language, and “no” otherwise. • We call the recognizer of the tokens as a finite automaton. • A finite automaton can be: deterministic(DFA) or non-deterministic (NFA) • This means that we may use a deterministic or non-deterministic automaton as a lexical analyzer. • Both deterministic and non-deterministic finite automaton recognize regular sets. • Which one? – deterministic – faster recognizer, but it may take more space – non-deterministic – slower, but it may take less space – Deterministic automatons are widely used lexical analyzers. • First, we define regular expressions for tokens; Then we convert them into a DFA to get a lexical analyzer for our tokens. – Algorithm1: Regular Expression  NFA  DFA (two steps: first to NFA, then to DFA) – Algorithm2: Regular Expression  DFA (directly convert a regular expression into a DFA) Notes | CodeReplugd
  • 28. Non-Deterministic Finite Automaton (NFA) • A non-deterministic finite automaton (NFA) is a mathematical model that consists of: – S - a set of states – Σ - a set of input symbols (alphabet) – move – a transition function move to map state-symbol pairs to sets of states. – s0 - a start (initial) state – F – a set of accepting states (final states) • ε- transitions are allowed in NFAs. In other words, we can move from one state to another one without consuming any symbol. • A NFA accepts a string x, if and only if there is a path from the starting state to one of accepting states such that edge labels along this path spell out x. Notes | CodeReplugd
  • 29. NFA (Example) a 0 is the start state s0 {2} is the set of final states F a b Σ = {a,b} 0 1 2 start S = {0,1,2} b Transition Function: a b 0 {0,1} {0} Transition graph of the NFA 1 _ {2} 2 _ _ The language recognized by this NFA is (a|b) * a b Notes | CodeReplugd
  • 30. Deterministic Finite Automaton (DFA) • A Deterministic Finite Automaton (DFA) is a special form of a NFA. • no state has ε- transition • for each symbol a and state s, there is at most one labeled edge a leaving s. i.e. transition function is from pair of state-symbol to state (not set of states) a b a The language recognized by a b 0 1 2 this DFA is also (a|b) * a b b Notes | CodeReplugd
  • 31. Implementing a DFA • Le us assume that the end of a string is marked with a special symbol (say eos). The algorithm for recognition will be as follows: (an efficient implementation) s s0 { start from the initial state } c  nextchar { get the next character from the input string } while (c != eos) do { do until the en dof the string } begin s  move(s,c) { transition function } c  nextchar end if (s in F) then { if s is an accepting state } return “yes” else return “no” Notes | CodeReplugd
  • 32. Implementing a NFA S  ε-closure({s0}) { set all of states can be accessible from s0 by ε- transitions } c  nextchar while (c != eos) { begin s  ε-closure(move(S,c)) { set of all states can be accessible from a state in S c  nextchar by a transition on c } end if (S∩F != Φ) then { if S contains an accepting state } return “yes” else return “no” • This algorithm is not efficient. Notes | CodeReplugd
  • 33. Converting A Regular Expression into A NFA (Thomson’s Construction) • This is one way to convert a regular expression into a NFA. • There can be other ways (much efficient) for the conversion. • Thomson’s Construction is simple and systematic method. It guarantees that the resulting NFA will have exactly one final state, and one start state. • Construction starts from simplest parts (alphabet symbols). To create a NFA for a complex regular expression, NFAs of its sub-expressions are combined to create its NFA, Notes | CodeReplugd
  • 34. Thomson’s Construction (cont.) ε • To recognize an empty string ε i f a • To recognize a symbol a in the alphabet Σ i f • If N(r1) and N(r2) are NFAs for regular expressions r1 and r2 • For regular expression r1 | r2 ε N(r1) ε i ε f NFA for r1 | r2 ε N(r2) Notes | CodeReplugd
  • 35. Thomson’s Construction (cont.) • For regular expression r1 r2 i N(r1) N(r2) f Final state of N(r2) become final state of N(r1r2) NFA for r1 r2 • For regular expression r* ε ε ε i N(r) f ε NFA for r* Notes | CodeReplugd
  • 36. Thomson’s Construction (Example - (a|b) * a ) a a a: ε ε (a | b) ε ε b b b: ε a ε ε ε (a|b) * ε ε ε b ε ε a ε ε ε ε a (a|b) * a ε b ε ε Notes | CodeReplugd
  • 37. Converting a NFA into a DFA (subset construction) put ε-closure({s0}) as an unmarked state into the set of DFA (DS) while (there is one unmarked S1 in DS) do ε-closure({s0}) is the set of all states can be accessible begin from s0 by ε-transition. mark S1 for each input symbol a dofrom a state s in S there is a transition on set of states to which a 1 begin S2  ε-closure(move(S1,a)) if (S2 is not in DS) then add S2 into DS as an unmarked state transfunc[S1,a]  S2 end end • a state S in DS is an accepting state of DFA if a state in S is an accepting state of NFA • the start state of DFA is ε-closure({s0}) Notes | CodeReplugd
  • 38. Converting a NFA into a DFA (Example) 2 3 a ε 0 1 ε ε 6 7 8 ε a ε ε 4 5 b ε S0 = ε-closure({0}) = {0,1,2,4,7} S0 into DS as an unmarked state ⇓ mark S0 ε-closure(move(S0,a)) = ε-closure({3,8}) = {1,2,3,4,6,7,8} = S1 S1 into DS ε-closure(move(S0,b)) = ε-closure({5}) = {1,2,4,5,6,7} = S2 S2 into DS transfunc[S0,a]  S1 transfunc[S0,b]  S2 ⇓ mark S1 ε-closure(move(S1,a)) = ε-closure({3,8}) = {1,2,3,4,6,7,8} = S1 ε-closure(move(S1,b)) = ε-closure({5}) = {1,2,4,5,6,7} = S2 transfunc[S1,a]  S1 transfunc[S1,b]  S2 ⇓ mark S2 ε-closure(move(S2,a)) = ε-closure({3,8}) = {1,2,3,4,6,7,8} = S1 ε-closure(move(S2,b)) = ε-closure({5}) = {1,2,4,5,6,7} = S2 transfunc[S2,a]  S1 transfunc[S2,b]  S2 Notes | CodeReplugd
  • 39. Converting a NFA into a DFA (Example – cont.) S0 is the start state of DFA since 0 is a member of S0={0,1,2,4,7} S1 is an accepting state of DFA since 8 is a member of S1 = {1,2,3,4,6,7,8} a S1 a S0 b a b S2 b Notes | CodeReplugd
  • 40. Converting Regular Expressions Directly to DFAs • We may convert a regular expression into a DFA (without creating a NFA first). • First we augment the given regular expression by concatenating it with a special symbol #. r  (r)# augmented regular expression • Then, we create a syntax tree for this augmented regular expression. • In this syntax tree, all alphabet symbols (plus # and the empty string) in the augmented regular expression will be on the leaves, and all inner nodes will be the operators in that augmented regular expression. • Then each alphabet symbol (plus #) will be numbered (position numbers). Notes | CodeReplugd
  • 41. Regular Expression  DFA (cont.) (a|b) * a  (a|b) * a # augmented regular expression • Syntax tree of (a|b) * a # • # 4 * a 3 • each symbol is numbered (positions) | • each symbol is at a leave a b 1 2 • inner nodes are operators Notes | CodeReplugd
  • 42. followpos Then we define the function followpos for the positions (positions assigned to leaves). followpos(i) -- is the set of positions which can follow the position i in the strings generated by the augmented regular expression. For example, ( a | b) * a # 1 2 3 4 followpos(1) = {1,2,3} followpos(2) = {1,2,3} followpos is just defined for leaves, it is not defined for inner nodes. followpos(3) = {4} followpos(4) = {} Notes | CodeReplugd
  • 43. firstpos, lastpos, nullable • To evaluate followpos, we need three more functions to be defined for the nodes (not just for leaves) of the syntax tree. • firstpos(n) -- the set of the positions of the first symbols of strings generated by the sub-expression rooted by n. • lastpos(n) -- the set of the positions of the last symbols of strings generated by the sub-expression rooted by n. • nullable(n) -- true if the empty string is a member of strings generated by the sub-expression rooted by n false otherwise Notes | CodeReplugd
  • 44. How to evaluate firstpos, lastpos, nullable n nullable(n) firstpos(n) lastpos(n) leaf labeled ε true Φ Φ leaf labeled false {i} {i} with position i | nullable(c1) or firstpos(c1) ∪ firstpos(c2) lastpos(c1) ∪ lastpos(c2) c1 c2 nullable(c2) • nullable(c1) and if (nullable(c1)) if (nullable(c2)) c1 c2 nullable(c2) firstpos(c1) ∪ firstpos(c2) lastpos(c1) ∪ lastpos(c2) * true else firstpos(c1) firstpos(c1) else lastpos(c2) lastpos(c1) c1 Notes | CodeReplugd
  • 45. How to evaluate followpos • Two-rules define the function followpos: 1. If n is concatenation-node with left child c1 and right child c2, and i is a position in lastpos(c1), then all positions in firstpos(c2) are in followpos(i). 2. If n is a star-node, and i is a position in lastpos(n), then all positions in firstpos(n) are in followpos(i). • If firstpos and lastpos have been computed for each node, followpos of each position can be computed by making one depth- first traversal of the syntax tree. Notes | CodeReplugd
  • 46. Example -- ( a | b) * a # green – firstpos {1,2,3} • {4} blue – lastpos {1,2,3} {3} {4}# {4} • 4 {1,2}*{1,2}{3}a{3} 3 Then we can calculate followpos {1,2} | {1,2} {1} a {1} {2}b {2} followpos(1) = {1,2,3} 1 2 followpos(2) = {1,2,3} followpos(3) = {4} followpos(4) = {} • After we calculate follow positions, we are ready to create DFA for the regular expression. Notes | CodeReplugd
  • 47. Algorithm (RE  DFA) • Create the syntax tree of (r) # • Calculate the functions: followpos, firstpos, lastpos, nullable • Put firstpos(root) into the states of DFA as an unmarked state. • while (there is an unmarked state S in the states of DFA) do – mark S – for each input symbol a do • let s1,...,sn are positions in S and symbols in those positions are a • S’  followpos(s1) ∪ ... ∪ followpos(sn) • move(S,a)  S’ • if (S’ is not empty and not in the states of DFA) – put S’ into the states of DFA as an unmarked state. • the start state of DFA is firstpos(root) • the accepting states of DFA are all states containing the position of # Notes | CodeReplugd
  • 48. Example -- ( a | b) * a # 1 2 3 4 followpos(1)={1,2,3} followpos(2)={1,2,3} followpos(3)={4} followpos(4)={} S1=firstpos(root)={1,2,3} ⇓ mark S1 a: followpos(1) ∪ followpos(3)={1,2,3,4}=S2 move(S1,a)=S2 b: followpos(2)={1,2,3}=S1 move(S1,b)=S1 ⇓ mark S2 a: followpos(1) ∪ followpos(3)={1,2,3,4}=S2 move(S2,a)=S2 b: followpos(2)={1,2,3}=S1 move(S2,b)=S1 b a a S1 S2 b start state: S1 accepting states: {S2} Notes | CodeReplugd
  • 49. Example -- ( a | ε) b c* # 1 2 3 4 followpos(1)={2} followpos(2)={3,4} followpos(3)={3,4} followpos(4)={} S1=firstpos(root)={1,2} ⇓ mark S1 a: followpos(1)={2}=S2 move(S1,a)=S2 b: followpos(2)={3,4}=S3 move(S1,b)=S3 ⇓ mark S2 b: followpos(2)={3,4}=S3 move(S2,b)=S3 S2 ⇓ mark S3 a b c: followpos(3)={3,4}=S3 move(S3,c)=S3 S1 b S3 c start state: S1 Notes | CodeReplugd
  • 50. Minimizing Number of States of a DFA • partition the set of states into two groups: – G1 : set of accepting states – G2 : set of non-accepting states • For each new group G – partition G into subgroups such that states s1 and s2 are in the same group iff for all input symbols a, states s1 and s2 have transitions to states in the same group. • Start state of the minimized DFA is the group containing the start state of the original DFA. • Accepting states of the minimized DFA are the groups containing the accepting states of the original DFA. Notes | CodeReplugd
  • 51. Minimizing DFA - Example a G1 = {2} 2 a G2 = {1,3} 1 b a b 3 G2 cannot be partitioned because move(1,a)=2 move(1,b)=3 b move(3,a)=2 move(2,b)=3 So, the minimized DFA (with minimum states) b a {1,3} a {2} b Notes | CodeReplugd
  • 52. Minimizing DFA – Another Example a 2 a a Groups: {1,2,3} {4} 1 b 4 a b {1,2} {3} a b 3 b no more partitioning 1->2 1->3 2->2 2->3 b 3->4 3->3 So, the minimized DFA b {3} a b {1,2} a b a {4} Notes | CodeReplugd
  • 53. Some Other Issues in Lexical Analyzer • The lexical analyzer has to recognize the longest possible string. – Ex: identifier newval -- n ne new newv newva newval • What is the end of a token? Is there any character which marks the end of a token? – It is normally not defined. – If the number of characters in a token is fixed, in that case no problem: + - – But <  < or <> (in Pascal) – The end of an identifier : the characters cannot be in an identifier can mark the end of token. – We may need a lookhead • In Prolog: p :- X is 1. p :- X is 1.5. The dot followed by a white space character can mark the end of a number. But if that is not the case, the dot must be treated as a part of the number. Notes | CodeReplugd
  • 54. Some Other Issues in Lexical Analyzer (cont.) • Skipping comments – Normally we don’t return a comment as a token. – We skip a comment, and return the next token (which is not a comment) to the parser. – So, the comments are only processed by the lexical analyzer, and the don’t complicate the syntax of the language. • Symbol table interface – symbol table holds information about tokens (at least lexeme of identifiers) – how to implement the symbol table, and what kind of operations. • hash table – open addressing, chaining • putting into the hash table, finding the position of a token from its lexeme. • Positions of the tokens in the file (for the error handling). Notes | CodeReplugd

Hinweis der Redaktion

  1. \n
  2. \n
  3. \n
  4. \n
  5. \n
  6. \n
  7. \n
  8. \n
  9. \n
  10. \n
  11. \n
  12. \n
  13. \n
  14. \n
  15. \n
  16. \n
  17. \n
  18. \n
  19. \n
  20. \n
  21. \n
  22. \n
  23. \n
  24. \n
  25. \n
  26. \n
  27. \n
  28. \n
  29. \n
  30. \n
  31. \n
  32. \n
  33. \n
  34. \n
  35. \n
  36. \n
  37. \n
  38. \n
  39. \n
  40. \n
  41. \n
  42. \n
  43. \n
  44. \n
  45. \n
  46. \n
  47. \n
  48. \n
  49. \n
  50. \n
  51. \n
  52. \n
  53. \n
  54. \n