CC410: System Programming Dr. Manal Helal – Fall 2014 – Lecture 12–Compilers.

Slides:



Advertisements
Similar presentations
Mooly Sagiv and Roman Manevich School of Computer Science
Advertisements

1 Pass Compiler 1. 1.Introduction 1.1 Types of compilers 2.Stages of 1 Pass Compiler 2.1 Lexical analysis 2.2. syntactical analyzer 2.3. Code generation.
CPSC Compiler Tutorial 9 Review of Compiler.
Introduction The compilation approach uses a program called a compiler, which translates programs written in a high-level programming language into machine.
1 Foundations of Software Design Lecture 24: Compilers, Lexers, and Parsers; Intro to Graphs Marti Hearst Fall 2002.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
1 Chapter 5 Compilers Source Code (with macro) Macro Processor Expanded Code Compiler or Assembler obj.
1 Terminology l Statement ( 敘述 ) »declaration, assignment containing expression ( 運算式 ) l Grammar ( 文法 ) »a set of rules specify the form of legal statements.
Chapter 2 A Simple Compiler
BİL744 Derleyici Gerçekleştirimi (Compiler Design)1.
Compilers -- Basic functions
Describing Syntax and Semantics
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
Syntax and Semantics Structure of programming languages.
Compiler1 Chapter V: Compiler Overview: r To study the design and operation of compiler for high-level programming languages. r Contents m Basic compiler.
Chapter 1 Introduction Dr. Frank Lee. 1.1 Why Study Compiler? To write more efficient code in a high-level language To provide solid foundation in parsing.
Chapter 10: Compilers and Language Translation Invitation to Computer Science, Java Version, Third Edition.
CSC 338: Compiler design and implementation
PART I SISTEM UTILITIES Lecture 6 Compilers Ştefan Stăncescu 1.
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style COMPILER DESIGN Review Joey Paquet,
Joey Paquet, Lecture 12 Review. Joey Paquet, Course Review Compiler architecture –Lexical analysis, syntactic analysis, semantic.
Review 1.Lexical Analysis 2.Syntax Analysis 3.Semantic Analysis 4.Code Generation 5.Code Optimization.
1.  10% Assignments/ class participation  10% Pop Quizzes  05% Attendance  25% Mid Term  50% Final Term 2.
Syntax and Semantics Structure of programming languages.
Bernd Fischer RW713: Compiler and Software Language Engineering.
Review: Compiler Phases: Source program Lexical analyzer Syntax analyzer Semantic analyzer Intermediate code generator Code optimizer Code generator Symbol.
CPS 506 Comparative Programming Languages Syntax Specification.
D Goforth COSC Translating High Level Languages.
Introduction Lecture 1 Wed, Jan 12, The Stages of Compilation Lexical analysis. Syntactic analysis. Semantic analysis. Intermediate code generation.
D Goforth COSC Translating High Level Languages Note error in assignment 1: #4 - refer to Example grammar 3.4, p. 126.
Introduction to Compiling
Muhammad Idrees, Lecturer University of Lahore 1 Top-Down Parsing Top down parsing can be viewed as an attempt to find a leftmost derivation for an input.
Compiler Principles Fall Compiler Principles Lecture 6: Parsing part 5 Roman Manevich Ben-Gurion University.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 4.
Compiler Design Introduction 1. 2 Course Outline Introduction to Compiling Lexical Analysis Syntax Analysis –Context Free Grammars –Top-Down Parsing –Bottom-Up.
Compiler Introduction 1 Kavita Patel. Outlines 2  1.1 What Do Compilers Do?  1.2 The Structure of a Compiler  1.3 Compilation Process  1.4 Phases.
Compiler Construction By: Muhammad Nadeem Edited By: M. Bilal Qureshi.
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
C H A P T E R T W O Linking Syntax And Semantics Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
Compiler Construction CPCS302 Dr. Manal Abdulaziz.
More yacc. What is yacc – Tool to produce a parser given a grammar – YACC (Yet Another Compiler Compiler) is a program designed to compile a LALR(1) grammar.
Overview of Compilation Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 2.
Bottom Up Parsing CS 671 January 31, CS 671 – Spring Where Are We? Finished Top-Down Parsing Starting Bottom-Up Parsing Lexical Analysis.
Compiler Syntactic Analysis r Two general classes of parsing techniques m Bottom-up (Operator-Precedence parsing) Begin with the terminal nodes.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 1 Ahmed Ezzat.
Presented by : A best website designer company. Chapter 1 Introduction Prof Chung. 1.
CS416 Compiler Design1. 2 Course Information Instructor : Dr. Ilyas Cicekli –Office: EA504, –Phone: , – Course Web.
CS510 Compiler Lecture 1. Sources Lecture Notes Book 1 : “Compiler construction principles and practice”, Kenneth C. Louden. Book 2 : “Compilers Principles,
Syntax and Semantics Structure of programming languages.
Compiler Summary Lexical analysis—scanner – String of characters  token – Finite automata. Syntactical analysis – parser – Sequence of tokens –> language.
Chapter 3 – Describing Syntax
Introduction to Compiler Construction
System Software Unit-1 (Language Processors) A TOY Compiler
A Simple Syntax-Directed Translator
CS 326 Programming Languages, Concepts and Implementation
Programming Languages Translator
PROGRAMMING LANGUAGES
Compiler Lecture 1 CS510.
Compiler Construction
CS416 Compiler Design lec00-outline September 19, 2018
Compiler Designs and Constructions
Introduction CI612 Compiler Design CI612 Compiler Design.
CPSC 388 – Compiler Design and Construction
Compilers B V Sai Aravind (11CS10008).
R.Rajkumar Asst.Professor CSE
CC410: System Programming
CS416 Compiler Design lec00-outline February 23, 2019
High-Level Programming Language
Chapter 10: Compilers and Language Translation
Lec00-outline May 18, 2019 Compiler Design CS416 Compiler Design.
Presentation transcript:

