10/16/081 Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC Based in part on slides by Mattox.

Slides:



Advertisements
Similar presentations
Programming Language Concepts
Advertisements

Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
5/24/20151 Programming Languages and Compilers (CS 421) Reza Zamani Based in part on slides by Mattox Beckman,
ISBN Chapter 3 Describing Syntax and Semantics.
CS5371 Theory of Computation
Chapter 3 Describing Syntax and Semantics Sections 1-3.
Context-Free Grammars Lecture 7
A basis for computer theory and A means of specifying languages
Chapter 3 Describing Syntax and Semantics Sections 1-3.
1 Foundations of Software Design Lecture 23: Finite Automata and Context-Free Grammars Marti Hearst Fall 2002.
Chapter 3 Describing Syntax and Semantics Sections 1-3.
Dr. Muhammed Al-Mulhem 1ICS ICS 535 Design and Implementation of Programming Languages Part 1 Fundamentals (Chapter 4) Compilers and Syntax.
Chapter 2 Syntax A language that is simple to parse for the compiler is also simple to parse for the human programmer. N. Wirth.
1 Syntax and Semantics The Purpose of Syntax Problem of Describing Syntax Formal Methods of Describing Syntax Derivations and Parse Trees Sebesta Chapter.
1 Introduction to Parsing Lecture 5. 2 Outline Regular languages revisited Parser overview Context-free grammars (CFG’s) Derivations.
CPSC 388 – Compiler Design and Construction Parsers – Context Free Grammars.
9/18/20151 Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC Based in part on slides by Mattox.
BİL 744 Derleyici Gerçekleştirimi (Compiler Design)1 Syntax Analyzer Syntax Analyzer creates the syntactic structure of the given source program. This.
CS 355 – PROGRAMMING LANGUAGES Dr. X. Topics Introduction The General Problem of Describing Syntax Formal Methods of Describing Syntax.
1 Chapter 3 Describing Syntax and Semantics. 3.1 Introduction Providing a concise yet understandable description of a programming language is difficult.
Classification of grammars Definition: A grammar G is said to be 1)Right-linear if each production in P is of the form A  xB or A  x where A and B are.
Context Free Grammars CIS 361. Introduction Finite Automata accept all regular languages and only regular languages Many simple languages are non regular:
Context-Free Grammars
Context-Free Grammars and Parsing
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
Grammars CPSC 5135.
PART I: overview material
C H A P T E R TWO Syntax and Semantic.
ISBN Chapter 3 Describing Syntax and Semantics.
10/25/20151 Programming Languages and Compilers (CS 421) Grigore Rosu 2110 SC, UIUC Slides by Elsa Gunter, based.
1 Syntax In Text: Chapter 3. 2 Chapter 3: Syntax and Semantics Outline Syntax: Recognizer vs. generator BNF EBNF.
11 Chapter 4 Grammars and Parsing Grammar Grammars, or more precisely, context-free grammars, are the formalism for describing the structure of.
CFG1 CSC 4181Compiler Construction Context-Free Grammars Using grammars in parsers.
CPS 506 Comparative Programming Languages Syntax Specification.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 3: Introduction to Syntactic Analysis.
ISBN Chapter 3 Describing Syntax and Semantics.
LESSON 04.
1Computer Sciences Department. Book: INTRODUCTION TO THE THEORY OF COMPUTATION, SECOND EDITION, by: MICHAEL SIPSER Reference 3Computer Sciences Department.
Grammars Hopcroft, Motawi, Ullman, Chap 5. Grammars Describes underlying rules (syntax) of programming languages Compilers (parsers) are based on such.
Grammars CS 130: Theory of Computation HMU textbook, Chap 5.
Chapter 3 Context-Free Grammars Dr. Frank Lee. 3.1 CFG Definition The next phase of compilation after lexical analysis is syntax analysis. This phase.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
2/1/20161 Programming Languages and Compilers (CS 421) Grigore Rosu 2110 SC, UIUC Slides by Elsa Gunter, based in.
Programming Languages and Design Lecture 2 Syntax Specifications of Programming Languages Instructor: Li Ma Department of Computer Science Texas Southern.
LECTURE 4 Syntax. SPECIFYING SYNTAX Programming languages must be very well defined – there’s no room for ambiguity. Language designers must use formal.
1 Introduction to Parsing. 2 Outline l Regular languages revisited l Parser overview Context-free grammars (CFG ’ s) l Derivations.
Programming Language Concepts (CIS 635) Elsa L Gunter 4303 GITC NJIT,
1 CS Programming Languages Class 04 September 5, 2000.
3/13/20161 Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC Based in part on slides by Mattox.
Copyright © 2006 Addison-Wesley. All rights reserved.1-1 ICS 410: Programming Languages Chapter 3 : Describing Syntax and Semantics Syntax.
Compiler Construction Lecture Five: Parsing - Part Two CSC 2103: Compiler Construction Lecture Five: Parsing - Part Two Joyce Nakatumba-Nabende 1.
Chapter 3 – Describing Syntax CSCE 343. Syntax vs. Semantics Syntax: The form or structure of the expressions, statements, and program units. Semantics:
Syntax(1). 2 Syntax  The syntax of a programming language is a precise description of all its grammatically correct programs.  Levels of syntax Lexical.
Chapter 3: Describing Syntax and Semantics
Chapter 3 – Describing Syntax
Programming Languages and Compilers (CS 421)
Programming Languages and Compilers (CS 421)
CS510 Compiler Lecture 4.
Introduction to Parsing (adapted from CS 164 at Berkeley)
Syntax (1).
Compiler Construction (CS-636)
Programming Languages and Compilers (CS 421)
Programming Languages and Compilers (CS 421)
CSC 4181Compiler Construction Context-Free Grammars
Programming Languages and Compilers (CS 421)
CSC 4181 Compiler Construction Context-Free Grammars
Programming Languages and Compilers (CS 421)
Programming Languages and Compilers (CS 421)
COMPILER CONSTRUCTION
Programming Languages and Compilers (CS 421)
Presentation transcript:

