Lecture 4: Lexical and Syntax Analysis

Size: px
Start display at page:

Download "Lecture 4: Lexical and Syntax Analysis"

Transcription

1 CS 515 Progrmming Lngug nd Compilrs I Lctur 4: Lxicl nd Syntx Anlysis Zhng (Eddy) Zhng Rutgrs Univrsity Fll 2017, 9/26/2017 1

2 2 Lctur 4A: Lxicl nd Syntx Anlysis II (Th lcturs r sd on th slids copyrightd y Kith Coopr nd Lind Torczon from Ric Univrsity.)

3 3 Rviw: Thr Pss Compilr Front End IR Middl End IR Bck End Mchin cod Errors Prsr IR Scnnr nd Prsr collort to chck th syntx of th input progrm. 1. Scnnr mps strm of chrctrs into words (words r th fundmntl unit of syntx) strm of chrctrs Scnnr Smntic Elortion Scnnr producs strm of tokns for th prsr Tokn is <prt of spch, word> Prt of spch is unit in th grmmr 2. Prsr mps strm of words into sntnc in grmmticl modl of th input lngug

4 4 Rviw: Compilr Front End Front End IR Middl End IR Bck End Mchin cod Errors Prsr IR Scnnr nd Prsr collort to chck th syntx of th input progrm. 1. Scnnr mps strm of chrctrs into words (words r th fundmntl unit of syntx) strm of chrctrs Scnnr Smntic Elortion Scnnr producs strm of tokns for th prsr Tokn is <prt of spch, word> Prt of spch is unit in th grmmr 2. Prsr mps strm of words into sntnc in grmmticl modl of th input lngug

5 5 Rviw: Th Compilr s Front End Scnnr looks t vry chrctr - Convrts strm of chrctrs to strm of tokns, or clssifid words: <prt of spch, word> Prsr IR - Efficincy & sclility mttr Scnnr is only prt of compilr tht looks t vry chrctr Prsr looks t vry tokn -Dtrmins if th strm of tokns forms strm of chrctrs Scnnr Smntic Elortion Front End sntnc in th sourc lngug -Fits tokns to som syntctic modl, or grmmr, for th sourc lngug

6 6 Rviw: Th Compilr s Front End How cn w utomt th construction of scnnr & prsrs? Scnnr Prsr IR - Spcify syntx with rgulr xprssions (REs) - Construct finit utomton & scnnr from th rgulr xprssion strm of chrctrs Scnnr Smntic Elortion Prsr - Spcify syntx with contxt-fr grmmrs Front End (CFGs) - Construct push-down utomton & prsr from th CFG

7 7 Rviw: Automt W cn rprsnt this cod for rcognizing not s n utomton c nxt chrctr If c = n thn { c nxt chrctr if c = o thn { c nxt chrctr if c = t thn rturn <NOT, not > ls rport rror } ls rport rror } ls rport rror s 0 s n Any chrctr s 1 o O(1) pr input chrctr, if rnchs r O(1) Trnsition Digrm for not Stts drwn with doul lins indict succss s 2 t s 3

8 8 Rviw: Automt To rcogniz lrgr st of kywords is (rltivly) sy For xmpl, th st of words { if, int, not, thn, to, writ } s 2 if s 1 f n s 3 t s 4 int on-to-on mp from finl stts to words (my prts of spch) s 0 i n s 5 o s 6 t s 7 not t s 8 h s 9 s 10 s 11 n thn Cost is O(1) pr chrctr w o s 12 to s 13 r s 14 i s 15 t s 16 s 17 writ

9 9 Rviw: Spcifying An Automton W list th words Ech word s splling is uniqu nd concis W cn sprt thm with th symol, rd s or Th spcifiction: Thr is som sutlty hr. W introducd nottion for conctntion nd choic (or ltrntion). Conctntion: is followd y Altrntion: is ithr or if int not thn to writ is simpl rgulr xprssion for th lngug ccptd y our utomton

10 10 Rviw: Spcifying An Automton W introducd nottion for W list th words conctntion nd choic (or Ech word s splling is uniqu nd concis ltrntion). W cn sprt thm with th symol, rd s or Conctntion: Th spcifiction: is followd y Altrntion: if int not thn to writ is ithr or is simpl rgulr xprssion for th lngug ccptd y our utomton

11 11 Rviw: Spcifying An Automton Evry Rgulr Exprssion corrsponds to n utomton -W cn construct, utomticlly, n utomton from th RE -W cn construct, utomticlly, n RE from ny utomton -W cn convrt n utomton sily nd dirctly into cod -Th utomton nd th cod oth hv O(1) cost pr input chrctr An RE spcifiction lds dirctly to n fficint scnnr

12 12 Rviw: Unsignd Intgrs Th RE corrsponds to n utomton nd n implmnttion 0 s 0 s s 2 unsignd intgr unsignd intgr c nxt chrctr n 0 if c = 0 thn rturn <CONSTANT,n> ls if ( 1 c 9 ) thn { n toi(c) c nxt chrctr whil ( 0 c 9 ) { t toi(c) n n * 10 + t } rturn <CONSTANT,n> } ls rport rror Th utomton nd th cod cn gnrtd utomticlly

13 13 Rviw: Rgulr Exprssion A forml dfintion Rgulr Exprssions ovr n Alpht Σ If x Σ, thn x is n RE dnoting th st { x } or th lngug L = { x } If x nd y r REs thn 1. xy is n RE dnoting L(x)L(y) = { pq p L(x) nd q L(y) } 2. x y is n RE dnoting L(x) L(y) 3. x * is n RE dnoting L(x) * = 0 k < L(x) k (Kln Closur) St of ll strings tht r zro or mor conctntions of x 4. x + is n RE dnoting L(x) + = 1 k < L(x) k (Positiv Closur) St of ll strings tht r on or mor conctntions of x ε is n RE dnoting th mpty st

14 14 Rviw: Rgulr Exprssion How do ths oprtors hlp? ε is n RE dnoting th mpty st In prctic, th mpty string is oftn usful Rgulr Exprssions ovr n Alpht Σ If x is in Σ, thn x is n RE dnoting th st { x } or th lngug L = { x } Th splling of ny lttr in th lpht is n RE If x nd y r REs thn 1. xy is n RE dnoting L(x)L(y) = { pq p L(x) nd q L(y) } If w conctnt lttrs, th rsult is n RE (splling of words) 2. x y is n RE dnoting L(x) L(y) Any finit list of words cn writtn s n RE, ( w0 w1 w2 wn ) 3. x * is n RE dnoting L(x) * = 0 k < L(x) k 4. x + is n RE dnoting L(x) + = 1 k < L(x) k W cn us closur to writ finit dscriptions of infinit, ut countl, sts

