CSE 3302 Programming Languages

Slides:



Advertisements
Similar presentations
Compiler Construction
Advertisements

Chapter 2-2 A Simple One-Pass Compiler
Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
Chapter 2 Syntax. Syntax The syntax of a programming language specifies the structure of the language The lexical structure specifies how words can be.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 330 Programming Language Structures Ch.2: Syntax and Semantics Fall 2005.
CS 330 Programming Languages 10 / 16 / 2008 Instructor: Michael Eckmann.
ALGOL 60 Design by committee of computer scientists: Naur, Backus, Bauer, McCarthy, van Wijngaarden, Landin, etc. Design by committee of computer scientists:
Chapter3: Language Translation issues
Chapter 3 Program translation1 Chapt. 3 Language Translation Syntax and Semantics Translation phases Formal translation models.
Yu-Chen Kuo1 Chapter 2 A Simple One-Pass Compiler.
(2.1) Grammars  Definitions  Grammars  Backus-Naur Form  Derivation – terminology – trees  Grammars and ambiguity  Simple example  Grammar hierarchies.
CSC 8310 Programming Languages Meeting 2 September 2/3, 2014.
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
Bindings and scope  Bindings and environments  Scope and block structure  Declarations Programming Languages 3 © 2012 David A Watt, University.
Semantics CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University
1 Names, Scopes and Bindings Aaron Bloomfield CS 415 Fall
CSE 3302 Programming Languages Chengkai Li, Weimin He Spring 2008 Syntax (cont.) Lecture 4 – Syntax (Cont.), Spring CSE3302 Programming Languages,
Lesson 3 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
3-1 Chapter 3: Describing Syntax and Semantics Introduction Terminology Formal Methods of Describing Syntax Attribute Grammars – Static Semantics Describing.
Basic Semantics Attributes, Bindings, and Semantic Functions
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Basic Semantics Associating meaning with language entities.
CPS 506 Comparative Programming Languages Syntax Specification.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
D Goforth COSC Translating High Level Languages Note error in assignment 1: #4 - refer to Example grammar 3.4, p. 126.
The Functions and Purposes of Translators Syntax (& Semantic) Analysis.
1 Parsers and Grammar. 2 Categories of Grammar Rules  Declarations or definitions. AttributeDeclaration ::= [ final ] [ static ] [ access ] datatype.
Semantics CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
CSE 3302 Programming Languages
Lecture 9 Symbol Table and Attributed Grammars
COMPILER CONSTRUCTION
Chapter 3 – Describing Syntax
Names and Attributes Names are a key programming language feature
A Simple Syntax-Directed Translator
Constructing Precedence Table
CS510 Compiler Lecture 4.
Lecture #12 Parsing Types.
Chapter 3 Context-Free Grammar and Parsing
Chapter 3 – Describing Syntax
CSE 3302 Programming Languages
Parsing with Context Free Grammars
Chapter 8: Control Structures
Semantic Analysis Chapter 6.
4 (c) parsing.
CSE 3302 Programming Languages
CPSC 388 – Compiler Design and Construction
Syntax-Directed Definition
Mini Language Interpreter Programming Languages (CS 550)
Chapter 2: A Simple One Pass Compiler
CSE 3302 Programming Languages
COP4020 Programming Languages
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
CSE401 Introduction to Compiler Construction
Chapter 2: A Simple One Pass Compiler
Lecture 7: Introduction to Parsing (Syntax Analysis)
R.Rajkumar Asst.Professor CSE
CSCE 330 Programming Language Structures Ch.2: Syntax and Semantics
LL and Recursive-Descent Parsing Hal Perkins Autumn 2011
Designing a Predictive Parser
Statement-Level Control Structures
LL and Recursive-Descent Parsing
Semantic Analysis Chapter 6.
CSE 3302 Programming Languages
BNF 9-Apr-19.
LL and Recursive-Descent Parsing Hal Perkins Autumn 2009
CSE 3302 Programming Languages
LL and Recursive-Descent Parsing Hal Perkins Winter 2008
COMPILER CONSTRUCTION
Faculty of Computer Science and Information System
Presentation transcript:

CSE 3302 Programming Languages Syntax (cont.) Semantics Chengkai Li Fall 2007 Lecture 3 - Syntax, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007 Parsing Parsing: Determine if a grammar can generate a given token string. That is, to construct a parse tree for the token string. Two ways of constructing the parse tree Top-down (from root towards leaves) Can be constructed more easily by hand Bottom-up (from leaves towards root) Can handle a larger class of grammars Parser generators tend to use bottom-up methods Lecture 3 - Syntax, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007 Top-Down Parser Recursive-descent parser: A special kind of top-down parser: single left-to-right scan, with one lookahead symbol. Backtracking (trial-and-error) may happen Predictive parser: The lookahead symbol determines which production to apply, without backtracking Lecture 3 - Syntax, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

Recursive-Descent Parser Types in Pascal type ® simple | array [ simple ] of type simple ® integer | char Input: array [ integer ] of char Parse tree type simple array type [ ] of integer simple char Lecture 3 - Syntax, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

