Final Project - PowerPoint PPT Presentation

ian-stanley
final project n.
Skip this Video
Loading SlideShow in 5 Seconds..
Final Project PowerPoint Presentation
Download Presentation
Final Project

play fullscreen
1 / 14
Download Presentation
Final Project
70 Views
Download Presentation

Final Project

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. Final Project

  2. 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

  3. 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

  4. 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

  5. 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

  6. 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. 

  7. Code Example – Main() Scan, Parse, and Code Generation main(argc,argv) int argc; char *argv[]; { … getsym(); block(0,0); interpret(); … } Code Execution

  8. 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); }

  9. 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

  10. 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;

  11. 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;

  12. 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;

  13. 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++ ;

  14. 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(); } } }