15 15 Rviw: Rgulr Exprssion Lt th nottion [0-9] shorthnd for ( ) RE Exmpls: Non-ngtiv intgr [0-9] [0-9] * or [0-9] + No lding zros 0 [1-9] [0-9] * Algol-styl Idntifir ([-z] [A-Z]) ([-z] [A-Z] [0-9]) * Dciml numr 0 [1-9] [0-9] *. [0-9] * Rl numr (( 0 [1-9] [0-9] * ) (0 [1-9] [0-9] *. [0-9] * ) E [0-9] [0-9] * Ech of ths REs corrsponds to n utomton nd n implmnttion.

16 16 Rviw: From RE to Scnnr Construct scnnrs dirctly from REs using utomt thory Thr r svrl wys to prform this construction Clssic pproch is two-stp mthod Build utomt for ch pic of th RE using simpl tmplt-drivn mthod Build spcific vrition on n utomton tht hs trnsitions on ε nd non-dtrministic choic (multipl trnsitions from stt on th sm symol) This construction is clld Thompson s construction - 2. Convrt th nwly uilt utomton into dtrministic utomton Dtrministic utomton hs no ε-trnsitions nd ll choics r singl-vlud This construction is clld th sust construction - Givn th dtrministic utomton, minimiz it to rduc th numr of stts - Minimiztion is spc optimiztion. Both th originl utomton nd th miniml on tk O(1) tim pr chrctr

17 17 Rviw: Thompson s Construction Th Ky Id - For ch RE symols nd oprtor, w hv smll tmplt - Build thm, in prcdnc ordr, nd join thm with ε-trnsitions S 0 S 1 S 0 S 1 S 3 S 4 NFA for NFA for S 0 S 1 S 2 S 3 S 4 S 5 S 0 S 1 S 3 S 4 NFA for NFA for * Prcdnc in REs: Prnthss, thn closur, thn conctntion, thn ltrntion

18 18 Rviw: Thompson s Construction Th Ky Id - For ch RE symols nd oprtor, w hv smll tmplt - Build thm, in prcdnc ordr, nd join thm with ε-trnsitions S 0 S 1 S 0 S 1 S 3 S 4 NFA for NFA for S 0 S 1 S 2 S 3 S 4 S 5 S 0 S 1 S 3 S 4 NFA for NFA for * Prcdnc in REs: Prnthss, thn closur, thn conctntion, thn ltrntion

19 19 Rviw: Thompson s Construction Th Ky Id - For ch RE symols nd oprtor, w hv smll tmplt - Build thm, in prcdnc ordr, nd join thm with ε-trnsitions S 0 S 1 S 0 S 1 S 3 S 4 NFA for NFA for S 0 S 1 S 2 S 3 S 4 S 5 S 0 S 1 S 3 S 4 NFA for NFA for * Prcdnc in REs: Prnthss, thn closur, thn conctntion, thn ltrntion

20 20 Rviw: Thompson s Construction Th Ky Id - For ch RE symols nd oprtor, w hv smll tmplt - Build thm, in prcdnc ordr, nd join thm with ε-trnsitions S 0 S 1 S 0 S 1 S 3 S 4 NFA for NFA for S 0 S 1 S 2 S 3 S 4 S 5 S 0 S 1 S 3 S 4 NFA for NFA for * Prcdnc in REs: Prnthss, thn closur, thn conctntion, thn ltrntion

21 21 Rviw: Thompson s Construction Th Ky Id - For ch RE symols nd oprtor, w hv smll tmplt - Build thm, in prcdnc ordr, nd join thm with ε-trnsitions S 0 S 1 S 0 S 1 S 3 S 4 NFA for NFA for S 0 S 1 S 2 S 3 S 4 S 5 S 0 S 1 S 3 S 4 NFA for NFA for * Prcdnc in REs: Prnthss, thn closur, thn conctntion, thn ltrntion

22 22 Rviw: Thompson s Construction Th Ky Id - For ch RE symols nd oprtor, w hv smll tmplt - Build thm, in prcdnc ordr, nd join thm with ε-trnsitions S 0 S 1 S 0 S 1 S 3 S 4 NFA for NFA for S 0 S 1 S 2 S 3 S 4 S 5 S 0 S 1 S 3 S 4 NFA for NFA for * Prcdnc in REs: Prnthss, thn closur, thn conctntion, thn ltrntion

23 23 Rviw: Thompson s Construction Lt s uild n NFA for ( c )* 1.,, & c S 0 S 1 S 0 S 1 2. c S 1 S 2 c S 0 S 1 S 0 S 5 c S 3 S 4 3. ( c ) * S 2 S 3 S 0 S 1 S 6 S 7 c S 4 S 5 4. ( c ) * S 0 S 1 S 4 S 5 S 2 S 3 S 8 S 9 c S 6 S 7 Prcdnc in REs: Prnthss, thn closur, thn conctntion, thn ltrntion

24 24 Rviw: Thompson s Construction Lt s uild n NFA for ( c )* 1.,, & c S 0 S 1 S 0 S 1 2. c S 1 S 2 c S 0 S 1 S 0 S 5 c S 3 S 4 3. ( c ) * S 2 S 3 S 0 S 1 S 6 S 7 c S 4 S 5 4. ( c ) * S 0 S 1 S 4 S 5 S 2 S 3 S 8 S 9 c S 6 S 7 Prcdnc in REs: Prnthss, thn closur, thn conctntion, thn ltrntion

25 25 Rviw: Thompson s Construction Lt s uild n NFA for ( c )* 1.,, & c S 0 S 1 S 0 S 1 2. c S 1 S 2 c S 0 S 1 S 0 S 5 c S 3 S 4 3. ( c ) * S 2 S 3 S 0 S 1 S 6 S 7 c S 4 S 5 4. ( c ) * S 0 S 1 S 4 S 5 S 2 S 3 S 8 S 9 c S 6 S 7 Prcdnc in REs: Prnthss, thn closur, thn conctntion, thn ltrntion

26 26 Rviw: Thompson s Construction Lt s uild n NFA for ( c )* 1.,, & c S 0 S 1 S 0 S 1 2. c S 1 S 2 c S 0 S 1 S 0 S 5 c S 3 S 4 3. ( c ) * S 2 S 3 S 0 S 1 S 6 S 7 c S 4 S 5 4. ( c ) * S 0 S 1 S 4 S 5 S 2 S 3 S 8 S 9 c S 6 S 7 Prcdnc in REs: Prnthss, thn closur, thn conctntion, thn ltrntion

27 27 Sust Construction Th Concpt - Build simplr utomton (no ε-trnsitions, no multi-vlud trnsitions) tht simults th hvior of th mor complx utomton - Ech stt in th nw utomton rprsnts st of stts in th originl NFA n 0 n 1 ( c )* n 4 n 5 n 2 n 3 n 8 n 9 c n 6 n 7 DFA DFA NFA d 0 d 1 c d 2 c d0 d1 n 0 n 1 n 2 n 3 n 4 n 6 n 9 d 3 d2 n 5 n 8 n 9 n 3 n 4 n 6 c d3 n 7 n 8 n 9 n 3 n 4 n 6

28 28 Sust Construction Th Concpt - Build simplr utomton (no ε-trnsitions, no multi-vlud trnsitions) tht simults th hvior of th mor complx utomton - Ech stt in th nw utomton rprsnts st of stts in th originl NFA n 0 n 1 ( c )* n 4 n 5 n 2 n 3 n 8 n 9 c n 6 n 7 DFA DFA NFA d 0 d 1 c d 2 c d0 d1 n 0 n 1 n 2 n 3 n 4 n 6 n 9 d 3 d2 n 5 n 8 n 9 n 3 n 4 n 6 c d3 n 7 n 8 n 9 n 3 n 4 n 6

29 29 Sust Construction Th Concpt - Build simplr utomton (no ε-trnsitions, no multi-vlud trnsitions) tht simults th hvior of th mor complx utomton - Ech stt in th nw utomton rprsnts st of stts in th originl NFA n 0 n 1 ( c )* n 4 n 5 n 2 n 3 n 8 n 9 c n 6 n 7 DFA DFA NFA d 0 d 1 c d 2 c d0 d1 n 0 n 1 n 2 n 3 n 4 n 6 n 9 d 3 d2 n 5 n 8 n 9 n 3 n 4 n 6 c d3 n 7 n 8 n 9 n 3 n 4 n 6

30 30 Sust Construction Th Concpt - Build simplr utomton (no ε-trnsitions, no multi-vlud trnsitions) tht simults th hvior of th mor complx utomton - Ech stt in th nw utomton rprsnts st of stts in th originl NFA n 0 n 1 ( c )* n 4 n 5 n 2 n 3 n 8 n 9 c n 6 n 7 DFA DFA NFA d 0 d 1 c d 2 c d0 d1 n 0 n 1 n 2 n 3 n 4 n 6 n 9 d 3 d2 n 5 n 8 n 9 n 3 n 4 n 6 c d3 n 7 n 8 n 9 n 3 n 4 n 6

31 31 Sust Construction Th Concpt - Build simplr utomton (no ε-trnsitions, no multi-vlud trnsitions) tht simults th hvior of th mor complx utomton - Ech stt in th nw utomton rprsnts st of stts in th originl NFA n 0 n 1 ( c )* n 4 n 5 n 2 n 3 n 8 n 9 c n 6 n 7 DFA DFA NFA d 0 d 1 c d 2 c d0 d1 n 0 n 1 n 2 n 3 n 4 n 6 n 9 d 3 d2 n 5 n 8 n 9 n 3 n 4 n 6 c d3 n 7 n 8 n 9 n 3 n 4 n 6

32 32 Sust Construction Th Concpt - Build simplr utomton (no ε-trnsitions, no multi-vlud trnsitions) tht simults th hvior of th mor complx utomton - Ech stt in th nw utomton rprsnts st of stts in th originl NFA n 0 n 1 ( c )* n 4 n 5 n 2 n 3 n 8 n 9 c n 6 n 7 DFA DFA NFA d 0 d 1 c d 2 c d0 d1 n 0 n 1 n 2 n 3 n 4 n 6 n 9 d 3 d2 n 5 n 8 n 9 n 3 n 4 n 6 c d3 n 7 n 8 n 9 n 3 n 4 n 6

33 33 Sust Construction Th Concpt - Build simplr utomton (no ε-trnsitions, no multi-vlud trnsitions) tht simults th hvior of th mor complx utomton - Ech stt in th nw utomton rprsnts st of stts in th originl NFA n 0 n 1 ( c )* n 4 n 5 n 2 n 3 n 8 n 9 c n 6 n 7 DFA DFA NFA d 0 d 1 c d 2 c d0 d1 n 0 n 1 n 2 n 3 n 4 n 6 n 9 d 3 d2 n 5 n 8 n 9 n 3 n 4 n 6 c d3 n 7 n 8 n 9 n 3 n 4 n 6

34 34 Sust Construction Th Concpt - Build simplr utomton (no ε-trnsitions, no multi-vlud trnsitions) tht simults th hvior of th mor complx utomton - Ech stt in th nw utomton rprsnts st of stts in th originl NFA n 0 n 1 ( c )* n 4 n 5 n 2 n 3 n 8 n 9 c n 6 n 7 DFA DFA NFA d 0 d 1 c d 2 c d0 d1 n 0 n 1 n 2 n 3 n 4 n 6 n 9 d 3 d2 n 5 n 8 n 9 n 3 n 4 n 6 c d3 n 7 n 8 n 9 n 3 n 4 n 6

35 35 Sust Construction Th Concpt - Build simplr utomton (no ε-trnsitions, no multi-vlud trnsitions) tht simults th hvior of th mor complx utomton - Ech stt in th nw utomton rprsnts st of stts in th originl NFA n 0 n 1 ( c )* n 4 n 5 n 2 n 3 n 8 n 9 c n 6 n 7 DFA DFA NFA d 0 d 1 c d 2 c d0 d1 n 0 n 1 n 2 n 3 n 4 n 6 n 9 d 3 d2 n 5 n 8 n 9 n 3 n 4 n 6 c d3 n 7 n 8 n 9 n 3 n 4 n 6

36 36 Minimiztion DFA minimiztion lgorithms work y discovring stts tht r quivlnt in thir contxts nd rplcing multipl stts with singl on -Minimiztion rducs th numr of stts, ut dos not chng th costs d 0 d 1 c d 2 c s 0 s 1 c Miniml DFA Stt Originl DFA Stt s0 d0 s1 d1, d2, d3 d 3 c

37 37 Minimiztion DFA minimiztion lgorithms work y discovring stts tht r quivlnt in thir contxts nd rplcing multipl stts with singl on -Minimiztion rducs th numr of stts, ut dos not chng th costs d 0 d 1 c d 2 c s 0 s 1 c Miniml DFA Stt Originl DFA Stt s0 d0 s1 d1, d2, d3 d 3 c

38 38 Minimiztion DFA minimiztion lgorithms work y discovring stts tht r quivlnt in thir contxts nd rplcing multipl stts with singl on -Minimiztion rducs th numr of stts, ut dos not chng th costs d 0 d 1 c d 2 c s 0 s 1 c Miniml DFA Stt Originl DFA Stt s0 d0 s1 d1, d2, d3 d 3 c

39 39 Implmnting n Automton A common strtgy is to simult th DFA s xcution - Sklton prsr + tl tht ncors th utomton - Th scnnr gnrtor constructs th tl - Th sklton prsr dos not chng stt s 0 chr NxtChr( ) whil (chr ¹ EOF) { stt d[stt,chr] chr NxtChr( ) } if (stt is finl stt) thn rport succss ls rport n rror Trnsition tl for our miniml DFA δ c s0 s1 s s s1 s s1 s1 Simpl Sklton Scnnr

40 40 Automtic Scnnr Construction Scnnr Gnrtor - Tsks in spcifiction writtn s collction of rgulr xprssions - Comins thm into on RE using ltrntion ( ) - Builds th miniml utomton ( 2.4, EAC) - Emits th tls to driv sklton scnnr ( 2.5 EAC) Sourc Cod Sklton Scnnr Tls <prt of spch, word> pirs Spcifictions (s RE) Scnnr Gnrtor

41 41 Automtic Scnnr Construction Scnnr Gnrtor - As ltrntiv, th gnrtor cn produc cod rthr thn tls - Dirct-codd scnnrs r ugly, ut oftn fstr thn tl-drivn scnnrs - Othr thn spd, th two r quivlnt Sourc Cod Sklton Scnnr Tls <prt of spch, word> pirs Spcifictions (s RE) Scnnr Gnrtor

42 42 Automton vrsus Scnnr Automton ccpts or rjcts word - Runs until it xhusts th input nd ccpts or rjcts th strm Scnnr looks t th whol progrm nd rturns ll of th tokns - Must rk th input strm into sprt words - Must cptur nd clssify th lxm - Must dcid whn it hs lookd yond th nd of word Scnnr gnrtors uild th utomton for st of REs nd thn convrt th utomton Considr th RE: r [0-9] + And its miniml DFA: s 0 r [0-9] s 1 s 2 [0-9] into n fficint scnnr

43 43 Tl-Drivn Scnnr Th scnnr dos mor thn rcogniz word - Ctgoriz th tokn nd cptur its splling - Build DFA crfully such tht finl stt mps to ctgory - Rmmr th chrctrs t ch trnsition to cptur th splling

44 44 Tl-Drivn Scnnr Th scnnr dos mor thn rcogniz word - Ctgoriz th tokn nd cptur its splling - Build DFA crfully such tht finl stt mps to ctgory - Rmmr th chrctrs t ch trnsition to w cptur th splling chr nxt chrctr stt s 0 lxm null string whil (chr EOF ) { lxm lxm chr stt δ[stt,chr] chr nxt chrctr } If (stt S A ) thn rsult <PoS(stt), lxm> ls rsult <invlid, > rturn rsult δ r Trnsition Tl (δ) 0,1,2,3,4, Any 5,6,7,8,9 Othr s 0 s 1 s s s 1 s s 2 s s 2 s s 2 s s s s s

45 45 Tl-Drivn Scnnr Th scnnr dos mor thn rcogniz word - Ctgoriz th tokn nd cptur its splling - Build DFA crfully such tht finl stt mps to ctgory - Rmmr th chrctrs t ch trnsition to w cptur th splling Th scnnr nds to rcogniz whn on tokn nds nd nw on gins - Nd to rturn ll th words, in ordr, ut on t tim - Do not wnt forc lnks or dlimitrs vrywhr - Should x + y * z nd x+y*z scn diffrntly?

46 46 Rcognizing Tokn Boundris Two potntil solutions - Rquir dlimitrs twn vry tokn might ugly nd pinful - Run th utomton to n rror or EOF, nd thn ck up to finl stt // rcogniz words chr nxt chrctr stt s 0 lxm mpty string clr stck push (d) whil (stt ¹ s ) do chr nxt chrctr lxm lxm chr if stt S A thn clr stck push (stt) stt d(stt,chr) nd // cln up finl stt whil (stt S A nd stt d) do stt pop() trunct lxm roll ck th input on chr nd // rport th rsults if (stt S A ) thn rsult = <PoS(stt), lxm> ls rsult = <invlid, > rturn rsult

47 47 Tl-Drivn Scnnr Th scnnr dos mor thn rcogniz word - Ctgoriz th tokn nd cptur its splling - Build DFA crfully such tht finl stt mps to ctgory - Rmmr th chrctrs t ch trnsition to w cptur th splling Th scnnr nds to rcogniz whn on tokn nds nd nw on gins - Nd to rturn ll th words, in ordr, ut on t tim - Do not wnt forc lnks or dlimitrs vrywhr - Should x + y * z nd x+y*z scn diffrntly? Th scnnr nds to dl with th cs whn multipl mtchs r possil - Two or mor ctgoris for th sm xct string - Two or mor mtchs for givn string

48 48 Amiguous Rgulr Exprssions Amiguity is not lwys d thing - Somtims, it is sir to spcify n miguous RE = * ** *= < <= = > >= - Th scnnr nds to hndl miguous REs in n pproprit wy Nd n pproch to spcify th xpctd hvior - Scnnr gnrtors ssign priority or prcdnc y th ordr in which th pttrns ppr in th spcifiction - First mtching pttrn tks prcdnc

49 49 Exmpl: Rcognizing Kywords for nd whil For n RE such s for whil ( [-z] [-z] [0-9])* ) w wnt n utomt such s f o r s 2 s 3 s 4 [^o] [^r] [-z] [0-9] [^x] mns ny chrctr in Σ xcpt x [-] [g-v] [x-z] s 0 s 10 [-z] [0-9] [-z] [0-9] w s 5 [^h] [^i] [^l] [^] h i l s 6 s 7 s 8 s 9 s4 for s9 whil s10 gnrl idntifir

