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.

Slides:



Advertisements
Similar presentations
CPSC 388 – Compiler Design and Construction
Advertisements

Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
Names and Bindings.
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.
Compiler Construction
CPSC Compiler Tutorial 9 Review of Compiler.
CS 330 Programming Languages 10 / 16 / 2008 Instructor: Michael Eckmann.
Names, Bindings, Type Checking, and Scopes
1 Contents Introduction A Simple Compiler Scanning – Theory and Practice Grammars and Parsing LL(1) Parsing LR Parsing Lex and yacc Semantic Processing.
ISBN Chapter 5 Names, Bindings, Type Checking, and Scopes Names Variables The Concept of Binding Type Checking Strong Typing Type Compatibility.
Copyright © 1995 by Addison-Wesley Publishing Co. 1 Names - Design issues: - Maximum length? - Are connector characters allowed? - Are names case sensitive?
Building An Interpreter After having done all of the analysis, it’s possible to run the program directly rather than compile it … and it may be worth it.
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
1 Chapter 5: Names, Bindings and Scopes Lionel Williams Jr. and Victoria Yan CSci 210, Advanced Software Paradigms September 26, 2010.
COP4020 Programming Languages
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.
Names and Binding In procedural programming, you write instructions the manipulate the “state” of the process where the “state” is the collection of variables.
ISBN Chapter 5 Names, Bindings, Type Checking, and Scopes.
COMP4730/2003/lec5/H.Melikian Names, Bindings,Type Checking and Scopes (Chapter 5) - Design issues: - Maximum length? - Are connector characters allowed?
CSC3315 (Spring 2009)1 CSC 3315 Programming Languages Hamid Harroud School of Science and Engineering, Akhawayn University
1 CS Programming Languages Class 07 September 14, 2000.
Names Variables Type Checking Strong Typing Type Compatibility 1.
5-1 Chapter 5: Names, Bindings, Type Checking, and Scopes Variables The Concept of Binding Type Checking Strong Typing Type Compatibility Scope and Lifetime.
1 Programming Languages Tevfik Koşar Lecture - II January 19 th, 2006.
March 12, ICE 1341 – Programming Languages (Lecture #6) In-Young Ko Programming Languages (ICE 1341) Lecture #6 Programming Languages (ICE 1341)
CST320 - Lec 11 Why study compilers? n n Ties lots of things you know together: –Theory (finite automata, grammars) –Data structures –Modularization –Utilization.
Introduction A variable can be characterized by a collection of properties, or attributes, the most important of which is type, a fundamental concept in.
1 Type Checking Type checking ensures that the operands and the operator are of compatible types Generalized to include subprograms and assignments Compatible.
Unit-1 Introduction Prepared by: Prof. Harish I Rathod
1.  10% Assignments/ class participation  10% Pop Quizzes  05% Attendance  25% Mid Term  50% Final Term 2.
CPS 506 Comparative Programming Languages Syntax Specification.
1 Compiler Design (40-414)  Main Text Book: Compilers: Principles, Techniques & Tools, 2 nd ed., Aho, Lam, Sethi, and Ullman, 2007  Evaluation:  Midterm.
Chapter 1 Introduction Major Data Structures in Compiler
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.
Structure of Programming Languages Names, Bindings, Type Checking, and Scopes.
Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 12 Lecturer: Dr. Emad Nabil 1-1.
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
How to execute Program structure Variables name, keywords, binding, scope, lifetime Data types – type system – primitives, strings, arrays, hashes – pointers/references.
Names and Binding In Text: Chapter 4.
1 Structure of Compilers Lexical Analyzer (scanner) Modified Source Program Parser Tokens Semantic Analysis Syntactic Structure Optimizer Code Generator.
LECTURE 3 Compiler Phases. COMPILER PHASES Compilation of a program proceeds through a fixed series of phases.  Each phase uses an (intermediate) form.
ISBN Chapter 5 Names, Bindings, Type Checking, and Scopes.
 Welcome Back!  Introduction  Webpage: ◦
Names, Scope, and Bindings Programming Languages and Paradigms.
Overview of Compilation Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 2.
Names, Bindings, Type Checking and Scopes. Chapter 5 Topics Introduction Names Variables The Concept of Binding Type Checking Strong Typing Type Equivalence.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Syntax Analysis Or Parsing. A.K.A. Syntax Analysis –Recognize sentences in a language. –Discover the structure of a document/program. –Construct (implicitly.
CC410: System Programming Dr. Manal Helal – Fall 2014 – Lecture 12–Compilers.
Chapter 5 Names, Bindings, Type Checking CSCE 343.
5.2 Names - We discuss all user-defined names here
5.2 Names - We discuss all user-defined names here
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Compiler Design (40-414) Main Text Book:
Introduction to Compiler Construction
Names and Attributes Names are a key programming language feature
Type Checking Generalizes the concept of operands and operators to include subprograms and assignments Type checking is the activity of ensuring that the.
Type Checking, and Scopes
A Simple Syntax-Directed Translator
Chapter 5 Names, Bindings, Type Checking, and Scopes.
-by Nisarg Vasavada (Compiled*)
Chapter 5 Names, Bindings, Type Checking, and Scopes.
CSE 3302 Programming Languages
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
Names and Binding In Text: Chapter 5.
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Presentation transcript:

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 Using tools

2 LL(1) Another top-down parser It’s a table-driven parser. LL(1) L – first L, the input is from left to right L – second L, leftmost derivation (top-down) 1 – one token look ahead Grammar pre-req: No left recursion Unit productions are okay, but should minimize MUST left factor to ensure one-token look ahead. Procedure: Compute First and Follow sets from the grammar

3 LL(1) Example E -> TX X -> +TX | -TX | T -> FY Y -> *FY | /FY | F -> id | num | ( E ) FirstFollow E id, num, ( X +, -, T id, num, ( Y *, /, F id, num, (

4 LL(1) Example E -> TX X -> +TX | -TX | T -> FY Y -> *FY | /FY | F -> id | num | ( E ) FirstFollow E id, num, ($, ) X +, -, T id, num, ( Y *, /, F id, num, (

5 LL(1) Example E -> TX X -> +TX | -TX | T -> FY Y -> *FY | /FY | F -> id | num | ( E ) FirstFollow E id, num, ($, ) X +, -, T id, num, (+, - Y *, /, F id, num, (*, /,

6 LL(1) Example E -> TX X -> +TX | -TX | T -> FY Y -> *FY | /FY | F -> id | num | ( E ) FirstFollow E id, num, ($, ) X +, -, $, ) T id, num, (+, -, $, ) Y *, /, +, -, $, ) F id, num, (*, /, +, -, $, )

7 LL(1) Example E -> TX X -> +TX | -TX | T -> FY Y -> *FY | /FY | F -> id | num | ( E ) FirstFollow E id, num, ($, ) X +, -, $, ) T id, num, (+, -, $, ) Y *, /, +, -, $, ) F id, num, (*, /, +, -, $, ) id+-()*/num$ E -> TX X ->+TX-TX T ->FY Y -> *FY/FY F ->id( E )num

8 In-Class Exercise #9 E -> TX X -> +TX | -TX | T -> FY Y -> *FY | /FY | F -> id | num | ( E ) FirstFollow E id, num, ($, ) X +, -, $, ) T id, num, (+, -, $, ) Y *, /, +, -, $, ) F id, num, (*, /, +, -, $, ) id+-()*/num$ E -> TX X ->+TX-TX T ->FY Y -> *FY/FY F ->id( E )num Parse “a * b + 3” using this table

9 In-Class Exercise #9 id+-()*/num$ E -> TX X ->+TX-TX T ->FY Y -> *FY/FY F ->id( E )num Parse “a * b + 3” using this table Stack Input Action $E a * b + 3 $ [E, a] -> TX $XT a * b + 3 $

10 Lab 3 The purpose of this lab is to demonstrate that LL(1) parsers can parse any LL(1) table (any language). Input: Three LL(1) tables, one for each test language. Input program / string Output: Showing parsing steps (stack, input, action) and final result (Yes/No) Lab 3 demo

11 Schedule Week 7: Wednesday / Friday : Back-end compilation steps, project info Week 8: Monday, 5/19, Lab3 due, Project Symposium Tuesday, 5/20, Lab2 & lab3 check-off 12 to 2pm, 3:30 to 5:30 Wednesday, 5/21, Test#2, check-off continues for Lab2 & Lab3 Friday, 5/23, project work day, no class. Week 9: Monday: holiday Week 10: Thursday: Final project due Friday: Project check-off in class / pizza party

12 Output from Parser Yes/No parser Most compilers are one-pass. You don’t read the input file or revisit the input tokens again after read. In order to process the parsed statements, need to save them into some sort of data structure. Parse tree / list is the most common choice. A simplified language is another choice, but it requires parsing again.

13 Parse Tree Data Decl int x if stmt Expr && Assign Simple_expr

14 Week 7 Questions / Concerns What’s due: Lab3 next Monday 5/19 Test #2 next Wednesday, 5/21 – covering recursive descent & LL(1) Including grammar modifications Information for the project Additions to the symbol table Semantic Analysis Binding Type binding / checking Scope Lifetime Intermediate representation Back-end compiler

15 Structure of Compilers Lexical Analyzer (scanner) Modified Source Program Syntax Analysis (Parser) Tokens Semantic Analysis Syntactic Structure Optimizer Code Generator Intermediate Representation Target machine code Symbol Table skeletal source program preprocessor

16 Compiler revisions What’s included with every new release?? Language changes are few and far in between. Last C++ standard was finalized in Compilers are finally catching up with some of the changes. Examples: auto keyword Range-based for loop Back-end optimization and code generation. Most common

17 auto keyword in C++ Before C++0x standard, auto is a storage specifier on variables. There are 3 specifiers: static auto - optional extern //File1.cpp //File2.cpp int f1() { extern int g_x; int g_x; static int y; auto int z; //same as int z } //z goes out of scope automatically

18 auto keyword in C++ After C++0x The old auto keyword has been replaced with a new usage (new grammar). auto x = 5; //auto binds a type to x based on the assigned value. //you can only do this once however since x can only //be declared once. map, vector > table; pair, vector >::iterator, bool> status; status = table.insert(make_pair(key, rule)); auto status = table.insert(…);

19 Range-based for loop Before C++0x for (int i =0; i< MAX; i++) cout << someArray[i]; for_each(someArray, someArray+MAX, myFunction); void myFunction(int i) { cout << i; } After C++0x (not available in VS2010 but available from VS2012 on) for (int i : someArray) cout << i; for (int i : someVector) cout << i;

20 Symbol Table Revisited Information about each symbol Name Type Use (variable, function, parameters, typename, etc.) Scope Lifetime

21 Binding Binding is association of meaning/operation to a symbol Static It occurs before runtime and remains unchanged throughout program execution. Dynamic It occurs at runtime and can change in the course of program execution.

22 Type Binding Before a variable can be referenced in a program, it must be bound to a data type. Two important questions to ask: 1. How is the type specified? Explicit Declaration Implicit Declaration All variable names that start with the letters ‘i’ - ‘r’ are integer, real is an array, %something is a hash structure Determined by context and value

23 Type Binding 2. When does the binding take place Explicit declaration (static) Implicit declaration (static) Determined by context and value (dynamic) Dynamic Type Binding When a variable gets a value, the type of the variable is determined right there and then.

24 Dynamic Type Binding Specified through an assignment statement (set x ‘(1 2 3)) <== x becomes a list (set x ‘a) <== x becomes an atom Advantage: flexibility (generic program units) Disadvantages: 1. High cost (dynamic type checking and interpretation) 2. Type error detection by the compiler is difficult 24

25 Type Checking Type checking is the activity of ensuring that the operands of an operator are of compatible types A compatible type is one that is either legal for the operator, or is allowed under language rules to be implicitly converted, by compiler-generated code, to a legal type. This automatic conversion is called a coercion. Two kinds of type checking Static Type Checking (Compile time) what we will be doing for the final project. Dynamic Type Checking (Run time) Rarely done because it’s expensive and slow. 25 -

26 Static Type Checking & Strong Typing Advantage of strong typing: Allows the detection of the misuses of variables that result in type errors. Languages: 1. FORTRAN 77 is not: parameters, EQUIVALENCE 2. Pascal is not: variant records 3. Modula-2 is not: variant records, WORD type 4. C and C++ are not: parameter type checking can be avoided; unions are not type checked. 5. Ada is, almost (UNCHECKED CONVERSION is loophole) (Java is similar) 26

27 Type Coercion / Warning / Errors Type Coercion / Promotion Different types but no info lost by changing it to a different type. double x; x = 1; //okay, no info lost Type Warning int y; y = 3.5; //type warning, lost information Type Errors int z; z = “Hello”; //type error

28 Scope Most programming languages use Static scoping rules. Scope of a variable is determined by compile time. Very few programming language use dynamic scoping rules. Scope of a variable is determined at run time.

29 Scope int x = 10; void f1(); void main() { int x = 20; f1(); cout << x; } void f1() { cout << x; } What’s the output?

30 Scope int x = 10; void f1(); void main() { int x = 20; f1(); cout << x; } void f1() { cout << x; } What’s the output?

31 Static scope Based on program text To connect a name reference to a variable, you (or the compiler) must find the declaration. Search process: search declarations, first locally, then in increasingly larger enclosing scopes, until one is found for the given name. Static scoping is also block scoping.

32 Static Scope: C++ int global_X; int main() { int x; …. { int y; … } void f1() { int z; … } global_x main f1 y x z

33 Static Scope: C++ int global_X; int main() { int x; …. { int y; … } void f1() { int z; … } global_x (0) main f1 y (2) x (1) Scopes are easily marked with a number. Each { introduces a new scope – higher number

34 Dynamic Scope int x = 10; void f1(); void main() { int x = 20; f1(); cout << x; } void f1() { cout << x; } What’s the output? Global Main f1 x 10 x 20 Which x? Dynamic scope uses the closest x on the call stack Dynamic link

35 Scope vs. Lifetime Scope and lifetime are sometimes closely related, but are different concepts!! Consider a static variable in a C or C++ function void someFunction() { static int x;... }

36 Structure of Compilers Lexical Analyzer (scanner) Modified Source Program Syntax Analysis (Parser) Tokens Semantic Analysis Syntactic Structure Optimizer Code Generator Intermediate Representation Target machine code Symbol Table skeletal source program preprocessor

37 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

38 Intermediate Representation Intermediate Representations are also called by the form the intermediate language takes: tuples abstract syntax trees Triples Simplied language

39 Intermediate Form In general, an intermediate form is kept around only until the compiler generates code; then it cane be discarded. Another difference in compilers is how much of the program is kept in intermediate form; this is related to the question of how much of the program the compiler looks at before it starts to generate code. There is a wide spectrum of choices.

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

41 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