10/16/081 Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC Based in part on slides by Mattox Beckman, as updated by Vikram Adve and Gul Agha

10/16/08 2 Grammars Grammars are formal descriptions of which strings over a given character set are in a particular language Language designers write grammar Language implementers use grammar to know what programs to accept Language users use grammar to know how to write legitimate programs

10/16/08 3 Types of Formal Language Descriptions Regular expressions, regular grammars Context-free grammars, BNF grammars, syntax diagrams Finite state automata Whole family more of grammars and automata – covered in automata theory

10/16/08 4 Sample Grammar Language: Parenthesized sums of 0’s and 1’s ::= 0 ::= 1 ::= + ::= ( )

10/16/08 5 BNF Grammars Start with a set of characters, a,b,c,… We call these terminals Add a set of different characters, X,Y,Z,… We call these nonterminals One special nonterminal S called start symbol

10/16/08 6 BNF Grammars BNF rules (aka productions) have form X ::= y where X is any nonterminal and y is a string of terminals and nonterminals BNF grammar is a set of BNF rules such that every nonterminal appears on the left of some rule

10/16/08 7 Sample Grammar Terminals: ( ) Nonterminals: Start symbol = ::= 0 ::= 1 ::= + ::= ( ) Can be abbreviated as ::= 0 | 1 | + | ( )

10/16/08 8 BNF Deriviations Given rules X::= yZw and Z::=v we may replace Z by v to say X => yZw => yvw Sequence of such replacements called derivation Derivation called right-most if always replace the right-most non-terminal

10/16/08 9 BNF Derivations Start with the start symbol: =>

10/16/08 10 BNF Derivations Pick a non-terminal =>

10/16/08 11 Pick a rule and substitute: ::= + => + BNF Derivations

10/16/08 12 Pick a non-terminal: => + BNF Derivations

10/16/08 13 Pick a rule and substitute: ::= ( ) => + => ( ) + BNF Derivations

10/16/08 14 Pick a non-terminal: => + => ( ) + BNF Derivations

10/16/08 15 Pick a rule and substitute: ::= + => + => ( ) + => ( + ) + BNF Derivations

10/16/08 16 Pick a non-terminal: => + => ( ) + => ( + ) + BNF Derivations

10/16/08 17 Pick a rule and substitute: ::= 1 => + => ( ) + => ( + ) + => ( + 1 ) + BNF Derivations

10/16/08 18 Pick a non-terminal: => + => ( ) + => ( + ) + => ( + 1 ) + BNF Derivations

10/16/08 19 Pick a rule and substitute: ::= 0 => + => ( ) + => ( + ) + => ( + 1 ) + => ( + 1 ) + 0 BNF Derivations

10/16/08 20 Pick a non-terminal: => + => ( ) + => ( + ) + => ( + 1 ) + => ( + 1 ) + 0 BNF Derivations

10/16/08 21 Pick a rule and substitute ::= 0 => + => ( ) + => ( + ) + => ( + 1 ) + => ( + 1 ) 0 => ( ) + 0 BNF Derivations

10/16/08 22 ( ) + 0 is generated by grammar => + => ( ) + => ( + ) + => ( + 1 ) + => ( + 1 ) + 0 => ( ) + 0 BNF Derivations

10/16/08 23 ::= 0 | 1 | + | ( ) =>

