SlideShare ist ein Scribd-Unternehmen logo
1 von 12
10. Explain the process of constructing an NFA from a regular expression. Find
NFA for the expression (a/b)x a/(a/b)(a/b). Convert the obtained NFA into DFA
Ans:
Algorithm for RE to NFA:-
Algorithm : Thomson’s Construction
            An NFA from a RE (Regular Expression)
Input : A regular Expression r over an alphabet E.
Output : An NFA N accepting L(r).
         Here the following notations and used,
         I -- initial state
         F -- final state
         R -- regular state
         N -- NFA

   1. For E, Construct the NFA.


                           E
         Start         i            f


   2. For a in E, Construct the NFA.

         Start                 a        f


   3. suppose N(s) and N(t) are NFA’s for regular expressions S and F

   a) For the regular expression S/t, Construct the following composite NFA N(S/t)




                                                                        E
                    E

        start
                       i                                                        f

                   E
                                                                            E
b) For the regular expression St, Construct the Composite NFA N(st)

   Diagram




   c) For the regular expression S*, construct the Composite NFA N(S*)

   Diagram




   d) For the Parenthesized regular expression (S) , use N(s) itself as the NFA.
      The Construction produces an NFA N® with the following properties.

  1. N(r) has at most twice as many states as the number of symbols and operators in r.
  2. N(r) has exactly one start state and one accepting state. The accepting state has no
     out going transitions.
  3. Each state of N(r) has either one out going transition on a symbol in E on at most
     two out going E- transitions.

  Diagram




11. What is a Shift-reduse Parser? Explain in detail the conflicts that may occur
during Shift-reduce Parsing?
       Shift-reduse Parsing is a type of bottom up Parsing that Constraint a parse tree for
an input beginning at the leaves and working towards the root conflicts during shift-
reduce Parsing .

Conflicts during Shift-reduce Parsing
       The general Shift-reduce technique.
• Perform shift action when there is no handle on the stack.
        • Perform reduce action when there is a handle on the top of the stack.
        There are two problems that this Parser faces.
        1. Shift-reduce conflict:
              What action to take in case both shift and reduce actions are valid?
        2. Reduce-reduce conflict:
              Which rule to use for reduction if reduction is possible by more one rule?
        These conflicts come either because of ambiguous grammars or parsing method is not
        powerful enough.

        Examples:
        1. Shift reduce conflict:
           Consider the grammar E       E + E/E * E/id and input id + id * id



Stack         Input             Action            Stack             Input            Action
$E+E          *id$        reduce by E   E+E       $E+E               *id$             Shift
$E            *id$               Shift            $E+E*               id$             Shift
$E*             id$              Shift            $E+E*id               $        reduce by E    id
$E*id             $      reduce by E   id         $E+E*E                $       reduce by E    E*E
$E*E             $      reduce by E    E*E        $E+E                 $        reduce by E    E+E

   2. Reduce-reduce Conflict:
     Consider the grammar,
       M      R+R/R+C/R
       R      C
   And input C+C

Stack         Input           Action           Stack                Input            Action
 $            C+C $            Shift             $                   C+C $            Shift
$C             +C$      reduce by R       C      $                   C+C $       reduce by R    C
$R             +C$              Shift           $R                   +C $             Shift
$R+             C$              Shift          $R                    +C $             Shift
$R+C             $      reduce by R       C    $R+C                    $        reduce by M    R+C
$R+R             $      reduce by M        R+R $M                      $
$M               $

   12. Explain Procedure calls with a suitable eg.
       Procedure Calls:
       • The procedure or function is such an important and frequently used programming
          construct.
       • It is imperative for a compiler to generate good code for procedure calls and
          returns.
       • The run time routine that handle procedure argument passing, calls and returns are
          part of the run time support package.
•   Let us consider a grammar for a simple Procedure call statement.
                 S      call id(E list)
                 E      E list, E
                 E list      E
    The translation for a call includes a calling sequence of actions taken on entry to and
    exit from each procedure

    Calling sequence:
       While calling sequence differ, even for implementations of the same language.
    • Allocate space for activation record (AR) on stack.
    • Evaluate arguments to procedure and make available at known location.
    • Save state of calling Procedure used to resume execution after call.
    • Save return address (in known location)
    • Generate jump to the beginning of the called Procedure.

    Return sequences:
     The full, actions will take place.
    • If the called Procedure is a f2, save the result in a known place.
    • Restore the activation record of the calling Procedure.
    • Generate a jump to the return address(of calling Procedure)

    Syntax Directed Translation Scheme of procedure call

1. S    call id (E list) for each item P or queue do emit(‘Param’ P); emit(‘call’ id place)
        The code for S , is th code for E list, Which evaluates the arguments, followed by
    a param P statement for each argument, followed by a call Statement

2. E list    E list , E append E place to end of queue

3. E list     E    initialize queue to contain only E place

         Here queue is emptied and then gets a single pointer to the symbol table location
for the name that denotes the value of E.

1. Transition diagram for relational operators
    Diagram
2. Software tools:
    * Structure editor
    * Pretty printer
    * Static checker
    * Interpreters
         - that analysis the source program
3. Disadvantage of operator precedence parsing
    * It is hard to handle tokens like the minns sign which has two different

      precedence (unary or binary)
    * Only a small class of grammars can be parsed using this technique.
4. Methods of representing a syntax tree.
   * Array representation
   * Linked list representation

What are the issues of the lexical analyzer?
 Lexical analysis Vs parsing
   The reasons for separating lexical analysis from parsing are basically software
engineering concerns.
1. Simplicity of Design
    When one detects a well defined sub task, it is often good to separate out the task for
eg a parser embedding the conventions for comments and white space is more complex
than one that assume comments and white space have already been removed by a lexical
analyzer

2. Efficiency
    With the task separated, it is easier to apply specialized techniques for eg specialized
