Welcome to CS 445 Compiler and Translator Design











- Slides: 11
Welcome to CS 445 Compiler and Translator Design Clinton Jeffery jeffery@uidaho. edu JEB 230
What’s a Compiler • Translates human equations to machine code • Originally done by a human • Compared with colloquial English: to pull together the sequence of machine instructions needed to perform a given computing task
Why Study Compilers? • Experience with large-scale applications development. • A shining triumph of CS theory • A basic element of language R&D • Ideas and tools useful in many applications
Tools We Use • C and “make” • Lex and Yacc • Gdb and valgrind • E-mail • Web, Blackboard
Kinds of Compilers • Native code • Virtual machine • JIT • Preprocessor, macro processor • Interpreter
Phases of a Compiler • Lexical analysis • Syntax Analysis • Semantic Analysis • Intermediate Code Generation • Optimization • Final Code Generation • Linking
Example • Compile: position = initial + rate * 60 • Intermediate: t 1 = inttoreal(60) • t 2 = id 3 * t 1 • t 3 = id 2 + t 2 • id 1 = t 3 • Final: MOVF id 3, R 2 • MULF #60. 0, R 2 • MOVF id 2, R 1 • ADDF R 2, R 1 • MOVF R 1, id 1
Overview of Lexical Analysis Read Sections 3 -5 of Lexical Analysis with Flex; read the textbook chapter on lexical analysis • “Lexical” means: pertaining to the words or vocabulary of a language • Maps individual symbols into such “words”, which we call “tokens” • Commonly called a scanner
Meanings of the word “token” • A single word from the source code • An integer code for the category a word belongs to • A set of lexical attributes that are computed from a single word of input • An instance of a class (given by category)
Traits of Scanners • They convert (1+) chars to tokens • Detect boundaries between tokens • Identify/categorize kinds of tokens • Discard comments and whitespace • Remember line/col #’s for error reporting • Report lexical errors • Run as fast as possible
Categorizing • From a language specification (written doc, usually semi-formal) • Identify all word categories • For each category, a set of characters and combination/sequence rules l (natural languages use dictionaries and surrounding context) • Often uses a popular formal notation with which you probably are familiar.