lecture-02-6up

lecture-02-6up - Last time • What are compilers •...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Last time • What are compilers? • Phases of a compiler Scanner Parser Semantic Routines Optimizer Code Generation Source code Tokens Syntax tree IR IR Executable Sunday, August 29, 2010 Extra: Front-end vs. Back-end • Scanner + Parser + Semantic actions + (high level) optimizations called the front-end of a compiler • IR-level optimizations and code generation (instruction selection, scheduling, register allocation) called the back-end of a compiler • Can build multiple front-ends for a particular back-end • e.g., gcc & g++, or many front-ends which generate CIL • Can build multiple back-ends for a particular front-end • e.g. , gcc allows targeting different architectures Scanner Parser Semantic Routines Optimizer Code Generation Source code Tokens Syntax tree IR IR Executable front end back end Sunday, August 29, 2010 The MICRO compiler: a simple example Sunday, August 29, 2010 High level structure • Single pass compiler: no intermediate representation • Scanner tokenizes input stream, but is called by parser on demand • As parser recognizes constructs, it invokes semantic routines • Semantic routines build symbol table on-the-¡y and directly generate code for a 3-address virtual machine Sunday, August 29, 2010 The Micro language • Tokens are de¢ned by regular expressions • Tokens: BEGIN, END, READ, WRITE, ID, LITERAL, LPAREN, RPAREN, SEMICOLON, COMMA, ASSIGN_OP, PLUS_OP, MINUS_OP, SCANEOF • Implicit identi¢er declaration (no need to predeclare variables): ID = [A-Z][A-Z0-9]* • Literals (numbers): LITERAL = [0-9]+ • Comments (not passed on as tokens): --(Not(\n))*\n • Program: • BEGIN {statements} END Sunday, August 29, 2010 The Micro language • One data type—all ID s are integers • Statement: ID := EXPR • Expressions are simple arithmetic expressions which can contain identi¢ers • Note: no unary minus • Input/output READ( ID , ID , ...) WRITE( EXPR , EXPR , ...) Sunday, August 29, 2010 Scanner • Scanner algorithm provided in book (pages 28/29) • What the scanner can identify corresponds to what the Fnite automaton for a regular expression can accept • IdentiFes the next token in the input stream • Read a token (process Fnite automaton until accept state found) • Identify its type (determine which accept state the ¡A is in) • Return type and “value” ( e.g. , type = LITERAL , value = 5) Sunday, August 29, 2010 Recognizing tokens • Skip spaces • If the Frst non-space character is: • letter : read until non-alphanumeric. Check for reserved words (“begin,” “end”). Return reserved word or ( ID and variable name) • digit : read until non-digit. Return LITERAL and number • ( ) ; , +: return single character • : : next must be = . Return ASSIGN_OP •- : if next is also- skip to end of line, otherwise return MINUS_OP • “unget” the next character that had to be read to Fnd end of ID s, reserved words, literals and minus ops....
View Full Document

This note was uploaded on 02/19/2012 for the course ECE 468 taught by Professor Test during the Fall '08 term at Purdue.

Page1 / 7

lecture-02-6up - Last time • What are compilers •...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online