 Welcome Back!  Introduction  Webpage: ◦

Slides:



Advertisements
Similar presentations
Compilers Course 379K, TTH 9:30-11:00 Instructor: Dr. Doron A. Peled Office Hours: Mon 11:00-12:00.
Advertisements

Introduction to Compiler Construction
1 Week 9 Questions / Concerns Hand back Test#2 What’s due: Final Project due next Thursday June 5. Final Project check-off on Friday June 6 in class. Next.
1 Week 2 Questions / Concerns Schedule this week: Homework1 & Lab1a due at midnight on Friday. Sherry will be in Klamath Falls on Friday Lexical Analyzer.
1 Chapter 5 Compilers Source Code (with macro) Macro Processor Expanded Code Compiler or Assembler obj.
Chapter 4 Lexical and Syntax Analysis Sections 1-4.
ISBN Chapter 4 Lexical and Syntax Analysis The Parsing Problem Recursive-Descent Parsing.
1 Terminology l Statement ( 敘述 ) »declaration, assignment containing expression ( 運算式 ) l Grammar ( 文法 ) »a set of rules specify the form of legal statements.
BİL744 Derleyici Gerçekleştirimi (Compiler Design)1.
– 1 – CSCE 531 Spring 2006 Lecture 8 Bottom Up Parsing Topics Overview Bottom-Up Parsing Handles Shift-reduce parsing Operator precedence parsing Readings:
Lexical and syntax analysis
Lecture 2 Phases of Compiler. Preprocessors, Compilers, Assemblers, and Linkers Preprocessor Compiler Assembler Linker Skeletal Source Program Source.
CS 3240: Languages and Computation Course Overview Sasha Boldyreva.
1 CST 320 COMPILER METHODS. 2 Week 1 Introduction Go over syllabus Grammar Review Compiler Overview Preprocessor Symbol Table Preprocessor Directives.
September 7, September 7, 2015September 7, 2015September 7, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University.
ICS611 Introduction to Compilers Set 1. What is a Compiler? A compiler is software (a program) that translates a high-level programming language to machine.
1 Week 4 Questions / Concerns Comments about Lab1 What’s due: Lab1 check off this week (see schedule) Homework #3 due Wednesday (Define grammar for your.
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.
1 Week 3 Questions / Concerns What’s due: Lab1b due Friday at midnight Lab1b check-off next week (schedule will be announced on Monday) Homework #2 due.
Introduction to Compiler Construction Robert van Engelen COP5621 Compiler Construction Copyright Robert.
CSC 338: Compiler design and implementation
CISC 471 First Exam Review Game Questions. Overview 1 Draw the standard phases of a compiler for compiling a high level language to machine code, showing.
Lexical Analysis - An Introduction. The Front End The purpose of the front end is to deal with the input language Perform a membership test: code  source.
Lexical Analysis - An Introduction Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at.
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style COMPILER DESIGN Review Joey Paquet,
CST 320 Compiler Methods Dr. Sherry Yang PV 171 (541)
CST320 - Lec 11 Why study compilers? n n Ties lots of things you know together: –Theory (finite automata, grammars) –Data structures –Modularization –Utilization.
Lesson 3 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
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.
Unit-1 Introduction Prepared by: Prof. Harish I Rathod
Parsing Lecture 5 Fri, Jan 28, Syntax Analysis The syntax of a language is described by a context-free grammar. Each grammar rule has the form A.
1 Week 7 Questions / Concerns What’s due: Lab3 next Monday 5/19 Coming up: Lab2 & Lab3 check-off next week Lab3: LL(1) Bottom-Up Parsers LR(1) parsers.
Elements of Computing Systems, Nisan & Schocken, MIT Press, Chapter 10: Compiler I: Syntax Analysis slide 1www.nand2tetris.org Building.
Lecture # 4 Chapter 1 (Left over Topics) Chapter 3 (continue)
CPS 506 Comparative Programming Languages Syntax Specification.
Overview of Previous Lesson(s) Over View  A program must be translated into a form in which it can be executed by a computer.  The software systems.
. n COMPILERS n n AND n n INTERPRETERS. -Compilers nA compiler is a program thatt reads a program written in one language - the source language- and translates.
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 CONSTRUCTION Lesson 1 – TDDD16 TDDB44 Compiler Construction 2010 Kristian Stavåker (Erik Hansson.
Compiler Design Introduction 1. 2 Course Outline Introduction to Compiling Lexical Analysis Syntax Analysis –Context Free Grammars –Top-Down Parsing –Bottom-Up.
1 Week 6 Questions / Concerns What’s due: Lab2 part b due on Friday HW#5 due on Thursday Coming up: Project posted. You can work in pairs. Lab2 part b.
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.
Chapter 1: Introduction 1 Compiler Designs and Constructions Chapter 1: Introduction Objectives: Course Objectives Introduction Dr. Mohsen Chitsaz.
Top-Down Parsing CS 671 January 29, CS 671 – Spring Where Are We? Source code: if (b==0) a = “Hi”; Token Stream: if (b == 0) a = “Hi”; Abstract.
1 Week 5 Questions / Concerns What’s due: Lab2 part a due on Sunday Lab1 check-off by appointment Test#1 HW#5 next Thursday Coming up: Lab3 Posted. Discuss.
What is a compiler? –A program that reads a program written in one language (source language) and translates it into an equivalent program in another language.
Compiler Construction CPCS302 Dr. Manal Abdulaziz.
ICS312 Introduction to Compilers Set 23. What is a Compiler? A compiler is software (a program) that translates a high-level programming language to machine.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 1 Ahmed Ezzat.
CS416 Compiler Design1. 2 Course Information Instructor : Dr. Ilyas Cicekli –Office: EA504, –Phone: , – Course Web.
CC410: System Programming Dr. Manal Helal – Fall 2014 – Lecture 12–Compilers.
System Software Theory (5KS03).
Teaching Compiler Design
PRINCIPLES OF COMPILER DESIGN
Introduction to Compiler Construction
Programming Languages Translator
Compiler Construction
PROGRAMMING LANGUAGES
Compiler Lecture 1 CS510.
CS416 Compiler Design lec00-outline September 19, 2018
Introduction to Compiler Construction
Lexical and Syntax Analysis
CSE401 Introduction to Compiler Construction
Lecture 4: Lexical Analysis & Chomsky Hierarchy
CS416 Compiler Design lec00-outline February 23, 2019
Introduction to Compiler Construction
Introduction to Compiler Construction
Presentation transcript:

 Welcome Back!  Introduction  Webpage: ◦

 Announcements ◦ No lab tomorrow  Take a look at HW#1  Take a look at Lab#1 ◦ #include, #define, #ifndef/#else/#endif ◦ Add the capability to your language even if it’s not # preprocessor directives ◦ Remove comments (any style)  //  /* */ - multiple lines ◦ Separate or integrated with lexical analyzer? ◦ More details later today and Friday  Introduction to Compilers

Here is a simple grammar: S -> cABd A -> aA | B -> bbB | Here is an input string: caaabbbbbbd Parse it!!

PROGRAM -> program id ( IDLIST ) ; DECLS SUBPROG_DECLS COMPOUND_STAT. IDLIST -> id | IDLIST, id DECLS -> DECLS var IDLIST : TYPE ; | λ TYPE -> integer | real First part of input string: program example(input, output); var x, y : integer;

 Questions? Comments?  No Class on Monday. HW#1 due on Wednesday  In-Class Exercise #1  Preprocessor in general  CFront was a preprocessor for early C++  Remove comments  Change cases if the language is not case sensitive  Lexical Analyzer ◦ How much to read it at a time? ◦ Implement it as a DFA (don’t have to worry about whitespaces)  Lab1 details ◦ Go over #ifndef/#else/#endif  See a real #include example  But don’t have to handle real #include files in this lab ◦ Go over #include ◦ Go over #define

SUBPROG_DECLS -> SUBPROG_DECLS SUB_DECL ; | λ SUB_DECL -> SUB_HEAD DECLS COMPOUND_STAT SUB_HEAD -> function id ARGS : TYPE ; ARGS -> ( PARAMLIST ) | λ PARAMLIST -> IDLIST : TYPE | PARAMLIST ; IDLIST : TYPE DECLS -> DECLS var IDLIST : TYPE ; | λ IDLIST -> id | IDLIST, id TYPE -> integer COMPOUND_STAT -> begin OPTIONAL_STAT end OPTIONAL_STAT -> STATLIST | λ function gcd(a, b: integer): integer; begin end;

 HW#1 due today. Questions?  Lab#1 due next Wednesday. Check off next Thursday.  Nothing to check off tomorrow in lab, but Sherry will be around all day.  Preprocessor in general  CFront was a preprocessor for early C++  Remove comments  Change cases if the language is not case sensitive  Lab1 details ◦ Python Example  Finish introduction slides

 Lab#1 due on Wednesday. Check-off on Thursday.  HW#2 due on Friday, Jan. 27 th.  In-Class Exercise #2  Top-down vs. Bottom-up Parsers  Top-down parsers ◦ Recursive Descent ◦ LL(1) table driven  Given a grammar, make sure it works for the parser that you are building.  If not, change it.

 Grammar Modifications ◦ Remove Lambda productions. ◦ Remove useless productions ◦ Remove unit productions. ◦ Remove left recursion in the grammar. ◦ Left factor the grammar.

 Lab#1 due today. Check-off tomorrow in lab (PV114) from 10am to 3:45pm. Please contact Sherry for other times.  HW#2 due on Friday. Just a draft for now.  Grammar Transformations ◦ Remove unit productions ◦ Remove left recursion (for top-down parsers) ◦ Left factor the grammar (to minimize backtracking)  Lab#2 – Top-down parser  In-Class Exercise #3

Program source file Lab#1 Lab#2 List of tokens Type Value token

 HW#2 due today. Just a draft for now.  Grammar Transformations ◦ Remove unit productions ◦ Remove left recursion (for top-down parsers) ◦ Left factor the grammar (to minimize backtracking)  Lab#2 – Top-down parser  In-Class Exercise #3

 HW#2 due today  HW#3 due on Wednesday  Lab#1 check-off continues this week.  In-Class Exercise#4  Finish grammar transformations ◦ Remove Unit productions ◦ Remove Left recursion ◦ Left Factor  Start looking at lab#2 ◦ Recursive Descent Parser

 Remove left recursion from the following grammar EXPR -> EXPR + TERM | EXPR – TERM | TERM TERM -> TERM * FACTOR | TERM / FACTOR | FACTOR FACTOR -> ( EXPR ) | ID | NUM  Verify that your new grammar works for: xy + ab * 35

 HW#3 Due today. Questions?  Comment on HW#2  In-Class Exercise #5: EXPR grammar  Code Example ◦ Recursive Descent Parser  More about Recursive Descent Parsers

 Lab#2 Due on Wednesday 2/8  Lab#2 check-off on Thursday 2/9  Test#1 on Friday 2/10  Go Over HW#`3  Recursive Descent Parser example posted (EXPR grammar)  Recursive Descent Parser more details  Using First set to improve top-down parsers  Using Follow set to handle lambdas.  Compute First & Follow sets

 Top-Down Parser  Easiest Parser to write.  Ideal for small languages.  Grammar Pre-req: ◦ NO LEFT RECURSION ◦ Left Factor (preferred) ◦ Removal of Unit Productions (helpful)

 Simple Algorithm ◦ bool function for each non-terminal in the grammar ◦ HUGE nested if-else statements trying to match each rule for a non-terminal. ◦ Always return true for lambda rules. ◦ Only advance token AFTER having matched a terminal/token.

 Lab#2 Due on Wednesday 2/8  Lab#2 check-off on Thursday 2/9 (1-6pm)  Test#1 on Friday 2/10  Using First set to improve top-down parsers  Using Follow set to handle lambdas.  Compute First & Follow sets  Use First & Follow sets to construct LL(1) table.  LL(1) table-driven parser

 Lab#2 Due Tomorrow in lab for check-off.  Lab#2 check-off on Thursday 2/9 (1-6pm)  Will hand out graded homework in lab.  Test#1 on Friday 2/10. What’s on it?  No lab & class next Thursday & Friday (Feb )  Review First & Follow sets computation  Use First & Follow sets to construct LL(1) table. ◦ In-Class Exercise #6  Finish EXPR grammar

 Use grammar to construct a parse tree.  How to modify grammar: ◦ Remove unit productions ◦ Remove left recursion ◦ Left factor a grammar  How to construct a small grammar for a given small subset of a language.  General knowledge of preprocessors and Recursive Descent Parser

 Circle the first column to determine which token that starts that rule S-> A B c First(A)- A-> a A-> B-> b B->

 Watch out for lambdas S-> A B c First(A)- + First(B)- A-> a A-> B-> b B->

 Watch out for lambdas S-> A B c First(A) - + First(B) - + c A-> a A-> B-> b B->

 What comes after? S-> A B c c follows B A-> a A-> B-> b B->

 What comes after? S-> A B c First(B) - follows A A-> a A-> B-> b B->

 Watch for lambdas S-> A B c c follows A A-> a A-> B-> b B->

S -> ABCd A -> e | f | B -> g | h | C -> p | q

 Hand back Test#1  No lab & class this Thursday & Friday (Feb )  Lab#2 continues this week (during office hours)  Finish EXPR grammar  Take a closer look at Follow set  How to construct the LL(1) parser (Lab#3)  Homework #4

EXPR -> TERM X X -> + TERM X | - TERM X | TERM -> FACTOR Z Z -> * FACTOR Z | / FACTOR Z | FACTOR -> id id $ +, -, $ id +, -, $ *, /, +, -, $ id *, /, +, -, $

 Engineering Month celebration, Tuesday, Feb. 22 nd 5:30pm, Shasta Complex. Free Pizza & Free T- Shirts.  Interview Tips & Tricks TODAY 5pm PV147  No lab & class this Thursday & Friday (Feb )  Lab#2 continues this week (during office hours)  Take a closer look at Follow set  Take a look at LL(1) parser implementation  Why isn’t it LL(1)?  In-Class Exercise #7  Homework #4 due on Monday, Feb. 20 st  Lab#3 due on Monday, Feb. 27 th.

 Compute First & Follow sets for this grammar.  Construct a LL(1) table using First & Follow sets S -> Ac | Bd A -> cA | B -> caB |

 Homework #4 due today. Questions?  Lab#3 due on Monday, Feb. 27 th.  Engineering Month celebration, Tuesday, Feb. 22 nd 5:30pm, Shasta Complex. Free Pizza & Free T- Shirts.  Lab#2 continues this week  Top-Down Parsers vs. Bottom-Up Parsers  Bottom-Up Parsers  Looking Ahead: ◦ Using Tools (Demo) ◦ Semantic Analysis ◦ Back-End Compiler (Project Options)

Grammar Program / Engine Test Program Test.c

Grammar Program / Engine Test Program Test.c Yes/No Tree structure

LL(1) table Engine Test Program Test.c Yes/No Tree structure Stack

0) S' –> S 1) S –> XX 2) X –> aX 3) X –> b