buffering techniques for reading input characters and processing tokens can significantly
speed up the performance of a compiler

3. Portability
     Input alphabet peculiarities and other device specific anomalies can be restricted ti
the lexical analyzer. The representation of special or non standard symbols such as in
Pascal can be isolated in the lexical analysis.

1. Error recovery actions in a lexical analyzer:
    * Panic mode recovery
    * deleting an extraneous character
    * Inserting a missing character
    * Replacing incorrect character by a correct
    * Transposing two adjacent characters.

2. What is basic block?
- Is a sequence of consecutive statements in which flow of control enters at the
beginning and leaves at the end without any halt or possibility of branching except at the
end.

3. Finite automata: The generalized transition diagram for RE is called finite automata.

Conversion of an NFA into a DFA
   • An algorithm for constructing from an NFA into a DFA that recognizes the same
      language is called the subset construction, it is useful for simulating an NFA by a
      computer program.

   Algorithm : Subset construction- constructing a DFA from an NFA.
   Input : An NFA N
   Output : A DFA D accepting the same language
   Method : This algorithm constructs a transition table.

       D transition for D. Each DFA state is a set of NFA states and we construct D tran
    so that D will simulate “ in parallel” all possible moves N can make on a given input
    string.
      The following operations keep track of sets of NFA states (S represents an NFA
    state and T a set of NFA states)
Operation                                        Description
E- closure(S)                                    Set of NFA states reachable from NFA
                                                 state S on E-Transitions alone

E- closure(T)                                   Set of NFA states reachable from some
                                                NFA state S in T on E-Transitions alone
Move (T, a)                                     Set of NFA states to which there is a
                                                transition on input symbol a from some
                                                NFA state S in T


   •   The initial state of D is the set E-closure (S0), Where S0 is the start state of D we
       assume each state of D is initially ‘unmarked’. Then perform the algorithm.

       Initially E-closure(S0) is the only state in D states and it is unmarked! While there
       is an unmarked state T in D states do begin
                begin
                     mark T
                     for each input symbol a do
                begin
                     U=E-closure (move (T , a ));
                     If U is not in D states then
                     add U as an unmarked state to D states;
                     D Tran [T , a ]=U
                     end
end     [ The Subset Construction]
           States and transitions are added to D using the subset construction algorithm.
       A state of D is an accepting state if it is a set of NFA states containing at least one
       accepting state of N.
           A simple algorithm to complete E-closure(T) uses a stack to hold states whose
       edges have not been checked for E-labeled transitions such a procedure is.
              begin
                  push all states in T on to stack
                  initialize E-closure(T) to T
                  while stack is not empty do
              begin
                  pop T, the top element, off of stack;
                  for each state w with an edge from T to W labeled E do
                  if w is not in E-closure (T) do
              begin
                  add U to E-closure (T);
                  push W onto Stack
                  end
              end
          end            [Computation of E-closure]

       eg: Construct DFA for the following NFA

       Diagram




                      NFA for (a/b) n* abb

Solution: The start state of the equivalent DFA is E-closure(0), which is
                            A = {0,1,2,4,7}
          The input symbol alphabet is {a,b}
The subset construction algorithm tells us to mark A and then to compute E-closure
(move (A, a)).
  We first compute move (A, a), the set of states of N having transitions on a from
members of A.
  Among the states 0,1,2,4 and 7 only 2 and 7 have such transitions to 3 and 8.
  So E-closure (move ({0, 1, 2, 4, 7}, a))
     =E-closure ({3, 8})
    ={1,2,3,4,6,7,8}=>call this set as B
Dtran [A, a] =B
       E-closure (move({0,1,2,4,7},b))
     =E-closure(5)
     ={1,2,4,5,6,7}=>c

Dtran [A, b]=c
     =E-closure(move(B, a))
    =E-closure (move({1,2,3,4,6,7,8},a))
    =E-closure(3, 8) = B

Dtran [B, a]= B
     =E-closure(move(B, b))
    =E-closure (move({1,2,3,4,6,7,8},b))
    =E-closure(5, 9) = {1,2,4,5,6,7,9}=>D

Dtran [B, b]=D
     =E-closure(move(C, a))
    =E-closure (move({1,2,4,5,6,7},a))
    =E-closure(3, 8) = B

Dtran [c, a]=B
     =E-closure(move(C, b))
    =E-closure (move({1,2,4,5,6,7},b))
    =E-closure(5) = C

Dtran [c, b]=c
     =E-closure(move(D, a))
    =E-closure (move({1,2,4,5,6,7,9},a))
    =E-closure(3, 8) = B

Dtran [D, a]=B
     =E-closure(move(D, b))
    =E-closure (move({1,2,3,4,5,6,7,9},b))
    =E-closure(5, 10)
    = {1,2,4,5,6,7,10}=>E

Dtran [D, b]=E
     =E-closure(move(E, a))
    =E-closure (move({1,2,3,4,5,6,7,10},a))
    =E-closure(3, 8) = B

Dtran [E, a]=B
     =E-closure(move(E, b))
    =E-closure (move({1,2,4,5,6,7,10},b))
    =E-closure(5) = C
Dtran [E, b]=c
      Since A is the start state and state E is the only accepting state (E contains, the find
state of NFA 10)

                             States                          I/P symbol
                                                         a                b
                               A                         B                C
                               B                         B                D
                               C                         B                C
                               D                         B                E
                               *E                        B                C

Transition Diagram (DFA)
Diagram




13. Explain in detail about the error recovery strategies in parsing.
    Error-recovery Strategies:
        A Parser uses the full, strategies to recover from a synthetic error.
                * Panic mode
                * Phrase level
                * Error Productions
                * Global connections.
1. Panic mode recovery
        On discovering an error, the parser discards input symbols one at a time until one
of a designated set of synchronizing token is formed without checking for additional
errors.
        Eg: for synchronizing tokens are delimiters such as; or end

