Professional Documents
Culture Documents
Compiler Desgin Quantum Seb
Compiler Desgin Quantum Seb
which recognizes a language I. Then the DFA D < Q, 3,q),8,,F > can be constructed for language L as : Step 1: Initially Q’= 6. Step2:Addq, to Q. Step 3 : For each state in Q’, find the possible set of states for each input symbol using transition function of NFA. If this set of states is not in Q’, add itto @’.Introduction to Compiler 1-14 C (CS/IT-Sem-5) Step 4: Final state of DFA will be all states which contain F (final states of NFA). Que 1.11. | Construct the minimized DFA for the regular expression (0+ 1)*@4+1) 10. AKTU 2016-17, Marks 10 Answer Given regular expression : (0 + 1)*(0 + 1)10 NFA for given regular expression : @ +1)7(0 + 1910 © ~@°8+-ET 0 Ifwe remove ewe get [-: € can be neglected so q, = q, = 4.1 Now, we convert above NFA into DFA: Transition table for NFA: at 0 1 >a, %% 1% qs, > q, WG % > a4 6 6 Transition table for DFA: we ° 1 Let 7G 1,45 %, 45 9, a8A 14s 1% 19.44 9,9, a8 B 9,959, 91959 995% 154,08 C «UG 14s 145s 959,88 DCompiler Design 1-15 C (CS/IT-Sem-5) Transition diagram for DFA : i Qo 1Q CO 0 ot 0 1 aA B B B B Cc Cc D Cc aD B c For minimization divide the rows of transition table into 2 sets, as Set-1: It consists of non-final state rows. A B B B B Cc Cc D Cc Set-2 : It consists of final state rows. «D B Cc No two rows are similar. So, the DFA is already minimized. Que 1.12. | How does finite automata useful for lexical analysis ? Answer 1. Lexical analysis is the process of reading the source text of a program and converting it into a sequence of tokens. 2. The lexical structure of every programming language can be specified by aregular language, a common way to implement a lexical analyzer is to: a. Specify regular expressions for all of the kinds of tokens in the language. b. The disjunction of all of the regular expressions thus describes any possible token in the language. c. Convert the overall regular expression specifying all possible tokens into a Deterministic Finite Automaton (DFA). 4. Translate the DFA into a program that simulates the DFA. This program is the lexical analyzer.Introduction to Compiler 1-16 C (CS/IT-Sem-5) 3. This approach is so useful that programs called lexical analyzer generators exist to automate the entire process, Que 1.13. | Write down the regular expression for 1. The set of all string over {a,b} such that fifth symbol from right isa. 2. The set of alll string over {0, 1} such that every block of four consecutive symbol contain at least two zero. AKTU 2017-18, Marks 10 Answer 1. DFA for all strings over (a, b} such that fifth symbol from right isa: Regular expression : (a +b)" a (a+b) (a +b) (a+b) (a +b) 2. Regular expression : {00(0 + 1) (0 + 1) O(0 + 1) 0(0 + 1) + 0(0 + 1) (0 + 1)0 + (0 + 1) 00(0 +1) + (0+ 10(0 + 1) 0+ (0 + 1) (0+ 100] Que 1.14. | Convert following NFA to equivalent DFA and hence minimize the number of states in the DFA. ac Fig, 1.14.1. AKTU 2018-19, Marks 07 Answer Transition table for e-NFA : oe a 6 © é % 4 Y ® {dp} qn Gs % {dor Gah 4 C7 4 4 4 4 s-closure of (qo) = {9g 91» 2} e-closure of {q;) = {q,) e-closure of {go} = (qo)Compiler Design 1-17 C (CS/IT-Sem-5) Transition table for NFA : aE a b © {QoQ %2) | {40r% 92) | 4192) } G0 My Ga) 19-92) | (Qo, G2) | Lae) |ldq-41- 92h {qo} 4 9 4 Let {9 9» I} =A {ay 49) =B {qq} =C Transition table for NFA: az a b ec A B A B A é A c 4 4 4 Transition table for DFA : ae a A A B A c $ So, DFA is given by ela} ty lo 2e/>/> Ia a,b,c Fig. 1.14.2. PART-4 Implementation of Lexical Analyzers, Lexical Analyzer Generator, LEX Compiler.Introduction to Compiler 1-18 C (CS/IT-Sem-5) Questions-Answers Long Answer Type and Medium Answer Type Questions Que 1.15. | Explain the implementation of lexical analyzer. Answer Lexical analyzer can be implemented in following step : 1. Input tothe lexical analyzer is a source program 2. By using input buffering scheme, it scans the source program. 3. Regular expressions are used to represent the input patterns. 4, Now this input pattern is converted into NFA by using finite automation machine. Regular expression Finite automata 1 Input program <—} Lexical analyzer 1 f Symbol table Tokenized output file Fig. 1.15.1. Implementation of lexical analyzer. This NFA are then converted into DFA and DFA are minimized by using different method of minimization. o 6. The minimized DFA are used to recognize the pattern and broken into lexemes. 7. Each minimized DFA is associated with a phase in a programming language which will evaluate the lexemes that match the regular expression. 8. The tool then constructs a state table for the appropriate finite state machine and creates program code which contains the table, the evaluation phases, and a routine which uses them appropriately. Que 1.16. | Write short notes on lexical analyzer generator. Answer 1. For efficient design of compiler, various tools are used to automate the phases of compiler. The lexical analysis phase can be automated using a tool called LEX.