StateActiongoto Id+*()$ETF0S5S4123 1S6accept 2R2S7R2 3R4R4R4 4S5S4823 5R6R6R6 6S5S493 7S5S410 8S6S11 9R1S7R1R1 10R3R3R3R3 11R5R5R5R5 (0) E’ -> E (4) T -> F (1) E-> E + T (5) F -> ( E ) (2) E -> T (6) F -> id (3) T -> T * F

Lab#3 due on Monday, Feb. 27 th.Lab#3 due on Monday, Feb. 27 th. Engineering Month celebration, Wednesday, Feb. 22 nd 5:30pm, Shasta Complex. Free Pizza & Free T- Shirts.Engineering Month celebration, Wednesday, Feb. 22 nd 5:30pm, Shasta Complex. Free Pizza & Free T- Shirts. Lab#2 check-off continues this weekLab#2 check-off continues this week LR(1) Parser Table ExampleLR(1) Parser Table Example In-Class Exercise #8 Final Project OptionsFinal Project Options Looking Ahead:Looking Ahead: Using Tools (Demo) Semantic Analysis Back-End Compiler (Project Options) CST320 Week 7 Wednesday

Final Project Options Parser GeneratorsParser Generators Given a grammar, generate the code for a Recursive Descent Parser Given a grammar, generate the LL(1) table to be used in a LL(1) parser Program TranslatorsProgram Translators Convert a program in one language (i.e. C) to another language (i.e. Python) Convert a program in one language (i.e. C) to a low-level language (i.e. Assembly, Microsoft IL, etc.) CST116 Cheat DetectorCST116 Cheat Detector Your parser MUST work for typical CST116 labs (get them from Todd or Randy) Your parser must read in at least 3 files to compare. Compute software metrics (see final project description) and report on how similar or different the programs are.

