Peterson and Bruce S. Davie, “Computer Networks: A Systems Approach”, Fourth

download Peterson and Bruce S. Davie, “Computer Networks: A Systems Approach”, Fourth

of 48

Transcript of Peterson and Bruce S. Davie, “Computer Networks: A Systems Approach”, Fourth

  • 8/10/2019 Peterson and Bruce S. Davie, Computer Networks: A Systems Approach, Fourth

    1/48

    OS and CD Lab Manual

    Objective:

    To provide an understanding of the design aspects of operating system.

    To provide an efficient understanding of the language translation peculiarities by designing acomplete translator for a mini language.

    Recommended Systems/Software Requirements:

    Intel based desktop PC with minimum of 166 M! or faster processor with at least 6" M#$%M and 1&& M# free disk space

    Turbo C or TC' complier in (indows )P or *inu+ ,perating -ystem.

    Developed By:

    K. Ravi Chythanya

    Table of Contents

  • 8/10/2019 Peterson and Bruce S. Davie, Computer Networks: A Systems Approach, Fourth

    2/48

    S !o "ro#ram$s !ame "a#e !o

    "art %1 -imulate the following CP/ -cheduling %lgorithms

    a 0C0-b -0c Priority

    d $ound $obin

    "62

    1&3 -imulate M4T and M0T 13' -imulate #ankers algorithm for 5eadlock %voidance 16" -imulate #ankers %lgorithm for deadlock Prevention 17 -imulate all Page $eplacement %lgorithms

    a 0I0,b *$/c ,ptimal

    333"36

    6 -imulate Paging Techni8ue of Memory Management 32

    "art &9 5esign a le+ical analy:er for given language .the le+ical analy:er

    should ignore redundant spaces; tabs and new lines.'1

    2 Implement the le+ical analy:er using *e+; fle+ or other le+icalanaly:er generating tools.

    ''

    5esign predictive parser for the given language '61& 5esign a *%*$ bottom up parser for the given language "111 Convert the #

  • 8/10/2019 Peterson and Bruce S. Davie, Computer Networks: A Systems Approach, Fourth

    3/48

    "%RT %

    '( Simulate t)e followin# C"* sc)edulin# al#orit)msa 0C0-b -0c Priorityd $ound $obin

    a( +C+S:%,M: % -ro#ram to simulate t)e +C+S C"* sc)edulin# al#orit)m

    "RO.R%M:

    >include?stdio.h@

  • 8/10/2019 Peterson and Bruce S. Davie, Computer Networks: A Systems Approach, Fourth

    4/48

    >include?conio.h@void mainABchar pn1&D1&DEint arr1&D;bur1&D;star1&D;finish1&D;tat1&D;wt1&D;i;nEint totwtF&;tottatF&E

    clrscrAEprintfAGHnter the number of processesGEscanfAGJdG;KnEforAiF&Ei?nEiLLBprintfAGHnter the Process

  • 8/10/2019 Peterson and Bruce S. Davie, Computer Networks: A Systems Approach, Fourth

    5/48

    Hnter the Process include?stdio.h@>include?conio.h@>include?string.h@void mainABint et3&D;at1&D;n;i;Q;temp;st1&D;ft1&D;wt1&D;ta1&DEint totwtF&;tottaF&E

    float awt;ataEchar pn1&D1&D;t1&DEclrscrAEprintfAGHnter the number of processGEscanfAGJdG;KnEforAiF&Ei?nEiLLBprintfAGHnter process name; arrival time K e+ecution timeGEflushallAEscanfAGJsJdJdG;pniD;KatiD;KetiDEforAiF&Ei?nEiLLforAQF&EQ?nEQLLB

  • 8/10/2019 Peterson and Bruce S. Davie, Computer Networks: A Systems Approach, Fourth

    6/48

    ifAetiD?etQDBtempFatiDEatiDFatQDEatQDFtempEtempFetiDE

    etiDFetQDEetQDFtempEstrcpyAt;pniDEstrcpyApniD;pnQDEstrcpyApnQD;tEforAiF&Ei?nEiLLBifAiFF&stiDFatiDE

    elsestiDFftiN1DEwtiDFstiDNatiDEftiDFstiDLetiDEtaiDFftiDNatiDEtotwtLFwtiDEtottaLFtaiDEawtFAfloattotwtnEataFAfloattottanEprintfAGOnPnameOtarrivaltimeOte+ecutiontimeOtwaitingtimeOttatimeGEforAiF&Ei?nEiLLprintfAGOnJsOtJ7dOtOtJ7dOtOtJ7dOtOtJ7dG;pniD;atiD;etiD;wtiD;taiDEprintfAGOn%verage waiting time isJfG;awtEprintfAGOn%verage turnaroundtime isJfG;ataEgetchAE

    O*T"*T:

    ,n-ut:

    Hnter the number of processes 'Hnter the Process

  • 8/10/2019 Peterson and Bruce S. Davie, Computer Networks: A Systems Approach, Fourth

    7/48

    %verage (aiting Time 6.6669%verage Turn %round Time 19.''''

    c( "riority:

    %,M:% -ro#ram to simulate t)e -riority C"* sc)edulin# al#orit)m

    "RO.R%M:

    >include?stdio.h@>include?conio.h@>include?string.h@void mainABint et3&D;at1&D;n;i;Q;temp;p1&D;st1&D;ft1&D;wt1&D;ta1&DEint totwtF&;tottaF&Efloat awt;ataEchar pn1&D1&D;t1&DEclrscrAEprintfAGHnter the number of processGEscanfAGJdG;KnEforAiF&Ei?nEiLLBprintfAGHnter process name;arrivaltime;e+ecution time K priorityGEflushallAEscanfAGJsJdJdJdG;pniD;KatiD;KetiD;KpiDE

    forAiF&Ei?nEiLLforAQF&EQ?nEQLLBifApiD?pQDBtempFpiDEpiDFpQDEpQDFtempEtempFatiDEatiDFatQDEatQDFtempE

    tempFetiDEetiDFetQDEetQDFtempE

  • 8/10/2019 Peterson and Bruce S. Davie, Computer Networks: A Systems Approach, Fourth

    8/48

    strcpyAt;pniDEstrcpyApniD;pnQDEstrcpyApnQD;tEforAiF&Ei?nEiLL

    BifAiFF&BstiDFatiDEwtiDFstiDNatiDEftiDFstiDLetiDEtaiDFftiDNatiDEelseBstiDFftiN1DE

    wtiDFstiDNatiDEftiDFstiDLetiDEtaiDFftiDNatiDEtotwtLFwtiDEtottaLFtaiDEawtFAfloattotwtnEataFAfloattottanEprintfAGOnPnameOtarrivaltimeOte+ecutiontimeOtpriorityOtwaitingtimeOttatimeGEforAiF&Ei?nEiLLprintfAGOnJsOtJ7dOtOtJ7dOtOtJ7dOtOtJ7dOtOtJ7dG;pniD;atiD;etiD;piD;wtiD;taiDEprintfAGOn%verage waiting time isJfG;awtEprintfAGOn%verage turnaroundtime isJfG;ataEgetchAE

    O*T"*T:,n-ut:

    Hnter the number of processes '

    Hnter the Process

  • 8/10/2019 Peterson and Bruce S. Davie, Computer Networks: A Systems Approach, Fourth

    9/48

    d( Round Robin:%,M:% -ro#ram to simulate t)e Round Robin C"* sc)edulin# al#orit)m

    "RO.R%M:

    >include?stdio.h@>include?conio.h@void mainABint et'&D;ts;n;i;+F&;totF&Echar pn1&D1&DEclrscrAEprintfAGHnter the no of processesGEscanfAGJdG;KnE

    printfAGHnter the time 8uantumGEscanfAGJdG;KtsEforAiF&Ei?nEiLLBprintfAGenter process name K estimated timeGEscanfAGJs JdG;pniD;KetiDEprintfAGThe processes areGEforAiF&Ei?nEiLLprintfAGprocess Jd JsOnG;iL1;pniDEforAiF&Ei?nEiLL

    totFtotLetiDEwhileA+RFtotBforAiF&Ei?nEiLLBifAetiD@tsB+F+LtsEprintfAGOn Js N@ JdG;pniD;tsE

    etiDFetiDNtsE

    elseifAAetiD?FtsKKetiDRF&B+F+LetiDEprintfAGOn Js N@ JdG;pniD;etiDEetiDF&EprintfAGOn Total Hstimated TimeJdG;+EgetchAE

    O*T"*T:

  • 8/10/2019 Peterson and Bruce S. Davie, Computer Networks: A Systems Approach, Fourth

    10/48

    ,n-ut:

    Hnter the no of processes 3Hnter the time 8uantum '

    Hnter the process name K estimated time p1 13Hnter the process name K estimated time p3 17

    Out-ut:p1 N@ 'p3 N@ 'p1 N@ 'p3 N@ 'p1 N@ 'p3 N@ '

    p1 N@ 'p3 N@ 'p3 N@ '

    Total Hstimated Time 39

    0( Simulate t)e M1T and M+T

    M1T:%,M: % -ro#ram to simulate t)e M1T

    "RO.R%M:

    >include?stdio.h@>include?conio.h@void mainA

  • 8/10/2019 Peterson and Bruce S. Davie, Computer Networks: A Systems Approach, Fourth

    11/48

    Bint mF&;m1F&;m3F&;p;countF&;iEclrscrAEprintfAGenter the memory capacityGEscanfAGJdG;KmEprintfAGenter the no of processesGE

    scanfAGJdG;KpEforAiF&Ei?pEiLLBprintfAGOnenter memory re8 for processJd G;iL1EscanfAGJdG;Km1EcountFcountLm1EifAm1?FmBifAcountFFmBprintfAGthere is no further memory remainingGE

    elseBprintfAGthe memory allocated for processJd is Jd G;iL1;mEm3FmNm1EprintfAGOnremaining memory is JdG;m3EmFm3EelseBprintfAGmemory is not allocated for processJdG;iL1EprintfAGOne+ternal fragmentation for this process isJdG;m3EgetchAE

    O*T"*T:

  • 8/10/2019 Peterson and Bruce S. Davie, Computer Networks: A Systems Approach, Fourth

    12/48

    M+T:%,M:% "ro#ram to simulate t)e M+T

    "RO.R%M:

    >include?stdio.h@>include?conio.h@void mainABint m;p;s;p1Eint m1"D;i;f;f1F&;f3F&;fra1;fra3EclrscrAEprintfAGHnter the memory si:eGEscanfAGJdG;KmEprintfAGHnter the no of partitionsGEscanfAGJdG;KpEsFmpEprintfAGHach partn si:e isJdG;sEprintfAGOnHnter the no of processesGE

    scanfAGJdG;Kp1EforAiF&Ei?p1EiLLB

  • 8/10/2019 Peterson and Bruce S. Davie, Computer Networks: A Systems Approach, Fourth

    13/48

    printfAGOnHnter the memory re8 for processJdG;iL1EscanfAGJdG;Km1iDEifAm1iD?FsBprintfAGOnProcess is allocated in partitionJdG;iL1Efra1FsNm1iDE

    printfAGOnInternal fragmentation for process isJdG;fra1Ef1Ff1Lfra1EelseBprintfAGOnProcess not allocated in partitionJdG;iL1Efra3FsEf3Ff3Lfra3EprintfAGOnH+ternal fragmentation for partition isJdG;fra3E

    printfAGOnProcessOtmemoryOtallocatedmemoryGEforAiF&Ei?p1EiLLprintfAGOnJ7dOtJ7dOtJ7dG;iL1;s;m1iDEfFf1Lf3EprintfAGOnThe tot no of fragmentation isJdG;fEgetchAE

    O*T"*T:

  • 8/10/2019 Peterson and Bruce S. Davie, Computer Networks: A Systems Approach, Fourth

    14/48

    2( Simulate &an3ers %l#orit)m for Deadloc3 %voidance

    %,M: % -ro#ram to simulate t)e &an3ers %l#orit)m for Deadloc3 %voidance

    "RO.R%M:

    #ankers algorithm for deadlock avoidance.>include?stdio.h@>include?conio.h@void mainABint n;r;i;Q;k;p;uF&;sF&;mEint block1&D;run1&D;active1&D;newre81&DEint ma+1&D1&D;resalloc1&D1&D;resre81&D1&DEint totalloc1&D;tote+t1&D;simalloc1&DEclrscrAEprintfAGHnter the no of processesGEscanfAGJdG;KnEprintfAGHnter the no of resource classesGE

    scanfAGJdG;KrEprintfAGHnter the total e+isted resource in each classGEforAkF1Ek?FrEkLLscanfAGJdG;Ktote+tkDEprintfAGHnter the allocated resourcesGEforAiF1Ei?FnEiLLforAkF1Ek?FrEkLLscanfAGJdG;KresallocEprintfAGHnter the process making the new re8uestGEscanfAGJdG;KpEprintfAGHnter the re8uested resourceGE

    forAkF1Ek?FrEkLLscanfAGJdG;Knewre8kDEprintfAGHnter the process which are n blocked or runningGE

  • 8/10/2019 Peterson and Bruce S. Davie, Computer Networks: A Systems Approach, Fourth

    15/48

    forAiF1Ei?FnEiLLBifAiRFpBprintfAGprocess JdOnG;iL1EscanfAGJdJdG;KblockiD;KruniDE

    blockpDF&ErunpDF&EforAkF1Ek?FrEkLLB

    QF&EforAiF1Ei?FnEiLLBtotallockDFQLresallociDkDE

    QFtotallockDE

    forAiF1Ei?FnEiLLBifAblockiDFF1SSruniDFF1activeiDF1EelseactiveiDF&EforAkF1Ek?FrEkLLBresallocpDkDLFnewre8kDEtotallockDLFnewre8kDEforAkF1Ek?FrEkLLBifAtote+tkDNtotallockD?&BuF1EbreakE

    ifAuFF&BforAkF1Ek?FrEkLLsimallockDFtotallockDEforAsF1Es?FnEsLLforAiF1Ei?FnEiLLBifAactiveiDFF1B

    QF&EforAkF1Ek?FrEkLL

    BifAAtote+tkDNsimallockD?Ama+iDkDNresallociDkDB

  • 8/10/2019 Peterson and Bruce S. Davie, Computer Networks: A Systems Approach, Fourth

    16/48

    QF1EbreakEifAQFF&B

    activeiDF&EforAkF1Ek?FrEkLLsimallockDFresallociDkDEmF&EforAkF1Ek?FrEkLLresre8pDkDFnewre8kDEprintfAG5eadlock willnt occurGEelse

    BforAkF1Ek?FrEkLLBresallocpDkDFnewre8kDEtotallockDFnewre8kDEprintfAG5eadlock will occurGEgetchAE

    O*T"*T:

  • 8/10/2019 Peterson and Bruce S. Davie, Computer Networks: A Systems Approach, Fourth

    17/48

    4( Simulate &an3ers %l#orit)m for Deadloc3 "revention

    %,M: % -ro#ram to simulate &an3ers %l#orit)m for Deadloc3 "revention

    "RO.R%M:

    >include?stdio.h@>include?conio.h@void mainABint cl1&D1&D;al1&D1&D;av1&D;i;Q;k;m;n;c;ne1&D1&D;flagF&EclrscrAEprintfAGOnHnter the matri+GEscanfAGJd JdG;Km;KnEprintfAGOnHnter the claim matri+GEforAiF&Ei?mEiLL

    BforAQF&EQ?nEQLLBscanfAGJdG;KcliDQDEprintfAGOnHnter allocated matri+GEforAiF&Ei?mEiLLBforAQF&EQ?nEQLLBscanfAGJdG;KaliDQDEprintfAGOnThe need matri+GEforAiF&Ei?mEiLLBforAQF&EQ?nEQLLBneiDQDFcliDQDNaliDQDEprintfAGOtJdG;neiDQDE

    printfAGOnGEprintfAGOnHnter avaliable matri+GEforAiF&Ei?'EiLLscanfAGJdG;aviDEprintfAGClaim matri+OnGEforAiF&Ei?mEiLLBforAQF&EQ?nEQLLB

    printfAGOtJdG;cliDQDEprintfAGOnGE

  • 8/10/2019 Peterson and Bruce S. Davie, Computer Networks: A Systems Approach, Fourth

    18/48

    printfAGOn allocated matri+OnGEforAiF&Ei?mEiLLBforAQF&EQ?nEQLLB

    printfAGOtJdG;aliDQDEprintfAGOnGEprintfAG available matri+OnGEforAiF&Ei?'EiLLBprintfAGOtJdG;aviDEforAkF&Ek?mEkLLforAiF&Ei?mEiLL

    BforAQF&EQ?nEQLLBifAavQD@FneiDQDflagF1EelsebreakEifAflagFF1KK QFFnN1goto aEa ifAflagFF&BprintfAGunsafestateGEifAflagFF1BflagF&EforAiF&Ei?mEiLLBforAQF&EQ?nEiLL

    BavQDLFaliDQDEaliDQDF1EprintfAGOn safe stateGEforAiF&Ei?nEiLLprintfAGOt available matri+JdG;aviDEgetchAE

    O*T"*T:

  • 8/10/2019 Peterson and Bruce S. Davie, Computer Networks: A Systems Approach, Fourth

    19/48

    5( Simulate all "a#e Re-lacement %l#orit)msa( +,+Ob( LR*

    c( O-timal

    a( +,+O:%,M: % -ro#ram to simulate +,+O "a#e Re-lacement %l#orit)m

    "RO.R%M:

    >include?stdio.h@>include?conio.h@void mainABint a7D;b3&D;pF&;8F&;mF&;h;k;i;81F1;Q;uEchar fF0E

  • 8/10/2019 Peterson and Bruce S. Davie, Computer Networks: A Systems Approach, Fourth

    20/48

    clrscrAEprintfAGHnter numbersGEforAiF&Ei?13EiLLscanfAGJdG;KbiDEforAiF&Ei?13EiLLBifApFF&

    BifA8@F'8F&Ea8DFbiDE8LLEifA81?'B81F8EprintfAGOnJdG;biDE

    printfAGOtGEforAhF&Eh?81EhLLprintfAGJdG;ahDEifAApFF&KKA81FF'BprintfAGNN@JcG;fEmLLEpF&EforAkF&Ek?8N1EkLLBifAbiL1DFFakDpF1EprintfAGOn

  • 8/10/2019 Peterson and Bruce S. Davie, Computer Networks: A Systems Approach, Fourth

    21/48

    b( LR*:%,M: % -ro#ram to simulate LR* "a#e Re-lacement %l#orit)m

    "RO.R%M:

    >include?stdio.h@>include?conio.h@void mainABint gF&;a7D;b3&D;pF&;8F&;mF&;h;k;i;81F1;Q;uEchar fF0EclrscrAEprintfAGHnter noGEforAiF&Ei?13EiLLscanfAGJdG;KbiDE

    forAiF&Ei?13EiLLBifApFF&BifA8@F'8F&Ea8DFbiDE8LLEifA81?'B81F8EgF1E

    printfAGOnJdG;biDE

  • 8/10/2019 Peterson and Bruce S. Davie, Computer Networks: A Systems Approach, Fourth

    22/48

    printfAGOtGEforAhF&Eh?81EhLLprintfAGJdG;ahDEifAApFF&KKA81FF'KKAgRF1BprintfAGNN@JcG;fE

    mLLEpF&EgF&EifA81FF'BforAkF&Ek?8N1EkLLBifAbiL1DFFakDpF1E

    forAQF&EQ?81EQLLBuF&EkFiEwhileAk@AiN3KKAk@F&BifAbkDFFaQDuLLEkNNEifAuFF&8FQEelseBforAkF&Ek?8EkLLBifAbiL1DFFakDpF1E

    printfAGOn

  • 8/10/2019 Peterson and Bruce S. Davie, Computer Networks: A Systems Approach, Fourth

    23/48

    c( O-timal:

    %,M: % -ro#ram to simulate O-timal "a#e Re-lacement %l#orit)m

    "RO.R%M:

    >include?stdio.h@>include?conio.h@void mainABint pn13D;m'DFB&;&;&;m1'D;i;Q;kEint flag;f;pfF&;:EclrscrAEprintfAGenter pgsGEforAiF&Ei?13EiLLscanfAGJdG;KpniDE

    QF&EforAiF&Ei?'EiLLBwhileAQ?13BflagF&EforAkF&Ek?'EkLLBifAmkDFFpnQDBflagF1E

    QLLE

    iNNEifAflagFF1breakEifAflagFF&BmQDFpnQDEflagF1E

    QLLE

    ifAflagFF1breakE

  • 8/10/2019 Peterson and Bruce S. Davie, Computer Networks: A Systems Approach, Fourth

    24/48

    forAiFQEi?13EiLLBflagF&EforAQF&EQ?'EQLLB

    ifApniDFFmQDflagF1EifAflagFF&Bm1&DF&Em11DFm13DF&EforAQF&EQ?'EQLLBfF&EforAkFiL1Ek?13EkLLBifAmQDFFpnkD

    Bm1QDFkEfF1EifAfFF1breakE:FAm1&D@m11DSSAm1&D@m13DUm1&Dm13DKKAm11D@m13DUm11Dm13DEforAQF&EQ?'EQLLBifApn:DFFmQDBmQDFpniDEpfLLEprintfAGno of faultsJdG;pfE

    getchAE

    O*T"*T:

    6( Simulate "a#in# tec)nique of Memory Mana#ement

  • 8/10/2019 Peterson and Bruce S. Davie, Computer Networks: A Systems Approach, Fourth

    25/48

    %,M: % -ro#ram to simulate "a#in# tec)nique of memory mana#ement

    "RO.R%M:

    >include?stdio.h@>include?conio.h@mainABint np;ps;iEint VsaEclrscrAEprintfAGenter how many pagesOnGEscanfAGJdG;KnpEprintfAGenter the page si:e OnGEscanfAGJdG;KpsE

    saFAintVmallocA3VnpEforAiF&Ei?npEiLLB saiDFAintmallocApsE printfAGpageJdOt address JuOnG;iL1;saiDEgetchAE

    O*T"*T:

  • 8/10/2019 Peterson and Bruce S. Davie, Computer Networks: A Systems Approach, Fourth

    26/48

    "%RT &

    7( 8rite a -ro#ram to desi#n le9ical analyer%,M: % -ro#ram to desi#n Le9ical %nalyer

    "RO.R%M:

    >include?string.h@>include?ctype.h@>include?stdio.h@void keywordAchar str1&DB

    ifAstrcmpAGforG;strFF&SSstrcmpAGwhileG;strFF&SSstrcmpAGdoG;strFF&SSstrcmpAGintG;strFF&SSstrcmpAGfloatG;strFF&SSstrcmpAGcharG;strFF&SSstrcmpAGdoubleG;strFF&SSstrcmpAGstaticG;strFF&SSstrcmpAGswitchG;strFF&SS

  • 8/10/2019 Peterson and Bruce S. Davie, Computer Networks: A Systems Approach, Fourth

    27/48

    strcmpAGcaseG;strFF&printfAGOnJs is a keywordG;strEelseprintfAGOnJs is an identifierG;strEmainA

    B0I*H Vf1;Vf3;Vf'Echar c;str1&D;st11&DEint num1&&D;linenoF&;tokenvalueF&;iF&;QF&;kF&E printfAGOnHnter the c programGEVgetsAst1EVf1FfopenAGinputG;GwGEwhileAAcFgetcharARFH,0putcAc;f1EfcloseAf1Ef1FfopenAGinputG;GrGEf3FfopenAGidentifierG;GwGE

    f'FfopenAGspecialcharG;GwGEwhileAAcFgetcAf1RFH,0BifAisdigitAcBtokenvalueFcN&EcFgetcAf1EwhileAisdigitAcBtokenvalueVF1&LcN&EcFgetcAf1EnumiLLDFtokenvalueEungetcAc;f1Eelse ifAisalphaAcBputcAc;f3EcFgetcAf1EwhileAisdigitAcSSisalphaAcSScFFWSScFFXB

    putcAc;f3EcFgetcAf1EputcA ;f3EungetcAc;f1Eelse ifAcFF SScFFOtprintfAG GE

    else ifAcFFOnlinenoLLE

    elseputcAc;f'E

  • 8/10/2019 Peterson and Bruce S. Davie, Computer Networks: A Systems Approach, Fourth

    28/48

    fcloseAf3EfcloseAf'EfcloseAf1EprintfAGOnThe nos in the program areGEforAQF&EQ?iEQLLprintfAGJdG;numQDE

    printfAGOnGEf3FfopenAGidentifierG;GrGEkF&EprintfAGThe keywords and identifiersareGEwhileAAcFgetcAf3RFH,0BifAcRF strkLLDFcEelseB strkDFO&E

    keywordAstrE kF&EfcloseAf3Ef'FfopenAGspecialcharG;GrGEprintfAGOn-pecial characters areGEwhileAAcFgetcAf'RFH,0printfAGJcG;cEprintfAGOnGEfcloseAf'EprintfAGTotal no. of lines areJdG;linenoE

    O*T"*T:

    Hnter the C programaLbVc

    CtrlN5

    The noYs in the program areThe keywords and identifiers area is an identifier and terminalb is an identifier and terminalc is an identifier and terminal-pecial characters areL V

    Total no. of lines are 1V

  • 8/10/2019 Peterson and Bruce S. Davie, Computer Networks: A Systems Approach, Fourth

    29/48

    ;( 8rite a -ro#ram to im-lement t)e le9ical analyer usin# le9 tool%,M: % -ro#ram to im-lement t)e Le9ical %nalyer

    "RO.R%M:

    V program name is le+p.l VJB V program to recogni:e a c program V int C,MMH.V B printfAGOnJs is a P$HP$,CH--,$ 5I$HCTI4HG;yyte+tEint Sfloat S

    char Sdouble Swhile Sfor Sdo Sif Sbreak Scontinue Svoid Sswitch Scase S

    long Sstruct Sconst S

  • 8/10/2019 Peterson and Bruce S. Davie, Computer Networks: A Systems Approach, Fourth

    30/48

    typedef Sreturn Selse Sgoto BprintfAGOnOtJs is a ZH=(,$5G;yyte+tEGVG BC,MMH

  • 8/10/2019 Peterson and Bruce S. Davie, Computer Networks: A Systems Approach, Fourth

    31/48

    VInput

    Xvi var.c

    >include?stdio.h@mainA

    B int a;bE,utput

    Xle+ le+.lXcc le+.yy.cX.a.out var.c>include?stdio.h@ is a P$HP$,CH--,$ 5I$HCTI4H

    0/

  • 8/10/2019 Peterson and Bruce S. Davie, Computer Networks: A Systems Approach, Fourth

    32/48

  • 8/10/2019 Peterson and Bruce S. Davie, Computer Networks: A Systems Approach, Fourth

    33/48

    int lenE lenFstrlenAsE ifAlastentryL1@FM%) HrrorWMessageAG-ymbpl table is fullGE ifAlastcharLlenL1@FM%) HrrorWMessageAG*e+emes array is fullGE

    lastentryFlastentryL1E symtablelastentryD.tokenFtokE symtablelastentryD.le+ptrFKle+emeslastcharL1DE lastcharFlastcharLlenL1E strcpyAsymtablelastentryD.le+ptr;sE return lastentryEVvoid Initiali:eAB struct entry VptrE forAptrFkeywordsEptrN @tokenEptrL1

    insertAptrN@le+ptr;ptrN@tokenEVint le+erAB int tE int val;iF&E whileA1 B tFgetcharAE ifAtFF SStFFOtE else ifAtFFOn linenoFlinenoL1E else ifAisdigitAt B ungetcAt;stdinE scanfAGJdG;KtokenvalE return

  • 8/10/2019 Peterson and Bruce S. Davie, Computer Networks: A Systems Approach, Fourth

    34/48

    return symtablevalD.tokenE else ifAtFFH,0 return 5,

  • 8/10/2019 Peterson and Bruce S. Davie, Computer Networks: A Systems Approach, Fourth

    35/48

    B switchAlookahead B case V tFlookaheadE MatchAlookaheadE 0AE

    displayAt;

  • 8/10/2019 Peterson and Bruce S. Davie, Computer Networks: A Systems Approach, Fourth

    36/48

    printfAG%nd place E at the endOnGE printfAGPress CtrlN! to terminateOnGE parserAE

    O*T"*T:

    Program for recursive decent parsingHnter the e+pression %nd place E at the endPress CtrlN! to terminateaLbVcEIdentifier aIdentifier bIdentifier c%rithmetic ,perator V%rithmetic ,perator L

    3V'E

  • 8/10/2019 Peterson and Bruce S. Davie, Computer Networks: A Systems Approach, Fourth

    37/48

    double dvalE

    Jtoken ?dval@ 5I[ITJtype ?dval@ e+prJtype ?dval@ term

    Jtype ?dval@ factor

    JJ

    line e+pr On BprintfAGJgOnG;X1EEe+pr e+pr L term BXXFX1 L X' E S term E

    term term V factor BXXFX1 V X' E S factor Efactor A e+pr BXXFX3 E

    S 5I[IT E

    JJint mainAByyparseAEyyerrorAchar VsBprintfAGJsG;sE

    Out-ut:

    Xle+ parser.l

    Xyacc \d parser.yXcc le+.yy.c y.tab.c \ll \lmX.a.out3L'7.&&&&V

  • 8/10/2019 Peterson and Bruce S. Davie, Computer Networks: A Systems Approach, Fourth

    38/48

    ''( Convert t)e &!+ rules into >%CC form and write code to #enerateabstract synta9 tree%,M: % -ro#ram to Convert t)e &!+ rules into >%CC form and write code to #enerate

    abstract synta9 tree

    "RO.R%M: ?int.l@JB >includeGy.tab.hG >include?stdio.h@ >include?string.h@ int *ine

  • 8/10/2019 Peterson and Bruce S. Davie, Computer Networks: A Systems Approach, Fourth

    39/48

    return $H*,PE OtD EOn *ineinclude?string.h@>include?stdio.h@struct 8uadB char op7DE char arg11&DE char arg31&DE char result1&DE]/%5'&DE

    struct stackB int items1&&DE int topEstkEint Inde+F&;tInde+F&;-t

  • 8/10/2019 Peterson and Bruce S. Davie, Computer Networks: A Systems Approach, Fourth

    40/48

    5H-CT T=PH 4%$*I-TE

    4%$*I-T 4%$ ; 4%$*I-T S 4%$E

    %--I[

  • 8/10/2019 Peterson and Bruce S. Davie, Computer Networks: A Systems Approach, Fourth

    41/48

    tIndFpopAEIndFpopAEpushAtIndEsprintfA]/%5IndD.result;GJdG;Inde+E#*,CZB

    IndFpopAEsprintfA]/%5IndD.result;GJdG;Inde+EEC,

  • 8/10/2019 Peterson and Bruce S. Davie, Computer Networks: A Systems Approach, Fourth

    42/48

    e+itA&E yyinFfpE yyparseAE printfAGOnOnOtOt NNNNNNNNNNNNNNNNNNNNNNNNNNNNGGOnOtOt Pos ,perator %rg1 %rg3 $esultG GOnOtOt

    NNNNNNNNNNNNNNNNNNNNGE forAiF&Ei?Inde+EiLL B printfAGOnOtOt JdOt JsOt JsOt JsOtJsG;i;]/%5iD.op;]/%5iD.arg1;]/%5iD.arg3;]/%5iD.resultE printfAGOnOtOt NNNNNNNNNNNNNNNNNNNNNNNGE printfAGOnOnGE return &Evoid pushAint data

    B stk.topLLE ifAstk.topFF1&& B printfAGOn -tack overflowOnGE e+itA&E stk.itemsstk.topDFdataEint popAB int dataE ifAstk.topFFN1 B printfAGOn -tack underflowOnGE e+itA&E dataFstk.itemsstk.topNNDE return dataEvoid %dd]uadrupleAchar op7D;char arg11&D;char arg31&D;char result1&D

    B strcpyA]/%5Inde+D.op;opE strcpyA]/%5Inde+D.arg1;arg1E strcpyA]/%5Inde+D.arg3;arg3E sprintfA]/%5Inde+D.result;GtJdG;tInde+LLE strcpyAresult;]/%5Inde+LLD.resultEyyerrorAB printfAGOn Hrror on line noJdG;*ine

  • 8/10/2019 Peterson and Bruce S. Davie, Computer Networks: A Systems Approach, Fourth

    43/48

    Xvi test.cmainAB int a;b;cE ifAa?b

    B aFaLbE whileAa?b B aFaLbE ifAa?Fb B cFaNbE

    else B cFaLbE

    ,utput

    Xle+ int.lXyacc \d int.yXgcc le+.yy.c y.tab.c \ll \lmX.a.out test.c

    Pos ,perator %rg1 %rg3 $esult

    & ? a b to

    1 FF to 0%*-H 7

    3 L a b t1' F t1 a

    " [,T, 77 ? a b t3

    6 FF t3 0%*-H 1&

    9 L a b t'2 F t' a

    [,T, 71& ?F a b t"

    11 FF t" 0%*-H 17

  • 8/10/2019 Peterson and Bruce S. Davie, Computer Networks: A Systems Approach, Fourth

    44/48

    13 N a b t71' F t7 c

    1" [,T, 1917 L a b t'16 F t6 c

    V

    '0( % "ro#ram to .enerate Mac)ine Code

    %,M: % "ro#ram to .enerate Mac)ine Code

    "RO.R%M:

    >include?stdio.h@>include?stdlib.h@>include?string.h@int label3&DEint noF&Eint mainAB 0I*H Vfp1;Vfp3E char fname1&D;op1&D;chE

    char operand12D;operand32D;result2DE int iF&;QF&E printfAGOn Hnter filename of the intermediate codeGE scanfAGJsG;KfnameE fp1FfopenAfname;GrGE fp3FfopenAGtarget.t+tG;GwGE ifAfp1FF

  • 8/10/2019 Peterson and Bruce S. Davie, Computer Networks: A Systems Approach, Fourth

    45/48

    fscanfAfp1;GJsG;opE iLLE ifAcheckWlabelAi fprintfAfp3;GOnlabel>JdG;iE ifAstrcmpAop;GprintGFF& B

    fscanfAfp1;GJsG;resultE fprintfAfp3;GOnOt ,/T JsG;resultE ifAstrcmpAop;GgotoGFF& B fscanfAfp1;GJs JsG;operand1;operand3E fprintfAfp3;GOnOt MP Js;label>JsG;operand1;operand3E labelnoLLDFatoiAoperand3E ifAstrcmpAop;GDFGFF& B

    fscanfAfp1;GJs Js JsG;operand1;operand3;resultE fprintfAfp3;GOnOt -T,$H JsJsD;JsG;operand1;operand3;resultE ifAstrcmpAop;GuminusGFF&

    B fscanfAfp1;GJs JsG;operand1;resultE fprintfAfp3;GOnOt *,%5 NJs;$1G;operand1E fprintfAfp3;GOnOt -T,$H $1;JsG;resultE switchAop&D B case V fscanfAfp1;GJs Js JsG;operand1;operand3;resultE fprintfAfp3;GOn Ot *,%5G;operand1E fprintfAfp3;GOn Ot *,%5 Js;$1G;operand3E fprintfAfp3;GOn Ot M/* $1;$&GE fprintfAfp3;GOn Ot -T,$H $&;JsG;resultE breakE case L fscanfAfp1;GJs Js JsG;operand1;operand3;resultE fprintfAfp3;GOn Ot *,%5 Js;$&G;operand1E fprintfAfp3;GOn Ot *,%5 Js;$1G;operand3E

    fprintfAfp3;GOn Ot %55 $1;$&GE fprintfAfp3;GOn Ot -T,$H $&;JsG;resultE breakE case N fscanfAfp1;GJs Js JsG;operand1;operand3;resultE fprintfAfp3;GOn Ot *,%5 Js;$&G;operand1E fprintfAfp3;GOn Ot *,%5 Js;$1G;operand3E fprintfAfp3;GOn Ot -/# $1;$&GE fprintfAfp3;GOn Ot -T,$H $&;JsG;resultE breakE case fscanfAfp1;GJs Js JsG;operand1;operand3;resultE fprintfAfp3;GOn Ot *,%5 Js;$&G;operand1E

    fprintfAfp3;GOn Ot *,%5 Js;$1G;operand3E fprintfAfp3;GOn Ot 5I4 $1;$&GE fprintfAfp3;GOn Ot -T,$H $&;JsG;resultE

  • 8/10/2019 Peterson and Bruce S. Davie, Computer Networks: A Systems Approach, Fourth

    46/48

    breakE case J fscanfAfp1;GJs Js JsG;operand1;operand3;resultE fprintfAfp3;GOn Ot *,%5 Js;$&G;operand1E fprintfAfp3;GOn Ot *,%5 Js;$1G;operand3E fprintfAfp3;GOn Ot 5I4 $1;$&GE fprintfAfp3;GOn Ot -T,$H $&;JsG;resultE

    breakE case F fscanfAfp1;GJs JsG;operand1;resultE fprintfAfp3;GOnOt -T,$H Js JsG;operand1;resultE breakE case @ QLLE fscanfAfp1;GJs Js JsG;operand1;operand3;resultE fprintfAfp3;GOn Ot *,%5 Js;$&G;operand1E fprintfAfp3;GOnOt [T Js;label>JsG;operand3;resultE labelnoLLDFatoiAresultE breakE case ? fscanfAfp1;GJs Js JsG;operand1;operand3;resultE

    fprintfAfp3;GOn Ot *,%5 Js;$&G;operand1E fprintfAfp3;GOnOt *T Js;label>JdG;operand3;resultE labelnoLLDFatoiAresultE breakE fcloseAfp3E fcloseAfp1E fp3FfopenAGtarget.t+tG;GrGE ifAfp3FF

  • 8/10/2019 Peterson and Bruce S. Davie, Computer Networks: A Systems Approach, Fourth

    47/48

    Xvi int.t+tFt1 3DFa & 1DFa 1 3DFa 3 '

    Vt1 6 t3La3D t3 t'Na3D t1 t3t' t3 t3uminus t3 t3print t3goto t3 t'Ft' uminus 37 t3Vt3 t' t'uminus t1 t1

    Lt1 t' t"print t",utput

    Hnter filename of the intermediate code int.t+t

    -T,$H t1;3-T,$H a&D;1-T,$H a1D;3-T,$H a3D;'

    *,%5 t1;$&*,%5 6;$1%55 $1;$&-T,$H $&;t'

    *,%5 a3D;$&*,%5 t3;$1%55 $1;$&-T,$H $&;t'

    *,%5 at3D;$&*,%5 t1;$1-/# $1;$&-T,$H $&;t3

    *,%5 t';$&*,%5 t3;$15I4 $ 1;$&-T,$H $&;t3*,%5 t3;$1-T,$H $1;t3

    *,%5 t3;$&[T 7;label>11*abel>11 ,/T t3

  • 8/10/2019 Peterson and Bruce S. Davie, Computer Networks: A Systems Approach, Fourth

    48/48

    MP t3;label>1'*abel>1' -T,$ H t'; *,%5 37;$1 -T,$H $1;t3

    *,%5 t3;$&

    *,%5 t';$1 M/* $1;$& -T,$H $&;t'

    *,%5 t1;$1 -T,$H $1;t1

    *,%5 t1;$& *,%5 t';$1 %55 $1;$& -T,$H $&;t"

    ,/T t"