50 50 Amiguous Rgulr Exprssions Most lngugs hv oth kywords nd idntifirs - for might th RE for th kyword for - ([-z] ) ([-z] [0-9] ) might th RE for n idntifir - Th input string for mtchs oth REs Kywords r somtims hndld s spcil cs - Us th simplr RE nd utomton shown hr - Build hsh tl of idntifirs & prlod kywords - Rcogniz idntifirs during th hsh lookup 1 Th Trdoff [-z] s 0 s 10 [-z] [0-9] Simplr utomton - Mor stts in th scnnr vrsus lookup cost on kywords - Encoding kywords in RE hs no significnt runtim cost - Eithr strtgy works & diffrnc in runtim cost is tiny

51 51 Automtic Gnrtion of Scnnrs nd Prsrs Scnnr Gnrtion Procss 1. Writ down th REs for th input lngug nd connct thm with 2. Build ig, simpl NFA 3. Build th DFA tht simults th NFA 4. Minimiz th numr of stts in th DFA 5. Gnrt n implmnttion from th miniml DFA Scnnr Gnrtors 1. lx, flx, jflx, nd such ll work long ths lins 2. Algorithms r wll-known nd wll-undrstood 3. Ky issus: finding th longst mtch rthr thn first mtch, nd nginring th intrfc to th prsr

52 52 Automtic Gnrtion of Scnnrs nd Prsrs Dsign Tim spcifictions s CFGs Prsr Gnrtor Compilr-uild Tim Spcifiction s micro Convrt spcifiction to syntx nd syntx spcifictions s REs Scnnr Gnrtor Compilr-uild tim ctul compilr cod Compil tim Prsr IR nnottions strm of chrctrs Scnnr Smntic Elortion Front End Compil Tim Trnslt n ppliction in spcifid lngug into n xcutl form

53 53 Automtic Scnnr Gnrtion flx is scnnr gnrtor - flx follows th input convntions of lx - Tks fil tht includs REs for th words - Producs tl-drivn scnnr to rcogniz words nd rturn tokns - flx works with ison: th GNU LR(1) prsr gnrtor ison producs fil of dfinitions to - connct scnnr prts of spch to trminl symols in th grmmr (*.t.h) sourc cod Scnnr <prt of spch, word> pirs spcifictions writtn s rgulr xprssions Scnnr Gnrtor

54 54 Us flx to Gnrt Scnnrs A flx input fil hs thr prts Dfinitions sction - Holds #includ s, dclrtions, nd dfinitions (.g., DIGIT [0-9]) - Mchnism to insrt vrtim cod t th top of gnrtd scnnr cod Ruls sction - Holds sris of ruls, which consist of pttrn nd n ction - pttrn is just n RE - ction is frgmnt of C cod; ithr singl sttmnt or lock in rckts - Ruls r prioritizd in ordr of pprnc - Mchnism to insrt ritrry cod into scnning routin Usr cod sction - Holds cod tht is copid, vrtim, to th scnnr output fil - This sction is optionl good plc to put th min function Smpl Cod: /* Compnion sourc cod for "flx & ison", pulishd y O'Rilly */ /* f1-1 just lik unix wc */ %{ int chrs = 0; int words = 0; int lins = 0; %} %% [-za-z]+ { words++; chrs += strln(yytxt); } \n { chrs++; lins++; }. { chrs++; } %% min() { yylx(); printf("%8d%8d%8d\n", lins, words, chrs); } Sctions r sprtd y th dlimitr %% Cod: /il/usrs/zz124/cs515_2017/compilr_smpls/flxandbisonbsics

55 55 Spcifying RE Pttrns in flx flx hs rich spcifiction lngug mtchs th chrctr. mtchs ny chrctr xcpt \n [c] is clss; it mtchs c [f-jz] mtchs f g h i j Z f-j is numricl rng in collting squnc [^c] is ngtd clss; it mtchs ny chrctr xcpt,, or c [-z]{-}[iou] mtchs lowrcs consonnt lttrs (st sutrction) x * mtchs zro or mor instncs 1 of x x + mtchs on or mor instncs of x x? mtchs zro or on instnc of x x{3} mtchs xctly 3 x s, s would xxx x{3,} mtchs 3 or mor instncs of x x{3,5} mtchs 3, 4, or 5 instncs of x {nm} mtchs nm dfind in th dfinition sction of th fil \n mtchs th nwlin chrctr \, \, \f, \r, \t, \v mtch thir C scps \x mtchs x; usful for mtching oprtors in th RE nottion such s ^ xy mtchs RE x followd y RE y x y mtchs ithr RE x or RE y ^x mtchs x t th strt of lin x$ mtchs RE x t th nd of lin x/y mtchs x only if it is followd y RE y, ut dos not mtch y. (pushs y ck into th input strm) And, thr r mor options

56 Lctur 4B: Lxicl nd Syntx Anlysis III 56

57 57 Rviw: Compilr Front End Front End IR Middl End IR Bck End Mchin cod Errors Prsr IR Scnnr nd Prsr collort to chck th syntx of th input progrm. 1. Scnnr mps strm of chrctrs into words (words r th fundmntl unit of syntx) strm of chrctrs Scnnr Smntic Elortion Scnnr producs strm of tokns for th prsr Tokn is <prt of spch, word> Prt of spch is unit in th grmmr 2. Prsr mps strm of words into sntnc in grmmticl modl of th input lngug

58 58 Rviw: Th Compilr s Front End Scnnr looks t vry chrctr Convrts strm of chrctrs to strm of tokns, or clssifid words: <prt of spch, word> Efficincy & sclility mttr Scnnr is only prt of compilr tht looks t vry chrctr Prsr looks t vry tokn -Dtrmins if th strm of tokns forms strm of chrctrs Scnnr Prsr Smntic Elortion Front End IR sntnc in th sourc lngug -Fits tokns to som syntctic modl, or grmmr, for th sourc lngug

59 59 Rviw: Th Compilr s Front End How cn w utomt th construction of scnnr & prsrs? Scnnr Prsr IR - Spcify syntx with rgulr xprssions (REs) - Construct finit utomton & scnnr from th rgulr xprssion strm of chrctrs Scnnr Smntic Elortion Prsr - Spcify syntx with contxt-fr grmmrs Front End (CFGs) - Construct push-down utomton & prsr from th CFG

60 60 Th Study of Prsing Prsing is th procss of discovring drivtion for som sntnc -Nd mthmticl modl of syntx contxt-fr grmmr G -Nd n lgorithm to tst mmrship in L(G)

61 61 Spcifying Syntx: Contxt-Fr Grmmrs A CFG is four tupl, G = (S, N, T, P ) -S is th strt symol of th grmmr L(G) is th st of sntncs tht cn drivd from S -N is st of nontrminl symols or syntctic vrils { Gol, List, Pir } -T is st of trminl symols or words { (, ) } -P is st of productions or rwrit ruls 1 Gol List 2 List List Pir 3 Pir 4 Pir ( List ) 5 ( ) Prnthss Grmmr This grmmr is writtn in vrint of Bckus Nur Form (BNF)

62 62 Spcifying Syntx: Contxt-Fr Grmmrs A CFG is four tupl, G = (S, N, T, P ) -S is th strt symol of th grmmr L(G) is th st of sntncs tht cn drivd from S -N is st of nontrminl symols or syntctic vrils { Gol, List, Pir } -T is st of trminl symols or words { (, ) } -P is st of productions or rwrit ruls 1 Gol List 2 List List Pir 3 Pir 4 Pir ( List ) 5 ( ) Prnthss Grmmr L(G) includs sntncs such s ( ), (( )) ( ), & ( ) ( ) ( ) This grmmr is writtn in vrint of Bckus Nur Form (BNF)

63 63 Spcifying Syntx: Contxt-Fr Grmmrs Why not us rgulr xprssions to spcify PL syntx? Rgulr lngugs, rgulr xprssions, nd DFAs r limitd - Cn DFA count? As in L = { p k q k } or L = { wcw R w Σ* } - Finit stts mns no counting of unoundd vnt - Mtching itms in this fshion rquirs stck - Push p s onto th stck; pop thm off to mtch q s - Empty stck t th nd indicts n qul numr of p s nd q s Nithr of ths is rgulr lngug Dos this limittion mttr? - Sinc L = { p k q k } is th ssnc of mtching prnthss, rckts, nd th lik, this limittion is n importnt on - Evry progrmming lngug I know hs som mtching construct Bottom lin: REs for splling, CFGs for syntx

64 64 Spcifying Syntx: Contxt-Fr Grmmrs Why not us rgulr xprssions to spcify PL syntx? Rgulr lngugs, rgulr xprssions, nd DFAs r limitd - A DFA cnnot count, s in L = { p k q k } or L = { wcw R w Σ* } - Finit stts mns no counting of unoundd vnt - Mtching itms in this fshion rquirs stck - Push p s onto th stck; pop thm off to mtch q s - Empty stck t th nd indicts n qul numr of p s nd q s Nithr of ths is rgulr lngug Dos this limittion mttr? - Sinc L = { p k q k } is th ssnc of mtching prnthss, rckts, nd th lik, this limittion is n importnt on - Evry progrmming lngug I know hs som mtching construct Bottom lin: REs for splling, CFGs for syntx

65 65 Spcifying Syntx: Contxt-Fr Grmmrs Why not us rgulr xprssions to spcify PL syntx? Rgulr lngugs, rgulr xprssions, nd DFAs r limitd - A DFA cnnot count, s in L = { p k q k } or L = { wcw R w Σ* } - Finit stts mns no counting of unoundd vnt - Mtching itms in this fshion rquirs stck - Push p s onto th stck; pop thm off to mtch q s - Empty stck t th nd indicts n qul numr of p s nd q s Nithr of ths is rgulr lngug Dos this limittion mttr? - Sinc L = { p k q k } is th ssnc of mtching prnthss, rckts, nd th lik, this limittion is n importnt on - Evry progrmming lngug I know hs som mtching construct Bottom lin: REs for splling, CFGs for syntx

66 66 Spcifying Syntx: Contxt-Fr Grmmrs Why not us rgulr xprssions to spcify PL syntx? Rgulr lngugs, rgulr xprssions, nd DFAs r limitd - A DFA cnnot count, s in L = { p k q k } or L = { wcw R w Σ* } - Finit stts mns no counting of unoundd vnt - Mtching itms in this fshion rquirs stck - Push p s onto th stck; pop thm off to mtch q s - Empty stck t th nd indicts n qul numr of p s nd q s Nithr of ths is rgulr lngug Dos this limittion mttr? - Sinc L = { p k q k } is th ssnc of mtching prnthss, rckts, nd th lik, this limittion is n importnt on - Evry progrmming lngug I know hs som mtching construct Bottom lin: REs for splling, CFGs for syntx

67 67 Prsing Th point of prsing is to discovr grmmticl drivtion for sntnc A drivtion consists of sris of rwrit stps S γ0 γ1 γ2 γn 1 γn sntnc S is th strt symol of th grmmr Ech γi is sntntil form - If γ contins only trminl symols, γ is sntnc in L(G) - If γ contins 1 or mor non-trminls, γ is sntntil form To gt γi from γi 1, xpnd som NT A γi 1 y using A β - Rplc th occurrnc of A γi 1 with β to gt γi - Rplcing th lftmost NT t ch stp, crts lftmost drivtion - Rplcing th rightmost NT t ch stp, crts rightmost drivtion NT mns nontrminl symol

