560 Present

Embed Size (px)

Citation preview

  • 8/11/2019 560 Present

    1/21

    Compiler ConstructionCompiler Construction

    ToolsToolsLibby RasnickLibby Rasnick

    Christopher Newport UniversityChristopher Newport UniversityCPSC 560CPSC 560

    Sprin !00"Sprin !00"

  • 8/11/2019 560 Present

    2/21

    ContentsContents

    #e$inin Compiler Construction Tools %aka CCTse$inin Compiler Construction Tools %aka CCTs&Uses $or CCTsUses $or CCTs

    CCTs in the Compiler StructureCCTs in the Compiler Structure

    Le'ical (naly)erLe'ical (naly)er

    Synta' (naly)erSynta' (naly)er

    Semantic (naly)erSemantic (naly)er

    *nterme+iate Co+e ,enerator*nterme+iate Co+e ,enerator

    Co+e -ptimi)erCo+e -ptimi)erCo+e ,eneratorCo+e ,enerator

    Compiler Construction .it / CocktailCompiler Construction .it / Cocktail

    Re$erencesRe$erences

  • 8/11/2019 560 Present

    3/21

    #e$inin CCTs#e$inin CCTs

    prorams or environments that assistprorams or environments that assist

    in the creation o$ an entire compiler orin the creation o$ an entire compiler or

    its partsits parts

  • 8/11/2019 560 Present

    4/21

    Uses $or CCTsUses $or CCTs

    enerate le'ical analy)ersenerate le'ical analy)ers

    synta' analy)erssynta' analy)ers

    semantic analy)erssemantic analy)ersinterme+iate co+einterme+iate co+e

    optimi)e+ taret co+eoptimi)e+ taret co+e

  • 8/11/2019 560 Present

    5/21

    CCTs in the Compiler StructureCCTs in the Compiler Structure

  • 8/11/2019 560 Present

    6/21

    Le'ical (naly)erLe'ical (naly)er

    scanner eneratorsscanner enerators

    input1 source proraminput1 source proram

    output1 le'ical analy)eroutput1 le'ical analy)er task o$ rea+in characters $rom sourcetask o$ rea+in characters $rom source

    proram an+ reconi)in tokens or basicproram an+ reconi)in tokens or basic

    syntactic components 2"3syntactic components 2"3 maintains a list o$ reserve+ wor+smaintains a list o$ reserve+ wor+s

  • 8/11/2019 560 Present

    7/21

    Le'ical (naly)erLe'ical (naly)er

    4le' %$ast le'ical analy)er enerator&4le' %$ast le'ical analy)er enerator&

    'ample which speci$ies a scanner which'ample which speci$ies a scanner which

    replaces the strin username7 with thereplaces the strin username7 with theuser8s loin nameuser8s loin name

    9999username print$%9s7 etloin%&&:username print$%9s7 etloin%&&:

  • 8/11/2019 560 Present

    8/21

    Synta' (naly)erSynta' (naly)er

    parser eneratorsparser enerators

    input1 conte't/$ree rammarinput1 conte't/$ree rammar

    output1 synta' analy)eroutput1 synta' analy)erthe task o$ the synta' analy)er is tothe task o$ the synta' analy)er is topro+uce a representation o$ the sourcepro+uce a representation o$ the sourceproram in a $orm +irectly representin itsproram in a $orm +irectly representin its

    synta' structure; This representation issynta' structure; This representation isusually in the $orm o$ a binary tree or similarusually in the $orm o$ a binary tree or similar+ata structure 2"3+ata structure 2"3

  • 8/11/2019 560 Present

    9/21

    Synta' (naly)erSynta' (naly)er& C4, into a Cproramproram

  • 8/11/2019 560 Present

    10/21

    Semantic (naly)erSemantic (naly)er

    synta'/+irecte+ translatorssynta'/+irecte+ translatorsinput1 parse treeinput1 parse tree

    output1 routines to enerate */co+eoutput1 routines to enerate */co+e

    The role o$ the semantic analy)er is to +eriveThe role o$ the semantic analy)er is to +erivemetho+s by which the stuctures constructe+ by themetho+s by which the stuctures constructe+ by thesynta' analy)er may be evaluate or e'ecute+; 2"3synta' analy)er may be evaluate or e'ecute+; 2"3

    type checkertype checker

    two common tactics1two common tactics1G $latten the semantic analy)er8s parse treeG $latten the semantic analy)er8s parse tree

    G embe+ semantic analy)er wEin synta' analy)erG embe+ semantic analy)er wEin synta' analy)er

    %synta'/+riven translation&%synta'/+riven translation&

  • 8/11/2019 560 Present

    11/21

    *nterme+iate Co+e ,enerator*nterme+iate Co+e ,enerator

    (utomatic co+e enerators(utomatic co+e enerators

    input1 */co+e rulesinput1 */co+e rules

    output1 cru+e taret machine proramoutput1 cru+e taret machine proram

    The task o$ the co+e enerator is toThe task o$ the co+e enerator is totraverse this tree pro+ucin $unctionallytraverse this tree pro+ucin $unctionallyeHuivalent obIect co+e;7 2"3eHuivalent obIect co+e;7 2"3

    three a++ress co+e is one typethree a++ress co+e is one type

  • 8/11/2019 560 Present

    12/21

    *nterme+iate Co+e ,enerator*nterme+iate Co+e ,enerator

    'ample J K % F y& E !'ample J K % F y& E !a 1M a 1M

    b 1M yb 1M y

    c 1M a F bc 1M a F b

    a 1M ca 1M c

    b 1M !b 1M !

    c 1M a E bc 1M a E b

    a 1M Ja 1M Jb 1M cb 1M c

    c 1M a K bc 1M a K b

    expr

    7 + expr

    expr / 2

    expr( )

    8 *y

  • 8/11/2019 560 Present

    13/21

    Co+e -ptimi)erCo+e -ptimi)er

    #ata $low enines#ata $low enines

    input1 */co+einput1 */co+e

    output1 trans$orme+ co+eoutput1 trans$orme+ co+e

    This improvement is achieve+ by proramThis improvement is achieve+ by proramtrans$ormations that are tra+itionally calle+trans$ormations that are tra+itionally calle+optimizationsoptimizations althouh the term althouh the term

    optimi)ation8 is a misnomer because thereoptimi)ation8 is a misnomer because thereis rarely a uarantee that the resultin co+eis rarely a uarantee that the resultin co+eis the best possible;7 2>3is the best possible;7 2>3

  • 8/11/2019 560 Present

    14/21

    Co+e -ptimi)erCo+e -ptimi)er

    Peephole -ptimi)ationPeephole -ptimi)ation

    machine or assembly co+e is use+ alonmachine or assembly co+e is use+ alonwith knowle+e o$ taret machine8swith knowle+e o$ taret machine8s

    instruction set to replace */co+e instructionsinstruction set to replace */co+e instructionswith shorter or more Huickly e'ecute+with shorter or more Huickly e'ecute+instructions / this is repeate+ as much as isinstructions / this is repeate+ as much as is

    necessary 2"3necessary 2"3

  • 8/11/2019 560 Present

    15/21

    Co+e -ptimi)erCo+e -ptimi)er

    Common -ptimi)in Trans$ormations 2!3Common -ptimi)in Trans$ormations 2!3

    -ptim; Name-ptim; Name ReHuire+ (nalysisReHuire+ (nalysis Trans$ormationTrans$ormationconstant $ol+inconstant $ol+in simulate+ e'ec;simulate+ e'ec; eliminationelimination

    +ea+ co+e elim;+ea+ co+e elim; simulate+ e'ec;simulate+ e'ec; eliminationelimination

    loop unrollinloop unrollin loop struct; stat;sloop struct; stat;s motion %replic;&motion %replic;&

    lineari)in arrayslineari)in arrays loop structureloop structure eliminationelimination

    loa+Estore optim;loa+Estore optim; #4(#4( motionmotion

    branch chaininbranch chainin statisticsstatistics selection %+ec&selection %+ec&math i+entitiesmath i+entities nonenone selection eliminationselection elimination

    common sube'p;common sube'p; simulate+ e'ec;simulate+ e'ec; eliminationelimination

  • 8/11/2019 560 Present

    16/21

    Co+e -ptimi)erCo+e -ptimi)er

    'ample J K % F y& E !'ample J K % F y& E !a 1M ya 1M y

    a 1M a F a 1M a F

    a 1M a E !a 1M a E !a 1M K Ja 1M K J

    expr

    7 + expr

    expr / 2

    expr( )

    8 *y

  • 8/11/2019 560 Present

    17/21

    Co+e ,eneratorCo+e ,enerator

    (utomatic co+e enerators(utomatic co+e enerators

    input1 optimi)e+ %trans$orme+& */co+einput1 optimi)e+ %trans$orme+& */co+e

    output1 taret machine proramoutput1 taret machine proram

    'ample J K % F y& E !'ample J K % F y& E !

    Loa+ a yLoa+ a y

    Dult a Dult a #iv a !#iv a !

    (++ a J(++ a J

  • 8/11/2019 560 Present

    18/21

    Compiler Construction Tool .itsCompiler Construction Tool .its

    tool kits contain all %or almost all& phases o$tool kits contain all %or almost all& phases o$a compilera compiler

    'ample1 Cocktail 2J3'ample1 Cocktail 2J3

    #evelope+ until >OO" at .arlsruhe research#evelope+ until >OO" at .arlsruhe researchlab the ,erman National Researchlab the ,erman National Research

    Center $or *n$ormation Technoloy;Center $or *n$ormation Technoloy;

  • 8/11/2019 560 Present

    19/21

    Compiler Construction Tool .itsCompiler Construction Tool .itsComponents1Components1

    Re'Re'%Reular pression tool&%Reular pression tool&

    / scanner enerator/ scanner enerator

    LalrLalr/ an L(LR%>& parser enerator acceptin/ an L(LR%>& parser enerator acceptin

    rammars in e'ten+e+

  • 8/11/2019 560 Present

    20/21

    Re$erencesRe$erences

    >> Aho, Alfred V., Sethi, Ravi and Ullman, JeffreyD. Compilers: principles, techniques, andtools. (1986). Reading: Addison-Wesley.

    !! Peters, James, Pittman, Thomas. The art ofcompiler design: theory and practice.

    (1992). Englewood Cliffs: Prentice Hall.

    3 Watson, Des. High-level languages and theircompilers. (1989). Wokingham: Addison-Wesley.

  • 8/11/2019 560 Present

    21/21

    Re$erencesRe$erences

    Q en Christopher; %!00"&; 4ree CompilerConstruction Tools; http1EEwww;the$reecountry;comEproramminEcompilercontructiontools%0>Darch !00"&;

    5 The Le' =acc Pae; http1EE+inosaur;compilertools;net %0> Darch !00"&;

    6 Compiler Construction .its; http1EEcatalo;

    compilertools;net %0> Darch !00"&;J The Cocktail Compiler Toolbo'; http1EEwww;$irst;

    m+;+eEcocktailE %0> Darch !00"&;

    http://www.thefreecountry.com/programming/compilercontructiontoolshttp://www.thefreecountry.com/programming/compilercontructiontoolshttp://dinosaur.compilertools.net/http://dinosaur.compilertools.net/http://catalog.compilertools.net/http://catalog.compilertools.net/http://www.first.gmd.de/cocktail/http://www.first.gmd.de/cocktail/http://www.first.gmd.de/cocktail/http://www.first.gmd.de/cocktail/http://www.first.gmd.de/cocktail/http://catalog.compilertools.net/http://catalog.compilertools.net/http://catalog.compilertools.net/http://dinosaur.compilertools.net/http://dinosaur.compilertools.net/http://dinosaur.compilertools.net/http://www.thefreecountry.com/programming/compilercontructiontoolshttp://www.thefreecountry.com/programming/compilercontructiontoolshttp://www.thefreecountry.com/programming/compilercontructiontoolshttp://www.thefreecountry.com/programming/compilercontructiontools