编译原理龙书答案完整性高第二章2.2ExercisesforSection2.22.2.1Considerthecontext-freegrammar:S->SS+|SS*|aShowhowthestringaa+a*canbegeneratedbythisgrammar.Constructaparsetreeforthisstring.Whatlanguagedoesthisgrammargenerate?Justifyyouranswer.answerS->SS*->SS+S*->aS+S*->aa+S*->aa+a*L={Postfixexpressionconsistingofdigits,plusandmultiplesigns}2.2.2Whatlanguageisgeneratedbythefollowinggrammars?Ineachcasejustifyyouranswer.S->0S1|01S->+SS|-SS|aS->S(S)S|εS->aSbS|bSaS|ε⧗S->a|S+S|SS|S*|(S)answerL={0n1n|n>=1}L={Prefixexpressionconsistingofplusandminussigns}L={Matchedbracketsofarbitraryarrangementandnesting,includesε}L={Stringhasthesameamountofaandb,includesε}?2.2.3WhichofthegrammarsinExercise2.2.2areambiguousanswerNoNoYesYesYes2.2.4Constructunambiguouscontext-freegrammarsforeachofthefollowinglanguages.Ineachcaseshowthatyourgrammariscorrect.Arithmeticexpressionsinpostfixnotation.Left-associativelistsofidentifiersseparatedbycommas.Right-associativelistsofidentifiersseparatedbycommas.Arithmeticexpressionsofintegersandidentifierswiththefourbinaryoperators+,-,*,/.answer1.E->EEop|num2.list->list,id|id3.list->id,list|id4.expr->expr+term|expr-term|termterm->term*factor|term/factor|factorfactor->id|num|(expr)5.expr->expr+term|expr-term|termterm->term*unary|term/unary|unaryunary->+factor|-factorfactor->id|num|(expr)2.2.5Showthatallbinarystringsgeneratedbythefollowinggrammarhavevaluesdivisibleby3.Hint.Useinductiononthenumberofnodesinaparsetree.num->11|1001|num0|numnumDoesthegrammargenerateallbinarystringswithvaluesdivisibleby3?answerproveanystringderivedfromthegrammarcanbeconsideredtobeasequenceconsistingof11,1001and0,andnotprefixedwith0.thesumofthisstringis:sum=Σn(21+20)*2n+Σm(23+20)*2m=Σn3*2n+Σm9*2mItisobviouslycandivisibleby3.No.Considerstring"10101",itisdivisibleby3,butcannotderivedfromthegrammar.Question:anygeneralprove?2.2.6Constructacontext-freegrammarforromannumerals.Note:wejustconsiderasubsetofromannumeralswhichislessthan4k.answerwikipedia:Roman_numeralsviawikipedia,wecancategorizethesinglenomannumeralsinto4groups:I,II,III|IV|V,VI,VII,VIII|IXthengettheproduction:digit->smallDigit|IV|VsmallDigit|IXsmallDigit->I|II|III|εandwecanfindasimplewaytomapromantoarabicnumerals.Forexample:XII=>X,II=>10+2=>12CXCIX=>C,XC,IX=>100+90+9=>199MDCCCLXXX=>M,DCCC,LXXX=>1000+800+80=>1880viatheuppertworules,wecanderivetheproduction:romanNum->thousandhundredtendigitthousand->M|MM|MMM|εhundred->smallHundred|CD|DsmallHundred|CMsmallHundred->C|CC|CCC|εten->smallTen|XL|LsmallTen|XCsmallTen->X|XX|XXX|εdigit->smallDigit|IV|VsmallDigit|IXsmallDigit->I|II|III|ε2.3ExercisesforSection2.32.3.1Constructasyntax-directedtranslationschemethattrans­latesarithmeticexpressionsfrominfixnotationintoprefixnotationinwhichanoperatorappearsbeforeitsoperands;e.g.,-xyistheprefixnotationforx-y.Giveannotatedparsetreesfortheinputs9-5+2and9-5*2.。
answerproductions:expr->expr+term|expr-term|termterm->term*factor|term/factor|factorfactor->digit|(expr)translationschemes:expr->{print("+")}expr+term|{print("-")}expr-term|termterm->{print("*")}term*factor|{print("/")}term/factor|factorfactor->digit{print(digit)}|(expr)2.3.2Constructasyntax-directedtranslationschemethattrans­latesarithmeticexpressionsfrompostfixnotationintoinfixnotation.Giveannotatedparsetreesfortheinputs95-2*and952*-.answerproductions:expr->exprexpr+|exprexpr-|exprexpr*|exprexpr/|digittranslationschemes:expr->expr{print("+")}expr+|expr{print("-")}expr-|{print("(")}expr{print(")*(")}expr{print(")")}*|{print("(")}expr{print(")/(")}expr{print(")")}/|digit{print(digit)}AnotherreferenceanswerE->{print("(")}E{print(op)}E{print(")"}}op|digit{print(digit)}2.3.3Constructasyntax-directedtranslationschemethattrans­latesintegersintoromannumeralsanswerassistantfunction:repeat(sign,times)//repeat('a',2)='aa'translationschemes:num->thousandhundredtendigit{num.roman=thousand.roman||hundred.roman||ten.roman||digit.roman;print(num.roman)}thousand->low{thousand.roman=repeat('M',low.v)}hundred->low{hundred.roman=repeat('C',low.v)}|4{hundred.roman='CD'}|high{hundred.roman='D'||repeat('X',high.v-5)}|9{hundred.roman='CM'}ten->low{ten.roman=repeat('X',low.v)}|4{ten.roman='XL'}|high{ten.roman='L'||repeat('X',high.v-5)}|9{ten.roman='XC'}digit->low{digit.roman=repeat('I',low.v)}|4{digit.roman='IV'}|high{digit.roman='V'||repeat('I',high.v-5)}|9{digit.roman='IX'}low->0{low.v=0}|1{low.v=1}|2{low.v=2}|3{low.v=3}high->5{high.v=5}|6{high.v=6}|7{high.v=7}|8{high.v=8}2.3.4Constructasyntax-directedtranslationschemethattrans­latesromannumeralsintointegers.answerproductions:romanNum->thousandhundredtendigitthousand->M|MM|MMM|εhundred->smallHundred|CD|DsmallHundred|CMsmallHundred->C|CC|CCC|εten->smallTen|XL|LsmallTen|XCsmallTen->X|XX|XXX|εdigit->smallDigit|IV|VsmallDigit|IXsmallDigit->I|II|III|εtranslationschemes:romanNum->thousandhundredtendigit{romanNum.v=thousand.v||hundred.v||ten.v||digit.v;print(romanNun.v)}thousand->M{thousand.v=1}|MM{thousand.v=2}|MMM{thousand.v=3}|ε{thousand.v=0}hundred->smallHundred{hundred.v=smallHundred.v}|CD{hundred.v=smallHundred.v}|DsmallHundred{hundred.v=5+smallHundred.v}|CM{hundred.v=9}smallHundred->C{smallHundred.v=1}|CC{smallHundred.v=2}|CCC{smallHundred.v=3}|ε{hundred.v=0}ten->smallTen{ten.v=smallTen.v}|XL{ten.v=4}|LsmallTen{ten.v=5+smallTen.v}|XC{ten.v=9}smallTen->X{smallTen.v=1}|XX{smallTen.v=2}|XXX{smallTen.v=3}|ε{smallTen.v=0}digit->smallDigit{digit.v=smallDigit.v}|IV{digit.v=4}|VsmallDigit{digit.v=5+smallDigit.v}|IX{digit.v=9}smallDigit->I{smallDigit.v=1}|II{smallDigit.v=2}|III{smallDigit.v=3}|ε{smallDigit.v=0}2.3.5Constructasyntax-directedtranslationschemethattrans­latespostfixarithmeticexpressionsintoequivalentprefixarithmeticexpressions.answerproduction:expr->exprexprop|digittranslationscheme:expr->{print(op)}exprexprop|digit{print(digit)}ExercisesforSection2.42.4.1Constructrecursive-descentparsers,startingwiththefollow­inggrammars:S->+SS|-SS|aS->S(S)S|εS->0S1|01Answer1)S->+SS|-SS|avoidS(){switch(lookahead){case"+":match("+");S();S();break;case"-":match("-");S();S();break;case"a":match("a");break;default:thrownewSyntaxException();}}voidmatch(Terminalt){if(lookahead=t){lookahead=nextTerminal();}else{thrownewSyntaxException()}}2)S->S(S)S|εvoidS(){if(lookahead=="("){S();match("(");S();match(")");S();}}3)S->0S1|01voidS(){switch(lookahead){case"0":match("0");S();match("1");break;case"1"://match(epsilon);break;default:thrownewSyntaxException();}}ExercisesforSection2.62.6.1ExtendthelexicalanalyzerinSection2.6.5toremovecom­ments,definedasfollows:Acommentbeginswith//andincludesallcharactersuntiltheendofthatline.Acommentbeginswith/*andincludesallcharactersthroughthenextoccurrenceofthecharactersequence*/.2.6.2ExtendthelexicalanalyzerinSection2.6.5torecognizetherelationaloperators.2.6.3ExtendthelexicalanalyzerinSection2.6.5torecognizefloat­ingpointnumberssuchas2.,3.14,and.5.AnswerSourcecode:commit8dd1a9aCodesnippet(src/lexer/Lexer.java):publicTokenscan()throwsIOException,SyntaxException{for(;;peek=(char)stream.read()){if(peek==''||peek=='\t'){continue;}elseif(peek=='\n'){line=line+1;}else{break;}}//handlecommentif(peek=='/'){peek=(char)stream.read();if(peek=='/'){//singlelinecommentfor(;;peek=(char)stream.read()){if(peek=='\n'){break;}}}elseif(peek=='*'){//blockcommentcharprevPeek='';for(;;prevPeek=peek,peek=(char)stream.read()){if(prevPeek=='*'&&peek=='/'){break;}}}else{thrownewSyntaxException();}}//handlerelationsignif("".indexOf(peek)>-1){StringBufferb=newStringBuffer();b.append(peek);peek=(char)stream.read();if(peek=='='){b.append(peek);}returnnewRel(b.toString());}//handlenumber,notypesensitiveif(Character.isDigit(peek)||peek=='.'){BooleanisDotExist=false;StringBufferb=newStringBuffer();do{if(peek=='.'){isDotExist=true;}b.append(peek);peek=(char)stream.read();}while(isDotExist==true?Character.isDigit(peek):Character.isDigit(peek)||peek=='.');returnnewNum(newFloat(b.toString()));}//handlewordif(Character.isLetter(peek)){StringBufferb=newStringBuffer();do{b.append(peek);peek=(char)stream.read();}while(Character.isLetterOrDigit(peek));Strings=b.toString();Wordw=words.get(s);if(w==null){w=newWord(Tag.ID,s);words.put(s,w);}returnw;}Tokent=newToken(peek);peek='';returnt;}ExercisesforSection2.82.8.1For-statementsinCandJavahavetheform:for(exprl;expr2;expr3)stmtThefirstexpressionisexecutedbeforetheloop;itistypicallyusedforinitializ­ingtheloopindex.Thesecondexpressionisatestmadebeforeeachiterationoftheloop;theloopisexitediftheexpressionbecomesO.Theloopitselfcanbethoughtofasthestatement{stmtexpr3;}.Thethirdexpressionisexecutedattheendofeachiteration;itistypicallyusedtoincrementtheloopindex.Themeaningofthefor-statementissimilartoexpr1;while(expr2){stmtexpr3;}DefineaclassForforfor-statements,similartoclassIfinFig.2.43.AnswerclassForextendsStmt{ExprE1;ExprE2;ExprE3;StmtS;publicFor(Exprexpr1,Exprexpr2,Exprexpr3,Stmtstmt){E1=expr1;E2=expr2;E3=expr3;S=stmt;}publicvoidgen(){E1.gen();Labelstart=newLable();Lalelend=newLable();emit("ifFalse"+E2.rvalue().toString()+"goto"+end);S.gen();E3.gen();emit("goto"+start);emit(end+":")}}2.8.2TheprogramminglanguageCdoesnothaveabooleantype.ShowhowaCcompilermighttranslateanif-statementintothree-addresscode.AnswerReplaceemit("isFalse"+E.rvalue().toString()+"goto"+after);withemit("ifNotEqual"+E.rvalue().toString()+"0goto"+after);oremit("isNotEqualZero"+E.rvalue().toString()+"goto"+after);
2025/11/27 8:37:48 658KB 龙书答案 完整性高
1
【文档说明】本文是自己整理的一些重点知识点,也是面试中会被问到的知识点【文档结构】决策树信息熵(Entropy)什么是决策树决策树的构建过程决策树分割属性选择决策树量化纯度决策树量化纯度信息增益率计算方式决策树的停止条件决策树算法效果评估决策树生成算法ID3算法ID3算法优缺点C4.5算法8CART算法8ID3\C4.5\CART分类回归树算法总结分类树和回归树的区别决策树优化策略决策树的剪枝决策树剪枝过程附录:
2025/8/19 18:48:52 1.43MB 算法 TD_tree 决策时 KNN
1
本算法是ID3算法的matlab版本,配有测试数据
2025/7/10 2:21:24 1KB ID3算法
1
matlab实现ID3决策树代码,例程中使用西瓜数据集,运行decisionTree.m即可
2025/6/26 2:30:41 7KB matlab 模式识别 决策树
1
绝地求生:易语言全套加速器源码【更新日期:2018年1月10日】使用说明:用FTP将后台文件用二进制上传到网站根目录后安装网站安装好后登陆后台添加产品然后把激活码输入都源码里,产品ID,通信秘钥在后台添加产品后获取,并输入然后搜索HTTP把所有HTTP网址改成自己的域名即可后台设置公告(重要)不设置会导致软件无法启动格式为公告内容||网址添加游戏:在M文件夹中route就是存放游戏路由表的地方打开1||英雄联盟||http://域名/m/route/yxlm.txt||http://域名/m/route/1.png||0序号||游戏名称||游戏路由表地址||游戏LOGO地址||后面的0代表国内游戏,1则为国外游戏路由表格式参考:add1.207.140.0mask255.255.255.0IPMETRICdefaultIFIFZadd14.17.12.0mask255.255.255.0IPMETRICdefaultIFIFZadd14.17.13.0mask255.255.255.0IPMETRICdefaultIFIFZadd14.17.14.0mask255.255.255.0IPMETRICdefaultIFIFZ线路添加:网游加速的线路分别存放在这两个文件里面国内跟国外,格式为线路名称||IP||用户名||密码网络加速则存放在编写好节点文档后,采用附赠的加密工具加密后上传到网站根目录即可充值系统注:后台必须把充值卡位数改成10位以内,太多字符会导致充值出错!1.首先打开注册个账号http://www.1ka123.com/dashboard/index2.打开雷盾的后台获取客户ID3.地址高级=》在线售卡4.这里写上客户ID然后点编辑刷新5.获取提卡数据6.在易卡上面找到商品/分类的商品列表点编辑7.平台对接写雷盾对接密匙写提卡数据然后点保存这样子就可以实现无卡的时候自动补卡了软件对接:打开源码目录下的扫码支付模块按下列格式写入你的卡类信息比如我这里增加月卡修改完即可收款实现自动充值
1
ID3源代码
2025/3/24 17:54:30 165KB ID3
1
用python编写的决策树ID3算法,运用了Car-Evaluation的例子。
BUG较少,综合了网上的优秀代码,并进一步形成自己的代码。
代码基本有注释,风格良好,能够很快看懂。
内含有比较规范的报告文档,包含所有流程图,说明图,以及文档风格绝对不错,无需更改,建议下载!该算法所测试的数据集如下(已经打包在内,并已经生成xls格式,方便直接使用):已知:UCI标准数据集Car-Evaluation,定义了汽车性价比的4个类别;
求:用ID3算法建立Car-Evaluation的属性描述决策树Car-Evaluation训练数据集文件:1.car_databases.pdf2.car_evalution-databases.pdf
2025/2/7 20:05:03 1.17MB 决策树 ID3 Car-Evaluati 人工智能
1
使用matlab实现id3决策树算法,给出使用方法及举例,并附带相关测试集合。
2024/12/30 19:41:20 13KB 决策树 id3 matlab 源码
1
模式识别决策树编程运用matlab做的递归包括id3算法pep算法randomforest算法!
2024/10/3 6:08:44 284KB 决策树 递归
1
决策树ID3算法matlab源代码片段function[TreeRulesMatrix]=DecisionTree(DataSet,AttributName)%输入为训练集,为离散后的数字,如记录1:11321;
%前面为属性列,最后一列为类标ifnargin<1error('请输入数据集');elseifisstr(DataSet)[DataSetAttributValue]=readdata2(DataSet);elseAttributValue=[];end
2024/9/13 9:14:03 44KB ID3算法matlab源代码
1
共 40 条记录 首页 上一页 下一页 尾页
在日常工作中,钉钉打卡成了我生活中不可或缺的一部分。然而,有时候这个看似简单的任务却给我带来了不少烦恼。 每天早晚,我总是得牢记打开钉钉应用,点击"工作台",再找到"考勤打卡"进行签到。有时候因为工作忙碌,会忘记打卡,导致考勤异常,影响当月的工作评价。而且,由于我使用的是苹果手机,有时候系统更新后,钉钉的某些功能会出现异常,使得打卡变得更加麻烦。 另外,我的家人使用的是安卓手机,他们也经常抱怨钉钉打卡的繁琐。尤其是对于那些不太熟悉手机操作的长辈来说,每次打卡都是一次挑战。他们总是担心自己会操作失误,导致打卡失败。 为了解决这些烦恼,我开始思考是否可以通过编写一个全自动化脚本来实现钉钉打卡。经过一段时间的摸索和学习,我终于成功编写出了一个适用于苹果和安卓系统的钉钉打卡脚本。
2024-04-09 15:03 15KB 钉钉 钉钉打卡