68 68 Prsing Th point of prsing is to discovr grmmticl drivtion for sntnc Rul Sntntil Form 1 Gol List Drivtion Gol 1 List 3 Pir 5 ( ) Prs 2 List List Pir 3 Pir 4 Pir ( List ) 5 ( ) Drivtion of ( ) Prnthss Grmmr

69 69 Prsing Th point of prsing is to discovr grmmticl drivtion for sntnc Rul Sntntil Form 1 Gol List Drivtion Gol 1 List 3 Pir 5 ( ) Prs 2 List List Pir 3 Pir 4 Pir ( List ) 5 ( ) Drivtion of ( ) Prnthss Grmmr

70 70 Prsing Th point of prsing is to discovr grmmticl drivtion for sntnc Rul Sntntil Form 1 Gol List Drivtion Gol 1 List 3 Pir 5 ( ) Prs 2 List List Pir 3 Pir 4 Pir ( List ) 5 ( ) Drivtion of ( ) Prnthss Grmmr

71 71 Prsing Th point of prsing is to discovr grmmticl drivtion for sntnc Rul Sntntil Form 1 Gol List Drivtion Gol 1 List 3 Pir 5 ( ) Prs 2 List List Pir 3 Pir 4 Pir ( List ) 5 ( ) Drivtion of ( ) Prnthss Grmmr

72 72 Prsing Th point of prsing is to discovr grmmticl drivtion for sntnc Rul Sntntil Form 1 Gol List Drivtion Gol 1 List 3 Pir 5 ( ) Prs 2 List List Pir 3 Pir 4 Pir ( List ) 5 ( ) Drivtion of ( ) Prnthss Grmmr

73 73 Prsing Th point of prsing is to discovr grmmticl drivtion for sntnc Rul Sntntil Form 1 Gol List Drivtion Gol 1 List 3 Pir 5 ( ) Prs 2 List List Pir 3 Pir 4 Pir ( List ) 5 ( ) Drivtion of ( ) Prnthss Grmmr

74 74 Prsing Th point of prsing is to discovr grmmticl drivtion for sntnc Drivtion Rul Sntntil Form Gol 1 List 2 List Pir 5 List ( ) 3 Pir ( ) 4 ( List ) ( ) Prs 1 Gol List 2 List List Pir 3 Pir 4 Pir ( List ) 5 ( ) Prnthss Grmmr 3 ( Pir ) ( ) 5 ( ( ) ) ( ) W dnot this prticulr drivtion: Gol * ( ( ) ) ( ) Drivtion of ( ( ) ) ( )

75 75 Prsing Th point of prsing is to discovr grmmticl drivtion for sntnc Drivtion Rul Sntntil Form Gol 1 List 2 List Pir 5 List ( ) 3 Pir ( ) 4 ( List ) ( ) Prs 1 Gol List 2 List List Pir 3 Pir 4 Pir ( List ) 5 ( ) Prnthss Grmmr 3 ( Pir ) ( ) 5 ( ( ) ) ( ) W dnot this prticulr drivtion: Gol * ( ( ) ) ( ) Drivtion of ( ( ) ) ( )

76 76 Prsing Th point of prsing is to discovr grmmticl drivtion for sntnc Drivtion Rul Sntntil Form Gol 1 List 2 List Pir 5 List ( ) 3 Pir ( ) 4 ( List ) ( ) Prs 1 Gol List 2 List List Pir 3 Pir 4 Pir ( List ) 5 ( ) Prnthss Grmmr 3 ( Pir ) ( ) 5 ( ( ) ) ( ) W dnot this prticulr drivtion: Gol * ( ( ) ) ( ) Drivtion of ( ( ) ) ( )

77 77 Prsing Th point of prsing is to discovr grmmticl drivtion for sntnc Drivtion Rul Sntntil Form Gol 1 List 2 List Pir 5 List ( ) 3 Pir ( ) 4 ( List ) ( ) Prs 1 Gol List 2 List List Pir 3 Pir 4 Pir ( List ) 5 ( ) Prnthss Grmmr 3 ( Pir ) ( ) 5 ( ( ) ) ( ) W dnot this prticulr drivtion: Gol * ( ( ) ) ( ) Drivtion of ( ( ) ) ( )

78 78 Prsing Th point of prsing is to discovr grmmticl drivtion for sntnc Drivtion Rul Sntntil Form Gol 1 List 2 List Pir 5 List ( ) 3 Pir ( ) 4 ( List ) ( ) Prs 1 Gol List 2 List List Pir 3 Pir 4 Pir ( List ) 5 ( ) Prnthss Grmmr 3 ( Pir ) ( ) 5 ( ( ) ) ( ) W dnot this prticulr drivtion: Gol * ( ( ) ) ( ) Drivtion of ( ( ) ) ( )

79 79 Prsing Th point of prsing is to discovr grmmticl drivtion for sntnc Drivtion Rul Sntntil Form Gol 1 List 2 List Pir 5 List ( ) 3 Pir ( ) 4 ( List ) ( ) Prs 1 Gol List 2 List List Pir 3 Pir 4 Pir ( List ) 5 ( ) Prnthss Grmmr 3 ( Pir ) ( ) 5 ( ( ) ) ( ) W dnot this prticulr drivtion: Gol * ( ( ) ) ( ) Drivtion of ( ( ) ) ( )

80 80 Prsing Th point of prsing is to discovr grmmticl drivtion for sntnc Drivtion Rul Sntntil Form Gol 1 List 2 List Pir 5 List ( ) 3 Pir ( ) 4 ( List ) ( ) Prs 1 Gol List 2 List List Pir 3 Pir 4 Pir ( List ) 5 ( ) Prnthss Grmmr 3 ( Pir ) ( ) 5 ( ( ) ) ( ) W dnot this prticulr drivtion: Gol * ( ( ) ) ( ) Drivtion of ( ( ) ) ( )

81 81 Prsing A drivtion corrsponds to prs tr or syntx tr Rul Sntntil Form Gol Gol 1 List List Drivtion 2 List Pir 5 List ( ) 3 Pir ( ) 4 ( List ) ( ) Prs List Pir Pir ( ) 3 ( Pir ) ( ) 5 ( ( ) ) ( ) ( ) List Drivtion of ( ( ) ) ( ) Pir Gol * ( ( ) ) ( ) ( )

82 82 Prsing A drivtion corrsponds to prs tr or syntx tr Rul Sntntil Form Gol Gol 1 List List Drivtion 2 List Pir 5 List ( ) 3 Pir ( ) 4 ( List ) ( ) Prs List Pir Pir ( ) 3 ( Pir ) ( ) 5 ( ( ) ) ( ) ( ) List Drivtion of ( ( ) ) ( ) Th drivtion givs us th grmmticl structur of th input sntnc, which ws missing in th DFA / RE sd rcognizrs. Pir ( ) Gol * ( ( ) ) ( )

83 83 Two Ctgoris of Systmtic Prsrs Top-down prsrs (LL(1), rcursiv dscnt s EAC 3.3) - Strt with th root of th prs tr nd grow towrd th lvs - At ch stp, pick production & try to mtch th input - Bd pick my nd to cktrck - Som grmmrs r cktrck fr List Gol List Pir Bottom-up prsrs (LR(1), oprtor prcdnc s EAC 3.4) - Strt t th lvs nd grow towrd th root - As input is consumd, ncod possiilitis in n intrnl stt - Strt in stt vlid for lgl first tokns - W cn mk th procss dtrministic Pir ( ) ( ) List Pir Gol * ( ( ) ) ( ) ( )

84 84 Two Ctgoris of Systmtic Prsrs Top-down prsrs (LL(1), rcursiv dscnt s EAC 3.3) - Strt with th root of th prs tr nd grow towrd th lvs - At ch stp, pick production & try to mtch th input - Bd pick my nd to cktrck - Som grmmrs r cktrck fr List Gol List Pir Bottom-up prsrs (LR(1), oprtor prcdnc s EAC 3.4) - Strt t th lvs nd grow towrd th root - As input is consumd, ncod possiilitis in n intrnl stt - Strt in stt vlid for lgl first tokns - W cn mk th procss dtrministic Pir ( ) ( ) List Pir Gol * ( ( ) ) ( ) ( )

85 85 Bottom-up Prsrs How dos ottom-up prsr uild (discovr) drivtion? Th prsr uss stck to hold grmmr symols, oth trminl symols & nontrminl symols (In ssnc), it pushs tokns onto stck until th stck holds th right-hnd sid (rhs) of som production (rwrit rul) Whn it finds n rhs t th top of th stck, it rwrits th rhs with th rul s lhs (clld rduction or rduc ction ) How dos it rcogniz n rhs, or (mor prcisly) th nxt rhs in th drivtion of th input sntnc? For th momnt, ssum tht w hv n orcl to nswr tht qustion Applying th orcl to th stck yilds on of thr rsults: A production <lhs:rhs> whos rhs is t th top of th stck An indiction tht th stck is consistnt with som futur rs An indiction tht th stck cnnot ld to n rhs

86 86 A concptul ottom-up, shift-rduc prsr push $ onto th stck word NxtTokn() rpt until (top of stck = S nd word = EOF) ction orcl(stck,word) if (ction is futur & word EOF) thn push word word NxtTokn() ls if (ction is <lhs:rhs>) thn pop rhs symols off th stck push lhs onto th stck ls rport n rror rk out of loop rport succss Hr: $ is usd s n invlid symol S is th gol symol of th grmmr G push() nd pop() implmnt simpl stck

87 87 A concptul ottom-up, shift-rduc prsr push $ onto th stck word NxtTokn() rpt until (top of stck = S nd word = EOF) ction orcl(stck,word) if (ction is futur & word EOF) thn push word word NxtTokn() ls if (ction is <lhs:rhs>) thn pop rhs symols off th stck push lhs onto th stck ls rport n rror rk out of loop A shift-rduc prsr hs four kinds of ctions: Shift: nxt word is movd from input to th stck Rduc: TOS is rhs of rduction pop rhs off th stck push lhs onto th stck Error: rport th prolm to usr Accpt: (norml xit from loop) rport succss nd stop prsing Shift, Accpt, & Error r O(1) Rduc is O( rhs ) rport succss

88 88 A concptul ottom-up, shift-rduc prsr push $ onto th stck word NxtTokn() rpt until (top of stck = S nd word = EOF) ction orcl(stck,word) if (ction is futur & word EOF) thn push word word NxtTokn() ls if (ction is <lhs:rhs>) thn pop rhs symols off th stck push lhs onto th stck ls rport n rror rk out of loop A shift-rduc prsr hs four kinds of ctions: Shift: nxt word is movd from input to th stck Rduc: TOS is rhs of rduction pop rhs off th stck push lhs onto th stck Error: rport th prolm to usr Accpt: (norml xit from loop) rport succss nd stop prsing Shift, Accpt, & Error r O(1) Rduc is O( rhs ) rport succss

89 89 A concptul ottom-up, shift-rduc prsr push $ onto th stck word NxtTokn() rpt until (top of stck = S nd word = EOF) ction orcl(stck,word) if (ction is futur & word EOF) thn push word word NxtTokn() ls if (ction is <lhs:rhs>) thn pop rhs symols off th stck push lhs onto th stck ls rport n rror rk out of loop A shift-rduc prsr hs four kinds of ctions: Shift: nxt word is movd from input to th stck Rduc: TOS is rhs of rduction pop rhs off th stck push lhs onto th stck Error: rport th prolm to usr Accpt: (norml xit from loop) rport succss nd stop prsing Shift, Accpt, & Error r O(1) Rduc is O( rhs ) rport succss

90 90 A concptul ottom-up, shift-rduc prsr push $ onto th stck word NxtTokn() rpt until (top of stck = S nd word = EOF) ction orcl(stck,word) if (ction is futur & word EOF) thn push word word NxtTokn() ls if (ction is <lhs:rhs>) thn pop rhs symols off th stck push lhs onto th stck ls rport n rror rk out of loop A shift-rduc prsr hs four kinds of ctions: Shift: nxt word is movd from input to th stck Rduc: TOS is rhs of rduction pop rhs off th stck push lhs onto th stck Error: rport th prolm to usr Accpt: (norml xit from loop) rport succss nd stop prsing Shift, Accpt, & Error r O(1) Rduc is O( rhs ) rport succss