2. Phrase level recovery
        * On discovering an error, a Parser may perform local connection on the
remaining input.
        * ie, it may replace a prefix of the remaining input by some string that allows the
parser to continue.
        Eg: for local corrections are
            - replace a comma by a semicolon
            - delete a extra semicolon
            - insert a missing semicolon
3. Error Productions
        * Parser can be constructed with the extended grammar so that we can generate
appropriate error diagnostic, to indicate the erroneous construct that has been recognized
in the input.

4. Global connections
        * There are algorithms for choosing a minimal sequence of changes to obtain
global least cost connections.

       * Given an incorrect input string x and grammar G,
       * These algorithms will find a parse free for a related string y.
       * Such that the number of insertions, deletiuons and changes of tokens required to
                transform x into y is as small as possible

14. Regular Expressions:
       * Each regular expression denotes a language. A language denoted by regular
expressions is called regular set.
       * We use RE to describe tokens of a programming language.
       * Token is either a single string or a collection of strings of a certain type.
       * An identifier is defined to be a letter followed by zero or more letters & digits.
       * In RE notation, it can be write as
               identifier=letter (letter/digit)*

       * ‘1’(Vertical bar) means ‘or’ ie, Union
          ‘(‘ ‘)’ used to group sub expressions
         ‘*’(closure operation) means zero or more instances.
           Eg: Keyword = BEGIN|END|IF|THEN|ELSE
                 Constant = digit+
                 Relop = <|<=|=< >|>|>=
                 Identifier = letter(letter|digit)*
       * Where letter stands for A|B|C|D………Z
          Where digit stands for 0|2|3|…………..9
       Suppose r and s REs denoting the language L® & L(s) then,
                 1. (r)|(s) is a RE denoting L(r)U/L(s)
                 2. (r)(s) is a RE denoting L(r) L(s)
                 3. (r)* is a RE denoting (L(r))*
                 4. (r) is a RE denoting L(r)
       * The following table gives the algebric properties of RE,
          Axion                                          Description
       r/s = s/r                                       1 is cumulative

       r/(s/t)=(r/s)/t                                1 is associative

       r(s/t)=rs/rt
            &                                         concatenation distributes over
       (s/t)r=sr/tr
er=r
       re=r                                   e is the identity element for concatenation

       r* =(r/e)*                             relation between * and e

       r** = r**                              r* is independent

       15. Deterministic Finite Automata :( DFA)
               * Finite automation is deterministic if,
       1. It has no transitions on input E
       2. For each state S and input symbol a, there is at most one edge labeled a leaving
       S.
               * DFAS are easier to simulate by a program than NFAs
               * DFA can be exponentially larger than NFA[n states in a NFA controlled
       require as many as 2n states in a DFA]
               * A DFA can take only one path through the state grapg.
               * Completely determined by input.
               * It has one transition per input per state & has no E-moves.
               * If we use a transition table to represent the transition function of DFA,
       then each entry in the transition table is a single state.
               * DFA accepting the same language (a/b)*abb
       Diagram




        * For DFA & NFA we can find accepting the same language.
        * The states of the DFA represent subsets of the set of all states of the NFA. This
algorithm is often called the subset construction.
* NFA can be implemented using transition table, row   for           each state
                                                column               each input and E

        state                   input                   symbol
                                 a                        b
          0                    {0,1}                    {0}
          1                      -                      {2}
          2                      -                      {3}
* Advantage of TT:
         it provides fast access to the transitions of a given state on a given character.
* Disadvantage:
         It can take up lot of SPACE when input is large most transitions are to the empty
set.
* A path can be represented by sequence of state transitions called moves.
         0 a 0 a 1 b 2 b 3
         [accepting the input string aabb]
* NFA can have multiple transitions for one I/P ina given state & have E-moves
* NFA can choose whether to make E-moves and which of multiple trasition for a single
I/P to take.
* Actually NFAs do not have free will. If would be more accurate to say an execution of
an NFA marks ‘all’ choice from a set of states to a set of states.
* Acceptance of NFAs
         An NFA can be ‘ in multiple states’

                1
                 0      1

            0
* Rule: NFA accepts if at least one of its current states is a final state.

Weitere ähnliche Inhalte

Was ist angesagt? (17)

Lexicalanalyzer
LexicalanalyzerLexicalanalyzer
Lexicalanalyzer
 
Syntax analysis
Syntax analysisSyntax analysis
Syntax analysis
 
Compiler Design Unit 3
Compiler Design Unit 3Compiler Design Unit 3
Compiler Design Unit 3
 
Operator precedence
Operator precedenceOperator precedence
Operator precedence
 
Optimization of dfa
Optimization of dfaOptimization of dfa
Optimization of dfa
 
Ch3
Ch3Ch3
Ch3
 
Parsing
ParsingParsing
Parsing
 
Lecture 05 syntax analysis 2
Lecture 05 syntax analysis 2Lecture 05 syntax analysis 2
Lecture 05 syntax analysis 2
 
[ASM]Lab4
[ASM]Lab4[ASM]Lab4
[ASM]Lab4
 
Chapter 5 Syntax Directed Translation
Chapter 5   Syntax Directed TranslationChapter 5   Syntax Directed Translation
Chapter 5 Syntax Directed Translation
 
Compiler Design QA
Compiler Design QACompiler Design QA
Compiler Design QA
 
Ch4a
Ch4aCh4a
Ch4a
 
Syntactic analysis in NLP
Syntactic analysis in NLPSyntactic analysis in NLP
Syntactic analysis in NLP
 
Syntaxdirected
SyntaxdirectedSyntaxdirected
Syntaxdirected
 
Intermediate code generation1
Intermediate code generation1Intermediate code generation1
Intermediate code generation1
 
Syntax directed translation
Syntax directed translationSyntax directed translation
Syntax directed translation
 
C language basics
C language basicsC language basics
C language basics
 

Andere mochten auch

