资源描述:
《词法分析程序(C语言编写,针对PL_0语言)》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、#include#include#include#defineNORW13/*ofreservedwords*/#defineTXMAX100/*lengthofidentifiertable*/#defineNMAX14/*maxnumberofdigitsinnumbers*/#defineAL10/*lengthofidentifiers*/#defineAMAX2047/*maxinumaddress*/#defineLEVMAX3/*maxdeptho
2、fblocknesting*/#defineCXMAX200/*sizeofcodearray*/#defineSTACKSIZE500char*symbol[32]={"nul","ident","number","plus","minus","times","slash","oddsym","eql","neq","lss","leq","gtr","geq","lparen","rparen","comma","semicolon","period","becomes","beginsym","endsym","
3、ifsym","thensym","whilesym","writesym","readsym","dosym","callsym","constsym","varsym","procsym"};/*typeofsymbols*/char*word[NORW]={"begin","call","const","do","end","if","odd","procedure","read","then","var","while","write"};/*tableofreservedwords*/char*wsym[NO
4、RW]={"beginsym","callsym","constsym","dosym","endsym","ifsym","oddsym","procsym","readsym","thensym","varsym","whilesym","writesym"};char*mnemonic[8]={"lit","opr","lod","sto","cal","ini","jmp","jpc"};charch;/*lastcharread*/charid[AL+1];/*lastidentifierread*/char
5、sym[10];/*lastsymbolread*/charline[81];chara[AL+1],fname[AL+1];enumobject{constant,variable,procedur};enumobjectkind;enumfct{lit,opr,lod,sto,cal,ini,jmp,jpc};enumlistswitcher{false,true};/*truesetlistobjectcode*/enumlistswitcherlistswitch;FILE*fa;FILE*fa1,*fa2;F
6、ILE*fin,*fout;intnum;/*lastnumberread*/intcc;/*charactercount*/intll;/*linelength*/intcx;/*codeallocationindex*/interr;intlev=0,tx=0,dx=3;intlinecnt=0;structinstruction{enumfctf;/*functioncode*/intl;/*level*/inta;/*displacementaddr*/};/*lit0,a:loadconstantaopr0,
7、a:executeopralodl,a:loadvariable1,astol,a:storevariable1,acall,a:callprocedureaatlevel1int0,a:incrementt-registerbyajmp0,a:jumptoajpc0,a:jumpconditionaltoa*/structinstructioncode[CXMAX+1];structtable1{charname[AL+1];enumobjectkind;intval,level,adr,size;};structt
8、able1table[TXMAX+1];structnode{char*pa[32];}*declbegsys,*statbegsys,*facbegsys,*tempsetsys;intin(str,set)char*str;structnode*set;{inti=0;while(set->pa[i]!=NULL){if(st