Download as pdf
Download as pdf
You are on page 1of 8
++ vrvsauurs 2.3 SPECIFICATION OF TOKENS (REGULAR EXPRESSIONS) ¢ To specify'tokens; Regular Expressions are used, ¢ It provides convenient and useful notation for representing tokens . Regular Expressions define the language accepted by finite Autor (Transition Diagram). . Regular Expressions are defined over an alphabet > ¢ If Ris a Regular Expression, then regular expression. Language : It is a collection of strings over some fj can be denoted by e. “xed alphabet. Empty st Eg. if L (Language) = set of strings of os'¢ then L = (00, 01, 10, 11) Soflength two Mm MD a MR) represents language denote’ NY esc av . ee ke): then L*=LuLtuLtu... Lv LY =@, 111, 111,......) Operations on Languag. Here * can be 0, 1, 2, 3, If La = (00, 10} & La = (01, 11) (Operation Description Example Union La La= {eet of etring in Li] LiULa =(00, 10, 01, 11) & strings in Le Concatenation —_|Lila= {Set of string in LilLila = (0001, 0011, 1001, followed by strings 101) ind G Kleen closure of Li [15 18 uu urdu La" = {e, 00, 10, 1010, 0010, L i 1000, 0000, 000000, i= UL! 001000, ...) 0 Positive Closure 1 <1. uizu {(00, 10, 1010, 0010, y =, 1000, 0000,000000, =U, 001000,......) Rules of Regular Expressions 1. eis a Regular expression. 2. Union of two Regular Expressions Riand Rs. ie, RitRrorRy| Reis also a Regular Expression, 3. Concatenation of two Regular Expressions Ri and Re = ie. RiRs is also a Regular Expression. 4. Clesure of Regular Expression R i.e, R* is also a Regular Expression. 5. _IfR is Regular Expression then (R) is also a Regular Expression, “\ Algebric Laws . we + -RAIRe=Rel Ri or Ri+R2=Ra+ Rs (Commutative) 2 Ril Rel Rs) = (Ri Re) Ra (Associative) or Ri + (Ret Rs) = (Ri+ Re) + Rs ISAS Cony Y - wits SR (Re Ro = ARD Re 4 RRS | Ry = Ri Re | RRs Distr," or Ry (Re + Ro) = Rik + RiRs 5. eR=Re=R (Coneatens, Example 2: Find Regular Expression for following Language. (LE 61M MD enced (Hint 1, Jt. =e, =I, P= 1 = 111, Ans.1* ()L= fe, 1, MIL TIL, eset Ans. (1D= (€)_ L= Set of all strings of 0's and 1's Ans. (0+ or |) (d)_L=Set of al! strings of O's and I's ending with 11. Ans. (0+ 11 (©) L=Set of all strings of 0's and 1's beginning with 0 and Ending with 1 Ans.0(0+1)*1 fe, 0,1, 01, 11, 00, 000, 101, ....) Example 3 : Write Regular Expressions for following language over B= a (a) Strings of length zero or one. Ans. ¢|a |b or (¢+a+6) (0) Strings of length two. Ans.aa | ab | ba | bb or (aa +ab + ba + bb) (©) Strings of Even length, Ans. (aa|.ab|ba|bb)* or (ac + ab + ba + bb)* (@) Set of all strings of a's and b's having atleast two occurrences of aa. Ans. (a + 6)* aa (a + b)* aa (a + b)* Example 4 : Write Regular Expressions for all types of Tokens. Ans, (a) Keywords = begin|end |if| then else (0) Identifier =letter (letter + digit)* (© Constant = digit digit* = digit” (@ _Relation-op = <|>|<=|>=|<> Example 6. Write Regular Expression in which second letter from right end of st is I where E = (0,1) Sol. (0+ 141 (0+1) PS N .oncel Anais ———__—_—_ a Faample 6 Write Regular Expression for =a, b) (a) =set of strings having atleast one occurrence of double letter % ans(a + B* (a+ BD) (a4 6) (b) L=set of strings having double leter at Beginning and Ending of string ‘ans. (aa + 60) (a+ )* (aa + bb) (9 L=set of string having double etter at beginning or on ending of string ‘ans. (aa + bb) (a+ 6)* + (a+)* (aa + Bb) + (aa + bb) (a + bY* (aa + bb). Example 7. Write regular expression for set of words having a, ¢, i, 0, w appearing in thearder although not necessarity consecutively. ‘ans. Ia = (0d) +f] +n] + [p+ [v 2] + Blankspace)* Required Regular Expression will be aaaeaiaoaua 24, RECOGNITION OF TOKEN (FINITE AUTOMATA) Regular Expression are used to represent various type of Tokens. From these Regular Expression, Finite Automata is generated. Finite Automata is basically other name of Lexical Analyzer which checks or recognize whether a string is ‘Token or not. Finite Automata : It is a Machine or a Recognizer for a language that is used to check whether string is accepted by a language or not. © It gives answer “Yes” if string is accepted else “no”. In Finite Automata, Finite means finite number of states & Automata means, the Automatic Machine which works without any interference of human being. Repre tation of Finite Automata : There are 2 ways to represent finite Automata 1. Transition Diagram : ‘& edges. Eg. It is a directed graph or flow chart having states b+—© Initial State Final State 0, 1,2 + States 0 + Initial state @, b+ Input symbols. 2 ‘Transition Table : 2 > Final State «Finite Automata can be Represented by 5 tuple (@, £4, q.,p) () Qis finite non-empty set of states. (b) Lis finite set of input symbols (©) 41s transition function, (d) q © Qisinitial state (© FC Qisthe set of final states. a Example 8: Design Finite Automata which accepts string “abb ‘Ans. States Q= (a0, 91, 92, 9) Input symbols =a, b} ‘Transition Function 5 8 (a2, )= 4, 4 (41, B)= 42, 8qs, B= a) Initial State © Final State (F) : 2.4.1 Types of Finite Automata (a) Deterministic Finite Automata (DFA) (6) Non-Deterministic Finite Automata (NFA) (a) Deterministic Finite Automata (DFA): “Deterministic” meant on each input there is one and only one state to which the automata can tion from its current state. * DFAisa5-tuple (6, 2,6, ge, F) where (@) Qs finite non-empty set of etates (6) Lis finite set of input-symbols (©) 6 is. transition function to move from current state to next sts? 8:QxE=Q (© qo€ Qis initial state (© FS Qis-set of final states sci nna » {) Non-Deterministic Finite Automata (NF =— mata, ) + “Non De enna there can be | possible transitions. So, output in non-determinusbie form wven inpul fi « NFAis 5-tuple (, E, 3, qo, F) where (a) Qis finite non-empty set of states (b) Eis finite set of input symbols (o 6 is transition function to move from current state to next state b:Qxr+2 (@ qwE Qis initial state (@ FS Qisset of final states. + Difference Between DFA and NFA DFA NFA. T. Bvery transition from one state | There can be multiple transit to. other is unique & | an input i.e, non-deterministic. deterministic in nature. 2, Null transitions (6) are not ions for Null Transitions (¢) are allowed means transition form current state to next allowed. state without any input. 3, Transition function ‘Transition function 8: Qx +2, 5:Qxz=Q 4, Requires less memory as transitions & states are less 5. E.gDFA In fig. Bach State has unique input symbol == {a, b} outgoing from it. Requires more memory. Eg NFA In fig. ‘As, there are multiple transitions of input symbol ‘a’ on state 0. & no transition from state 2, ie nom deterministic. 25 CONVERSION OF REGULAR EXPRESSION TO FINITE AUTOMATA (NFA) — “ is basically a . d token Recognizer which is nothing hu nvert Regular Expression into NFA ‘A Regular Expression Recognize a token, we nee Automata (NFA). So, we can co! ‘Algorithm for conversion of RE to NFA: Input : A Regular Expression R Output : NFA accepting language denoted by R Method : 1, Fore, NFAis 2 Fora, NFAis 3. Fora+b, ora|b NFA is 4. Forab, NFA is 5. Fora, NFA jis Example 9: — Draw NFA for Regular Expression (a+b)* Ans B Ans. Ans. “1

You might also like