140 likes | 226 Views
Final Project. Description. Enhance the compiler in HW3 PL/0 instead of a subset of PL/0 Procedures Procedure calls, including recursive calls Begin, End statement If statement While statement. Change to Symbol Table.
E N D
Description • Enhance the compiler in HW3 • PL/0 instead of a subset of PL/0 • Procedures • Procedure calls, including recursive calls • Begin, End statement • If statement • While statement
Change to Symbol Table • The symbol table must be expanded to include attributes which allows the compiler to associate literal numbers and addresses with each identifier declared • For example • Identifier of a constant – constant value • Identifier of a variable – address, L and M • Identifier of a procedure – entry address and level
Common Mistakes in HW3 • No driver • You are making a real compiler. The input is the source file. The output is the bytecode file. • Should not require the user to run scanner, parser, and then the virtual machine • No code generation • Code is generated in parser
Code Generation Example Expression { IF ((token == plus) || (token == minus)) { addop = token; /*delaying the transmission of the arithmetic operator*/ GetToken; Term; IF (addop == minus) emit (opr, 0, 1 ) ; } ELSE Term; WHILE ((token == plus) || (token == minus)) { addop = token; GetToken; Term ; IF (addop == plus) emit (opr, 0, 2 ); /* addition */ ELSE emit (opr, 0, 3 ); /* substraction */ } } “The procedure emit(opcode, level, modifier) generates code” a := -5 a := b+c-7
Sources to Help You • In the following URL you will find the original PL/0 compiler written by Niklaus Wirth in Pascal: http://www.246.dk/pl0.html • In this URL: http://www.cosc.canterbury.ac.nz/tad.takaoka/cosc230/p2.c you will find a PL/0 compiler in C. • Do not copy. We will check.
Code Example – Main() Scan, Parse, and Code Generation main(argc,argv) int argc; char *argv[]; { … getsym(); block(0,0); interpret(); … } Code Execution
Code Example – Block() block(lev, tx) { … do { if (sym==constsym) { … } if (sym==varsym) { … } while(sym==procsym) { … } }while ((sym==constsym)||(sym==varsym)||(sym==procsym)); statement(lev,&tx); gen(opr,0,0); }
Code Example – Becomes Statement statement(lev,ptx) int lev, *ptx; { int i, cx1,cx2; if (sym==ident){ i=position(id,ptx); if(i==0) error(11); else if (table[i].kind!=variable) { error(12); i=0; }; getsym(); if (sym==becomes) getsym(); else error(13); expression(lev,ptx); if (i!=0) gen(sto, lev-table[i].level, table[i].adr); } else a:=5+b
Code Example – Call Statement if (sym==callsym) { getsym(); if (sym!=ident) error(14); else { i=position(id, ptx); if(i==0) error(11); else if (table[i].kind==procedure) gen(cal,lev-table[i].level, table[i].adr); else error(15); getsym(); } } else call fact;
Code Example – If Statement if (sym==ifsym) { getsym(); condition(lev,ptx); if (sym==thensym) getsym(); else error(16); cx1=cx; gen(jpc,0,0); statement(lev, ptx); code[cx1].a=cx; } else if n = 0 then f := 1;
Code Example – Begin, End Statement if (sym==beginsym) { getsym(); statement(lev,ptx); while((sym==semicolon)||(sym==beginsym)|| (sym==ifsym)||(sym==whilesym)|| (sym==callsym)||(sym==writesym)) { if(sym==semicolon) getsym(); else error(10); statement(lev,ptx); } if (sym==endsym) getsym(); else error(17); } else begin ans1:=n; n:= n-1; if n = 0 then f := 1; if n > 0 then call fact; f:=f*ans1; end;
Code Example – While Statement if (sym==whilesym) { cx1=cx; getsym(); condition(lev,ptx); cx2=cx; gen(jpc,0,0); if (sym==dosym) getsym(); else error(18); statement(lev,ptx); gen(jmp,0,cx1); code[cx2].a=cx; } else WHILE a > c DO a++ ;
Code Example – Write Statement if (sym==writesym) { getsym(); if (sym!=lparen) error(98); else { getsym(); expression(lev, ptx); gen(wrt,0,0); if (sym!=rparen) error(99); else getsym(); } } }