Final Project Options Small InterpreterSmall Interpreter Grammar Parser Runtime Environment int x; write “Enter a num”; read x; if (x > 3) write “Hi”; else write “Hello”; Enter a num: 45 Hello

StateActiongoto Id+*()$ETF0S5S4123 1S6accept 2R2S7R2 3R4R4R4 4S5S4823 5R6R6R6 6S5S493 7S5S410 8S6S11 9R1S7R1R1 10R3R3R3R3 11R5R5R5R5 (0) E’ -> E (4) T -> F (1) E-> E + T (5) F -> ( E ) (2) E -> T (6) F -> id (3) T -> T * F

Lab#3 due tonight.Lab#3 due tonight. Lab#3 check-off on ThursdayLab#3 check-off on Thursday Final ProjectFinal Project Check-off date on Thursday of dead week. Last day to turn in late labs is Friday of dead week. Incompletes will be given if not turned in by Friday of dead week. (One letter grade lower for completion later). Test #2 on Friday, Feb. 9 th.Test #2 on Friday, Feb. 9 th. Dead week – time for project, no new topics.Dead week – time for project, no new topics. Make-up test during Dead Week.Make-up test during Dead Week. CST320 Week 8 Monday

44 Conventional Translator skeletal source program preprocessor source program library, relocatable object files compiler assembler target assembly program loader / linker relocatable machine code absolute machine code

45 Structure of Compilers Lexical Analyzer (scanner) Source Program Syntax Analysis (Parser) Tokens Semantic Analysis Syntactic Structure Optimizer Code Generator Intermediate Representation Target machine code Symbol Table

Intermediate Representation Almost no compiler produces code without first converting a program into some intermediate representation that is used just inside the compiler. This intermediate representation is called by various names: –Internal Representation –Intermediate representation –Intermediate language

Intermediate Representation Different forms of intermediate representation: –tuples –Abstract Syntax Trees –Simplified language

48 Abstract Syntax Tree x = y + 3; = x+ y 3

49 Quadruples y= a*(x+b)/(x-c); T1= x+b;(+, 3, 4, 5) T2=a*T1; (*, 2, 5, 6) T3=x-c;(-, 3, 7, 8) T4=T2/T3;(/, 6, 8, 9) y=T4;(=, 0, 9, 1) y a x b T1 T2 c T3 T4