60 likes | 166 Views
Parsing and Symbol Table. if (sym==constsym) { getsym(); do { constdeclaration(lev,&tx,&dx); while(sym==comma) { getsym(); constdeclaration(lev,&tx,&dx); } if(sym==semicolon)getsym(); else error(5); } while (sym==ident); }
E N D
if (sym==constsym) { getsym(); do { constdeclaration(lev,&tx,&dx); while(sym==comma) { getsym(); constdeclaration(lev,&tx,&dx); } if(sym==semicolon)getsym(); else error(5); } while (sym==ident); } if (sym==varsym) { getsym(); do { vardeclaration(lev,&tx,&dx); while (sym==comma) { getsym(); vardeclaration(lev,&tx,&dx); } if(sym==semicolon) getsym(); else error(5); } while(sym==ident); } const N = 100, k = 6; M = 20; var a,b; c;
constdeclaration(lev,ptx,pdx) int lev,*ptx,*pdx; { if (sym==ident) { getsym(); if ((sym==eql) || (sym==becomes)) { if (sym==becomes) error(1); getsym(); if (sym==number) {enter(constant,ptx,pdx,lev); getsym();} } } } const N = 100, k = 6; M = 20;
while(sym==procsym) { getsym(); if(sym==ident){ enter(procedure,&tx,&dx,lev); getsym(); } else error(4); if (sym==semicolon) getsym(); else if (sym==lparen); else error(5); block(lev+1, tx); if(sym==semicolon) { getsym(); } else error(5); }
vardeclaration(lev,ptx,pdx) int *ptx,*pdx,lev; { if (sym==ident) {enter(variable, ptx,pdx,lev); getsym();} else error(4); } var a,b; c;
struct namerecord{ int kind; char name[10]; int val; int level; int adr; int indirect; }; enter(k,ptx,pdx,lev) int k; int *ptx; int *pdx;int lev; { char *id1; int ii,len; (*ptx)++; id1=id; len=strlen(id); for (ii=0;ii<=len;ii++){table[*ptx].name[ii]=*id1; id1++;} table[*ptx].kind=k; if (k==constant) table[*ptx].val=num; else if (k==variable) { table[*ptx].level=lev; table[*ptx].adr=*pdx; table[*ptx].indirect=flag; (*pdx)++; } else /* procedure */ { table[*ptx].level=lev;}} struct namerecord table[1000];