




























































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An overview of various topics in compiler design and programming languages, including pascal, lexical analysis, syntax analysis, code generation, optimization, and register management. It covers concepts such as regular grammars, hand-written lexical analyzers, context-free grammars, operator precedence, recursive descent parsing, shift-reduce parsing, intermediate code, symbol tables, code generation from trees, register assignment, array references, subroutine calls, optimization techniques like constant folding and partial evaluation, and types as tree structures.
Typology: Study notes
1 / 371
This page cannot be seen from the preview
Don't miss anything!





























































































CS 375, Compilers: Class Notes
Gordon S. Novak Jr. Department of Computer Sciences University of Texas at Austin [email protected] http://www.cs.utexas.edu/users/novak
Copyright c© Gordon S. Novak Jr.^1
(^1) A few slides reproduce figures from Aho, Lam, Sethi, and Ullman, Compilers: Principles, Techniques, and Tools, Addison-Wesley; these have footnote credits.
I wish to preach not the doctrine of ignoble ease, but the doctrine of the strenuous life.
Innovation requires Austin, Texas. We need faster chips and great compilers. Both those things are from Austin.
Pascal Test Program
{ program 4.9 from Jensen & Wirth: graph1.pas }
program graph1(output); const d = 0.0625; {1/16, 16 lines for [x,x+1]} s = 32; {32 character widths for [y,y+1]} h = 34; {character position of x-axis} c = 6.28318; {2pi} lim = 32; var x,y : real; i,n : integer; begin for i := 0 to lim do begin x := di; y := exp(-x)sin(cx); n := round(sy) + h; repeat write(’ ’); n := n- until n=0; writeln(’’) end end.
calling graph
Introduction
Assembly Language
Assembly Language is much easier to program in than Machine Language:
However, it still is fairly difficult to use:
High-Level Language
Sequential Phases of a Compiler^3
Input is a source program.
We may think of this as an analysis process followed by synthesis of the output.
These two modules are active throughout the compilation process:
(^3) This slide adapted from one by John Werth.
Data Flow through the Compiler
Source Program I/O V IF I>J THEN K := 0 Line Handler Chars V IF I>J THEN K := 0 Lexical Analyzer | Tokens | Res Id Op Id Res Id Op Num | IF I > J THEN K := 0 V Syntax Analyzer | | IF | /
Trees | > := | / \ /
| I J K 0 V Code Generator | LDA I | CMP J Code | BLE L | LDAI 0 | STA K V L17:
Lexical Analyzer
The Lexical Analyzer (or Lexer) will convert characters into “words” or tokens, such as:
The Lexical Analyzer may be called as a subroutine such as gettoken() to get the next token from the input string. It, in turn, calls the Line Handler routines.
The Lexical Analyzer returns a token data structure, consisting of:
Syntactic Analyzer
The Syntactic Analyzer (or Parser) will analyze groups of related tokens (“words”) that form larger constructs (“sentences”) such as arithmetic expressions and statements:
It will convert the linear string of tokens into structured representations such as expression trees and program flow graphs.
Lexical Analysis
If speed is needed, the Line Handler and Lexical Analyzer can be coded in assembly language.
The Lexical Analyzer does the following:
Character Classes
At the lowest level of grammar, there is a need to classify characters into classes. This can be done by lookup in an array indexed by the character code. Typical classes include:
Special characters may be mapped to consecutive integers to allow the resulting index to be used in case statements.
Char ASCII Class ... 0 608 0 1 618 0 ... A 1018 1 B 1028 1 ...
Hand-written Lexical Analyzer
A lexical analyzer can easily be written by hand. Typically, such a program will call functions getchar() and peekchar() to get characters from the input.
The lexical analyzer is likewise called as a function, with an entry such as gettoken(). The program is structured as:
Typically, a routine will process all tokens that look alike, e.g., all kinds of numbers, or both identifiers and reserved words.
Example Lexical Analyzer
/* The ‘‘big switch’’: guess token type, call a routine to parse it / TOKEN gettoken() { TOKEN tok; int c, cclass; tok = talloc(); / allocate a token / skipblanks(); / and comments */ if ((c = peekchar()) != EOF) { cclass = CHARCLASS[c]; if (cclass == ALPHA) identifier(tok); else if (cclass == NUMERIC) number(tok); else if (c == ’\’’) getstring(tok); else special(tok); } else EOFFLG = 1; return(tok); }