cs160-lec1

cs160-lec1 - CMPSC 160 Translation of Programming Languages...

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

View Full Document Right Arrow Icon
1 CMPSC 160 Translation of Programming Languages Lecture 1: Overview of Compilers CMPSC 160 – Fall 2010 Instructor: Tevfik Bultan (bultan@cs.ucsb.edu) Office: HFH 2123 Office Hours: TBD Teaching Assistant: Yutian Sun (sun@cs.ucsb.edu) Office Hours: TBD Information about the class will be posted on the class web page http://www.cs.ucsb.edu/~cs160/index.html Class mailing list: cs160@cs.ucsb.edu Messages sent to this address will go to the instructor, the TA and ALL the students in the class If you have questions that you do not want to share with other students send e-mail to bultan@cs.ucsb.edu and cc sun@cs.ucsb.edu
Background image of page 1

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

View Full DocumentRight Arrow Icon
2 Compilers Last lecture you discussed the following: Why do we need a compiler? Today, we will discuss the following: A) What are the steps of compilation? B) What is the architecture of the compiler? Compiler - Example Source code Written in a high-level programming language //simple example while (sum < total) { sum = sum + x*10; } Target code Assembly language, which in turn is translated to machine code L1: MOV total,R0 CMP sum,R0 JL L2 GOTO L3 L2: MOV #10,R0 MUL x,R0 ADD sum,R0 MOV R0,sum GOTO L1 L3: first instruction following the while statement
Background image of page 2
3 What is the Input? Input to the compiler is not //simple example while (sum < total) { sum = sum + x*10; } Input to the compiler is //simple\bexample\nwhile\b(sum\b<\btotal)\b{\n\tsum\b= \bsum\b+\bx*10;\n}\n How does the compiler recognize the keywords, identifiers, the structure, etc.? First Step: Lexical Analysis (Scanning) The compiler scans the input file and produces a stream of tokens WHILE,LPAREN,<ID,sum>,LT,<ID,total>,RPAREN,LBRACE, <ID,sum>,EQ,<ID,sum>,PLUS,<ID,x>,TIMES,<NUM,10>, SEMICOL,RBRACE Each token has a corresponding lexeme , the character string that corresponds to the token For example, “ while ” is the lexeme for token WHILE sum ”, “ x ”, “ total ” are lexemes for token ID
Background image of page 3

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

View Full DocumentRight Arrow Icon
Lexical Analysis (Scanning) Compiler uses a set of patterns to specify valid tokens tokens: LPAREN, ID, NUM, WHILE, etc. Each pattern is specified as a regular expression LPAREN should match: ( WHILE should match: while ID should match: [a-zA-Z][0-9a-zA-Z]* It uses finite automata to recognize these patterns a-zA-Z 0-9a-zA-Z ID automaton Lexical Analysis (Scanning) During the scan the lexical analyzer gets rid of the white space ( \b,\t,\n, etc.) and comments Important additional task: Error messages! – Var%1 Error! Not a token! – whle Error? It matches the identifier token. Natural language analogy: Tokens correspond to words and
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 17

cs160-lec1 - CMPSC 160 Translation of Programming Languages...

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

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