Listoflectures IntroductionandFunctionalProgramming ImperativeProgrammingandDataStructures 3 Environment 4 Evaluation 5 ObjectOrientedProgramming 6 Macrosand 7 VirtualMachinesandBytecode 8 GarbageCollectionandNativeCode 9 DistributedComputing 10 Logic 11 Guestlecture 12 Summary 1 2 TDDA69DataandProgramStructure Evaluation CyrilleBerger 2/57 Lecturegoal Lecturecontent Learnhowcomputerprogramsare evaluatedandinterpreted AbstractSyntax Parsingaprogram ContextFreeGrammar AbstractSyntaxTree Evaluation SubstitutionModel Environmentmodel Writinganevaluator 3/57 4/57 Howisaprograminterpreted? Sourcecode Parser Parser AbstractSyntaxTree Treevisitor Generator Sourcecode Bytecode VirtualMachine Assembler Assembly AbstractSyntaxTree ... OperatingSystem CPU 5/57 LexerandParser Parsingaprogram Atokenisawordoranatomic element,ieif,1,+... Alexerisconvertingasequence ofcharactersintoasequenceof tokens Aparsercombinestokensintoa datastructure 8 ReversePolishNotationCalculator(1/2) ReversePolishNotationCalculator(2/2) Evaluate:'145*23+/ Lexer:['1','4','5','*','2','3', '+','/','+'] Parser: 145*23+/+ Currenttoken:'<eof>' Currentstack: 5 Iftokenisanumber,pushitonthestack Iftokenisanoperator,removethefirsttwo elementsofthestackandpushtheresult Whatistheresultof evaluation:www.govote.at46 3415 9 10 Calculator 1+4*5/(2+ Lexer:['1''+''4''*''5''/''(''2' '+''3'')'] Howtoparse?Andhowto evaluate? ContextFreeGrammar 11 Recursivestructure ContextFreeGrammar Acontext-freegrammarisanatural notationforarecusivestructure Acontext-freegrammarisdefinedby G=(V,Σ,R,S) Programminglanguageshaverecursive structures STATEMENTcan ifEXPRESSIONthenSTATEMENTelseSTATEMENT whileEXPRESSIONdoSTATEMENTend STATEMENT;STATEMENT;.... EXPRESSION ... Σisasetofterminals keywords, Visasetofnon-terminals EXPRESSIONcan STATEMENT, S∊Visthestartsymbol Risthesetofproductionrules EXPRESSION+EXPRESSION EXPRESSION*EXPRESSION EXPRESSION(EXPRESSION) ... X→Y1,..., X∊VYi∊Σ⋃V⋃{Ɛ 13 14 Derivation ContextFreeGrammarforaCalculator Aderivationisa sequenceof productions TerminalsΣ={'+','*','/','-','(',')','0',... '9'} Non-terminalsEXPR,LITERAL Rules S→...→...→...→... EXPR→EXPR'+'EXPR Aderivationcanbe drawnasatree |EXPR'-'EXPR |EXPR'/'EXPR |EXPR'*'EXPR |'('EXPR')' | Startsymbolisthetree'sroot ForaproductionX→Y1...Yn, Y1...YnarethechildrenofX LITERAL→LITERAL ... LITERAL→LITERAL 15 X Y1 ... Yn 16 Parsetree Derivationofcalculator Grammar: E→E{'+'|'-'|'*'| '/'}E |'('E |[0-9]+ Derive: E 1 1+4*5/(2+ E + E 4 E * E 5 Aparse E / ( E Terminalsattheleaves Non-terminalsatthe interiornodes E E + 2 ) E 3 17 Anin-orderofthe leavesisthe originalinput Theparse treeshows the associationof operations ParsetreeUnicity Grammar: E→E{'+'|'-'|'*'|'/'}E |'('E') |[0-9]+ Derive: 1+4*5/(2+3) Whyisthat notunique? www.govote.at 571354 E 1 E + E 4 E * E 5 E 1 E + E 4 E * E 5 E / ( E 2 E E + ) E 3 18 Ambiguity Thisstringhastwoparsetrees:2*3+1 E / ( E 2 E E + E ) 2 E E * E 3 E + E 1 E 2 E * E + E 3 E 1 3 19 20 WhatisanAbstractSyntaxTree? Aparsertracesthederivationof asequenceoftokens Weneedastructural representationofaprogram,such asaparsetree AnAbstractSyntaxTreeisa parsetreewithlessdetail AbstractSyntaxTree 22 AbstractSyntaxTreevsParseTree(1/3) E 1 E + E 4 E * E 5 E / ( E 2 E E + ) E 3 Dowereally needall thosenodes? Canwe removethe'E' and parenthesis? AbstractSyntaxTreevsParseTree(2/3) E 1 E + E 4 + E * E 5 E / ( E 2 23 1 E E + ) E 4 * 5 / 2 + 3 3 24 AbstractSyntaxTreevsParseTree(3/3) Theybothcapturesthenesting structure Theyare ASTaremorecompactand easiertouse Lisp(1/2) Representinga treeasan Firstelementis array thevalue Secondelementisa listofchildren ['+'[1['*'[4['/'[5 ['+'[23]]]]]]]] InLisp: + 1 4 * 5 / 2 + 3 (+1(*4(/5(+2 25 26 Lisp(2/2) ThesyntaxofLispis EXPR→'('operatorEXPR...')' Operators Evaluation defineanewfunction (define(<name><formalparameters>)<body>) condanewfunction (cond(exprresult)(exprresult)...(Tdefault_result)) 27 SubstitutionModel Toevaluateacombinationwitha compoundprocedureasoperator: SubstitutionModel evaluatetheelementsofthecombination applytheprocedure(i.e.valueftheoperator) tothearguments(i.e.valuesoftheoperands): evaluatethebodyoftheprocedurewhere eachformalparameterisreplacedbythe correspondingargument 30 SubstitutionModel-Example(1/) SubstitutionModel-Example(1/2) (define(squarex)(*xx)) (define(sum-of-squaresxy) (+(squarex)(squarey))) (define(fa) (sum-of-squares(+a1)(*a2))) Howtoevaluate(f5)? Toevaluate: (f5) Fetchthevalueoff (sum-of-squares(+a1)(*a2))) Replaceaby5 (sum-of-squares(+51)(*52)) Evaluatethe (sum-of-squares610) Fetchthevalueofsum-of-squaresandreplacethe arguments: (+(square6)(square10)) Andso (+(*66)(*1010)) (+36100) 136 Thisisthesubstitutionmodelintheapplicativeorder 31 32 ApplicativevsNormal ApplicativevsNormal-Example(1/2) Recursivelyevaluateallthe subexpressions,butinwhatorder? Applicativeorder:evaluatetheargumentsand thenapply.Evaluatetheprocedurebodywith everyformalparameterreplacedwiththe correspondingargumentvalue Normalorder:fullyexpandandthenreduce. Recursivelyexpandeveryprocedureuntilonly primitiveoperatorsareleft.Evaluate(reduce) theresult. (define(squarex)(*xx)) Applicative order (square(*42)) (square(*8)) (*88) 64 Normalorder (square(*42)) (*(*42)(*42)) (*88) 64 Forprocedureapplicationsthatcanbemodeledwiththe substitutionmodelthatterminatewithlegitimatevalues, applicativeandnormalorderproducethesamevalue. 33 Normalorderandlazyevaluation ApplicativevsNormal-Example(2/2) if(objectandobject.count() andobject.sum()/ object.count()>0.90): print('Highsuccessrate!') (define(fooxy (if(=x0)y(*2x)))) Applicative order (foo10(/200)) error:divisionby zero 34 Normalorder (foo10(/200)) (if(=100)(/200) (*210)) (*210) 20 Whatistheresult?www.govote.at366333 35 36 Assignmentandthesubstitutionmodel(1/2) (define(make-withdrawbalance) (lambda(amount) (set!balance(-balanceamount)) balance)) (defineW(make-withdraw25)) (W20) 5 (W10) -5 Assignmentandthesubstitutionmodel(2/2) (define(make-withdrawbalance) (lambda(amount) (set!balance(-balanceamount)) balance)) (defineW(make-withdraw25)) (W ((make-withdraw25)20) ((lambda(amount)(set!balance(-25amount))25) 20) ((set!balance(-2520))25) 25 37 38 Whydoesn’tthesubstitutionmodelwork? Substitutionisbasedonthenotion thatsymbolsarenamesforvalues Usingset!changessymbolstoplaces wherevaluescanbestored Thevalueinsuchaplacecanchange usingassignmentwithset! Anewmodelofevaluationisneeded: theenvironmentmodelof evaluation. Environmentmodel 39 Environment Aframeisaset ofbindings betweenvalue andname Anenvironmentis asetofframe nil:... pi:3.14... ... Evaluationrules (global)I II a:6 f:[34] ... a:2 m:4 ... Afunctionisevaluated,bycreatinganew frameandbindingtheargumentsintheframe Afunctionisstoredasalambdarepresenting thefunctionbodyandapointertothe environmentwherethefunctioniscreated VI 41 Evaluationrules-Example(1/5) 42 Evaluationrules-Example(2/5) (define(make-withdrawbalance)...) (defineW1(make-withdraw100)) 43 44 Evaluationrules-Example(3/5) Evaluationrules-Example(4/5) (W150) Afterthecall: 45 46 Evaluationrules-Example(5/5) (defineW2(make-withdraw100)) Writinganevaluator 47 Usingthevisitorpatternforevaluation Howdoes interpeterwork inpractice? Wecanuse thevisitor pattern. + 1 4 * 5 / Visitorinterpreter ['+'[1 ['*'[4 ['/'[5 ['+'[23 ] ]]]]]]] defevaluate(node): ifnotisinstance(node,list): returnnode elifnode[0]=='+': returnevaluate(node[1] [0]) +evaluate(node[1] [1]) elifnode[0]=='-': returnevaluate(node[1] [0]) -evaluate(node[1][1]) elifnode[0]=='*': returnevaluate(node[1] [0]) *evaluate(node[1][1]) elifarray[0]=='/': returnevaluate(node[1] [0]) /evaluate(node[1][1]) + 2 3 + 1 4 * 5 / + 2 49 Condstatement 3 50 Whilestatement ['while'[<'x'10] ['set'['x'['+'['x' 1]]] defevaluate(node): ... elifnode[0]=='while': whileevaluate(node[1] [0]): evaluate(node[1][1]) ['cond'[[[<'x'0]['-'x]]], [[='x'0]0], ['T''x']]]] defevaluate(node): ... elifarray[0]=='cond': foreltinnode[1]: ifelt[0]=='T' orevaluate(elt[0]): returnevaluate(elt[2]) 51 52 Definingvalue Setvalue ['define'['x' defevaluate(node): ... elifnode[0]=='define': currentFrame.define( node[1][0], evaluate(node[1] [1])) ['set'['x' defevaluate(node): ... elifnode[0]=='set': currentFrame.set( node[1][0], evaluate(node[1] [1])) 53 DefiningaLambda 54 CallingaLambda ['lambda'[['x']['+''x' defevaluate(node): ... elifnode[0]== 'lambda': return(array[1][0], array[1][1], currentFrame) ['increment' defevaluate(node): ... else: (args,body,frame)= currentFrame.get(node[0]) previousFrame=currentFrame currentFrame=createFrame(frame) foriinrange(0,len(args)): name=args[i] value=evaluate(node[1][i]) currentFrame.define(name,value) ret=evaluate(body) currentFrame=previousFrame returnret 55 56 Summary ParserandAbstractSyntax EvaluationModels Howtobuildasimpleinterpreter 57/57