University of Amsterdam Programming Research Group Implementation of an Imperative Programming Language with Backtracking Vincent Partington Report P9712 July1997 University of Amsterdam Department of Computer Science Programming Research Group Implementation of an imperative programming language with backtracking Vincent Partington Report P9712 July 1997 V. Partington CWI P.O.Box94079 1090GB Amsterdam The Netherlands e-mail: [email protected] Universiteit van Amsterdam, 1997 Implementation of an imperative programming language with backtracking Doctoraalscriptievan VincentPartington [email protected] Supervisor: Prof.Dr. K.R. Apt [email protected] July1997 FacultyofMathematics,ComputerScience,PhysicsandAstronomy UniversityofAmsterdam PlantageMuidergracht24 1018VTAmsterdam Preface Theparadigmofimperativeprogrammingiswellknown;thefirstprogramminglanguagemostpeoplelearn is an imperativeone, andimperativeprogramminglanguagesare oftenused for everydayprogramming. Unfortunately, this causes many people to miss out on the advantages of logic programming, especially wereitconcernssearchalgorithms. Inthisreportweexplorethepossibilityofcombiningtheparadigmsofimperativeandlogicprogramming, by implementinga compiler for an imperative languagewith the extensionsproposedin [AS97]. These extensionsbringsomeoftheadvantagesoflogicprogrammingtoimperativeprogrammingbyintroducing thenotionsoffailureandbacktracking,whichhaveprovensosuccessfulinlogicprogramminglanguages likeProlog. PartIgivesaquickoverviewofthefeaturesofthisnewprogramminglanguagecalledAlma-0,whichwas baseduponModula-2[Wir85],anddescribestheabstractmachineusedtoimplementAlma-0(theAlma AbstractMachine,abbreviatedtoAAA),whichcombinesthefeaturesofabstractmachinesforimperative languageswiththoseofabstractmachinesforlogicallanguages. PartIIdescribestheimplementationoftheAlma-0compilerindetail,fromlexicalanalysistocodegener- ationandtherun-timesystem. Itnotonlydescribesthecurrentimplementation,butcommentarysections describetheproblemsencounteredduringtheimplementationandthemotivationsbehindcertainsolutions. Thesesectionshavebeendividedfromthemaintextbylargecaptions,andcanberecognizedbythelittle “c”afterthesectionnumber. IwouldliketothankKrzysztofAptforhissupport,andfortheenlighteningconversationsI’vehadwith himonmanyarainymorning,and,ifoneofthemorningswasnotrainy,itshouldhavebeen,justforthe effect. Myfellowstudentsshouldbethankedfortheirinterestingremarksanddiscussions,whetherthey concernedmyprojectornot. Finally,IwouldespeciallyliketothankmyparentsandMichelleforkeeping megoing,andfortellingmetogetonwithit. iii iv Contents Preface iii I Design 1 1 TheAlma-0programminglanguage 3 1.1 Extensions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 1.2 Inputandoutput . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 1.3 Missingfeatures. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 1.4 Smalldifferences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 2 TheAlmaAbstractArchitecture 9 2.1 Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 2.2 Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 2.3 Backtracking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 II Implementation 15 3 Overviewoftheimplementation 17 3.1 Compilerstructure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 3.2 Puttingittogether . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 4 Lexicalandsyntaxanalysis 21 4.1 Lexicalanalysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 4.2 Syntaxanalysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 4.3c Thedesignator/qualidentparseconflict . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 4.4c Parsinganexpressionasastatementandviceversa . . . . . . . . . . . . . . . . . . . . . 22 5 Symbolsandsymboltables 25 5.1 Differentkindsofsymbols . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 5.2 Symbolrepresentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 5.3 Thesymboltable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 5.4c Implementingtheobject-orienteddesigninC . . . . . . . . . . . . . . . . . . . . . . . . 29 6 Semanticanalysis 31 6.1 Typeanalysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 6.2 Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 6.3 Procedures. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 6.4c WhyAlma-0hasnoCARDINALtype . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 7 Intermediatecodegeneration 39 7.1 Coderepresentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 v 7.2 Translatinglanguageconstructsintocode . . . . . . . . . . . . . . . . . . . . . . . . . . 39 7.3c Orderofcodegeneration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 8 ImplementationoftheAAA 49 8.1 Codegeneration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 8.2 Run-timeenvironment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 8.3 GeneratingavalidCprogram. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 8.4c Programcounteremulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 8.5c Alternativelogimplementations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 Conclusionsandfurtherwork 57 A Syntaxoverview 59 A.1 Lexicalrules. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 A.2 Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 B ExampleAlma-0programs 63 B.1 knapsack.a0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 B.2 present.a0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 B.3 queens.a0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 B.4 squares.a0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66 Bibliography 69 vi