Challenge 1: Top-Down Parser Cannot Handle Left-Recursion expr ® expr - term | term term ® 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 Input: 3 – 4 – 5 Parse tree expr term - expr term - expr expr term - Lecture 3 - Syntax, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

Eliminating Left-Recursion expr ® expr - term | term term ® 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 expr ® term expr’ expr’ ® - term expr’ | ε (more complete example in Fig. 4.12, page 105-107) void expr(void) { term(); while (token == ‘-’) { match(‘-’); term(); } Lecture 3 - Syntax, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

Challenge 2: Backtracking is Inefficient Backtracking: trial-and-error type ® simple | array [ simple ] of type simple ® integer | char (Types in Pascal ) Input: array [ integer ] of char Parse tree type simple simple array type [ ] of char integer X Lecture 3 - Syntax, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

Challenge 2: Backtracking is Inefficient subscription ® term | term .. term term ® 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 We cannot avoid backtracking if the grammar has multiple productions to apply, given a lookahead symbol. Solution: Predictive Parser if we cannot decide early, defer the decision until we have seen enough. Change the grammar so that there is only one applicable production that is unambiguously determined by lookahead symbol. Lecture 3 - Syntax, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

Avoiding Backtracking by Left Factoring A ®  A’ A’ ® 1 | 2 expr ® term | term .. term term ® 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 expr ® term rest rest ® .. term | ε term ® 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 Lecture 3 - Syntax, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007 EBNF Left-recursion removal and left factoring make grammar rules hard to write and read, and difficult to construct parser EBNF: Simplified notations, building predicative parser is simple { } repetition [ ] optional expr ® expr - term | term expr ® term {- term} if-stmt ® if ( expr ) stmt | if ( expr ) stmt else stmt if-stmt ® if ( expr ) stmt [ else stmt ] Lecture 3 - Syntax, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007 Syntax Diagrams Written from EBNF, not BNF If-statement (more examples on page 101) if else statement expression ( ) if-statement Lecture 3 - Syntax, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007 Review of Parsing Grammar for Integer Arithmetic Expression Ambiguity 1: precedence (eliminated by precedence cascade) expr ® expr + expr | expr  expr | ( expr ) | number number ® number digit | digit digit ® 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 expr ® expr + expr | term term ® term  term | ( expr ) | number number ® number digit | digit digit ® 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 Lecture 3 - Syntax, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007 Review of Parsing Ambiguity 2: Associativity (eliminated by recursion) Ambiguity 3: Dangling-Else expr ® expr + term | term term ® term  factor | factor factor ®( expr ) | number number ® number digit | digit digit ® 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 Lecture 3 - Syntax, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007 Review of Parsing Parsers top-down (construct the parse tree from root towards leaves) recursive-descent (left-to-right scan, using single lookahead symbol) predicative parser (no backtracking, the lookahead symbol unambiguously determines the production to apply) bottom-up (construct the parse tree from leaves towards root) Lecture 3 - Syntax, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007 Review of Parsing Allowing Top-Down Parsers : Eliminating Left-Recursion Allowing Predicative Parsers: Removing Left-Factoring Alternative representations: make predicative parser easy to write EBNF Syntax Diagram Lecture 3 - Syntax, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007 Semantics Lecture 3 - Syntax, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007 Names Names: identify language entities variables, procedures, functions, constants, data types, … Attributes: properties of names Examples of attributes: Data type: int n = 5; ( data type: integer) Note that int itself is a name. Value: ( value: 5) Location: int * y; y = new int; ( a new value for y, a new location for *y) Parameters, return value: int func(int n) {...} … Lecture 3 - Syntax, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007 Binding Time Binding Time: the time when an attribute is bound to a name. Static binding (static attribute): occurs before execution Language definition/implementation time: The range of data type int translation time (parsing/semantic analysis): The data type of a variable link time: The body of external function load time: Location of global variable Dynamic binding (dynamic attribute): occurs during execution entry/exit from procedure or program: the value of local variables Lecture 3 - Syntax, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

K. Louden, Programming Languages Binding Binding: associating attributes to names declarations assignments declarations (prototype) and definition of a function The bindings can be explicit or implicit e.g. int x; Explicit binding: the data type of x Implicit binding: the location of x (static or dynamic, depending on where the declaration is) The declaration itself can be implicit: FORTRAN: no need to declare variables begin with I,J,K,L,M,N (integer variables) Chapter 5 K. Louden, Programming Languages

Where can declarations happen? Blocks ({}, begin-end, … Algol descendants: C/C++, Java, Pascal, Ada, …) e.g., C Function body Anywhere a statement can appear (compound statement) External/global structured data type class Lecture 3 - Syntax, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007 C++ Example … const int Maximum = 100; struct FullName {string Lastname, string FirstName}; class Student { private: struct FullName name; int Age; public: void setValue(const int a, struct FullName name); int TStudent(); }; void Student::setAge(const int a, string lName, string fName) { int i; Age = a; { int j; name.LastName = lName; name.FirstName = fName; } Lecture 3 - Syntax, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007 Scope of Binding Scope of Binding: the region of the program where the binding is maintained (is valid, applies). (Scope of Declaration: if all relevant bindings by the declaration have identical scope.) Block-structured language lexical scope (static scope): from the declaration to the end of the block containing the declaration. dynamic scope : introduced later. Lecture 3 - Syntax, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007 Example int x; void p(void) { char y; . . . { int i; } void q(void) { double z; main() { int w[10]; y i x p z q main w Lecture 3 - Syntax, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

Declaration before Use void p(void) { int x; . . . char y; } Exception in OO languages: Scope of local declarations inside a class declaration includes the whole class. public class { public int getValue() { return value; } int value; x y value Lecture 3 - Syntax, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007 Scope Hole Scope Hole: Declarations in nested blocks take precedence over the previous declarations. That is, binding becomes invisible/hidden. int x; void p(void) { char x; x = ‘a’; . . . } main() { x = 2; x (bound with character data type) x (bound with integer data type) Lecture 3 - Syntax, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

Access Hidden Declarations scope resolution operator :: (C++) int x; void p(void) { char x; x = ‘a’; ::x=42; . . . } main() { x = 2; x (bound with character data type) x (bound with integer data type) the hidden integer variable x Lecture 3 - Syntax, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007 Hide a Declaration File 1: File 2: extern int x; int x; extern int x; static int x;  Lecture 3 - Syntax, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007 Symbol Table Symbol Table: maintain bindings. Can be viewed as functions that map names to their attributes. Names Attributes SymbolTable Lecture 3 - Syntax, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007 Symbol Table int x = 1; char y = ‘a’; void p(void) { double x=2.5; printf(“%c\n”,y); } void q(void) { int y = 42; printf(“%d\n”,x); p(); main() { char x = ‘b’; q(); x integer, 1, global y character, ‘a’, global Lecture 3 - Syntax, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007 Symbol Table int x = 1; char y = ‘a’; void p(void) { double x=2.5; printf(“%c\n”,y); } void q(void) { int y = 42; printf(“%d\n”,x); p(); main() { char x = ‘b’; q(); The symbol table in p: the bindings available in p double, 2.5, local to p x integer, 1, global y character, ‘a’, global Lecture 3 - Syntax, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007 Symbol Table int x = 1; char y = ‘a’; void p(void) { double x=2.5; printf(“%c\n”,y); } void q(void) { int y = 42; printf(“%d\n”,x); p(); main() { char x = ‘b’; q(); The symbol table in q: the bindings available in q x integer, 1, global integer, 42, local to q y character, ‘a’, global Lecture 3 - Syntax, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007 Symbol Table int x = 1; char y = ‘a’; void p(void) { double x=2.5; printf(“%c\n”,y); } void q(void) { int y = 42; printf(“%d\n”,x); p(); main() { char x = ‘b’; q(); The symbol table in main: the bindings available in main character, ‘b’, local to main x integer, 1, global y character, ‘a’, global Lecture 3 - Syntax, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007 Symbol Table The symbol table in previous slides are built during compilation The bindings are used in generating the machine code Result: 1 a E.g., semantics of q The symbol table in q: the bindings available in q x integer, 1, global void q(void) { int y = 42; printf(“%d\n”,x); p(); } integer, 42, local to q y character, ‘a’, global Lecture 3 - Syntax, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

Static vs. Dynamic Scopes Static scope (lexical scope): scope maintained statically (during compilation) follow the layout of source codes used in most languages Dynamic scope: scope maintained dynamically (during execution) follow the execution path few languages use it (The bindings cannot be determined statically, may depend on user input). Lisp: considered a bug by its inventor. Perl: can choose lexical or dynamic scope Lecture 3 - Syntax, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

What if it used dynamic scope? int x = 1; char y = ‘a’; void p(void) { double x=2.5; printf(“%c\n”,y); } void q(void) { int y = 42; printf(“%d\n”,x); p(); main() { char x = ‘b’; q(); x integer, 1, global y character, ‘a’, global Lecture 3 - Syntax, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

What if it used dynamic scope? int x = 1; char y = ‘a’; void p(void) { double x=2.5; printf(“%c\n”,y); } void q(void) { int y = 42; printf(“%d\n”,x); p(); main() { char x = ‘b’; q(); The symbol table in main: the bindings available in main character, ‘b’, local to main x integer, 1, global y character, ‘a’, global Lecture 3 - Syntax, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

What if it used dynamic scope? int x = 1; char y = ‘a’; void p(void) { double x=2.5; printf(“%c\n”,y); } void q(void) { int y = 42; printf(“%d\n”,x); p(); main() { char x = ‘b’; q(); The symbol table in q: the bindings available in q character, ‘b’, local to main x integer, 1, global 98 integer, 42, local to q y character, ‘a’, global Lecture 3 - Syntax, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

What if it used dynamic scope? int x = 1; char y = ‘a’; void p(void) { double x=2.5; printf(“%c\n”,y); } void q(void) { int y = 42; printf(“%d\n”,x); p(); main() { char x = ‘b’; q(); The symbol table in p: the bindings available in p double, 2.5, local to p character, ‘b’, local to main x integer, 1, global 98 * integer, 42, local to q y character, ‘a’, global Lecture 3 - Syntax, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007