CC410: System Programming Dr. Manal Helal – Fall 2014 – Lecture 12–Compilers

2 Lexical analizer Syntax analizer Intermediate code generator (semantic analyzer) Code generator Optimization (optional) Source program Lexical units (Tokens) Parse trees Intermediate code Computer Machine language Symbol table Chapter 5 - Compilers

Terminology »Statement –Declaration, assignment containing expression »Grammar –A set of rules specify the form of legal statements »Syntax vs. Semantics –Example: assuming I, J, K: integer and X,Y: float –I:=J+K vs. X:= Y+I »Compilation –Matching statements written by the programmer to structures defined by the grammar and generating the appropriate object code. 3

Basic Compiler »Lexical analysis - scanner –Scanning the source statement, recognizing and classifying the various tokens »Syntactic analysis - parser –Recognizing the statement as some language construct. –Construct a parser tree (syntax tree) »Code generation – code generator –Generate assembly language codes that require an assembler –Generate machine codes (Object codes) directly 4

Scanner 5 SUM:=0; SUMSQ:= 0; READ (VALUE); PROGRAM STATS VAR SUM,SUMSQ,I

Grammar »Grammar: a set of rules –Backus-Naur Form (BNF) –Ex: Figure 5.2 »Terminology –Define symbol ::= –Nonterminal symbols <> –Alternative symbols | –Terminal symbols 6

Simplified Pascal Grammar 7

Parser » ::= READ ( ) » ::= id |,id » ::= id := » ::= | + | - » ::= | * | DIV » ::= id | int | ( ) »READ(VALUE) »SUM := 0 »SUM := SUM + VALUE »MEAN := SUM DIV 100 8

Syntax Tree 9

Syntax Tree for Program

11

Lexical Analysis »Function –Scanning the program to be compiled and recognizing the tokens that make up the source statements. »Tokens –Tokens can be keywords, operators, identifiers, integers, floating-point numbers, character strings, etc. –Each token is usually represented by some fixed-length code, such as an integer, rather than as a variable-length character string (see Figure 5.5) –Token type, Token specifier (value) (see Figure 5.6) 12

Scanner Output »Token specifier –Identifier name, integer value, (type) »Token coding scheme –Figure

14 Can be entered directly into a symbol table and a pointer used in the output of the lexicon