10/16/08 24 BNF Semantics The meaning of a BNF grammar is the set of all strings consisting only of terminals that can be derived from the Start symbol

10/16/08 25 Extended BNF Grammars Alternatives: allow rules of from X::=y|z Abbreviates X::= y, X::= z Options: X::=y[v]z Abbreviates X::=yvz, X::=yz Repetition: X::=y{v}*z Can be eliminated by adding new nonterminal V and rules X::=yz, X::=yVz, V::=v, V::=vV

10/16/08 26 Regular Grammars Subclass of BNF Only rules of form ::= or ::= Defines same class of languages as regular expressions Important for writing lexers (programs that convert strings of characters into strings of tokens)

10/16/08 27 Example Regular grammar: ::=  ::= 0 ::= 1 ::= 0 Generates even length strings where every initial substring of even length has same number of 0’s as 1’s

10/16/08 28 Graphical representation of derivation Each node labeled with either non-terminal or terminal If node is labeled with a terminal, then it is a leaf (no sub-trees) If node is labeled with a non-terminal, then it has one branch for each character in the right-hand side of rule used to substitute for it Parse Trees

10/16/08 29 Example Consider grammar: ::= | + ::= | * ::= 0 | 1 Problem: Build parse tree for 1 * as an

10/16/08 30 Example cont. 1 * 1 + 0: is the start symbol for this parse tree

10/16/08 31 Example cont. 1 * 1 + 0: Use rule: ::=

10/16/08 32 Example cont. 1 * 1 + 0: * Use rule: ::= *

10/16/08 33 Example cont. 1 * 1 + 0: * 1 + Use rules: ::= 1 and ::= +

10/16/08 34 Example cont. 1 * 1 + 0: * 1 + Use rule: ::=

10/16/08 35 Example cont. 1 * 1 + 0: * Use rules: ::= 1 | 0

10/16/08 36 Example cont. 1 * 1 + 0: * Fringe of tree is string generated by grammar

10/16/08 37 Your Turn: 1 * * 1

10/16/08 38 Parse Tree Data Structures Parse trees may be represented by OCaml datatypes One datatype for each nonterminal One constructor for each rule Defined as mutually recursive collection of datatype declarations

10/16/08 39 Example Recall grammar: ::= | + ::= | * ::= 0 | 1 type exp = Factor2Exp of factor | Plus of factor * factor and factor = Bin2Factor of bin | Mult of bin * exp and bin = Zero | One

10/16/08 40 Example cont. 1 * 1 + 0: *

10/16/08 41 Example cont. Can be represented as Factor2Exp (Mult(One, Plus(Bin2Factor One, Bin2Factor Zero)))

10/16/08 42 Ambiguous Grammars and Languages A BNF grammar is ambiguous if its language contains strings for which there is more than one parse tree If all BNF’s for a language are ambiguous then the language is inherently ambiguous

10/16/08 43 Example: Ambiguous Grammar

10/16/08 44 Example What is the result for: * 5 + 6

10/16/08 45 Example What is the result for: * Possible answers: 41 = ((3 + 4) * 5) = 3 + (4 * (5 + 6)) 29 = (3 + (4 * 5)) + 6 = 3 + ((4 * 5) + 6) 77 = (3 + 4) * (5 + 6)

10/16/08 46 Example What is the value of: 7 – 5 – 2

10/16/08 47 Example What is the value of: 7 – 5 – 2 Possible answers: In Pascal, C++, SML assoc. left 7 – 5 – 2 = (7 – 5) – 2 = 0 In APL, associate to right 7 – 5 – 2 = 7 – (5 – 2) = 4

10/16/08 48 Two Major Sources of Ambiguity Lack of determination of operator precedence Lack of determination of operator assoicativity Not the only sources of ambiguity

10/16/08 49 How to Enforce Associativity Have at most one recursive call per production When two or more recursive calls would be natural leave right-most one for right assoicativity, left-most one for left assoiciativity

10/16/08 50 Example ::= 0 | 1 | + | ( ) Becomes ::= | + ::= 0 | 1 | ( )

10/16/08 51 Operator Precedence Operators of highest precedence evaluated first (bind more tightly). Precedence for infix binary operators given in following table Needs to be reflected in grammar

10/16/08 52 Precedence Table - Sample FortanPascalC/C++AdaSML highest***, /, div, mod ++, --**div, mod, /, * *, /+, -*, /, % *, /, mod +, -, ^ +, - ::

10/16/08 53 First Example Again In any above language, * = 29 In APL, all infix operators have same precedence Thus we still don’t know what the value is (handled by associativity) How do we handle precedence in grammar?

10/16/08 54 Predence in Grammar Higher precedence translates to longer derivation chain Example: ::= | + | * Becomes ::= | + ::= | *