หลักสูตรการพัฒนาประสิทธิภาพการนำเสนอสินค้า
หลักสูตรการพัฒนาประสิทธิภาพการนำเสนอสินค้าหลักสูตรการพัฒนาประสิทธิภาพการนำเสนอสินค้า
หลักสูตรการพัฒนาประสิทธิภาพการนำเสนอสินค้าPik Lertsavetpong
 
Food Advertising Awards
Food  Advertising AwardsFood  Advertising Awards
Food Advertising AwardsStacey Inglis
 
Infinite Logistics Profile
Infinite Logistics ProfileInfinite Logistics Profile
Infinite Logistics ProfileBlogatize.net
 
приложение 4
приложение 4приложение 4
приложение 4farcrys
 
Mining and analyzing social media sample network w ora - hicss47 tutorial -...
Mining and analyzing social media   sample network w ora - hicss47 tutorial -...Mining and analyzing social media   sample network w ora - hicss47 tutorial -...
Mining and analyzing social media sample network w ora - hicss47 tutorial -...Dave King
 
technology
technologytechnology
technologyfarcrys
 
Pharma: Sky’s the Limit if You Have an ‘Entrepreneurial’ Mindset
Pharma: Sky’s the Limit if You Have an ‘Entrepreneurial’ MindsetPharma: Sky’s the Limit if You Have an ‘Entrepreneurial’ Mindset
Pharma: Sky’s the Limit if You Have an ‘Entrepreneurial’ MindsetAnup Soans
 
Pesquisa Ibope Alvorada FM Março de 2012
Pesquisa Ibope Alvorada FM Março de 2012Pesquisa Ibope Alvorada FM Março de 2012
Pesquisa Ibope Alvorada FM Março de 2012fabricandoweb
 
YouGO! Magazine 1/2011
YouGO! Magazine 1/2011YouGO! Magazine 1/2011
YouGO! Magazine 1/2011YouGO!
 
Συμμετοχή του ΤΕΕ Ειδικής Αγωγής Α΄ Βαθμίδας -(Ειδικό ΕΠΑΛ Σερρών) στον Διαγω...
Συμμετοχή του ΤΕΕ Ειδικής Αγωγής Α΄ Βαθμίδας -(Ειδικό ΕΠΑΛ Σερρών) στον Διαγω...Συμμετοχή του ΤΕΕ Ειδικής Αγωγής Α΄ Βαθμίδας -(Ειδικό ΕΠΑΛ Σερρών) στον Διαγω...
Συμμετοχή του ΤΕΕ Ειδικής Αγωγής Α΄ Βαθμίδας -(Ειδικό ΕΠΑΛ Σερρών) στον Διαγω...KESYPSERRON
 
Five Disruptive Forces that are Shaking Up Indian Pharma!
Five Disruptive Forces that are Shaking Up Indian Pharma! Five Disruptive Forces that are Shaking Up Indian Pharma!
Five Disruptive Forces that are Shaking Up Indian Pharma! Anup Soans
 

Andere mochten auch (20)

Presentatie e hvb pmonup definitie
Presentatie e hvb pmonup definitiePresentatie e hvb pmonup definitie
Presentatie e hvb pmonup definitie
 
หลักสูตรการพัฒนาประสิทธิภาพการนำเสนอสินค้า
หลักสูตรการพัฒนาประสิทธิภาพการนำเสนอสินค้าหลักสูตรการพัฒนาประสิทธิภาพการนำเสนอสินค้า
หลักสูตรการพัฒนาประสิทธิภาพการนำเสนอสินค้า
 
Food Advertising Awards
Food  Advertising AwardsFood  Advertising Awards
Food Advertising Awards
 
Career Solutions.Pptx
Career Solutions.PptxCareer Solutions.Pptx
Career Solutions.Pptx
 
Infinite Logistics Profile
Infinite Logistics ProfileInfinite Logistics Profile
Infinite Logistics Profile
 
приложение 4
приложение 4приложение 4
приложение 4
 
P kvsh1
P kvsh1P kvsh1
P kvsh1
 
Mining and analyzing social media sample network w ora - hicss47 tutorial -...
Mining and analyzing social media   sample network w ora - hicss47 tutorial -...Mining and analyzing social media   sample network w ora - hicss47 tutorial -...
Mining and analyzing social media sample network w ora - hicss47 tutorial -...
 
Student Reflections: Senegal
Student Reflections: SenegalStudent Reflections: Senegal
Student Reflections: Senegal
 
Sylvia Maisano
Sylvia MaisanoSylvia Maisano
Sylvia Maisano
 
technology
technologytechnology
technology
 
Pharma: Sky’s the Limit if You Have an ‘Entrepreneurial’ Mindset
Pharma: Sky’s the Limit if You Have an ‘Entrepreneurial’ MindsetPharma: Sky’s the Limit if You Have an ‘Entrepreneurial’ Mindset
Pharma: Sky’s the Limit if You Have an ‘Entrepreneurial’ Mindset
 
Pesquisa Ibope Alvorada FM Março de 2012
Pesquisa Ibope Alvorada FM Março de 2012Pesquisa Ibope Alvorada FM Março de 2012
Pesquisa Ibope Alvorada FM Março de 2012
 
Metcalfe feb
Metcalfe febMetcalfe feb
Metcalfe feb
 
YouGO! Magazine 1/2011
YouGO! Magazine 1/2011YouGO! Magazine 1/2011
YouGO! Magazine 1/2011
 
Mother
MotherMother
Mother
 
