【VC++开源代码栏目提醒】:网学会员--在 VC++开源代码编辑为广大网友搜集整理了:源代码 - 技术总结绩等信息,祝愿广大网友取得需要的信息,参考学习。
源
代码:1、词法分析/cifa fenxi chengxu/include ltstdio.hgtinclude ltctype.hgtinclude ltalloc.hgtinclude ltstdlib.hgtinclude ltstring.hgtdefine NULL 0FILE fpchar cbufferchar key8quotifquotquotelsequotquotforquotquotwhilequotquotdoquotquotreturnquotquotbreakquotquotcontinuequotchar border6quotquotquotquotquotquotquotquotquotquotquotquotchar arithmetic4quotquotquot-quotquotquotquot/quotchar relation6quotltquotquotltquotquotquotquotgtquotquotgtquotquotltgtquotchar consts20char label20int constnum0labelnum0int searchchar searchcharint wordtype int i0 switch wordtype case 1:for i0ilt7i if strcmpkeyisearchchar0 returni1 case 2:for i0ilt5i if strcmpborderisearchchar0 returni1 return0 case 3:for i0ilt3i if strcmparithmeticisearchchar0 returni1 return0 case 4:for i0ilt5i if strcmprelationisearchchar0 returni1 return0 case 5:for i0iltconstnumi if strcmpconstsisearchchar0 returni1 constsi-1char mallocsizeofsearchchar strcpyconstsi-1searchchar constnum returni case 6:for i0iltlabelnumi if strcmplabelisearchchar0 returni1 labeli-1char mallocsizeofsearchchar strcpylabeli-1searchchar labelnum returni char alphaprocesschar buffer int atype int i-1 char alphatp20 while isalphabufferisdigitbuffer alphatpibuffer bufferfgetcfp alphatpi10 if atypesearchalphatp1 printfquots 1dnquotalphatpatype-1 else atypesearchalphatp6 printfquots 6dnquotalphatpatype-1 returnbufferchar digitprocesschar buffer int i-1 char digittp20 int dtype while isdigitbuffer digittpibuffer bufferfgetcfp digittpi10 dtypesearchdigittp5 printfquots 5dnquotdigittpdtype-1 returnbufferchar otherprocesschar buffer int i-1 char othertp20 int otypeotypetp othertp0buffer othertp10 if otypesearchothertp3 printfquots 3dnquotothertpotype-1 bufferfgetcfp goto out if otypesearchothertp4 bufferfgetcfp othertp1buffer othertp20 if otypetpsearchothertp4 printfquots 4dnquotothertpotypetp-1 goto out else othertp10 printfquots 4dnquotothertpotype-1 goto out if buffer: bufferfgetcfp if buffer printfquot: 22nquot bufferfgetcfp goto out else if otypesearchothertp2 printfquots 2dnquotothertpotype-1 bufferfgetcfp goto out if buffernampampbuffer printfquotc errornot a wordnquotbuffer bufferfgetcfpout: returnbuffervoid main int i for i0ilt20i labeliNULL constsiNULL if fpfopenquotexample.cquotquotrquotNULL printfquoterrorquot elsecbuffer fgetcfpwhile cbufferEOF if isalphacbuffer cbufferalphaprocesscbuffer else if isdigitcbuffer cbufferdigitprocesscbuffer else cbufferotherprocesscbuffer printfquotovernquot getchar 2、递归向下分析include ltstdio.hgtincludeltdos.hgtincludeltstdlib.hgtincludeltstring.hgtchar a50 b50d200e10char chint n1i10flag1n5int Eint E1int Tint Gint Sint Fvoid inputvoid input1void outputvoid main /递归分析/int fpj0char x d0E d1 d2gt d3T d4G d5printfquot请输入字符串长度lt50以号结束)nquotdoscanfquotcquotampchajchjwhilechn1jchb0a0printfquot文法t分析串tt分析字符t剩余串nquotfE1if f0 returnif ch printfquotacceptnquot p0xdpwhilex printfquotcquotxpp1xdp /输出推导式/else printfquoterrornquotprintfquot回车返回nquotgetchargetchar return printfquotnquotprintfquot回车返回nquotgetchargetcharint E1 int ftprintfquotE??TGtquot flag1inputinput1fTif f0 return0tGif t0 return0else return1int E int ftprintfquotE??TGtquot e0Ee1e2gte3Te4Ge5outputflag1inputinput1fTif f0 return0tGif t0 return0else return1int T int ftprintfquotT??FStquote0Te1e2gte3Fe4Se5 outputflag1inputinput1fFif f0 return0tS if t0 return0else return1int G int fifch bi1chprintfquotG??TGtquot e0Ge1e2gte3e4Te5Ge6output flag0inputinput1chai1fTif f0 return0Greturn1printfquotG??tquote0Ge1e2gte3e4outputflag1 inputinput1return1int Sint ftifch bi1chprintfquotS??FStquot e0Se1e2gte3e4Fe5Se6outputflag0inputinput1chai1fFif f0 return0tSif t0 return0else return1printfquotS??tquote0Se1e2gte3e4outputflag1 ai1chinputinput1return1int F int fifch bi1chprintfquotF??Etquote0Fe1e2gte3e4Ee5e6outputflag0inputinput1chai1fEif f0 return0ifch bi1chprintfquotF??Etquot flag0inputinput1 chai1 else printfquoterrornquot return0 else ifchi bi1chprintfquotF??itquote0Fe1e2gte3ie4outputflag0inputinput1chai1 else printfquoterrornquotreturn0 return1void input int j0 for jlti1-flagj printfquotcquotbj /输出分析串/ printfquotttquot printfquotcttquotch /输出分析字符/ void input1 int j for ji11-flagjltn1j printfquotcquotaj /输出剩余字符/ printfquotnquotvoid output /推导式计算/int mkjq int i0m0k0q0 indndn1gtdn2nn2inii-2whiledigtampampi0 ii-1ii1whiledie0 ii1qimqkqwhiledmgt mm-1mm1whilemq dndmmm1nn1dn forj3ejjdnejnn1kk1whiledk dndknn1kk1dn3、LL(1)/LL1分析法源程序,只能在
VC中运行 /includeltstdio.hgtincludeltstdlib.hgtincludeltstring.hgtincludeltdos.hgtchar A20/分析栈/char B20/剩余串/char v120i/终结符 /char v220EGTSF/非终结符 /int j0b0top0l/L为输入串长度 /typedef struct type/产生式类型定义 /char origin/大写字符 /char array5/产生式右边字符 /int length/字符个数 /typetype etgg1ss1ff1/结构体变量 /type C1010/预测分析表 /void print/输出分析栈 /int a/指针/fora0alttop1aprintfquotcquotAaprintfquotttquot/print/void print1/输出剩余串/int jforj0jltbj/输出对齐符/printfquot quotforjbjltljprintfquotcquotBjprintfquottttquot/print1/void mainint mnk0flag0finish0char chxtype cha/用来接受Cmn//把文法产生式赋值结构体/e.originEstrcpye.arrayquotTGquote.length2t.originTstrcpyt.arrayquotFSquott.length2g.originGstrcpyg.arrayquotTGquotg.length3g1.originGg1.array0g1.length1 s.originSstrcpys.arrayquotFSquots.length3s1.originSs1.array0s1.length1f.originFstrcpyf.arrayquotEquotf.length3f1.originFf1.array0if1.length1form0mlt4m/初始化分析表/forn0nlt5nCmn.originN/全部赋为空/ /填充分析表/ C00eC03e C11gC14g1C15g1 C20tC23t C31s1C32sC34C35s1 C40f1C43f printfquot提示:本程序只能对由i构成的以结束的字符串进行分析nquot printfquot请输入要分析的字符串:quot do/读入分析串/ scanfquotcquotampch if chi ampampch ampampchampampchampampchampampch printfquot输入串中有非法字符nquot exit1 Bjch j whilech lj/分析串长度/ chB0/当前分析字符/ Atop AtopE/E进栈/ printfquot步骤tt分析栈 tt剩余字符 tt所用产生式 nquot do xAtop--/x为当前栈顶字符/ printfquotdquotk printfquotttquot forj0jlt5j/判断是否为终结符/ ifxv1j flag1 break ifflag1/如果是终结符/ ifx finish1/结束标记/ printfquotaccnquot/接受 / getchar getchar exit1 /if/ ifxch print print1 printfquotc匹配nquotch chBb/下一个输入字符/ flag0/恢复标记/ /if/ else/出错处理/ print print1 printfquotc出错nquotch/输出出错终结符/ exit1 /else/ /if/ else/非终结符处理/ forj0jlt4jifxv2jmj/行号/break forj0jlt5jifchv1jnj/列号/break chaCmn ifcha.originN/判断是否为空/ printprint1printfquotc-gtquotcha.origin/输出产生式/forj0jltcha.lengthj printfquotcquotcha.arrayjprintfquotnquotforjcha.length-1j 0j--/产生式逆序入栈/Atopcha.arrayjifAtop/为空则不进栈/top--/if/else/出错处理/printprint1printfquotc出错nquotx/输出出错非终结符/exit1/else//else/ whilefinish0/main/4、LL(2)/LL1分析法源程序,只能在
VC中运行 /includeltstdio.hgtincludeltstdlib.hgtincludeltstring.hgtincludeltdos.hgtchar A20/分析栈/char B20/剩余串/char v120i/终结符 /char v220EGTSF/非终结符 /int j0b0top0l/L为输入串长度 /typedef struct type/产生式类型定义 /char origin/大写字符 /char array5/产生式右边字符 /int length/字符个数 /typetype etgg1ss1ff1/结构体变量 /type C1010/预测分析表 /void print/输出分析栈 /int a/指针/fora0alttopaprintfquotcquotAaprintfquotttquot/print/void print1/输出剩余串/int jforj0jltbj/输出对齐符/printfquot quotforjbjltljprintfquotcquotBjprintfquottttquot/print1/void mainint mnk0flag0char chxtype cha/用来接受Cmn//把文法产生式赋值结构体/e.originEstrcpye.arrayquotTGquote.length2t.originTstrcpyt.arrayquotFSquott.length2g.originGstrcpyg.arrayquotTGquotg.length3g1.originGg1.array0g1.length1 s.originSstrcpys.arrayquotFSquots.length3s1.originSs1.array0s1.length1f.originFstrcpyf.arrayquotEquotf.length3f1.originFf1.array0if1.length1form0mlt4m/初始化分析表/forn0nlt5nCmn.originN/全部赋为空/ /填充分析表/ C00eC03e C11gC14g1C15g1 C20tC23t C31s1C32sC34C35s1 C40f1C43f printfquot提示:本程序只能对由i构成的以结束的字符串进行分析nquot printfquot请输入要分析的字符串:quot do/读入分析串/ scanfquotcquotampch if chi ampampch ampampchampampchampampchampampch printfquot输入串中有非法字符nquot exit1 Bjch j whilech lj/分析串长度/ chB0/当前分析字符/ AtopAtopE/E进栈/ xAtop/x为当前栈顶字符/ printfquot步骤tt分析栈 tt剩余字符 tt所用产生式 nquotloop: printfquotdquotk printfquotttquot forj0jlt5j/判断是否为终结符/ ifxv1j flag1 ifflag1/如果是终结符/ ifx printfquotaccnquot/接受 / getchar getchar exit1 /if/ ifxch print print1 printfquotc匹配nquotch top--/栈顶出栈/ chBb/下一个输入字符/ xAtop flag0/恢复标记/ goto loop /if/ else/出错处理/ print print1 printfquotc出错nquotch/输出出错终结符/ exit0 /else/ /if/else/非终结符处理/forj0jlt4jifxv2jmj/行号/forj0jlt5jifchv1jnj/列号/chaCmnifcha.originN/判断是否为空/printprint1printfquotc- quotcha.origin/输出产生式/forj0jltcha.lengthj printfquotcquotcha.arrayjprintfquotnquottop--forjcha.length-1j 0j--/产生式逆序入栈/Atopcha.arrayjifAtop/为空则不进栈/top--xAtop goto loop/if/else/出错处理/printprint1printfquotc出错nquotx/输出出错非终结符/exit0/else//else//main/5、逆波兰式includeltstdio.hgtincludeltmath.hgtdefine max 100char exmax /存储后缀表达式/void trans /将算术表达式转化为后缀表达式/char strmax /存储原算术表达式/char stackmax /作为栈使用/char chint sumijttop0printfquotnquotprintfquot输入一个求值的表达式,以结束。
nquotprintfquotnquotprintfquot算数表达式:quoti0 /获取用户输入的表达式/doiscanfquotcquotampstriwhilestri ampamp imax sumit1i1chstriiwhilechswitchchcase : /判定为左括号/topstacktopch break case : /判定为右括号/whilestacktop extstacktoptop--ttop--break case : /判定为加减号/case -: whiletop0ampampstacktopextstacktoptop--ttopstacktopchbreakcase : /判定为乘除号/ case /:whilestacktopstacktop/extstacktoptop--ttopstacktopchbreakcase :breakdefault:whilechgt0ampampchlt9 /判定为数字/extchtchstriii--exttchstriiwhiletop0extstacktopttop--extprintfquotnt原来表达式:quotforj1jltsumjprintfquotcquotstrj printfquotnt后缀表达式:quotexforj1jlttjprintfquotcquotexjvoid compvalue /计算后缀表达式的值/float stackmaxd /作为栈使用/char chint t1top0 /t为ex下标,top为stack下标/chextt whilechswitchch case : stacktop-1stacktop-1stacktop top-- break case -: stacktop-1stacktop-1-stacktop top-- break case : stacktop-1stacktop-1stacktop top-- break case /: ifstacktop0 stacktop-1stacktop-1/stacktop else printfquotnt除零错误nquot exit0 /异常退出/ top-- break default: d0 whilechgt0ampampchlt9 d10dch-0 /将数字字符转化为对应的数值/ chextt top stacktopdchexttprintfquotnt计算结果:gnquotstacktopmaintranscompvalue6、LR1includeltstdio.hgtincludeltstring.hgtchar action103quotS3quotquotS4quotNULL /ACTION表/ NULLNULLquotaccquot quotS6quotquotS7quotNULL quotS3quotquotS4quotNULL quotr3quotquotr3quotNULL NULLNULLquotr1quot quotS6quotquotS7quotNULL NULLNULLquotr3quot quotr2quotquotr2quotNULL NULLNULLquotr2quotint goto110212 /QOTO表/ 00 05 08 00 00 09 00 00 00char vt3ab /存放非终结符/char vn2SB /存放终结符/char LR4quotE-gtSquotquotS-gtBBquotquotB-gtaBquotquotB-gtbquot/存放产生式/int a10char b10c10c1int top1top2top3topmnvoid mainint ghijklpyzcountchar xcopy10copy110top10top20top30top0a00ya0b0count0z0printfquot请输入表达式nquotdoscanfquotcquotampc1ctop3c1top3top31whilec1printfquot步骤t状态栈tt符号栈tt输入串ttACTIONtGOTOnquotdoyzm0n0 /yz指向状态栈栈顶/gtopj0k0xctopcountprintfquotdtquotcountwhilemlttop1 /输出状态栈/printfquotdquotam mm1printfquotttquotwhilenlttop2 /输出符号栈/printfquotcquotbn nn1printfquotttquotwhileglttop3 /输出输入串/printfquotcquotcg gg1printfquottt.
上一篇:
VC++编程技术600个大型项目源码(一)
下一篇:
数字图像的可逆数字水印研究