91 91 A concptul ottom-up, shift-rduc prsr push $ onto th stck word NxtTokn() rpt until (top of stck = S nd word = EOF) ction orcl(stck,word) if (ction is futur & word EOF) thn push word word NxtTokn() ls if (ction is <lhs:rhs>) thn pop rhs symols off th stck push lhs onto th stck ls rport n rror rk out of loop A shift-rduc prsr hs four kinds of ctions: Shift: nxt word is movd from input to th stck Rduc: TOS is rhs of rduction pop rhs off th stck push lhs onto th stck Error: rport th prolm to usr Accpt: (norml xit from loop) rport succss nd stop prsing Shift, Accpt, & Error r O(1) Rduc is O( rhs ) rport succss

92 92 A concptul ottom-up, shift-rduc prsr Considr th input strm ( ) Stp Stck Word Orcl s nswr $ ( som futur rhs 1 $ ( ) som futur rhs 2 $ ( ) EOF < Pir : ( ) > 5 3 $ Pir EOF < List : Pir > 3 1 Gol List 2 List List Pir 3 Pir 4 Pir ( List ) 5 ( ) 4 $ List EOF < Gol : List> 1 5 $ Gol EOF xits whil loop & rports succss

93 93 A concptul ottom-up, shift-rduc prsr Considr th input strm ( ) Stp Stck Word Orcl s nswr $ ( som futur rhs 1 $ ( ) som futur rhs 2 $ ( ) EOF < Pir : ( ) > 5 3 $ Pir EOF < List : Pir > 3 1 Gol List 2 List List Pir 3 Pir 4 Pir ( List ) 5 ( ) 4 $ List EOF < Gol : List> 1 5 $ Gol EOF xits whil loop & rports succss

94 94 A concptul ottom-up, shift-rduc prsr Considr th input strm ( ) Stp Stck Word Orcl s nswr $ ( som futur rhs 1 $ ( ) som futur rhs 2 $ ( ) EOF < Pir : ( ) > 5 3 $ Pir EOF < List : Pir > 3 1 Gol List 2 List List Pir 3 Pir 4 Pir ( List ) 5 ( ) 4 $ List EOF < Gol : List> 1 5 $ Gol EOF xits whil loop & rports succss

95 95 A concptul ottom-up, shift-rduc prsr Considr th input strm ( ) Stp Stck Word Orcl s nswr $ ( som futur rhs 1 $ ( ) som futur rhs 2 $ ( ) EOF < Pir : ( ) > 5 3 $ Pir EOF < List : Pir > 3 1 Gol List 2 List List Pir 3 Pir 4 Pir ( List ) 5 ( ) 4 $ List EOF < Gol : List> 1 5 $ Gol EOF xits whil loop & rports succss

96 96 A concptul ottom-up, shift-rduc prsr Considr th input strm ( ) Stp Stck Word Orcl s nswr $ ( som futur rhs 1 $ ( ) som futur rhs 2 $ ( ) EOF < Pir : ( ) > 5 3 $ Pir EOF < List : Pir > 3 1 Gol List 2 List List Pir 3 Pir 4 Pir ( List ) 5 ( ) 4 $ List EOF < Gol : List> 1 5 $ Gol EOF xits whil loop & rports succss

97 97 A concptul ottom-up, shift-rduc prsr Considr th input strm ( ) Stp Stck Word Orcl s nswr $ ( som futur rhs 1 $ ( ) som futur rhs 2 $ ( ) EOF < Pir : ( ) > 5 3 $ Pir EOF < List : Pir > 3 1 Gol List 2 List List Pir 3 Pir 4 Pir ( List ) 5 ( ) 4 $ List EOF < Gol : List> 1 5 $ Gol EOF xits whil loop & rports succss

98 98 Mor Shifts nd Rduc Considr th input strm (( )) ( ) Mor complx xmpl Ky is rcognizing th diffrnc twn futur, n rror, nd nd rduction In n LR(1) prsr, th orcl is ncodd into two prs tls: nmd ACTION nd GOTO 1 Gol List 2 List List Pir 3 Pir 4 Pir ( List ) 5 ( ) Stp Stck Word Orcl s nswr $ ( som futur rhs 1 $ ( ( som futur rhs 2 $ ( ( ) som futur rhs 3 $ ( ( ) ) < Pir, ( ) > 5 4 $ ( Pir ) < List, Pir > 3 5 $ ( List ) som futur rhs 6 $ ( List ) ( < Pir, ( List ) > 4 7 $ Pir ( < List, Pir > 3 8 $ List ( som futur rhs 9 $ List ( ) som futur rhs 10 $ List ( ) EOF < Pir, ( ) > 5 11 $ List Pir EOF < List, List Pir > 2 12 $ List EOF < Gol, List > 1 13 $ Gol EOF xits whil loop & rports succss

99 99 How dos this work? An LR(1) prsr ncods stt informtion on th stck Tht stt informtion thrds togthr th rduc ctions to nsur tht th prsr uilds drivtion. -At ch point, th prsr is in stt tht rprsnts ll of th possil outcoms -From th comintion of th stt, th stck, nd th nxt word, it cn dcid whthr to shift, rduc, ccpt, or throw n rror (W will s xmpls.) All of tht knowldg is ncodd into th ACTION nd GOTO tls Th LR(1) Tl construction simults th st of prsr stts whn it uilds th ACTION nd GOTO tls S 3.4 of EAC ook

100 100 An LR(1) Sklton Prsr stck.push( INVALID ); stck.push(s 0 ); // initil stt word NxtWord(); loop forvr { s stck.top(); if ( ACTION[s,word] == rduc A ) thn { stck.popnum( 2* ); // pop RHS off stck s stck.top(); stck.push( A ); // push LHS, A stck.push( GOTO[s,A] ); // push nxt stt } ls if ( ACTION[s,word] == shift s i ) thn { stck.push(word); stck.push( s i ); word NxtWord(); } ls if ( ACTION[s,word] == ccpt & word == EOF) thn rk; ls throw syntx rror; } rport succss; Th Sklton LR(1) Prsr Follows th sic shift-rduc schm from prvious slids Rlis on stck & scnnr Stcks <symol,stt> pirs ACTION tl ncods th shift, rduc, ccpt, rror dcision GOTO thrds rduc ctions togthr to form potntil sntncs in L(G) Shifts words tims Rducs drivtion tims Accpts t most onc Dtcts rrors t th rlist possil point

101 101 LR(1) Tls for th Prnthsis Grmmr ACTION GOTO Stt ( ) EOF s 0 s 3 s 1 s 3 cc s 2 r 3 r 3 s 3 s 7 s 8 s 4 r 2 r 2 s 5 s 7 s 10 s 6 r 3 r 3 s 7 s 7 s 12 s 8 r 5 r 5 s 9 r 2 r 2 s 10 r 4 r 4 Stt List Pir s s 1 4 s 2 s s 4 s 5 9 s 6 s s 8 s 9 s 10 1 Gol List 2 List List Pir 3 Pir 4 Pir ( List ) 5 ( ) Action Tl Entris Entry Mning s 3 shift & go to stt 3 r 2 rduc y prod n 2 s 11 s 7 s 13 s 11 9 cc ccpt sntnc s 12 r 5 r 5 s 12 syntx rror s 13 r 4 r 4 s 13

102 102 Th Prnthsis Lngug: Prsing ( ) -Trc of th Sklton Prsr s Action on ( ) Stt Lookhd Stck Hndl Action ( $ 0 non 0 ( $ 0 non shift 3 3 ) $ 0 ( 3 non shift 8 8 EOF $ 0 ( 3 ) 8 Pir ( ) rduc 5 2 EOF $ 0 Pir 2 List Pir rduc 3 1 EOF $ 0 List 1 Gol List ccpt

103 103 Summry LR(1) Prsrs - Th prsr gnrtor uilds modl of ll possil stts of th prsr - If, in ch stt, th shift / rduc / ccpt dcision cn md with just th lft contxt nd 1 word of lookhd, th grmmr is n LR(1) grmmr. Thr r mny grmmrs tht r not LR(1) grmmrs Thr r LR(1) grmmrs for ll dtrministiclly prsl lngugs - W will look t th kinds of prolms tht mk grmmr non-lr(1) Proprtis w wnt in grmmr - Grmmr must unmiguous - Grmmr must nforc dsird structur or mning For xmpl, prcdnc in rithmtic xprssions * 3 = 7, not 9 - Grmmr must hv tht odd 1 word lookhd proprty - Som proprtis r lngug dsign, som r grmmr dsign

104 104 Rding Enginring A Compilr (EAC) Chptr 2.4 (Gnrtion of utomton nd th cod) Chptr (Thompson s Construction) Chptr (Sust Construction) Chptr nd Chptr (DFA Minimiztion) Chptr 2.5 (Implmnt n Automton) Chptr (Us of CFG for PL syntx) Chptr 3.3 (Top-down prsrs) Chptr 3.4 (Bottom-up prsrs)

FSA. CmSc 365 Theory of Computation. Finite State Automata and Regular Expressions (Chapter 2, Section 2.3) ALPHABET operations: U, concatenation, *

FSA. CmSc 365 Theory of Computation. Finite State Automata and Regular Expressions (Chapter 2, Section 2.3) ALPHABET operations: U, concatenation, * CmSc 365 Thory of Computtion Finit Stt Automt nd Rgulr Exprssions (Chptr 2, Sction 2.3) ALPHABET oprtions: U, conctntion, * otin otin Strings Form Rgulr xprssions dscri Closd undr U, conctntion nd * (if

More information

The Course covers: Lexical Analysis Syntax Analysis Semantic Analysis Runtime environments Code Generation Code Optimization. CS 540 Spring 2013 GMU 2

The Course covers: Lexical Analysis Syntax Analysis Semantic Analysis Runtime environments Code Generation Code Optimization. CS 540 Spring 2013 GMU 2 CS 540 Spring 2013 Th Cours covrs: Lxicl Anlysis Syntx Anlysis Smntic Anlysis Runtim nvironmnts Cod Gnrtion Cod Optimiztion CS 540 Spring 2013 GMU 2 Pr-rquisit courss Strong progrmming ckground in C, C++

More information

CSE303 - Introduction to the Theory of Computing Sample Solutions for Exercises on Finite Automata

CSE303 - Introduction to the Theory of Computing Sample Solutions for Exercises on Finite Automata CSE303 - Introduction to th Thory of Computing Smpl Solutions for Exrciss on Finit Automt Exrcis 2.1.1 A dtrministic finit utomton M ccpts th mpty string (i.., L(M)) if nd only if its initil stt is finl

More information

Last time: introduced our first computational model the DFA.

Last time: introduced our first computational model the DFA. Lctur 7 Homwork #7: 2.2.1, 2.2.2, 2.2.3 (hnd in c nd d), Misc: Givn: M, NFA Prov: (q,xy) * (p,y) iff (q,x) * (p,) (follow proof don in clss tody) Lst tim: introducd our first computtionl modl th DFA. Tody

More information

DFA (Deterministic Finite Automata) q a

DFA (Deterministic Finite Automata) q a Big pictur All lngugs Dcidl Turing mchins NP P Contxt-fr Contxt-fr grmmrs, push-down utomt Rgulr Automt, non-dtrministic utomt, rgulr xprssions DFA (Dtrministic Finit Automt) 0 q 0 0 0 0 q DFA (Dtrministic

More information

Winter 2016 COMP-250: Introduction to Computer Science. Lecture 23, April 5, 2016

Winter 2016 COMP-250: Introduction to Computer Science. Lecture 23, April 5, 2016 Wintr 2016 COMP-250: Introduction to Computr Scinc Lctur 23, April 5, 2016 Commnt out input siz 2) Writ ny lgorithm tht runs in tim Θ(n 2 log 2 n) in wors cs. Explin why this is its running tim. I don

More information

CS 6353 Compiler Construction, Homework #1. 1. Write regular expressions for the following informally described languages:

CS 6353 Compiler Construction, Homework #1. 1. Write regular expressions for the following informally described languages: CS 6353 Compilr Construction, Homwork #1 1. Writ rgular xprssions for th following informally dscribd languags: a. All strings of 0 s and 1 s with th substring 01*1. Answr: (0 1)*01*1(0 1)* b. All strings

More information

a b c cat CAT A B C Aa Bb Cc cat cat Lesson 1 (Part 1) Verbal lesson: Capital Letters Make The Same Sound Lesson 1 (Part 1) continued...

a b c cat CAT A B C Aa Bb Cc cat cat Lesson 1 (Part 1) Verbal lesson: Capital Letters Make The Same Sound Lesson 1 (Part 1) continued... Progrssiv Printing T.M. CPITLS g 4½+ Th sy, fun (n FR!) wy to tch cpitl lttrs. ook : C o - For Kinrgrtn or First Gr (not for pr-school). - Tchs tht cpitl lttrs mk th sm souns s th littl lttrs. - Tchs th

More information

Finite Automata. d: Q S Q. Finite automaton is M=(Q, S, d, q 0, F) Ex: an FA that accepts all odd-length strings of zeros: q 0 q 1. q i. q k.

Finite Automata. d: Q S Q. Finite automaton is M=(Q, S, d, q 0, F) Ex: an FA that accepts all odd-length strings of zeros: q 0 q 1. q i. q k. Finit Automt Bsic id: FA is mchin tht chngs stts whil procssing symols, on t tim. Finit st of stts: Q = {q 0, q 1, q 3,..., q k } Trnsition function: Initil stt: Finl stts: d: Q S Q q 0 Q F Q Finit utomton

More information

Notes on Finite Automata Department of Computer Science Professor Goldberg Textbooks: Introduction to the Theory of Computation by Michael Sipser

Notes on Finite Automata Department of Computer Science Professor Goldberg Textbooks: Introduction to the Theory of Computation by Michael Sipser Nots on Finit Automt Dprtmnt of Computr Scinc Profssor Goldrg Txtooks: Introduction to th Thory of Computtion y Michl Sipsr Elmnts of th Thory of Computtion y H. Lwis nd C. Ppdimitriou Ths nots contin

More information

Lecture 11 Waves in Periodic Potentials Today: Questions you should be able to address after today s lecture:

Lecture 11 Waves in Periodic Potentials Today: Questions you should be able to address after today s lecture: Lctur 11 Wvs in Priodic Potntils Tody: 1. Invrs lttic dfinition in 1D.. rphicl rprsnttion of priodic nd -priodic functions using th -xis nd invrs lttic vctors. 3. Sris solutions to th priodic potntil Hmiltonin

More information

INTEGRALS. Chapter 7. d dx. 7.1 Overview Let d dx F (x) = f (x). Then, we write f ( x)

INTEGRALS. Chapter 7. d dx. 7.1 Overview Let d dx F (x) = f (x). Then, we write f ( x) Chptr 7 INTEGRALS 7. Ovrviw 7.. Lt d d F () f (). Thn, w writ f ( ) d F () + C. Ths intgrls r clld indfinit intgrls or gnrl intgrls, C is clld constnt of intgrtion. All ths intgrls diffr y constnt. 7..

More information

Section 3: Antiderivatives of Formulas

Section 3: Antiderivatives of Formulas Chptr Th Intgrl Appli Clculus 96 Sction : Antirivtivs of Formuls Now w cn put th is of rs n ntirivtivs togthr to gt wy of vluting finit intgrls tht is ct n oftn sy. To vlut finit intgrl f(t) t, w cn fin

More information

TOPIC 5: INTEGRATION

TOPIC 5: INTEGRATION TOPIC 5: INTEGRATION. Th indfinit intgrl In mny rspcts, th oprtion of intgrtion tht w r studying hr is th invrs oprtion of drivtion. Dfinition.. Th function F is n ntidrivtiv (or primitiv) of th function

More information

CS415 Compilers. Lexical Analysis and. These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University

CS415 Compilers. Lexical Analysis and. These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University CS415 Compilers Lexicl Anlysis nd These slides re sed on slides copyrighted y Keith Cooper, Ken Kennedy & Lind Torczon t Rice University First Progrmming Project Instruction Scheduling Project hs een posted

More information

Math 61 : Discrete Structures Final Exam Instructor: Ciprian Manolescu. You have 180 minutes.

Math 61 : Discrete Structures Final Exam Instructor: Ciprian Manolescu. You have 180 minutes. Nm: UCA ID Numr: Stion lttr: th 61 : Disrt Struturs Finl Exm Instrutor: Ciprin nolsu You hv 180 minuts. No ooks, nots or lultors r llow. Do not us your own srth ppr. 1. (2 points h) Tru/Fls: Cirl th right

More information

Chapter 16. 1) is a particular point on the graph of the function. 1. y, where x y 1

Chapter 16. 1) is a particular point on the graph of the function. 1. y, where x y 1 Prctic qustions W now tht th prmtr p is dirctl rltd to th mplitud; thrfor, w cn find tht p. cos d [ sin ] sin sin Not: Evn though ou might not now how to find th prmtr in prt, it is lws dvisl to procd

More information

Minimal DFA. minimal DFA for L starting from any other

Minimal DFA. minimal DFA for L starting from any other Miniml DFA Among the mny DFAs ccepting the sme regulr lnguge L, there is exctly one (up to renming of sttes) which hs the smllest possile numer of sttes. Moreover, it is possile to otin tht miniml DFA

More information

Minimum Spanning Trees

Minimum Spanning Trees Minimum Spnning Trs Minimum Spnning Trs Problm A town hs st of houss nd st of rods A rod conncts nd only houss A rod conncting houss u nd v hs rpir cost w(u, v) Gol: Rpir nough (nd no mor) rods such tht:

More information

Integration Continued. Integration by Parts Solving Definite Integrals: Area Under a Curve Improper Integrals

Integration Continued. Integration by Parts Solving Definite Integrals: Area Under a Curve Improper Integrals Intgrtion Continud Intgrtion y Prts Solving Dinit Intgrls: Ar Undr Curv Impropr Intgrls Intgrtion y Prts Prticulrly usul whn you r trying to tk th intgrl o som unction tht is th product o n lgric prssion

More information

Designing finite automata II

Designing finite automata II Designing finite utomt II Prolem: Design DFA A such tht L(A) consists of ll strings of nd which re of length 3n, for n = 0, 1, 2, (1) Determine wht to rememer out the input string Assign stte to ech of

More information

Multi-Section Coupled Line Couplers

Multi-Section Coupled Line Couplers /0/009 MultiSction Coupld Lin Couplrs /8 Multi-Sction Coupld Lin Couplrs W cn dd multipl coupld lins in sris to incrs couplr ndwidth. Figur 7.5 (p. 6) An N-sction coupld lin l W typiclly dsign th couplr

More information

Enforcement of Opacity Security Properties Using Insertion Functions

Enforcement of Opacity Security Properties Using Insertion Functions Enforcmnt of Opcity Scurity Proprtis Using Insrtion Functions Yi-Chin Wu nd Stéphn Lfortun EECS Dprtmnt, Univrsity of Michign CDC 12 Dc. 13 th, 2012 Motivtion Scurity nd privcy concrns in onlin srvics

More information

Formal languages, automata, and theory of computation

Formal languages, automata, and theory of computation Mälrdlen University TEN1 DVA337 2015 School of Innovtion, Design nd Engineering Forml lnguges, utomt, nd theory of computtion Thursdy, Novemer 5, 14:10-18:30 Techer: Dniel Hedin, phone 021-107052 The exm

More information

COMP108 Algorithmic Foundations

COMP108 Algorithmic Foundations Grdy mthods Prudn Wong http://www.s.liv..uk/~pwong/thing/omp108/01617 Coin Chng Prolm Suppos w hv 3 typs of oins 10p 0p 50p Minimum numr of oins to mk 0.8, 1.0, 1.? Grdy mthod Lrning outoms Undrstnd wht

More information

CSC Design and Analysis of Algorithms. Example: Change-Making Problem

CSC Design and Analysis of Algorithms. Example: Change-Making Problem CSC 801- Dsign n Anlysis of Algorithms Ltur 11 Gry Thniqu Exmpl: Chng-Mking Prolm Givn unlimit mounts of oins of nomintions 1 > > m, giv hng for mount n with th lst numr of oins Exmpl: 1 = 25, 2 =10, =

More information

Ch 1.2: Solutions of Some Differential Equations

Ch 1.2: Solutions of Some Differential Equations Ch 1.2: Solutions of Som Diffrntil Equtions Rcll th fr fll nd owl/mic diffrntil qutions: v 9.8.2v, p.5 p 45 Ths qutions hv th gnrl form y' = y - b W cn us mthods of clculus to solv diffrntil qutions of

More information

AUTOMATA AND LANGUAGES. Definition 1.5: Finite Automaton

AUTOMATA AND LANGUAGES. Definition 1.5: Finite Automaton 25. Finite Automt AUTOMATA AND LANGUAGES A system of computtion tht only hs finite numer of possile sttes cn e modeled using finite utomton A finite utomton is often illustrted s stte digrm d d d. d q

More information

CS 314 Principles of Programming Languages

CS 314 Principles of Programming Languages C 314 Principles of Progrmming Lnguges Lecture 6: LL(1) Prsing Zheng (Eddy) Zhng Rutgers University Ferury 5, 2018 Clss Informtion Homework 2 due tomorrow. Homework 3 will e posted erly next week. 2 Top

More information

CISC 4090 Theory of Computation

CISC 4090 Theory of Computation 9/6/28 Stereotypicl computer CISC 49 Theory of Computtion Finite stte mchines & Regulr lnguges Professor Dniel Leeds dleeds@fordhm.edu JMH 332 Centrl processing unit (CPU) performs ll the instructions

More information

1 Introduction to Modulo 7 Arithmetic

1 Introduction to Modulo 7 Arithmetic 1 Introution to Moulo 7 Arithmti Bor w try our hn t solvin som hr Moulr KnKns, lt s tk los look t on moulr rithmti, mo 7 rithmti. You ll s in this sminr tht rithmti moulo prim is quit irnt rom th ons w

More information

Formal Concept Analysis

Formal Concept Analysis Forml Conpt Anlysis Conpt intnts s losd sts Closur Systms nd Implitions 4 Closur Systms 0.06.005 Nxt-Closur ws dvlopd y B. Gntr (984). Lt M = {,..., n}. A M is ltilly smllr thn B M, if B A if th smllst

More information

More on automata. Michael George. March 24 April 7, 2014

More on automata. Michael George. March 24 April 7, 2014 More on utomt Michel George Mrch 24 April 7, 2014 1 Automt constructions Now tht we hve forml model of mchine, it is useful to mke some generl constructions. 1.1 DFA Union / Product construction Suppose

More information

Overview HC9. Parsing: Top-Down & LL(1) Context-Free Grammars (1) Introduction. CFGs (3) Context-Free Grammars (2) Vertalerbouw HC 9: Ch.

Overview HC9. Parsing: Top-Down & LL(1) Context-Free Grammars (1) Introduction. CFGs (3) Context-Free Grammars (2) Vertalerbouw HC 9: Ch. Overview H9 Vertlerouw H 9: Prsing: op-down & LL(1) do 3 mei 2001 56 heo Ruys h. 8 - Prsing 8.1 ontext-free Grmmrs 8.2 op-down Prsing 8.3 LL(1) Grmmrs See lso [ho, Sethi & Ullmn 1986] for more thorough

More information

Garnir Polynomial and their Properties

Garnir Polynomial and their Properties Univrsity of Cliforni, Dvis Dprtmnt of Mthmtis Grnir Polynomil n thir Proprtis Author: Yu Wng Suprvisor: Prof. Gorsky Eugny My 8, 07 Grnir Polynomil n thir Proprtis Yu Wng mil: uywng@uvis.u. In this ppr,

More information

Scanner. Specifying patterns. Specifying patterns. Operations on languages. A scanner must recognize the units of syntax Some parts are easy:

Scanner. Specifying patterns. Specifying patterns. Operations on languages. A scanner must recognize the units of syntax Some parts are easy: Scnner Specifying ptterns source code tokens scnner prser IR A scnner must recognize the units of syntx Some prts re esy: errors mps chrcters into tokens the sic unit of syntx x = x + y; ecomes

More information

CIVL 8/ D Boundary Value Problems - Rectangular Elements 1/7

CIVL 8/ D Boundary Value Problems - Rectangular Elements 1/7 CIVL / -D Boundr Vlu Prolms - Rctngulr Elmnts / RECANGULAR ELEMENS - In som pplictions, it m mor dsirl to us n lmntl rprsnttion of th domin tht hs four sids, ithr rctngulr or qudriltrl in shp. Considr

More information

S i m p l i f y i n g A l g e b r a SIMPLIFYING ALGEBRA.

S i m p l i f y i n g A l g e b r a SIMPLIFYING ALGEBRA. S i m p l i y i n g A l g r SIMPLIFYING ALGEBRA www.mthltis.o.nz Simpliying SIMPLIFYING Algr ALGEBRA Algr is mthmtis with mor thn just numrs. Numrs hv ix vlu, ut lgr introus vrils whos vlus n hng. Ths

More information

Module graph.py. 1 Introduction. 2 Graph basics. 3 Module graph.py. 3.1 Objects. CS 231 Naomi Nishimura

Module graph.py. 1 Introduction. 2 Graph basics. 3 Module graph.py. 3.1 Objects. CS 231 Naomi Nishimura Moul grph.py CS 231 Nomi Nishimur 1 Introution Just lik th Python list n th Python itionry provi wys of storing, ssing, n moifying t, grph n viw s wy of storing, ssing, n moifying t. Bus Python os not

More information

Convert the NFA into DFA

Convert the NFA into DFA Convert the NF into F For ech NF we cn find F ccepting the sme lnguge. The numer of sttes of the F could e exponentil in the numer of sttes of the NF, ut in prctice this worst cse occurs rrely. lgorithm:

More information

A Simple Code Generator. Code generation Algorithm. Register and Address Descriptors. Example 3/31/2008. Code Generation

A Simple Code Generator. Code generation Algorithm. Register and Address Descriptors. Example 3/31/2008. Code Generation A Simpl Co Gnrtor Co Gnrtion Chptr 8 II Gnrt o for singl si lok How to us rgistrs? In most mhin rhitturs, som or ll of th oprnsmust in rgistrs Rgistrs mk goo tmporris Hol vlus tht r omput in on si lok

More information

Assignment 1 Automata, Languages, and Computability. 1 Finite State Automata and Regular Languages

Assignment 1 Automata, Languages, and Computability. 1 Finite State Automata and Regular Languages Deprtment of Computer Science, Austrlin Ntionl University COMP2600 Forml Methods for Softwre Engineering Semester 2, 206 Assignment Automt, Lnguges, nd Computility Smple Solutions Finite Stte Automt nd

More information

Finite Automata-cont d

Finite Automata-cont d Automt Theory nd Forml Lnguges Professor Leslie Lnder Lecture # 6 Finite Automt-cont d The Pumping Lemm WEB SITE: http://ingwe.inghmton.edu/ ~lnder/cs573.html Septemer 18, 2000 Exmple 1 Consider L = {ww

More information

CHAPTER 1 Regular Languages. Contents. definitions, examples, designing, regular operations. Non-deterministic Finite Automata (NFA)

CHAPTER 1 Regular Languages. Contents. definitions, examples, designing, regular operations. Non-deterministic Finite Automata (NFA) Finite Automt (FA or DFA) CHAPTER Regulr Lnguges Contents definitions, exmples, designing, regulr opertions Non-deterministic Finite Automt (NFA) definitions, equivlence of NFAs DFAs, closure under regulr

More information

CSE 373: AVL trees. Warmup: Warmup. Interlude: Exploring the balance invariant. AVL Trees: Invariants. AVL tree invariants review

CSE 373: AVL trees. Warmup: Warmup. Interlude: Exploring the balance invariant. AVL Trees: Invariants. AVL tree invariants review rmup CSE 7: AVL trs rmup: ht is n invrint? Mihl L Friy, Jn 9, 0 ht r th AVL tr invrints, xtly? Disuss with your nighor. AVL Trs: Invrints Intrlu: Exploring th ln invrint Cor i: xtr invrint to BSTs tht

More information

Talen en Automaten Test 1, Mon 7 th Dec, h45 17h30

Talen en Automaten Test 1, Mon 7 th Dec, h45 17h30 Tlen en Automten Test 1, Mon 7 th Dec, 2015 15h45 17h30 This test consists of four exercises over 5 pges. Explin your pproch, nd write your nswer to ech exercise on seprte pge. You cn score mximum of 100

More information

Let's start with an example:

Let's start with an example: Finite Automt Let's strt with n exmple: Here you see leled circles tht re sttes, nd leled rrows tht re trnsitions. One of the sttes is mrked "strt". One of the sttes hs doule circle; this is terminl stte

More information

# 1 ' 10 ' 100. Decimal point = 4 hundred. = 6 tens (or sixty) = 5 ones (or five) = 2 tenths. = 7 hundredths.

# 1 ' 10 ' 100. Decimal point = 4 hundred. = 6 tens (or sixty) = 5 ones (or five) = 2 tenths. = 7 hundredths. How os it work? Pl vlu o imls rprsnt prts o whol numr or ojt # 0 000 Tns o thousns # 000 # 00 Thousns Hunrs Tns Ons # 0 Diml point st iml pl: ' 0 # 0 on tnth n iml pl: ' 0 # 00 on hunrth r iml pl: ' 0

More information

1.4 Nonregular Languages

1.4 Nonregular Languages 74 1.4 Nonregulr Lnguges The number of forml lnguges over ny lphbet (= decision/recognition problems) is uncountble On the other hnd, the number of regulr expressions (= strings) is countble Hence, ll

More information

Context-Free Grammars and Languages

Context-Free Grammars and Languages Context-Free Grmmrs nd Lnguges (Bsed on Hopcroft, Motwni nd Ullmn (2007) & Cohen (1997)) Introduction Consider n exmple sentence: A smll ct ets the fish English grmmr hs rules for constructing sentences;

More information

Homework 3 Solutions

Homework 3 Solutions CS 341: Foundtions of Computer Science II Prof. Mrvin Nkym Homework 3 Solutions 1. Give NFAs with the specified numer of sttes recognizing ech of the following lnguges. In ll cses, the lphet is Σ = {,1}.

More information

, between the vertical lines x a and x b. Given a demand curve, having price as a function of quantity, p f (x) at height k is the curve f ( x,

, between the vertical lines x a and x b. Given a demand curve, having price as a function of quantity, p f (x) at height k is the curve f ( x, Clculus for Businss nd Socil Scincs - Prof D Yun Finl Em Rviw vrsion 5/9/7 Chck wbsit for ny postd typos nd updts Pls rport ny typos This rviw sht contins summris of nw topics only (This rviw sht dos hv

More information

Intermediate Math Circles Wednesday, November 14, 2018 Finite Automata II. Nickolas Rollick a b b. a b 4

Intermediate Math Circles Wednesday, November 14, 2018 Finite Automata II. Nickolas Rollick a b b. a b 4 Intermedite Mth Circles Wednesdy, Novemer 14, 2018 Finite Automt II Nickols Rollick nrollick@uwterloo.c Regulr Lnguges Lst time, we were introduced to the ide of DFA (deterministic finite utomton), one

More information

Syntax Analysis. Context-free grammar Top-down and bottom-up parsing. cs5363 1

Syntax Analysis. Context-free grammar Top-down and bottom-up parsing. cs5363 1 Syntax Analysis Contxt-fr grammar Top-down and bottom-up parsing cs5363 1 Front nd Sourc program for (w = 1; w < 100; w = w * 2); Input: a stram of charactrs f o r ( `w = 1 ; w < 1 0 0 ; w Scanning---

More information

Worked out examples Finite Automata

Worked out examples Finite Automata Worked out exmples Finite Automt Exmple Design Finite Stte Automton which reds inry string nd ccepts only those tht end with. Since we re in the topic of Non Deterministic Finite Automt (NFA), we will

More information

NFAs and Regular Expressions. NFA-ε, continued. Recall. Last class: Today: Fun:

NFAs and Regular Expressions. NFA-ε, continued. Recall. Last class: Today: Fun: CMPU 240 Lnguge Theory nd Computtion Spring 2019 NFAs nd Regulr Expressions Lst clss: Introduced nondeterministic finite utomt with -trnsitions Tody: Prove n NFA- is no more powerful thn n NFA Introduce

More information

First Midterm Examination

First Midterm Examination Çnky University Deprtment of Computer Engineering 203-204 Fll Semester First Midterm Exmintion ) Design DFA for ll strings over the lphet Σ = {,, c} in which there is no, no nd no cc. 2) Wht lnguge does

More information

UNCORRECTED SAMPLE PAGES 4-1. Naming fractions KEY IDEAS. 1 Each shape represents ONE whole. a i ii. b i ii

UNCORRECTED SAMPLE PAGES 4-1. Naming fractions KEY IDEAS. 1 Each shape represents ONE whole. a i ii. b i ii - Nming frtions Chptr Frtions Eh shp rprsnts ONE whol. i ii Wht frtion is shdd? Writ s frtion nd in words. Wht frtion is not shdd? Writ s frtion nd in words. i ii i ii Writ s mny diffrnt frtions s you

More information

QUESTIONS BEGIN HERE!

QUESTIONS BEGIN HERE! Points miss: Stunt's Nm: Totl sor: /100 points Est Tnnss Stt Univrsity Dprtmnt of Computr n Informtion Sins CSCI 710 (Trnoff) Disrt Struturs TEST for Fll Smstr, 00 R this for strtin! This tst is los ook

More information

First Midterm Examination

First Midterm Examination 24-25 Fll Semester First Midterm Exmintion ) Give the stte digrm of DFA tht recognizes the lnguge A over lphet Σ = {, } where A = {w w contins or } 2) The following DFA recognizes the lnguge B over lphet

More information

Harvard University Computer Science 121 Midterm October 23, 2012

Harvard University Computer Science 121 Midterm October 23, 2012 Hrvrd University Computer Science 121 Midterm Octoer 23, 2012 This is closed-ook exmintion. You my use ny result from lecture, Sipser, prolem sets, or section, s long s you quote it clerly. The lphet is

More information

Chapter Five: Nondeterministic Finite Automata. Formal Language, chapter 5, slide 1

Chapter Five: Nondeterministic Finite Automata. Formal Language, chapter 5, slide 1 Chpter Five: Nondeterministic Finite Automt Forml Lnguge, chpter 5, slide 1 1 A DFA hs exctly one trnsition from every stte on every symol in the lphet. By relxing this requirement we get relted ut more

More information

Java II Finite Automata I

Java II Finite Automata I Jv II Finite Automt I Bernd Kiefer Bernd.Kiefer@dfki.de Deutsches Forschungszentrum für künstliche Intelligenz Finite Automt I p.1/13 Processing Regulr Expressions We lredy lerned out Jv s regulr expression

More information

CS 361 Meeting 12 10/3/18

CS 361 Meeting 12 10/3/18 CS 36 Mting 2 /3/8 Announcmnts. Homwork 4 is du Friday. If Friday is Mountain Day, homwork should b turnd in at my offic or th dpartmnt offic bfor 4. 2. Homwork 5 will b availabl ovr th wknd. 3. Our midtrm

More information

CS 330 Formal Methods and Models

CS 330 Formal Methods and Models CS 330 Forml Methods nd Models Dn Richrds, George Mson University, Spring 2017 Quiz Solutions Quiz 1, Propositionl Logic Dte: Ferury 2 1. Prove ((( p q) q) p) is tutology () (3pts) y truth tle. p q p q

More information

Instructions for Section 1

Instructions for Section 1 Instructions for Sction 1 Choos th rspons tht is corrct for th qustion. A corrct nswr scors 1, n incorrct nswr scors 0. Mrks will not b dductd for incorrct nswrs. You should ttmpt vry qustion. No mrks

More information

Lexical Analysis Part III

Lexical Analysis Part III Lexicl Anlysis Prt III Chpter 3: Finite Automt Slides dpted from : Roert vn Engelen, Florid Stte University Alex Aiken, Stnford University Design of Lexicl Anlyzer Genertor Trnslte regulr expressions to

More information

XML and Databases. Outline. Recall: Top-Down Evaluation of Simple Paths. Recall: Top-Down Evaluation of Simple Paths. Sebastian Maneth NICTA and UNSW

XML and Databases. Outline. Recall: Top-Down Evaluation of Simple Paths. Recall: Top-Down Evaluation of Simple Paths. Sebastian Maneth NICTA and UNSW Smll Pth Quiz ML n Dtss Cn you giv n xprssion tht rturns th lst / irst ourrn o h istint pri lmnt? Ltur 8 Strming Evlution: how muh mmory o you n? Sstin Mnth NICTA n UNSW

More information

Table of contents: Lecture N Summary... 3 What does automata mean?... 3 Introduction to languages... 3 Alphabets... 3 Strings...

Table of contents: Lecture N Summary... 3 What does automata mean?... 3 Introduction to languages... 3 Alphabets... 3 Strings... Tle of contents: Lecture N0.... 3 ummry... 3 Wht does utomt men?... 3 Introduction to lnguges... 3 Alphets... 3 trings... 3 Defining Lnguges... 4 Lecture N0. 2... 7 ummry... 7 Kleene tr Closure... 7 Recursive

More information

CSE 373: More on graphs; DFS and BFS. Michael Lee Wednesday, Feb 14, 2018

CSE 373: More on graphs; DFS and BFS. Michael Lee Wednesday, Feb 14, 2018 CSE 373: Mor on grphs; DFS n BFS Mihl L Wnsy, F 14, 2018 1 Wrmup Wrmup: Disuss with your nighor: Rmin your nighor: wht is simpl grph? Suppos w hv simpl, irt grph with x nos. Wht is th mximum numr of gs

More information

On the Role of Fitness, Precision, Generalization and Simplicity in Process Discovery

On the Role of Fitness, Precision, Generalization and Simplicity in Process Discovery On th Rol of Fitnss, Prcision, Gnrliztion n Simplicity in Procss Discovry Joos Buijs Bouwijn vn Dongn Wil vn r Alst http://www.win.tu.nl/coslog/ Avncs in Procss ining ny procss iscovry n conformnc chcking

More information

Present state Next state Q + M N

Present state Next state Q + M N Qustion 1. An M-N lip-lop works s ollows: I MN=00, th nxt stt o th lip lop is 0. I MN=01, th nxt stt o th lip-lop is th sm s th prsnt stt I MN=10, th nxt stt o th lip-lop is th omplmnt o th prsnt stt I

More information

Thoery of Automata CS402

Thoery of Automata CS402 Thoery of Automt C402 Theory of Automt Tle of contents: Lecture N0. 1... 4 ummry... 4 Wht does utomt men?... 4 Introduction to lnguges... 4 Alphets... 4 trings... 4 Defining Lnguges... 5 Lecture N0. 2...

More information

Non-deterministic Finite Automata

Non-deterministic Finite Automata Non-deterministic Finite Automt Eliminting non-determinism Rdoud University Nijmegen Non-deterministic Finite Automt H. Geuvers nd T. vn Lrhoven Institute for Computing nd Informtion Sciences Intelligent

More information

Lecture 08: Feb. 08, 2019

Lecture 08: Feb. 08, 2019 4CS4-6:Theory of Computtion(Closure on Reg. Lngs., regex to NDFA, DFA to regex) Prof. K.R. Chowdhry Lecture 08: Fe. 08, 2019 : Professor of CS Disclimer: These notes hve not een sujected to the usul scrutiny

More information

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages CMSC 330: Orgniztion of Progrmming Lnguges Finite Automt 2 CMSC 330 1 Types of Finite Automt Deterministic Finite Automt (DFA) Exctly one sequence of steps for ech string All exmples so fr Nondeterministic

More information

Linear Algebra Existence of the determinant. Expansion according to a row.

Linear Algebra Existence of the determinant. Expansion according to a row. Lir Algbr 2270 1 Existc of th dtrmit. Expsio ccordig to row. W dfi th dtrmit for 1 1 mtrics s dt([]) = (1) It is sy chck tht it stisfis D1)-D3). For y othr w dfi th dtrmit s follows. Assumig th dtrmit

More information

2. Lexical Analysis. Oscar Nierstrasz

2. Lexical Analysis. Oscar Nierstrasz 2. Lexicl Anlysis Oscr Nierstrsz Thnks to Jens Plserg nd Tony Hosking for their kind permission to reuse nd dpt the CS132 nd CS502 lecture notes. http://www.cs.ucl.edu/~plserg/ http://www.cs.purdue.edu/homes/hosking/

More information

CS 461, Lecture 17. Today s Outline. Example Run

CS 461, Lecture 17. Today s Outline. Example Run Prim s Algorithm CS 461, Ltur 17 Jr Si Univrsity o Nw Mxio In Prim s lgorithm, th st A mintin y th lgorithm orms singl tr. Th tr strts rom n ritrry root vrtx n grows until it spns ll th vrtis in V At h

More information

This Week. Computer Graphics. Introduction. Introduction. Graphics Maths by Example. Graphics Maths by Example

This Week. Computer Graphics. Introduction. Introduction. Graphics Maths by Example. Graphics Maths by Example This Wk Computr Grphics Vctors nd Oprtions Vctor Arithmtic Gomtric Concpts Points, Lins nd Plns Eploiting Dot Products CSC 470 Computr Grphics 1 CSC 470 Computr Grphics 2 Introduction Introduction Wh do

More information

FABER Formal Languages, Automata and Models of Computation

FABER Formal Languages, Automata and Models of Computation DVA337 FABER Forml Lnguges, Automt nd Models of Computtion Lecture 5 chool of Innovtion, Design nd Engineering Mälrdlen University 2015 1 Recp of lecture 4 y definition suset construction DFA NFA stte

More information

1 Nondeterministic Finite Automata

1 Nondeterministic Finite Automata 1 Nondeterministic Finite Automt Suppose in life, whenever you hd choice, you could try oth possiilities nd live your life. At the end, you would go ck nd choose the one tht worked out the est. Then you

More information

CS103B Handout 18 Winter 2007 February 28, 2007 Finite Automata

CS103B Handout 18 Winter 2007 February 28, 2007 Finite Automata CS103B ndout 18 Winter 2007 Ferury 28, 2007 Finite Automt Initil text y Mggie Johnson. Introduction Severl childrens gmes fit the following description: Pieces re set up on plying ord; dice re thrown or

More information

QUESTIONS BEGIN HERE!

QUESTIONS BEGIN HERE! Points miss: Stunt's Nm: Totl sor: /100 points Est Tnnss Stt Univrsity Dprtmnt o Computr n Inormtion Sins CSCI 2710 (Trno) Disrt Struturs TEST or Sprin Smstr, 2005 R this or strtin! This tst is los ook

More information

V={A,B,C,D,E} E={ (A,D),(A,E),(B,D), (B,E),(C,D),(C,E)}

V={A,B,C,D,E} E={ (A,D),(A,E),(B,D), (B,E),(C,D),(C,E)} s s of s Computr Sin & Enginring 423/823 Dsign n Anlysis of Ltur 03 (Chptr 22) Stphn Sott (Apt from Vinohnrn N. Vriym) s of s s r strt t typs tht r pplil to numrous prolms Cn ptur ntitis, rltionships twn

More information

V={A,B,C,D,E} E={ (A,D),(A,E),(B,D), (B,E),(C,D),(C,E)}

V={A,B,C,D,E} E={ (A,D),(A,E),(B,D), (B,E),(C,D),(C,E)} Introution Computr Sin & Enginring 423/823 Dsign n Anlysis of Algorithms Ltur 03 Elmntry Grph Algorithms (Chptr 22) Stphn Sott (Apt from Vinohnrn N. Vriym) I Grphs r strt t typs tht r pplil to numrous

More information

Seven-Segment Display Driver

Seven-Segment Display Driver 7-Smnt Disply Drivr, Ron s in 7-Smnt Disply Drivr, Ron s in Prolm 62. 00 0 0 00 0000 000 00 000 0 000 00 0 00 00 0 0 0 000 00 0 00 BCD Diits in inry Dsin Drivr Loi 4 inputs, 7 outputs 7 mps, h with 6 on

More information

(a) v 1. v a. v i. v s. (b)

(a) v 1. v a. v i. v s. (b) Outlin RETIMING Struturl optimiztion mthods. Gionni D Mihli Stnford Unirsity Rtiming. { Modling. { Rtiming for minimum dly. { Rtiming for minimum r. Synhronous Logi Ntwork Synhronous Logi Ntwork Synhronous

More information

Theory of Computation Regular Languages. (NTU EE) Regular Languages Fall / 38

Theory of Computation Regular Languages. (NTU EE) Regular Languages Fall / 38 Theory of Computtion Regulr Lnguges (NTU EE) Regulr Lnguges Fll 2017 1 / 38 Schemtic of Finite Automt control 0 0 1 0 1 1 1 0 Figure: Schemtic of Finite Automt A finite utomton hs finite set of control

More information

UNTYPED LAMBDA CALCULUS (II)

UNTYPED LAMBDA CALCULUS (II) 1 UNTYPED LAMBDA CALCULUS (II) RECALL: CALL-BY-VALUE O.S. Basic rul Sarch ruls: (\x.) v [v/x] 1 1 1 1 v v CALL-BY-VALUE EVALUATION EXAMPLE (\x. x x) (\y. y) x x [\y. y / x] = (\y. y) (\y. y) y [\y. y /

More information

Using the Printable Sticker Function. Using the Edit Screen. Computer. Tablet. ScanNCutCanvas

Using the Printable Sticker Function. Using the Edit Screen. Computer. Tablet. ScanNCutCanvas SnNCutCnvs Using th Printl Stikr Funtion On-o--kin stikrs n sily rt y using your inkjt printr n th Dirt Cut untion o th SnNCut mhin. For inormtion on si oprtions o th SnNCutCnvs, rr to th Hlp. To viw th

More information

b. How many ternary words of length 23 with eight 0 s, nine 1 s and six 2 s?

b. How many ternary words of length 23 with eight 0 s, nine 1 s and six 2 s? MATH 3012 Finl Exm, My 4, 2006, WTT Stunt Nm n ID Numr 1. All our prts o this prolm r onrn with trnry strings o lngth n, i.., wors o lngth n with lttrs rom th lpht {0, 1, 2}.. How mny trnry wors o lngth

More information

Section: Other Models of Turing Machines. Definition: Two automata are equivalent if they accept the same language.

Section: Other Models of Turing Machines. Definition: Two automata are equivalent if they accept the same language. Section: Other Models of Turing Mchines Definition: Two utomt re equivlent if they ccept the sme lnguge. Turing Mchines with Sty Option Modify δ, Theorem Clss of stndrd TM s is equivlent to clss of TM

More information

CSI35 Chapter 11 Review

CSI35 Chapter 11 Review 1. Which of th grphs r trs? c f c g f c x y f z p q r 1 1. Which of th grphs r trs? c f c g f c x y f z p q r . Answr th qustions out th following tr 1) Which vrtx is th root of c th tr? ) wht is th hight

More information

The transformation to right derivation is called the canonical reduction sequence. Bottom-up analysis

The transformation to right derivation is called the canonical reduction sequence. Bottom-up analysis Bottom-up nlysis Shift-reduce prsing. Constructs the derivtion tree from ottom to top. Reduces the string to the strt symol. Produces reverse right derivtion. Exmple: G(E): 1. E E + T 2. T 3. T T * F 4.

More information

Abstract Interpretation: concrete and abstract semantics

Abstract Interpretation: concrete and abstract semantics Abstract Intrprtation: concrt and abstract smantics Concrt smantics W considr a vry tiny languag that manags arithmtic oprations on intgrs valus. Th (concrt) smantics of th languags cab b dfind by th funzcion

More information

Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2018

Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2018 Finite Automt Theory nd Forml Lnguges TMV027/DIT321 LP4 2018 Lecture 10 An Bove April 23rd 2018 Recp: Regulr Lnguges We cn convert between FA nd RE; Hence both FA nd RE ccept/generte regulr lnguges; More

More information

Converting Regular Expressions to Discrete Finite Automata: A Tutorial

Converting Regular Expressions to Discrete Finite Automata: A Tutorial Converting Regulr Expressions to Discrete Finite Automt: A Tutoril Dvid Christinsen 2013-01-03 This is tutoril on how to convert regulr expressions to nondeterministic finite utomt (NFA) nd how to convert

More information

Deterministic Finite Automata

Deterministic Finite Automata Finite Automt Deterministic Finite Automt H. Geuvers nd J. Rot Institute for Computing nd Informtion Sciences Version: fll 2016 J. Rot Version: fll 2016 Tlen en Automten 1 / 21 Outline Finite Automt Finite

More information

a b b a pop push read unread

a b b a pop push read unread A Finite Automton A Pushdown Automton 0000 000 red unred b b pop red unred push 2 An Exmple A Pushdown Automton Recll tht 0 n n not regulr. cn push symbols onto the stck cn pop them (red them bck) lter

More information