COP 3402 Systems Software Recitation Notes November 9
COP 3402 Systems Software Recitation Notes November 9 th , 2007
Recap of Compilation Process • Input is a file containing source code. • A compiler has several stages – Scanner (Lexical Analysis) • Reads in source program and generates a stream of tokens – Parser (Syntax Analysis) • Performs syntax analysis on the token stream according to grammar rules and generates parse trees.
Recap Cont’d • Compiler has other functions too. • Creates and Maintains the symbol table – Listing of all identifiers (variable, constants, and procedures), along with their attributes. • Generates Assembly Code after performing syntax analysis.
Code Generation • Project 4. • You are required to extend your parser such that it generates assembly code for the PL/0 machine. • Due just before thanksgiving.
Code Generation cont’d • Recall that each instruction for the PL/0 has the following format: <Opcode, L, M> • Opcode = Operation Code • L = Lexicographical level • M = Context sensitive – A number (INT, LIT) – A program address (JPC, JMP, CAL) – A data address (LOD, STO) – The identity of the operator OPR (OPR, 0, 2(ADD) or OPR, 0, 4(MUL))
Issues • How to determine Lexicographical level for each instruction • How to resolve complex arithmetic expressions involving identifiers/numbers.
EBNF for PL/0 <program> <block> <const-decl> <var-decl> <proc-decl> <statement> → → → <block>. <const-decl> <var-decl> <proc-decl> <statement> [const <ident> = number { , ident = number} ; ] [var <ident> {, <ident>} ; ] {procedure <ident> ; block ; } <statement> <ident> : = <expression> | call <ident> | begin <statement> { ; <statement> } end | if <condition> then <statement> |while <condition> do <statement> |e <condition> → odd <expression> | <expression> < relop> < expression> <relop> → = | < > | < | >= | <= <expression> → [<adding-operator> ] term {<adding-operator> term} <adding-operator> → +|<term> → <factor> {<multiplying-operator> <factor>} <multiplying-operator> → * | / <factor> → <ident> | <number> | ( <expression> ) <ident> → <letter> {<letter> | <digit>} <number> → <digit> { <digit> } <letter> → { a | b |. . . | y | z } <digit> → {0|1|2|3|4|5|6|7|8|9}
Lexicographical Level • We define the program and its procedures in terms of blocks <program> → <block> → <const-decl> <var-decl> <proc-decl> <statement> <proc-decl> → {procedure <ident> ; block ; } <statement> • In our grammar, Lexicographical level is determined by block. Every time we recurse into a block, a new lexicographical level is defined.
Expressions • Expressions in our grammar are defined as following <expression> → <adding-operator> → <term> → <multiplying-operator> → <factor> → <ident> → [<adding-operator> ] term {<adding-operator> term} +|<factor> {<multiplying-operator> <factor>} *|/ <ident> | <number> | ( <expression> ) <letter> {<letter> | <digit>} • It is possible to combine individual SIC instructions in a bottom-up manner to construct the expression.
Sample Code • http: //www. cosc. canterbury. ac. nz/tad. takaok a/cosc 230/p 1. c • This is a complete compiler for PL/0 written in C. – It reads in the token stream – Checks the syntax of the program – Generates the assembly code – Can also interpret the assembly code and generate output.
NOTE • The link is provided as a reference only. • DO NOT SUBMIT THE CODE FROM THE LINK AS YOUR PROJECT. • You will be given a ZERO grade if you do so. • The written document for the assignment will be uploaded over the weekend.
- Slides: 11