60 likes | 180 Vues
This document outlines the parsing process for handling constant and variable declarations within a programming language compiler. It details the implementation of a symbol table where constants, variables, and procedures are stored, along with their attributes such as type, level, and address. The code snippets demonstrate error handling, declaration syntax, and the structured way to manage identifiers using a record structure. The parsing process is a crucial component of compiler design, allowing for accurate compilation of source code.
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];