Συμμετοχή του ΤΕΕ Ειδικής Αγωγής Α΄ Βαθμίδας -(Ειδικό ΕΠΑΛ Σερρών) στον Διαγω...
Συμμετοχή του ΤΕΕ Ειδικής Αγωγής Α΄ Βαθμίδας -(Ειδικό ΕΠΑΛ Σερρών) στον Διαγω...Συμμετοχή του ΤΕΕ Ειδικής Αγωγής Α΄ Βαθμίδας -(Ειδικό ΕΠΑΛ Σερρών) στον Διαγω...
Συμμετοχή του ΤΕΕ Ειδικής Αγωγής Α΄ Βαθμίδας -(Ειδικό ΕΠΑΛ Σερρών) στον Διαγω...
 
Five Disruptive Forces that are Shaking Up Indian Pharma!
Five Disruptive Forces that are Shaking Up Indian Pharma! Five Disruptive Forces that are Shaking Up Indian Pharma!
Five Disruptive Forces that are Shaking Up Indian Pharma!
 
PhD project
PhD projectPhD project
PhD project
 
Best of The Talking Village Blog (2)
Best of The Talking Village Blog (2)Best of The Talking Village Blog (2)
Best of The Talking Village Blog (2)
 

Ähnlich wie Construct NFA from Regular Expression

02. chapter 3 lexical analysis
02. chapter 3   lexical analysis02. chapter 3   lexical analysis
02. chapter 3 lexical analysisraosir123
 
Regular Expression to Finite Automata
Regular Expression to Finite AutomataRegular Expression to Finite Automata
Regular Expression to Finite AutomataArchana Gopinath
 
Unitiv 111206005201-phpapp01
Unitiv 111206005201-phpapp01Unitiv 111206005201-phpapp01
Unitiv 111206005201-phpapp01riddhi viradiya
 
Regular expressions-Theory of computation
Regular expressions-Theory of computationRegular expressions-Theory of computation
Regular expressions-Theory of computationBipul Roy Bpl
 
Lecture 1 - Lexical Analysis.ppt
Lecture 1 - Lexical Analysis.pptLecture 1 - Lexical Analysis.ppt
Lecture 1 - Lexical Analysis.pptNderituGichuki1
 
System Programming Unit IV
System Programming Unit IVSystem Programming Unit IV
System Programming Unit IVManoj Patil
 
33-Procedures-Switch Case Statements-27-03-2024.pdf
33-Procedures-Switch Case Statements-27-03-2024.pdf33-Procedures-Switch Case Statements-27-03-2024.pdf
33-Procedures-Switch Case Statements-27-03-2024.pdfYash218469
 
New compiler design 101 April 13 2024.pdf
New compiler design 101 April 13 2024.pdfNew compiler design 101 April 13 2024.pdf
New compiler design 101 April 13 2024.pdfeliasabdi2024
 
Operator precedance parsing
Operator precedance parsingOperator precedance parsing
Operator precedance parsingsanchi29
 
LR(1) and SLR(1) parsing
LR(1) and SLR(1) parsingLR(1) and SLR(1) parsing
LR(1) and SLR(1) parsingR Islam
 
Chapter 5 -Syntax Directed Translation - Copy.ppt
Chapter 5 -Syntax Directed Translation - Copy.pptChapter 5 -Syntax Directed Translation - Copy.ppt
Chapter 5 -Syntax Directed Translation - Copy.pptFamiDan
 
Chapter 5 - Syntax Directed Translation.ppt
Chapter 5 - Syntax Directed Translation.pptChapter 5 - Syntax Directed Translation.ppt
Chapter 5 - Syntax Directed Translation.pptMulugetaGebino
 
Chapter_5_Syntax_Directed_Translation.ppt
Chapter_5_Syntax_Directed_Translation.pptChapter_5_Syntax_Directed_Translation.ppt
Chapter_5_Syntax_Directed_Translation.pptJigarThummar1
 

Ähnlich wie Construct NFA from Regular Expression (20)

02. chapter 3 lexical analysis
02. chapter 3   lexical analysis02. chapter 3   lexical analysis
02. chapter 3 lexical analysis
 
Regular Expression to Finite Automata
Regular Expression to Finite AutomataRegular Expression to Finite Automata
Regular Expression to Finite Automata
 
UNIX - Class1 - Basic Shell
UNIX - Class1 - Basic ShellUNIX - Class1 - Basic Shell
UNIX - Class1 - Basic Shell
 
Lexicalanalyzer
LexicalanalyzerLexicalanalyzer
Lexicalanalyzer
 
Lecture4 lexical analysis2
Lecture4 lexical analysis2Lecture4 lexical analysis2
Lecture4 lexical analysis2
 
Lec1.pptx
Lec1.pptxLec1.pptx
Lec1.pptx
 
3. Syntax Analyzer.pptx
3. Syntax Analyzer.pptx3. Syntax Analyzer.pptx
3. Syntax Analyzer.pptx
 
Ch3.ppt
Ch3.pptCh3.ppt
Ch3.ppt
 
Unitiv 111206005201-phpapp01
Unitiv 111206005201-phpapp01Unitiv 111206005201-phpapp01
Unitiv 111206005201-phpapp01
 
Regular expressions-Theory of computation
Regular expressions-Theory of computationRegular expressions-Theory of computation
Regular expressions-Theory of computation
 
Lecture 1 - Lexical Analysis.ppt
Lecture 1 - Lexical Analysis.pptLecture 1 - Lexical Analysis.ppt
Lecture 1 - Lexical Analysis.ppt
 
System Programming Unit IV
System Programming Unit IVSystem Programming Unit IV
System Programming Unit IV
 
Cd2 [autosaved]
Cd2 [autosaved]Cd2 [autosaved]
Cd2 [autosaved]
 
33-Procedures-Switch Case Statements-27-03-2024.pdf
33-Procedures-Switch Case Statements-27-03-2024.pdf33-Procedures-Switch Case Statements-27-03-2024.pdf
33-Procedures-Switch Case Statements-27-03-2024.pdf
 
New compiler design 101 April 13 2024.pdf
New compiler design 101 April 13 2024.pdfNew compiler design 101 April 13 2024.pdf
New compiler design 101 April 13 2024.pdf
 
