0% found this document useful (0 votes)
33 views

Programming Languages and Compilers - Notes

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
33 views

Programming Languages and Compilers - Notes

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 37
gu *04-A022, Subject ihe Reogramrsing Languages and Compilers Subject Code- CSE 4Oat Textbook- . 1: Pro meng Lan agp | wit. j 1 By chad 8 epee L. Scott eer Publecation 2 ieiads.2 ‘Srd Edition ' Course. Outttine- -O-Intzoduction +0 Compilation 1. Finite State Machéne (FSM) tant) wastqad aye 2: Lexical, Analy nin (Scanning) Cropore in 3. Syntax fas (Parent on Important) e 4 wy . Syrobol Table Ky 5: Ererzorz Recoverey ; oa 6. Semantic Analysis pe F. Intermediate Code Generation , sid oss -% Code Generation and es Ga ane = = 7 = 44. The Arct of Language Deaign: Need of Continuous Language ee © Evolution £2) Special, Purpose % C:Low Level Codzn > Prol Logical Relahonashep > rose bed Manipulation > Per or Ryton Scripting tet Personal. Preference ty) Expreessive Power. Vy Ease es use kore Novice VG Ease of emplernentation NC Standandésation Ney Open Aoutzce Tx) Excellent compilerz %, Economics, Paretionage and Inertia Language Design Criteria- > Reqularzit + Generale > Ortho > Uniforemity > Safe P Extensi bility ee 22-04-2029 (Friday) Y classification of Progreamméng Languages (Ads SU aan 4. Declarative - What computer. Ahould do Example- Functional. Languages: Lips Haskell Scheme Logic - based Languages: Proloqy Data. Flow Languages: td, val a+ Irpercative - How corpurer2 should do sForetens € Example- Van- Neumann Languages Serdiptin Languages: baths OAK. pal Object - 0 fented Languages: Amula 6F 23-04-3022 (Gaturedayy) Language Procossetcs/ Translators: Spurreco larginge af Translators Toxeget Language “sy The translator, translates the High Level Language preogram tnto toreget language quisalent Machtne Learning i ainguage) > Tk provides veniie nyeshagess whenever. the preogreannmer violates the Apeciheation ob High Level Language: Types Ok Tearslarors: sTpore ate three types ok Translators + 4. Complete . Tt translates a. ourece code pro written fo Hegp level Language to equivalent forget language: = iil ag Corsprlarion coral ler P “2 Trfonpretere- + Tt translates a. fOUtee code prLo7 at +p Machéne Leael Languog’s fist} oe ‘Trtereprretotion J ean 3. Asser) saorn ler - Th i 0. special, type a eariat ibe wil +ake only oa erence i aa rare, wariten @n Hégh Le sep by ‘Kep- { , * Trt | eqyereence behean Compiler and Interpreter: pn Fe | we brat Cornpilerz Tntercpreetere : " es | H taxes entire {It takes 0 ae Be J Tnpub progam oF atime. |eode ot aint: Tt produces sae Tt doesn’t preoduce | Oukput -dzate object code- intermediate nga i [object code. aa _ ies | E Compzlation is dene | Compilation and —_| oH before execution. 1 eon ane done Cae a hirmuttaneouny- ; kt is cormparzaltvely | tt ts Ts cormpariatvely | Speed | — kadter- inlower- | Mennorz Frement| Memora ‘owent Mernory-. its aoa ea +o at lens ats a Requircersert, creation of object | wedéate pt t oe laree produces: Display erercowes Dinplouy ercrotzs Ok Errore mmpflation acl one by” all at a He. lone. Bae. (eae Detection fs | Errore Detection is Det i cormparttvely corey anitvely bachest saan deficit. eater. Pertaining | Certs CH, = Python, Perl, PHP, | Rrogrearnnng typescript, ee: Rubyyetes 9 Languages ot Aguas rn nee eee sf Nftxing Comeflason and Interpretation : AoUTRCe prwogrzarn CIAVA Frograrr) CGormpiler /Treanslatere) Gye code) Fienonaéat Freograrn Input Troplemerting Compilation: Pro- Processing Gcannéng) - -yTt rernoyes white Apacer and comments- “Tt groups the characters into tokens: > Tt expends abbreviations: + Ht Identifies high-level ayntactic strcichumes (loops a Addihonal Features, =Macro preocessf > File Incluafory’ : > Rational Fre-Processor + Language Extension > Routines and Linkin P : >The cornpilerz uses a er poyan and a snebithe from kebrarey. forcten Program Compiler nits Mochzne Language Lenker) (Library Routines) Machene language Freogearn Fort Compilation Apsernbly Output: source Language mor rpg king Machene Longuag’ ~ Ir helps aa a and ophimizing: ‘ ve C Processor: he Kounce Lace C Pre Processor, Modiffed source proogmarn +g Tooke ae Boom Source we Source Translahon : Pe “Boot AreaPl eng: ary 4 Selk- Homtt "4 Cornptlerts. ieee Salk; Hoating Compileres ame Written fin the AaMNe language they éntend to > The cormpile- |» Exarnple- © compilerz Boo’ Extreapping = yuideculous the ntentionalt STR a tern derived from € ground. by pulléng on noton log letting oneselk Kf +he ones bootstraps - Comptlercs: Jose - In Terme, (IIT) 2 Turk tr-tine (AID) compilation | Lton or Tuuan-tirne corepilattons) % a Way of executive | computerz code that Ton execution | OKO program _ot tun tHime- (Q\so aynamte drzanslat- cles compilation durzin: yather than bekore execution: / 26-0L}-2022 vec (Tuesday) DEcrocode (Firm are) + YThe interpreter te artitien tr low-level enaizuctons Called miterrocodie. Giternriore) sntch i Komied tp Tread Only memory and executed by the handwiane- diate ‘orem, engin. troche oH Compelere > The structure of cornpelere design tp denided ato a bray Categoreles:- Dexign - f | iy Analisis (Front enc) Machine Independent ; Language Mt, fi Synthesis (Back. encl)+Machine Dependent» Language Tring Front end at Lisi Analysis | Ne 2 e Agurece code Phakee Ob Compélatten- Token Cae Atreearm Br ea Farne Rens fav tree - 1 ae hag Abstract Syntax - Tiree or oth ‘ Enteramediate forum —_ Modified interes — Target ——— Modified Target Code 1 1 igh tnizpamedéate Code, ' Bock end Scanner Cexical Analysts) Smee hays) IMachéne Inde lent Target Code Gene reason Coci Biscee 5 Wo cee re lee onl, Semantic Aral ath Oe lintermtedeate Bie Gener r Symbol Table Example- position = tnétfal trate ¥ 60 (Al arze footing vantobest S259 Sten rag] acta | c «tea AswrbitA eae : oe Bae ‘ Xda) K+) oe ik cay reo ton oBribite donutanftitenal < ee Paras ys Aryl) \Intenwedtate code Generation E ; In Inferavediate form, ao try S-Aderess ti tnt tO pleat (60) al +3 Adcheons Cocle, left kozfdgXti | : re: ate Aide muat contain 1 .: I A Operzand) and right side t3= Ido + ta Unt contain 2 fdp=.t3 . Code Optimizaton _. b= Ids ¥60-0 td = ida tO ce Geomatin’ a eens (MULF-Hultiplication| LDF Ra.» tds LDF- Load. ‘Ra, Ras He MULF Ra, R20 #60 / Target Lan @ ADDF-Ada CDF Nata alae ll & x Qe STF-Stone ADDF RisRiy Rae. a Bd Si cipky D 3 2F-O4. » (Fata a) m Deerence betreery Phase and Pass: Phase Puss > Compilatvon fsdone — |-»Varctous phases cee | En Varztous No. of Ateps- logicatiy grouped rec ano 3 Oe te hanes of compilation > The process Of cormpila- lenient Analysts, Syntax | —Hon can be carried Aalyatss Semantic 4) out th a angle pass | Ophmizatien , Target code Generation, Machcne ere - =cattan code optimiza ¥ Accorzdéng to the nurnberz passes s.dormpilercs ame of 4Wo types 1 One- Pass Cornpelerz {i Mulis- Pass Cormpilerz one Fos Compilerz - +A one-pass cormpilere isa compilere that passes through the pounce code of each compilation unit only once: Tk ts albo Known ap Naterzon Cornpilercs- Target Code Generacer bi rpecific code 4 Ol w mizahion Sia Mult-Pass, Compelere- . + my >A roultpars Comnpeletz 15 O ‘ype the poureee code or2 abstract Y peverzal twes- : ok corapzlete +hat preoceny tax tee Of prrograny Sai Pans "4 Semasnbie ean Seo ode Generator veal Apecific Code a ee [ Machine Code 2 Ophnktin ¢ woe rts — Vv Chatter 2: Lexical Anabysi gr WAN DAD RAAADRARAMA RARAAAATAAAA Tt th the firent phase of cormpelahon: Lexical Toke 24 sourte J) Anabuizere a Syntare | as program ee | Anodyzere > fa | | getNext Tokenc 2 Pe ee . | Syrrbat Table > Scanning is a Afmnple process that dont require toKenization of the input: S Proconser leKe Aeleton, o cormments, and compaction ok conpeciitive! waite apace, choractere into one are done” t' ™ Within this scanning. > Th Lexical Apallyris fokens are produced from the ourpur oly canner: : ; 50-04-2022 Sosureday Loxeme, Potterun, and Token: _Lexcime- > Lexornes are O Mreany ok characters Which can be Grouped ce based on a specific patteren- Ration: 2 ; ; Patterns are the descriptions that Lexeme can mepresent om can take. t (vor tmp ¥6) . VVUVVY Ve | Reece Ae ; Rawdo d wn | ne e 7tn Barn | avec ; or tc t+ nqt | NY caer ae et | dakt% exo | Aca) er eee. | onze ea | ty on a ae e — ye 0 TW Td + From Lexweme, we construct tokens — Token ih a tuple of 2 elements, Where the and element is optional Ctoken_name’, atietbute-value> -> Optional = = noctune Aymibolic. rep Tm epreacirtaayon Thee ee Ka xpecific lexeme addres of the tokens. Lexcewme token poster, Ka Keyword a $- : > When the Lexical Analyzer encounters a lexennnne sft _— generates a token name, and bls UP Of the mbot - fable with attribute name, type, ete. —— | The aitreibute value of a token fry the xymbot dolble: Fino the Lexemes and Tokens ode. r int geal into, int BS) : Uantie (a@=b) - a te (Arb) 040-5 ; else b=b-; z ‘ return OL; t Hns- Lenemer = 32 and Tokens= 3. WMI pot’ 40 the entra " | Lexemeh Token Pattern for, I while Keyword Roewsitt-; Wen LAYS NUM MO] Idenrfrers —lethert Ort — Clloniedd b oe me em Ok le onyaiqit (ie Sule \a* | 254 B16, OUT nun bere A numenic- conmant — kault” Wrerra Anything bernieen “” cue Ey pun tae Specfal Chonactetz | Example- POLOT = 20-68 > Identifrerz po 101 = 20268 “Tp keynored - Example- korn the pollonting a ‘Th y ae 05-05-2099 dnput Butering : Thurs Th input butjertings to avofa the Mon input from the isk the bukkers ane used: The lexical analypere uses the look: ahead fron those Input buygens to determine the tokens based Upon the Patterens watch: ~Bawicatty, a double bugyring ANOId the ‘over: heads axsoctat access. [] fe |= [rahe clx |x [a]é | ; Exponential 4 Ba ) Fem= mxcxec® Begin Lexeme The input buffering Acheme uses & pointers - O) Begin fr Begin Lexeme) by Forward technique fs Used to ed with the Memon Bukbert ends z W a Ino m, >The siriing of characters between the. two patintercs ib nrecognizedt on current lexeme- ahead und! a pattern match lexeme 15 identify, the frward | characterz on tts ruight: of the current > Porinand pointer scans found and whenevere a __ poimterz is set to the next ~ Begin pointerz wiorekhs the begining lexeme- Sorrel The Sentinels are used to ophimize de Mtnten of { oe ten kor the End ok the bile: >This a xpeciat chanactere that cannot occur tha pro (basically EOF) ured O4 Sentinel: “ 06:05: 2022 aw . t Frias Speciffeation af ToKens + md +Reguian Dekinitions ame she nequian expression W specification ok tokens Sireing and banguagen: Syenbot ST ts an alontreact entity which ts nat dekined- tho mechanisms based on hich ame very popular kore Fxample- \etteres 5 digits etc. Alphabet - >Fintte set of mgrobols T= [Olt Strzingp- > Finite Aequence ok mgroibols drann from an alphabet: Example “Hé”» “oloys : > Denoted bye: Length ok the Airting \w\- Po i +h ning o length 0= Null Atraing = ae ‘ Sn het Ok Airings igrorn forre. alphabets: B : EEE ——————— Er > tring te called Emp, language} eieaelel ie with no S" ( (@). } Exar le- Ly = $00, 0151051) ' ? [Fen R= $0, 10MOF L3= $051,00511,010; 101 ++ U } | Tnfinite = $.0300510 1000; 0105100510) «+f ) Nartioun Operrations 09 Steing: : Requlare a Context-freee context- Recutatee : Lanquage ea Sennsdve - Enuumerable Language Language “Type 3 Type S Types Typeo nop Us 2 fabio" n20F i 5 Linear Bounded cae futonnacta- Macite : La= Sab" | m9 OF L2=farb Automata Rurh- Down - Regulan Expressions: Regular Eepriessions ane Tt ih uned {eT descnibé used ‘p.apecity the tokens: all the ait that can be e opercatyorhs fuunfon , concate- built from the pation MO) appiced spthe Ngenbols of the sore alphabets: \ . - > yanious Tules to describe , the > Trere are baxically nequian exepreaion® oyere alphobet Aets overe niga: Rule?! oe a it eXPreANTON- % {a L@)=4 Rule-a? 6% O ‘ ] Rule-a* € th a TE errs expnesnion - 4 whenesL(6)= 16%: whene ‘ath a regroboal of | Rule-3: Fort each ‘EZ, ‘a Requlay. Expremon, Alphabet et 2s ‘a Lo) = §at- mequior expressions denoting Rule-4: If r and 4 are S mreppactnely the languages R a Concatenadoo > ma oft wh Ts a TLEQU operration 1 Known OF d tis also O. THe lon expreesNion L(ms)= R:S> The concatenation Of TWO Tee alarz exprzenicson an quia express ‘ cao] Union :- srs ts an ee expression» L (+8) = RUS: The | aon th known ah Uninn: Of TWO reequiont exxprtenoon and tt th also Regular encpreenion tity (een Closure: - n% th a Regquian expreNson » lanquage ts cloned under Kleen biel aoe L(n®) =R PA The meg lor Stan operation): : -3) = OF ty Powitive Closune= Lcdstaks apiisd (nt)=R'= BRE sit tha mequiare Expression yt | ee pals ad : | gat bab gape (30101 | bs eek Mat oo hccacmnl adk - OF-05-202a3 ee Precedencos Ok Operzatores {fn Requiare Evpresmon « Kleen Closune Concatenatior) » Union x 5, ; to1yU Examples- Ly= {Set ok all Mrciing & Os and ts ith atleark 2 conrery. -Ave Zemoes) = (041) co(oHt), | = {00; 000; 001, 100)='- =? 7 La= §we $017" Kuch that wi has 2 07 3 OccuTeNncerychad And the firext and second. Of which are not connecutive + = O*10%o10* (10% +€). L3- ={Set of all ning Of Os and 1s ening + With out - = (o+1)*on ; Ly = Sifelse; then, dor hele? — : 2 iftelke+ then # dot whele. : ie {atrcings Maxcting with @ Of Zero on more 2 b's followed a‘c’ io aye : \ wilted Erne i > Any lan aq e that can be defrred ie hurt th called ae. Lan “4s re. pat set of oe present in language Hicaets a — teegulare fet Z # Res Gor. Dekinction : See > Regular Definitions Exprrenion and ho! ane ured to give nares to the Regular Ke Names ane Used as thefrz Ayrnbols >A neguiatr defini form di) dana fory ‘TA A AEQMENCE ok definition ok the On > Tn' ch dé th O distinct name. for) ovetZ TU fdvd2 Where, 20 each rz ha requir expres Ato dei? Queahon- : Write a nequian definetvon kort the — identifiers Whech ane Mrdingh oK letters » aegis and —: Ba \ettercs 7 O|\Cl- ar - hes digit > Ol! jajalals sfelt|el9 zd ete (eters + 40g) 5 yab- pan AyBo* ot Oe Quemion: note a unsigned . lar Exprediion toating point) winich ane the _btreings eee \89E- fie : Write dewn the Re nurnbere Gnteget ort Kuch Ob- (5ag0 ,0-01284 5 Aras a eget ay eprint EC anes number =cigits- optyonal. - al Hr gna RO Extenions ok Requian Fx prresnion: oy : L.One ore Mone Tnxtance - | >The unan pomt fx operator, t+) mepresents the por, Clomune ok ‘Oo. neguian expreension and cts sa uage. - hetfknhwsa mequian ex preesinon then 1’ denote, (L(n)). Srtennter't. 2% Zeno or One Inatance- | | >The unary porthx operator, “2” means 0 or 1 Occuns de, 2? % equivalent. to (Elr2). ney L(n2) = LIDIé 3. Chanacter. Classes - >Arequian expromion ailaz|-+-|an vihene Ae ane the Ambo ok the alphabet can be replaced by @ Khon © hand Qz [0102 +++ An}. “3 >More importantly When 4 42 «-.- An forrrm a logical Kequence (fort exarople>, consecutive uppercare | lawerccay digits) 1 we can Teplace them by [a:— Qn] - | for, efamnple- [a bc] Atonds for. abl and (a-7] fs the Ahorethand kor alblc]---|z- New, lettere —> [A-Za-z-] , digit —> [0-9] id= letere — (letters - [digit . ; Tom aigit 10-4] ‘ digits > digit” a tacsaa ‘ number —> digits ( digits)? (EG-<12dmis)? Recogntsatton oh TeKenh Bernt > ff exprt then Att al lif expre then nem elne Atent ' d <: sia tee ee “9 expr > term nelop Torr od \tern ee a term > Td op |numbere “ a a dégit > {o-9] ie aigrts > digit". wad “numberz > dtgitscaligits) 2(€H-1? digits) Ege letter; > [A-Za-z-] ; ‘s td = leter- (leHere- [aligit)” ban aero ~ 10°05-2029 “4 Tuesda Recognixat Yon of ToKens = 4 Tranistion Diagrarn fore Relational, Operator: _Oreore Not equol > ¢ netuny) S Te BCLE) Lexb than eqpiod to {U— otherz es reesturin RELOP (LT) Leas than Sy ecinnt 3S RELOP (ER) equal to wa) @) FELSPLEE) qreeaten than equal ty E | return RELOP(GT) ee ore Traixttion Déagram kor deniers: Zq = letter - (leter- |aigit) ae Aigtt; - soit omen sey! retunn (qetToKenO s wtatdG) Trainston Diagnarn for Nurpbera ii) 5” dagits > ali ate )2 EL] 2 Aeqits) 2 number = aigits Caegits sotens kore a calculator Language : -: 12-05-2029 Th was (pace + tab + newléne) Bees cornment > Lparen > ce rpanen >) plus >+ minus > ~ Her > *& ania Sis , % eq > (letter ae a num > digit-dégit™ stab, |aitgi Gaigit|aigit) dQ” a. ei 13-05-2022. Finite Automata: fine > Finite Automata ane mainig of tno types ~Determintatte Finite Automata (DFA) * Non- Deleremintntic. Finite Automata. (NFA/NDFA) Detétumininbye Finite Automata : A DFA ts a. S-tuple (8,26,405F) Whene, Qk a finite Mote called the Mates. Tir afinite Atate called the alphabet: SAX ZA ththe transition purcten- A & & is the Kromet ware - F CA th the Act ok accept Mater?” | % contaéns no € transition: a) exactly one enitiol Mate. Zéz) contains multiple number Ok Binal Mates . aya Micing ‘w" gets accepted by a DFA by ae mingle 6 pe path tines fatbal Atate 2 the final Nos- Deteremintatic Finite Automata : SANFA th a S-tuple (As) 6) 4oF) where, @ is. finite stote called the Mater, = ts a finite state called the alphabet 6 O% Te —> PUB) fh the trannition funct¥on is go ts the Mart dote ECA th the notch accept mates eo a S contains € tranmbon. ‘nt. to dzfferent mates fpllovitng the the zy A ntate can Hanns, ame input AYmMbdL- 2B a Nin ‘wi gots accepted b ynulple poctins bron initial Mate a. NFA by folloning 4p final prate- ’ Zyp contains 1 tnival state: 4, wulhple Binal Mattes. Ny contain’ _ ar Expresnion and NFA: Equrétence of Regu! 4. RE.= 0 wh 2 =3@ g. RE-= & ©, * ) 3. RE=$ 6 O- Letty ard Te ome. dno trequlare expresxions and 1% 15 represented by NFA N, and 172 ib nepnesented by a NFA Na: 4. RE: = S.RE.= Ttta or Tra fo ae OR : ola 2) 16: RE-=TF em = Oredlere Of a E bracket a-% 2-Concatenahion —— Geuunien nen @) ccna a > atund Ol ind the FA lpr the follonsfng megulan expremion A wx@b)” (olble). a: > Cry b: = -a) ~ 4 ANY f Byte rata, NPA Yor es a a ap the expriemvion d* (dla) a die «p:aO—-0 a é NE <€ to DFA: €-closure- |e The tare: Mate of the i | hee oa DFA th the €-clomune ob depea: Find the Modes thot can be freow proent Atose fh each enput OG. ae 6 (Ra) = €-clowune (U6 (%2)) d Brample a | er i { 7 gst.) (G6,10))1)33 E 14-05-2022 Aimnizoton ok DFA: Tuesday sep |: 4 Divide the notes o the DFA into two equivalence clawed f-2; nal Atodcs and nen: final ntateh. | : ee Chee OA ale Repeatedly earch kor an equivalence class {x2 and a a mibol. fa’ Nuch that when qinen “Q? ah inputs the woke $x? wake Aranistion to ptates K>1 equivalence dows. m | Ee A LL Step-3 : a nuch a Way that all Then partition ‘y? thto K classes in to a member 4 tates in a ee nen class Would move the same dd clan on tnput ‘a’. eae When We ane unable to find done. then the DFA i4 minimized: 78 (rion- Firat) | a clams paretynon: We ane. A <6 (Non- Final) da B (on Frat). > D (Final) Ay E (Final) pera —t> (Frat). Exarnple- iis. Minimize the follonsing DFA: nner 2 (NF) \ << a) 5 thr) = 2 Sa 2 (NF) > 6 (NF) cde 5 (NF) 5 (NF) set 3(F) ° se (NF). Some ennonr ane detected through Lexical Analysts ANA toy, cannot be detected: : J STF the xicing ‘fis encounterrad tn a ¢ prograrn lereay Onolyser cannot tell wna “A atti ok Keyword “2h” or an undéclaned kunction ident fer. yen Gd and some SThe lexical analyser return the ® the exrcore- Other phase of cormpilatyorn to handle SWhen a lexical anabyserr fails ty detect an @ICIZOTes then thot care home Tecovery nireaegy ta ubed: Papié Mode ok recovery : ; +The ximpleat Tecovery rircateqy % the panic mode ck recovery: > Tr 4his Mizategu, We delete MALCCEMANE characters remaining enput Until the lexical analyser binds a well forawed token. (rom the 19-05-2022 Thursday (Gubmihution Class) * 20-05-2022 gl Errore: : ae > | | j Friday . (Gabmitutton Claas) Irn the Grarnenare grrecntely connplicates the panaere: >SHence Ih some Progranninin languages pragras ane ao | at centatn well Gonbed places. > Frageas Wat aenve ag aAfrrechves turn varelous Kiel ok reunhine checks on ott off (Crarnple- pointers ore nubaereipt checking) Sh dura cerctain ophnizaton on| bb: Ted enable ofr Afaable the perroremance progileng- SPragman that rerve an hints provide 4he cornpilere with Enformation about the source Program that may allow tt todo a bettere job- 2) Voniable ty? te very heavily used}: (x may be Kept fh a tater) . fi) Gubnoute fh “f? Th a pure function: Tt only abkects Hhe reat of the programm by the value tt returns: iit) Gubroute TH &? t not RecuNnANe- fy) 3a bits Auphie fore Kloating point varzionle 2

You might also like