Finite Automata (FA) »Starting State »Ending State(s) »Transitions upon scanning a character »The sequence of characters are recognized (accepted) by the FA if it moves it from a start state to an end state, otherwise, it is not recognized (rejected) a b c c a abcrecognized abccabcrecognized acnot recognized

Token Recognizer »By grammar – ::= | | – ::= A | B | C | D | … | Z – ::= 0 | 1 | 2 | 3 | … | 9 »By scanner - modeling as finite automata –Figure 5.8 (a) 16

17 Recognizing Identifier Identifiers allowing underscore (_)

Recognizing Integer »Allowing leading zeroes –Figure 5.8 (c) »Disallowing leading zeroes –Figure 5.8 (d)

Scanner - Implementation »Figure 5.10 (a) –Algorithmic code for identifier recognition »Tabular representation of finite automaton for Figure

Syntactic Analysis »Recognize source statements as language constructs or build the parse tree for the statements. –Bottom-up Operator-precedence parsing Shift-reduce parsing LR(0) parsing LR(1) parsing SLR(1) parsing LALR(1) parsing –Top-down Recursive-descent parsing LL(1) parsing 20

Operator-Precedence Parsing »Operator –Any terminal symbol (or any token) »Precedence –* » + –+ « * »Operator-precedence –Precedence relations between operators 21

Precedence Matrix for the Fig

Operator-Precedence Parse Example BEGIN READ ( VALUE ) ; 23

Operator-Precedence Parse Example 24

Operator-Precedence Parse Example 25

Operator-Precedence Parse Example 26

Operator-Precedence Parsing »Bottom-up parsing »Generating precedence matrix –Aho et al. (1988) 27

Shift-reduce Parsing with Stack »Figure

Recursive-Descent Parsing »Each nonterminal symbol in the grammar is associated with a procedure. » ::= READ ( ) » ::= | | | »Left Recursion – ::= | ; »Modification – ::= {; } 29

Recursive-Descent Parsing (cont’d.) 30

Recursive-Descent Parsing of READ 31

Recursive-Descent Parsing of IDLIST 32

Recursive-Descent Parsing (cont’d.) 33

Recursive-Descent Parsing of ASSIGN 34

Recursive-Descent Parsing of EXP 35

Recursive-Descent Parsing of TERM 36

Recursive-Descent Parsing of FACTOR 37

Recursive-Descent Parsing (cont’d.) 38

Recursive-Descent Parsing (cont’d.) 39

Recursive-Descent Parsing (cont’d.) 40

: = id 1 + int 32 * id 2 real | f | a | h | r | e | n | h | e | i | t | : | = | 3 | 2 | + | c | e | l | s | I | o | u | s | * | 1 |. | 8 | ; | Lexical analyzer (scanner) (converts from character stream into a stream of tokens.) Fahrenheit := 32 + celsious * 1.8 [ id, 1 ] [ : = ][ int, 32 ][ + ][id, 2 ][ * ][int, 1.8 ][; ] Symbol Table fahrenheit real celsious real 1212 Syntax analyzer (parser) (Construct syntactic structure of the program) EXAMPLE: Getchar() name attribute index in symbol table

42 Symbol Table fahrenheit real celsious real 1212 Intermediate code generator := id 1 +r+r inttoreal*r id 2 real 1.8 int 32 Intermediate code Temp1 := inttoreal(32) Temp2 := id2 Temp2 := Temp2 * 1.8 Temp1 := Temp1 + Temp2 id1 := Temp1

43 Symbol Table fahrenheit real celsious real 1212 Code optimizer Intermediate code Temp1 := inttoreal(32) Temp2 := id2 Temp2 := Temp2 * 1.8 Temp1 := Temp1 + Temp2 id1 := Temp1 Temp1 := id2 Temp1 := Temp1 * 1.8 Temp1 := Temp id1 := Temp1 optimized code

44 Symbol Table fahrenheit real celsious real 1212 Code generator Temp1 := id2 Temp1 := Temp1 * 1.8 Temp1 := Temp id1 := Temp1 optimized code movf id2, r1 mulf #1.8, r1 addf #32.0, r1 movf r1, id1 assembly instructions