Anda di halaman 1dari 17

Engineering

A Simple Compiler
Spring 2011 Instructor: Mike Wilson

CSE 431S: Translation of Computer Languages

Spring 2011

Engineering

Adding Calculator: ac

Language has
23 variables, named a-z except for f,i,p. 2 types: integer and float
Declare variables as ia fb

Arithmetic operations
a=5 b = a + 3.2

Ability to print results pb

CSE 431S: Translation of Computer Languages

Spring 2011

Engineering

Definition of ac Language
Our We

definition is informal and by example

allowed b = a + 3.2 Is a = b + 3.2 also allowed? need a formal definition of the language

We

CSE 431S: Translation of Computer Languages

Spring 2011

Engineering

ac Grammar

Figure from text: Crafting a Compiler


CSE 431S: Translation of Computer Languages Spring 2011 4

Engineering

Grammar Conventions

Grammars consist of
Nonterminals --- usually capitalized Terminals --- usually lower case Special character --- signifies the empty string Special Start symbol --- where the grammar starts Productions
AaB Applied to a string by replacing an occurrence of the LHS by the RHS xAb becomes xaBb

CSE 431S: Translation of Computer Languages

Spring 2011

Engineering

Grammar Types

Context Free Grammars (CFGs)


LHS is exactly one nonterminal

Linear Grammars
RHS contains no more than one nonterminal

Right/Left Linear Grammars


RHS has any nonterminal at right/left side of result

CSE 431S: Translation of Computer Languages

Spring 2011

Engineering

Tokens in ac

Figure from text: Crafting a Compiler

CSE 431S: Translation of Computer Languages

Spring 2011

Engineering

Static Semantics of ac
Concerned

with types

All variables must be declared before use Each operation plus/minus/assign must have operands of the same type
If not the same, we can use widening but never narrowing

CSE 431S: Translation of Computer Languages

Spring 2011

Engineering

Runtime Semantics of ac
Addition/Subtraction

sense

defined in the usual

Assignments

the RHS

replace the original value with

Print

displays the value of the variable

CSE 431S: Translation of Computer Languages

Spring 2011

Engineering

Scanner for ac
Scanner Peek Advance ScanDigits Peek Advance Peek Peek ScanDigits Advance Peek Advance Advance

LexicalError

Figures from text: Crafting a Compiler CSE 431S: Translation of Computer Languages Spring 2011 10

Engineering

Recursive Descent
Stmt Peek Match Match Val Expr Peek Match Match Error

Figure from text: Crafting a Compiler CSE 431S: Translation of Computer Languages Spring 2011 11

Engineering

Predict Sets for ac Grammar


floatdcl, intdcl, id, print, $ floatdcl, intdcl id, print, $ floatdcl intdcl id, print $ id print plus minus id, print, $ id inum fnum

Figure from text: Crafting a Compiler


CSE 431S: Translation of Computer Languages Spring 2011 12

Engineering

Traversing an AST

Use a post-order traversal


Visits all children, then the parent For this example, visits:
b, then c, then a
b

We use this traversal a lot in compilers

CSE 431S: Translation of Computer Languages

Spring 2011

13

Engineering

Processing Type Declarations


visit getType

EnterSymbol EnterSymbol

getID getID

EnterSymbol Error
LookupSymbol

Figure from text: Crafting a Compiler


CSE 431S: Translation of Computer Languages Spring 2011 14

Engineering

Processing Function Nodes

Figures from text: Crafting a Compiler

CSE 431S: Translation of Computer Languages

Spring 2011

15

Engineering

dc: Reverse Polish Calculator


Stack-based Architecture All arithmetic operations work on top of stack

+: pop the top two items on the stack, add them, push the result

26 registers a-z sr: pop the top of stack, store in register r lr: push value of register r onto stack p: print top of stack, does not pop k: Pop top of stack, set precision to this many digits

CSE 431S: Translation of Computer Languages

Spring 2011

16

Engineering

Code Generation

Figures from text: Crafting a Compiler

CSE 431S: Translation of Computer Languages

Spring 2011

17

Anda mungkin juga menyukai