A Simple Syntax-Directed Translator CS308 Compiler Theory
A Simple Syntax-Directed Translator CS308 Compiler Theory 1
Lecture Outline We shall look at a simple programming language and describe the initial phases of compilation. We start off by creating a simple'syntax directed translator that maps infix arithmetic to postfix arithmetic. This translator is then extended to cater for more elaborate programs such as (check page 39 Aho) -While (true){x=a[i];a[i]=a[j];a[j]=x; Which generates simplified intermediate code (as on pg40 Aho) CS308 Compiler Theory 2
Lecture Outline • We shall look at a simple programming language and describe the initial ph f il i ases of compilation. • We start off by creating a ‘simple’ syntax directed translator that maps infix arithmetic to postfix arithmetic. • This translator is then extended to cater for more elaborate programs such as (check page 39 Aho) such as (check page 39 Aho) – While (true) { x=a[i]; a[i]=a[j]; a[j]=x; } • Which generates simplified intermediate code (as on pg40 Aho) CS308 Compiler Theory 2
Two Main Phases (Analysis and Synthesis) Analysis Phase :-Breaks up a source program into constituent pieces and produces an internal representation of it called intermediate code. Synthesis Phase :-translates the intermediate code into the target program. During this lecture we shall focus on the analysis phase (compiler front end...see figure next slide) CS308 Compiler Theory 3
Two Main Phases (Analysis and Synthesis) • Analysis Phase :- Breaks up a source program into constituent pieces and d l f ll d d pro duces an interna l representation of it call e d intermed d iate co de. • Synthesis Phase :- translates the intermediate code into the target program. • During this lecture we shall focus on the analysis phase (compiler front end see figure next slide) end … see figure next slide) CS308 Compiler Theory 3
A Model of A Compiler Font End source Lexical tokens syntax Intermediate three-address Parser Code program Analyzer tree Generator code Symbol Table Figure 2.3:A model of a compiler front end CS308 Compiler Theory 4
A Model of A Compiler Font End CS308 Compiler Theory 4
Syntax vs.Semantics The syntax of a programming language describes the proper form ofits programs The semantics of the language defines what its programs mean. CS308 Compiler Theory 5
Syntax vs. Semantics • The syntax of a programming language describes the proper form of its programs • The semantics of the language defines what its programs mean semantics of the language defines what its programs mean. CS308 Compiler Theory 5