Programming Language Pragmatics FOURTH EDITION This page intentionally left blank Programming Language Pragmatics FOURTH EDITION Michael L. Scott Department of Computer Science University of Rochester AMSTERDAM (cid:129) BOSTON (cid:129) HEIDELBERG (cid:129) LONDON NEW YORK (cid:129) OXFORD (cid:129) PARIS (cid:129) SAN DIEGO SAN FRANCISCO (cid:129) SINGAPORE (cid:129) SYDNEY (cid:129) TOKYO Morgan Kaufmann is an imprint of Elsevier MorganKaufmannisanimprintofElsevier 225WymanStreet,Waltham,MA02451,USA Copyright©2016,2009,2006,1999ElsevierInc.Allrightsreserved. Coverimage:Copyright©2009,ShannonA.Scott. CummingNatureCenter,Naples,NY. Nopartofthispublicationmaybereproducedortransmittedinanyformorbyanymeans,electronicor mechanical,includingphotocopying,recording,oranyinformationstorageandretrievalsystem,without permissioninwritingfromthepublisher.Detailsonhowtoseekpermission,furtherinformationaboutthe Publisher’spermissionspoliciesandourarrangementswithorganizationssuchastheCopyrightClearanceCenter andtheCopyrightLicensingAgency,canbefoundatourwebsite:www.elsevier.com/permissions. ThisbookandtheindividualcontributionscontainedinitareprotectedundercopyrightbythePublisher(other thanasmaybenotedherein). Notices Knowledgeandbestpracticeinthisfieldareconstantlychanging.Asnewresearchandexperiencebroadenour understanding,changesinresearchmethods,professionalpractices,ormedicaltreatmentmaybecomenecessary. Practitionersandresearchersmustalwaysrelyontheirownexperienceandknowledgeinevaluatingandusing anyinformation,methods,compounds,orexperimentsdescribedherein.Inusingsuchinformationormethods theyshouldbemindfuloftheirownsafetyandthesafetyofothers,includingpartiesforwhomtheyhavea professionalresponsibility. Tothefullestextentofthelaw,neitherthePublishernortheauthors,contributors,oreditors,assumeanyliability foranyinjuryand/ordamagetopersonsorpropertyasamatterofproductsliability,negligenceorotherwise,or fromanyuseoroperationofanymethods,products,instructions,orideascontainedinthematerialherein. BritishLibraryCataloguinginPublicationData AcataloguerecordforthisbookisavailablefromtheBritishLibrary LibraryofCongressCataloging-in-PublicationData AcatalogrecordforthisbookisavailablefromtheLibraryofCongress ForinformationonallMKpublications visitourwebsiteathttp://store.elsevier.com/ ISBN:978-0-12-410409-9 About the Author MichaelL.ScottisaprofessorandpastchairoftheDepartmentofComputerSci- enceattheUniversityofRochester. HereceivedhisPh.D.incomputersciencesin 1985fromtheUniversityofWisconsin–Madison. From2014–2015hewasaVis- itingScientistatGoogle. Hisresearchinterestslieattheintersectionofprogram- minglanguages,operatingsystems,andhigh-levelcomputerarchitecture,withan emphasisonparallelanddistributedcomputing.HisMCSmutualexclusionlock, co-designedwithJohnMellor-Crummey,isusedinavarietyofcommercialand academic systems. Several other algorithms, co-designed with Maged Michael, Bill Scherer, and Doug Lea, appear in the java.util.concurrent standardli- brary. In 2006 he andDr. Mellor-Crummey shared the ACM SIGACT/SIGOPS EdsgerW.DijkstraPrizeinDistributedComputing. Dr.ScottisaFellowoftheAssociationforComputingMachinery,aFellowof theInstituteofElectricalandElectronicsEngineers,andamemberofUsenix,the UnionofConcernedScientists,andtheAmericanAssociationofUniversityPro- fessors. Theauthorofmorethan150refereedpublications,heservedasGeneral Chairofthe2003ACMSymposiumonOperatingSystemsPrinciples(SOSP)and asProgramChairofthe2007ACMSIGPLANWorkshoponTransactionalCom- puting (TRANSACT), the 2008 ACM SIGPLAN Symposium on Principles and Practice of Parallel Programming (PPoPP), and the 2012 International Confer- ence on Architectural Support forProgrammingLanguages andOperating Sys- tems (ASPLOS). In 2001 he received the University of Rochester’s Robert and Pamela Goergen Award for Distinguished Achievement and Artistry in Under- graduateTeaching. This page intentionally left blank vii To family and friends. This page intentionally left blank Contents Foreword xxiii Preface xxv I FOUNDATIONS 3 1 Introduction 5 1.1 TheArtofLanguageDesign 7 1.2 TheProgrammingLanguageSpectrum 11 1.3 WhyStudyProgrammingLanguages? 14 1.4 CompilationandInterpretation 17 1.5 ProgrammingEnvironments 24 1.6 AnOverviewofCompilation 26 1.6.1 LexicalandSyntax Analysis 28 1.6.2 SemanticAnalysis andIntermediate Code Generation 32 1.6.3 TargetCode Generation 34 1.6.4 CodeImprovement 36 1.7 SummaryandConcludingRemarks 37 1.8 Exercises 38 1.9 Explorations 39 1.10 BibliographicNotes 40 2 Programming Language Syntax 43 2.1 SpecifyingSyntax: RegularExpressionsandContext-FreeGrammars 44 2.1.1 TokensandRegularExpressions 45 2.1.2 Context-FreeGrammars 48 2.1.3 Derivations andParse Trees 50