Operator precedance parsing
Operator precedance parsingOperator precedance parsing
Operator precedance parsing
 
LR(1) and SLR(1) parsing
LR(1) and SLR(1) parsingLR(1) and SLR(1) parsing
LR(1) and SLR(1) parsing
 
Chapter 5 -Syntax Directed Translation - Copy.ppt
Chapter 5 -Syntax Directed Translation - Copy.pptChapter 5 -Syntax Directed Translation - Copy.ppt
Chapter 5 -Syntax Directed Translation - Copy.ppt
 
Chapter 5 - Syntax Directed Translation.ppt
Chapter 5 - Syntax Directed Translation.pptChapter 5 - Syntax Directed Translation.ppt
Chapter 5 - Syntax Directed Translation.ppt
 
Chapter_5_Syntax_Directed_Translation.ppt
Chapter_5_Syntax_Directed_Translation.pptChapter_5_Syntax_Directed_Translation.ppt
Chapter_5_Syntax_Directed_Translation.ppt
 

Kürzlich hochgeladen

Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfJemuel Francisco
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 
Millenials and Fillennials (Ethical Challenge and Responses).pptx
Millenials and Fillennials (Ethical Challenge and Responses).pptxMillenials and Fillennials (Ethical Challenge and Responses).pptx
Millenials and Fillennials (Ethical Challenge and Responses).pptxJanEmmanBrigoli
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxlancelewisportillo
 
Expanded definition: technical and operational
Expanded definition: technical and operationalExpanded definition: technical and operational
Expanded definition: technical and operationalssuser3e220a
 
Measures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataMeasures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataBabyAnnMotar
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONHumphrey A Beña
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptxiammrhaywood
 
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...JojoEDelaCruz
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management systemChristalin Nelson
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfTechSoup
 
Dust Of Snow By Robert Frost Class-X English CBSE
Dust Of Snow By Robert Frost Class-X English CBSEDust Of Snow By Robert Frost Class-X English CBSE
Dust Of Snow By Robert Frost Class-X English CBSEaurabinda banchhor
 
ROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxVanesaIglesias10
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17Celine George
 
EMBODO Lesson Plan Grade 9 Law of Sines.docx
EMBODO Lesson Plan Grade 9 Law of Sines.docxEMBODO Lesson Plan Grade 9 Law of Sines.docx
EMBODO Lesson Plan Grade 9 Law of Sines.docxElton John Embodo
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptxmary850239
 
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
 

Kürzlich hochgeladen (20)

Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 
Millenials and Fillennials (Ethical Challenge and Responses).pptx
Millenials and Fillennials (Ethical Challenge and Responses).pptxMillenials and Fillennials (Ethical Challenge and Responses).pptx
Millenials and Fillennials (Ethical Challenge and Responses).pptx
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
 
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptxFINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
 
Expanded definition: technical and operational
Expanded definition: technical and operationalExpanded definition: technical and operational
Expanded definition: technical and operational
 
Measures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataMeasures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped data
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptxYOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
 
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
 
Dust Of Snow By Robert Frost Class-X English CBSE
Dust Of Snow By Robert Frost Class-X English CBSEDust Of Snow By Robert Frost Class-X English CBSE
Dust Of Snow By Robert Frost Class-X English CBSE
 
ROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptx
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17
 
EMBODO Lesson Plan Grade 9 Law of Sines.docx
EMBODO Lesson Plan Grade 9 Law of Sines.docxEMBODO Lesson Plan Grade 9 Law of Sines.docx
EMBODO Lesson Plan Grade 9 Law of Sines.docx
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.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
 

