int main(int argc, char **argv){ if (argc > 1) { yyin = fopen(argv[1], "r"); } else{ char FILENAME[30]; printf("Please enter the file name:\n"); scanf("%s",FILENAME); if( (yyin = fopen(FILENAME,"r")) == NULL){ printf("Can not open the file!\n"); return 1; } } init(); my_yylloc.first_line = 1; if (yyparse() == 0){ printf("\n\n中间代码\n\n"); listcode(); int i = tx; printf("\n\n符号表\n\n"); for(;i > 0 ; i--) printf("%s %d %d\n",table[i].name,table[i].kind,table[i].val); //interpret(); } }
int main() { long i; globalinit(); printf("please input source program file name: "); //scanf("%s",infilename); printf("\n"); //if((infile=fopen(infilename,"r"))==NULL) if((infile=fopen("test1.pl0","r"))==NULL) { printf("File %s can't be opened.\n", infilename); exit(1); } getsym(); block(declbegsys|statbegsys|period); if(sym!=period) { error(9); } fclose(infile); if(err==0) { listcode(0); interpret(); } else { printf("%3ld errors in PL/0 program\n",err); } return (0); }
void block(unsigned long fsys) { long tx0; // initial table index long cx0; // initial code index long tx1; // save current table index before processing nested procedures long dx1; // save data allocation index dx = 3; tx0 = tx; table[tx].addr = cx; gen(jmp, 0, 0); if (lev > levmax) { error(32); } do { if (sym == constsym) { getsym(); do { constdeclaration(); while (sym == comma) { getsym(); constdeclaration(); } if (sym == semicolon) { getsym(); } else { error(5); } } while (sym == ident); } if (sym == varsym) { getsym(); do { vardeclaration(); while (sym == comma) { getsym(); vardeclaration(); } if (sym == semicolon) { getsym(); } else { error(5); } } while (sym == ident); } while (sym == procsym) { getsym(); if (sym == ident) { enter(proc); getsym(); } else { error(4); } if (sym == semicolon) { getsym(); } else { error(5); } lev = lev + 1; tx1 = tx; dx1 = dx; block(fsys | semicolon); lev = lev - 1; tx = tx1; dx = dx1; if (sym == semicolon) { getsym(); //printf("this sym is %d\n",sym); test(statbegsys | ident | procsym, fsys, 6); } else { error(5); } } test(statbegsys | ident, declbegsys, 7); } while (sym&declbegsys); code[table[tx0].addr].a = cx; table[tx0].addr = cx; // start addr of code cx0 = cx; gen(Int, 0, dx); statement(fsys | semicolon | endsym); gen(opr, 0, 0); // return test(fsys, 0, 8); listcode(cx0); }