Assignment 4. ue 09/18/17 1. ). Write regulr expressions tht define the strings recognized by the following finite utomt: b d b b b c c b) Write FA tht recognizes the tokens defined by the following regulr expression: ( (bc)*d)+ c) Write regulr expression tht defines Pscl-like fixed-deciml literl with no superfluous leding or triling zeroes. Tht is, 0.0, 123.01, nd 123005.0 re legl, but 00.0, 001.000, nd 002345.1000 re illegl. d) Let Rev be the opertor tht reverses strings. For exmple, Rev(bc) = cb. Let R be ny regulr expression. Rev(R) is the set of strings denoted by R, with ech string reversed. Is Rev(R) regulr set? Why? 2. Implement Micro Scnner river employing routines considered in clss. Scn 2 progrms from Ass-t 1. Scn two new progrms of your choice written on Micro lnguge employing your old Micro Scnner nd new Scnner river. Compre the outputs.
Scnning Lexicl Anlysis 2 How to specify the precise structure of tokens? Wht bout unprintble chrcters? Is 1..10 the sme s 1..10? How to correct errors? A precise definition of tokens is necessry to ensure tht lexicl rules re properly enforced. It lso llows lnguge designer to nticipte design flws. Scnner Genertor is n exmple of nonprocedurl progrmming. We do not tell it how to scn, but simply wht must be scnned. Regulr expressions nd finite utomt Regulr expressions re defined s follows. Ech regulr expression denotes set of strings ( regulr set). Ø is regulr expression denoting the empty set (the set contining no strings). λ is regulr expression denoting the set tht contins only the empty string. It is not the sme s Ø, becuse it contins one element. A string s of chrcters from the lphbet V is regulr expression denoting set contining only s. If s contins met-chrcters, i.e., (, ),, *, +,, s cn be quoted to void mbiguity. If A nd B re regulr expressions, then A B, AB, nd A* re lso regulr expressions, denoting the ltertion, ctention, nd Kleene closure of the corresponding regulr sets. The following opertions will be used for nottionl convenience. They re not strictly necessry, since their effect cn be obtined using stndrd regulr opertors: P is regulr expression. P+ denotes ll strings consisting of one or more strings in P ctented together: P+ =? P+ = P* λ If A is set of chrcters, Not(A) denotes ll chrcters in V not included in A: Not(A) = (V A); Wht is Not(A)*? Not(A)*= (V A)*
The set Ak represents ll strings formed by ctenting k (not necessrily different) symbols from A: Ak = AAA...A (k copies) 3 P+ = P* λ ; Not(A) = (V A); Not(A)*= (V A)*; Ak =AAA...A; Let us specify tokens. Let = (0... 9), L = (A... Z). A comment tht begins with nd ends with Eol. A fixed rel deciml literl. Comment = Not(Eol)* Eol Lit = +. + An identifier, composed of letters, digits, nd underscores, tht begins with letter, ends with letter or digit, nd contins no consecutive underscores. I = L(L )* (_(L )+)* A comment delimited by ## mrkers, which llows single # s within the comment body: Wht bout Comment2 = ##((# λ) Not(#))* ## It llows mny # mrkers within the comment body. If we need the only #, right expression is s follows: Comment3 = ## Not(#)*(# λ) Not(#)* ## A set of blnced brckets of the form [[[[...]]]] SetBrket = { [i ]i i 1} This is n infinite set. All the finite sets nd mny infinite sets re regulr but SetBrket is not regulr.
Finite Automt nd Scnners 4 A finite utomton (FA) cn be used to recognize the tokens specified by regulr expression. An FA is simple, idelized computer tht recognizes strings belonging to regulr sets. It consists of: A finite set of sttes A set of trnsitions (or moves) from one stte to nother, lbeled with chrcters in V A specil strt stte A set of finl, or ccepting, sttes FA cn be represented s trnsition digrms: b c The vlid tokens for the utomton shown in the digrm re described s (b(c)+)+ As n bbrevition, trnsition my be lbeled with more thn one chrcter, e.g., Not(c). The trnsition my be tken if the current input chrcter mtches ny of the chrcters lbeling the trnsition. c
eterministic Finite Automton (FA) is n FA which lwys hs unique next stte (for given stte nd chrcter). 5 Consider the expression, which defines n Ad comment: Not(Eol)* Eol Eol Not(Eol)
Consider exmples of regulr expressions nd corresponding FA. 6 You cn use the following formuls: P+ = P* λ ; Not(A) = (V A); Not(A)*= (V A)*; Ak; Let us specify tokens. Let = (0... 9), L = (A... Z). A FORTRAN-like rel literl (which requires digits on either or both sides of deciml point, or just string of digits). RelLit = (+( λ.)) (*. +) + λ (+( λ.)).
(+( λ.)) (λ. +) 7.. (+( λ.)) (*. +)..
8 An identifier consisting of letters, digits nd underscores ws defined s: I = L(L )* ( _(L )+)* L L L
You cn use the following formuls: P+ = P* λ ; Not(A) = (V A); Not(A)*= (V A)*; Ak; Let us specify tokens. Let = (0... 9), L = (A... Z). 9 Construct regulr expression nd FA for quoted strings: ( Not( ) )* Not(") " " "