Construct NFA from Regular Expression

  • 1. 10. Explain the process of constructing an NFA from a regular expression. Find NFA for the expression (a/b)x a/(a/b)(a/b). Convert the obtained NFA into DFA Ans: Algorithm for RE to NFA:- Algorithm : Thomson’s Construction An NFA from a RE (Regular Expression) Input : A regular Expression r over an alphabet E. Output : An NFA N accepting L(r). Here the following notations and used, I -- initial state F -- final state R -- regular state N -- NFA 1. For E, Construct the NFA. E Start i f 2. For a in E, Construct the NFA. Start a f 3. suppose N(s) and N(t) are NFA’s for regular expressions S and F a) For the regular expression S/t, Construct the following composite NFA N(S/t) E E start i f E E
  • 2. b) For the regular expression St, Construct the Composite NFA N(st) Diagram c) For the regular expression S*, construct the Composite NFA N(S*) Diagram d) For the Parenthesized regular expression (S) , use N(s) itself as the NFA. The Construction produces an NFA N® with the following properties. 1. N(r) has at most twice as many states as the number of symbols and operators in r. 2. N(r) has exactly one start state and one accepting state. The accepting state has no out going transitions. 3. Each state of N(r) has either one out going transition on a symbol in E on at most two out going E- transitions. Diagram 11. What is a Shift-reduse Parser? Explain in detail the conflicts that may occur during Shift-reduce Parsing? Shift-reduse Parsing is a type of bottom up Parsing that Constraint a parse tree for an input beginning at the leaves and working towards the root conflicts during shift- reduce Parsing . Conflicts during Shift-reduce Parsing The general Shift-reduce technique.
  • 3. • Perform shift action when there is no handle on the stack. • Perform reduce action when there is a handle on the top of the stack. There are two problems that this Parser faces. 1. Shift-reduce conflict: What action to take in case both shift and reduce actions are valid? 2. Reduce-reduce conflict: Which rule to use for reduction if reduction is possible by more one rule? These conflicts come either because of ambiguous grammars or parsing method is not powerful enough. Examples: 1. Shift reduce conflict: Consider the grammar E E + E/E * E/id and input id + id * id Stack Input Action Stack Input Action $E+E *id$ reduce by E E+E $E+E *id$ Shift $E *id$ Shift $E+E* id$ Shift $E* id$ Shift $E+E*id $ reduce by E id $E*id $ reduce by E id $E+E*E $ reduce by E E*E $E*E $ reduce by E E*E $E+E $ reduce by E E+E 2. Reduce-reduce Conflict: Consider the grammar, M R+R/R+C/R R C And input C+C Stack Input Action Stack Input Action $ C+C $ Shift $ C+C $ Shift $C +C$ reduce by R C $ C+C $ reduce by R C $R +C$ Shift $R +C $ Shift $R+ C$ Shift $R +C $ Shift $R+C $ reduce by R C $R+C $ reduce by M R+C $R+R $ reduce by M R+R $M $ $M $ 12. Explain Procedure calls with a suitable eg. Procedure Calls: • The procedure or function is such an important and frequently used programming construct. • It is imperative for a compiler to generate good code for procedure calls and returns. • The run time routine that handle procedure argument passing, calls and returns are part of the run time support package.
  • 4. Let us consider a grammar for a simple Procedure call statement. S call id(E list) E E list, E E list E The translation for a call includes a calling sequence of actions taken on entry to and exit from each procedure Calling sequence: While calling sequence differ, even for implementations of the same language. • Allocate space for activation record (AR) on stack. • Evaluate arguments to procedure and make available at known location. • Save state of calling Procedure used to resume execution after call. • Save return address (in known location) • Generate jump to the beginning of the called Procedure. Return sequences: The full, actions will take place. • If the called Procedure is a f2, save the result in a known place. • Restore the activation record of the calling Procedure. • Generate a jump to the return address(of calling Procedure) Syntax Directed Translation Scheme of procedure call 1. S call id (E list) for each item P or queue do emit(‘Param’ P); emit(‘call’ id place) The code for S , is th code for E list, Which evaluates the arguments, followed by a param P statement for each argument, followed by a call Statement 2. E list E list , E append E place to end of queue 3. E list E initialize queue to contain only E place Here queue is emptied and then gets a single pointer to the symbol table location for the name that denotes the value of E. 1. Transition diagram for relational operators Diagram
  • 5. 2. Software tools: * Structure editor * Pretty printer * Static checker * Interpreters - that analysis the source program 3. Disadvantage of operator precedence parsing * It is hard to handle tokens like the minns sign which has two different precedence (unary or binary) * Only a small class of grammars can be parsed using this technique. 4. Methods of representing a syntax tree. * Array representation * Linked list representation What are the issues of the lexical analyzer? Lexical analysis Vs parsing The reasons for separating lexical analysis from parsing are basically software engineering concerns. 1. Simplicity of Design When one detects a well defined sub task, it is often good to separate out the task for eg a parser embedding the conventions for comments and white space is more complex than one that assume comments and white space have already been removed by a lexical analyzer 2. Efficiency With the task separated, it is easier to apply specialized techniques for eg specialized buffering techniques for reading input characters and processing tokens can significantly speed up the performance of a compiler 3. Portability Input alphabet peculiarities and other device specific anomalies can be restricted ti the lexical analyzer. The representation of special or non standard symbols such as in Pascal can be isolated in the lexical analysis. 1. Error recovery actions in a lexical analyzer: * Panic mode recovery * deleting an extraneous character * Inserting a missing character * Replacing incorrect character by a correct * Transposing two adjacent characters. 2. What is basic block?
  • 6. - Is a sequence of consecutive statements in which flow of control enters at the beginning and leaves at the end without any halt or possibility of branching except at the end. 3. Finite automata: The generalized transition diagram for RE is called finite automata. Conversion of an NFA into a DFA • An algorithm for constructing from an NFA into a DFA that recognizes the same language is called the subset construction, it is useful for simulating an NFA by a computer program. Algorithm : Subset construction- constructing a DFA from an NFA. Input : An NFA N Output : A DFA D accepting the same language Method : This algorithm constructs a transition table. D transition for D. Each DFA state is a set of NFA states and we construct D tran so that D will simulate “ in parallel” all possible moves N can make on a given input string. The following operations keep track of sets of NFA states (S represents an NFA state and T a set of NFA states) Operation Description E- closure(S) Set of NFA states reachable from NFA state S on E-Transitions alone E- closure(T) Set of NFA states reachable from some NFA state S in T on E-Transitions alone Move (T, a) Set of NFA states to which there is a transition on input symbol a from some NFA state S in T • The initial state of D is the set E-closure (S0), Where S0 is the start state of D we assume each state of D is initially ‘unmarked’. Then perform the algorithm. Initially E-closure(S0) is the only state in D states and it is unmarked! While there is an unmarked state T in D states do begin begin mark T for each input symbol a do begin U=E-closure (move (T , a )); If U is not in D states then add U as an unmarked state to D states; D Tran [T , a ]=U end
  • 7. end [ The Subset Construction] States and transitions are added to D using the subset construction algorithm. A state of D is an accepting state if it is a set of NFA states containing at least one accepting state of N. A simple algorithm to complete E-closure(T) uses a stack to hold states whose edges have not been checked for E-labeled transitions such a procedure is. begin push all states in T on to stack initialize E-closure(T) to T while stack is not empty do begin pop T, the top element, off of stack; for each state w with an edge from T to W labeled E do if w is not in E-closure (T) do begin add U to E-closure (T); push W onto Stack end end end [Computation of E-closure] eg: Construct DFA for the following NFA Diagram NFA for (a/b) n* abb Solution: The start state of the equivalent DFA is E-closure(0), which is A = {0,1,2,4,7} The input symbol alphabet is {a,b} The subset construction algorithm tells us to mark A and then to compute E-closure (move (A, a)). We first compute move (A, a), the set of states of N having transitions on a from members of A. Among the states 0,1,2,4 and 7 only 2 and 7 have such transitions to 3 and 8. So E-closure (move ({0, 1, 2, 4, 7}, a)) =E-closure ({3, 8}) ={1,2,3,4,6,7,8}=>call this set as B
  • 8. Dtran [A, a] =B E-closure (move({0,1,2,4,7},b)) =E-closure(5) ={1,2,4,5,6,7}=>c Dtran [A, b]=c =E-closure(move(B, a)) =E-closure (move({1,2,3,4,6,7,8},a)) =E-closure(3, 8) = B Dtran [B, a]= B =E-closure(move(B, b)) =E-closure (move({1,2,3,4,6,7,8},b)) =E-closure(5, 9) = {1,2,4,5,6,7,9}=>D Dtran [B, b]=D =E-closure(move(C, a)) =E-closure (move({1,2,4,5,6,7},a)) =E-closure(3, 8) = B Dtran [c, a]=B =E-closure(move(C, b)) =E-closure (move({1,2,4,5,6,7},b)) =E-closure(5) = C Dtran [c, b]=c =E-closure(move(D, a)) =E-closure (move({1,2,4,5,6,7,9},a)) =E-closure(3, 8) = B Dtran [D, a]=B =E-closure(move(D, b)) =E-closure (move({1,2,3,4,5,6,7,9},b)) =E-closure(5, 10) = {1,2,4,5,6,7,10}=>E Dtran [D, b]=E =E-closure(move(E, a)) =E-closure (move({1,2,3,4,5,6,7,10},a)) =E-closure(3, 8) = B Dtran [E, a]=B =E-closure(move(E, b)) =E-closure (move({1,2,4,5,6,7,10},b)) =E-closure(5) = C
  • 9. Dtran [E, b]=c Since A is the start state and state E is the only accepting state (E contains, the find state of NFA 10) States I/P symbol a b A B C B B D C B C D B E *E B C Transition Diagram (DFA) Diagram 13. Explain in detail about the error recovery strategies in parsing. Error-recovery Strategies: A Parser uses the full, strategies to recover from a synthetic error. * Panic mode * Phrase level * Error Productions * Global connections. 1. Panic mode recovery On discovering an error, the parser discards input symbols one at a time until one of a designated set of synchronizing token is formed without checking for additional errors. Eg: for synchronizing tokens are delimiters such as; or end 2. Phrase level recovery * On discovering an error, a Parser may perform local connection on the remaining input. * ie, it may replace a prefix of the remaining input by some string that allows the parser to continue. Eg: for local corrections are - replace a comma by a semicolon - delete a extra semicolon - insert a missing semicolon
  • 10. 3. Error Productions * Parser can be constructed with the extended grammar so that we can generate appropriate error diagnostic, to indicate the erroneous construct that has been recognized in the input. 4. Global connections * There are algorithms for choosing a minimal sequence of changes to obtain global least cost connections. * Given an incorrect input string x and grammar G, * These algorithms will find a parse free for a related string y. * Such that the number of insertions, deletiuons and changes of tokens required to transform x into y is as small as possible 14. Regular Expressions: * Each regular expression denotes a language. A language denoted by regular expressions is called regular set. * We use RE to describe tokens of a programming language. * Token is either a single string or a collection of strings of a certain type. * An identifier is defined to be a letter followed by zero or more letters & digits. * In RE notation, it can be write as identifier=letter (letter/digit)* * ‘1’(Vertical bar) means ‘or’ ie, Union ‘(‘ ‘)’ used to group sub expressions ‘*’(closure operation) means zero or more instances. Eg: Keyword = BEGIN|END|IF|THEN|ELSE Constant = digit+ Relop = <|<=|=< >|>|>= Identifier = letter(letter|digit)* * Where letter stands for A|B|C|D………Z Where digit stands for 0|2|3|…………..9 Suppose r and s REs denoting the language L® & L(s) then, 1. (r)|(s) is a RE denoting L(r)U/L(s) 2. (r)(s) is a RE denoting L(r) L(s) 3. (r)* is a RE denoting (L(r))* 4. (r) is a RE denoting L(r) * The following table gives the algebric properties of RE, Axion Description r/s = s/r 1 is cumulative r/(s/t)=(r/s)/t 1 is associative r(s/t)=rs/rt & concatenation distributes over (s/t)r=sr/tr
  • 11. er=r re=r e is the identity element for concatenation r* =(r/e)* relation between * and e r** = r** r* is independent 15. Deterministic Finite Automata :( DFA) * Finite automation is deterministic if, 1. It has no transitions on input E 2. For each state S and input symbol a, there is at most one edge labeled a leaving S. * DFAS are easier to simulate by a program than NFAs * DFA can be exponentially larger than NFA[n states in a NFA controlled require as many as 2n states in a DFA] * A DFA can take only one path through the state grapg. * Completely determined by input. * It has one transition per input per state & has no E-moves. * If we use a transition table to represent the transition function of DFA, then each entry in the transition table is a single state. * DFA accepting the same language (a/b)*abb Diagram * For DFA & NFA we can find accepting the same language. * The states of the DFA represent subsets of the set of all states of the NFA. This algorithm is often called the subset construction.
  • 12. * NFA can be implemented using transition table, row for each state column each input and E state input symbol a b 0 {0,1} {0} 1 - {2} 2 - {3} * Advantage of TT: it provides fast access to the transitions of a given state on a given character. * Disadvantage: It can take up lot of SPACE when input is large most transitions are to the empty set. * A path can be represented by sequence of state transitions called moves. 0 a 0 a 1 b 2 b 3 [accepting the input string aabb] * NFA can have multiple transitions for one I/P ina given state & have E-moves * NFA can choose whether to make E-moves and which of multiple trasition for a single I/P to take. * Actually NFAs do not have free will. If would be more accurate to say an execution of an NFA marks ‘all’ choice from a set of states to a set of states. * Acceptance of NFAs An NFA can be ‘ in multiple states’ 1 0 1 0 * Rule: NFA accepts if at least one of its current states is a final state.