Programming Languages Ezra K. Mugisa, PhD (London), DIC (Imperial College) Visiting Academic Institute Of Computer Science Makerere University Otherwise.

Slides:



Advertisements
Similar presentations
C O N T E X T - F R E E LANGUAGES ( use a grammar to describe a language) 1.
Advertisements

Lecture # 8 Chapter # 4: Syntax Analysis. Practice Context Free Grammars a) CFG generating alternating sequence of 0’s and 1’s b) CFG in which no consecutive.
Grammars, constituency and order A grammar describes the legal strings of a language in terms of constituency and order. For example, a grammar for a fragment.
Chapter Chapter Summary Languages and Grammars Finite-State Machines with Output Finite-State Machines with No Output Language Recognition Turing.
ICE1341 Programming Languages Spring 2005 Lecture #4 Lecture #4 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University.
ISBN Chapter 3 Describing Syntax and Semantics.
CS5371 Theory of Computation
CS 330 Programming Languages 09 / 13 / 2007 Instructor: Michael Eckmann.
Chapter 3 Describing Syntax and Semantics Sections 1-3.
PZ02A - Language translation
Context-Free Grammars Lecture 7
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.
Normal forms for Context-Free Grammars
Dr. Muhammed Al-Mulhem 1ICS ICS 535 Design and Implementation of Programming Languages Part 1 Fundamentals (Chapter 4) Compilers and Syntax.
COP4020 Programming Languages
EECS 6083 Intro to Parsing Context Free Grammars
1 Syntax and Semantics The Purpose of Syntax Problem of Describing Syntax Formal Methods of Describing Syntax Derivations and Parse Trees Sebesta Chapter.
Chapter 4 Context-Free Languages Copyright © 2011 The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 1.
Formal Grammars Denning, Sections 3.3 to 3.6. Formal Grammar, Defined A formal grammar G is a four-tuple G = (N,T,P,  ), where N is a finite nonempty.
BİL 744 Derleyici Gerçekleştirimi (Compiler Design)1 Syntax Analyzer Syntax Analyzer creates the syntactic structure of the given source program. This.
A sentence (S) is composed of a noun phrase (NP) and a verb phrase (VP). A noun phrase may be composed of a determiner (D/DET) and a noun (N). A noun phrase.
Languages, Grammars, and Regular Expressions Chuck Cusack Based partly on Chapter 11 of “Discrete Mathematics and its Applications,” 5 th edition, by Kenneth.
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.
Grammars CPSC 5135.
PART I: overview material
Lecture # 9 Chap 4: Ambiguous Grammar. 2 Chomsky Hierarchy: Language Classification A grammar G is said to be – Regular if it is right linear where each.
ISBN Chapter 3 Describing Syntax and Semantics.
TextBook Concepts of Programming Languages, Robert W. Sebesta, (10th edition), Addison-Wesley Publishing Company CSCI18 - Concepts of Programming languages.
Copyright © by Curt Hill Grammar Types The Chomsky Hierarchy BNF and Derivation Trees.
1 Syntax In Text: Chapter 3. 2 Chapter 3: Syntax and Semantics Outline Syntax: Recognizer vs. generator BNF EBNF.
Copyright © Curt Hill Languages and Grammars This is not English Class. But there is a resemblance.
CMSC 330: Organization of Programming Languages Context-Free Grammars.
Parsing Introduction Syntactic Analysis I. Parsing Introduction 2 The Role of the Parser The Syntactic Analyzer, or Parser, is the heart of the front.
The College of Saint Rose CIS 433 – Programming Languages David Goldschmidt, Ph.D. from Concepts of Programming Languages, 9th edition by Robert W. Sebesta,
1 Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
ISBN Chapter 3 Describing Syntax and Semantics.
CS 208: Computing Theory Assoc. Prof. Dr. Brahim Hnich Faculty of Computer Sciences Izmir University of Economics.
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.
Grammars A grammar is a 4-tuple G = (V, T, P, S) where 1)V is a set of nonterminal symbols (also called variables or syntactic categories) 2)T is a finite.
Introduction Finite Automata accept all regular languages and only regular languages Even very simple languages are non regular (  = {a,b}): - {a n b.
Syntax Analyzer (Parser)
Context-Free Languages
CSC312 Automata Theory Lecture # 26 Chapter # 12 by Cohen Context Free Grammars.
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.
Chapter 4: Syntax analysis Syntax analysis is done by the parser. –Detects whether the program is written following the grammar rules and reports syntax.
Compiler Construction Lecture Five: Parsing - Part Two CSC 2103: Compiler Construction Lecture Five: Parsing - Part Two Joyce Nakatumba-Nabende 1.
Syntax Analysis By Noor Dhia Syntax analysis:- Syntax analysis or parsing is the most important phase of a compiler. The syntax analyzer considers.
Chapter 3 – Describing Syntax CSCE 343. Syntax vs. Semantics Syntax: The form or structure of the expressions, statements, and program units. Semantics:
Modeling Arithmetic, Computation, and Languages Mathematical Structures for Computer Science Chapter 8 Copyright © 2006 W.H. Freeman & Co.MSCS SlidesAlgebraic.
Chapter 3 – Describing Syntax
Describing Syntax and Semantics
Context-Free Grammars: an overview
CS510 Compiler Lecture 4.
Natural Language Processing - Formal Language -
Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
Lecture 7: Introduction to Parsing (Syntax Analysis)
CHAPTER 2 Context-Free Languages
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
COMPILER CONSTRUCTION
Presentation transcript:

Programming Languages Ezra K. Mugisa, PhD (London), DIC (Imperial College) Visiting Academic Institute Of Computer Science Makerere University Otherwise … Head, Computer Science Section Department of Mathematics & Computer Science The University of the West Indies Kingston, Jamaica

Related Courses MCSC 610: Survey of Computer Languages (3 CU) Content: Organization and types of programming languages. Analysis of imperative, object-oriented, and declarative language paradigms. A study and comparative analysis of high-level languages, fourth- generation languages, and command languages used in the development of software for management information systems. The logical and physical structure of programs and data. Concepts of structured programming. Data structures, file management, and their use in problem-solving. Students will complete a variety of high-level language computer programs. References :

MCSC 611: Programming Languages (3 CU) Content: Formal languages and language hierarchies, syntactic and semantic specification, abstract machines and corresponding languages, context-free languages, abstraction, modularity, and program structure. Fundamental programming language concepts. Analysis of imperative, object-oriented, and declarative language paradigms. Several programming languages will be analyzed. References : 1.Title:Programming Languages; Author:Terrence W. Pratt, Marvin V. Zelkowitz; ISBN: ; Edition:4th edition 2001; Publisher: Prentice Hall 2.Title: Programming Languages: Concepts and Constructs; Author: Ravi Sethi, AT&T Bell Laboratories; ISBN: ; Edition:2nd edition ; Publisher: Addison Wesley

Topics To Be Covered 1.Formal languages and language hierarchies 2.Syntactic and semantic specification 3.Abstract machines and corresponding languages 4.Context-free languages 5.Abstraction, modularity and program structure 6.Fundamental programming language concepts 7.Analysis of Programming Paradigms and the Languages that support them 1.imperative 2.object-oriented 3.declarative 8.(Several programming languages will be analysed.)

So What Is The Course About? This is not a survey course (see MCSC610) We shall develop a Theory of Programming Languages –i.e. we strive for a thorough understanding of what all programming languages are about Our TPL will essentially tell a story about programming languages The TPL will consist of –Coherent facts about Programming Languages –Ways of deriving new facts and verifying them

Course Objectives At the end of the course students should be able to –Define a programming language, essentially as a context-free language –Describe an abstract machine for context-free languages –Identify essential programming language concepts –Relate the (historical) development of programming languages to programming paradigms –Have a unifying view of programming languages

Formal Languages And Language Hierarchies We distinguish between Formal languages and Natural languages. A formal language is man-made (in very simple terms) whereas a natural language develops in less controlled ways. All programming languages are formal. Examples of natural languages include English, Luganda, Swahili, French – basically the languages humans (and other creatures) use for communicating with each other.

Formal Vs. Natural Formal languages are distinguished by an exact syntax (structure) and (hopefully) a precise semantics (meaning) as well. Typically if the syntax of a piece of communication is incorrect it will be rejected by the receiving agent, as its meaning may be difficult to ascertain.

Syntax Is Defined By A Grammar A formal grammar, G, is a finite formal description that defines (generates) a language, L, over some alphabet An alphabet denotes a finite set of symbols G defines the set of valid sentences in L A sentence is a sequence of symbols from a given alphabet L(G) is "the language defined by G"

Defining A Grammar A grammar is the 4-tuple: G = (N,T,P,S) where –N is a finite set of non-terminals (or rule names) –T is a finite set of terminals (token names) –P is a finite set of productions (rules); (a production has a left-hand and a right-hand side and may be seen as a rewriting rule) –S  N is the start symbol (the highest level rule - the goal)

An Example G 1 = (N, T, P, S) N = { E, I} T = {1,2,3,4,5, +, *} S = E P consists of the following productions: E → I E → E + E E → E * E I → 1 | 2 | 3 | 4 | 5

A Grammar Defines A Language A Grammar G defines language L(G) L(G) is the set of all possible terminal strings w that you can derive by starting at S and repeatedly applying rules (productions) Applying a rule means to replace the left- hand side of the rule with its right-hand- side

α Derives ß If you can convert α to ß by applying rules, we say that α derives ß or α  * ß where  means to derive in one step and  * means to derive in zero-or-more steps  + means to derive in one-or-more steps

An Example The language defined by G 1 is denoted by L(G 1 ) L(G 1 ) =

Deriving A String A partially derived string is called a sentential form and may contain both terminals and non-terminals If S  * α then α is a sentential form e.g. these sentential forms are generated by G: I + E, 5 + E, 3 * I A sentence contains only terminals

Language L(G) L(G) = {w  T* | S  * w}. That is, L(G) is the set of all sentences that can be reached by a derivation from the start symbol, S.

Is A Sentence Generated By A Grammar? This is the first question we try to answer when we compile our programs If you can find a derivation from S to the target sentence, then that sentence is in L(G) In other words if w  T* and S  * w then w  L(G)

An Example Is abc generated by the grammar with the following productions? A → Bc B → ab

Example (Contd.) Yes, the derivation looks like this A  Bc  abc Therefore since A  * abc abc must be a valid sentence of L(G) i.e. abc  L(G)

Leftmost And Rightmost Derivations Sometimes you have a choice as to which non-terminal to replace in a sentential form Let us try to derive the sentence 4 * 5 from G 1

Deriving 4 * 5 From G 1 We find that there are multiple derivations How then should we proceed or does it matter? The 2 extremes are 1.Replace the leftmost non-terminal first 2.Replace the rightmost non-terminal first

It Does Not Matter Every sentence has both leftmost and rightmost derivations The order of replacement differs but the same rules are applied for each non-terminal So the sentence structure is the same and, hence, has the same "meaning" It is just examined in a different order We may actually mix the 2 strategies in the same derivation

Derivation Trees Ignore The Order A derivation tree is a two-dimensional tree that records the derivation from start symbol to sentence. Interior nodes are non-terminals and leaves are terminals. Derivation trees are insensitive to derivation order (leftmost or rightmost)--you get the same tree regardless. In fact you can think of them as expanding all non- terminals in parallel. Only changing which rule you apply changes the tree.

Constructing A Derivation Tree Begin construction by creating a root node labelled with start symbol, S. Then, until every leaf node is a terminal node, add a child to non-terminal leaf node, A, for each symbol in α when applying rule A → α.

An Example Construct derivation trees for –4 + 5 –4 + 5 * 3

Does Rule Order Matter? To check w  L(G): 1.you choose which non-terminal in a sentential form to replace and 2.which rule to apply For a valid sentence, there will normally be exactly one replacement choice that will lead to complete derivation What happens when you have a choice of rules to apply, both of which yield valid derivations?

Ambiguous Grammar When you have a choice of rules in a valid derivation this means that … The derived sentence has multiple interpretations (or meanings) Your grammar is said to be ambiguous

An (Ambiguous) Example Let us derive * 3

Derivation Tree T 1

Derivation Tree T 2

Two Different Meanings Notice that grammar G 1 does not assign meaning to the symbols or to the productions. If we assign the normal arithmetic meaning to the symbols used in G 1 : Derivation tree T 1 gives : Derivation tree T 2 gives :

Can We Tell? There is no algorithm (it is undecidable) to check to see if a CFG is ambiguous In contrast, for certain subsets of CFGs, you can show that they are unambiguous Specifically, if you can generate a valid parser from the grammar There are tools that can help us

Grammar Types In the 1950's, Noam Chomsky defined a classification or hierarchy of grammars that neatly categorises the difficulty of describing a language.Noam Chomskyhierarchy of grammars There are four categories: Type-0: unrestricted Type-1: Context-sensitive grammar Type-2: Context-free grammar (CFG) Type-3: Regular grammar The difference is in the form of the productions

Comparison Power: Type-3  Type-2  Type-1  Type-0 Type 0 and type 1 languages are not used for computer languages because their power is not needed and no efficient means of generating parsers for them is known. Fortunately, for most things, a context-free grammar immediately presents itself or you can rephrase your problem such that a context-free grammar suffices.

Unrestricted Type-0: unrestricted; all formal grammars. Generates all languages recognizable by a Turing machine. That is, you can write a program by hand to recognize it, even if it's hard to do. Grammars have no restrictions on their form.

Context-Sensitive Grammar Type-1: Context-sensitive grammar. grammars where you can restrict validity of applying a rule to a certain context. Generates the context-sensitive languages has productions of the form: αAβ → αγβ where γ is non-empty A  N α, β, γ  (N  T)* (but you can have S → ε as long as S is not on the right hand side of a production). Replace A with γ but only in the context of α and β

Context-Free Grammar Type-2: Context-free grammar. Generates the context-free languages. These languages can all be recognised with pushdown automata. Productions have the form: A → α where A  N α  (N  T)*

Regular Grammar Type-3: Regular grammars; Generates the regular languages, these can be recognized with simple deterministic finite automata (DFA). Productions have the form: A → a or A → aB | Ba where A, B  N a  T you can have S → ε as long as S is not on the right hand side of a production. We use regular expressions in practice.

Syntactic and Semantic Specification

Abstract Machines and Corresponding Languages

Context-Free Languages

Abstraction

Modularity

Program Structure

Fundamental Programming Language Concepts

Analysis of Language Paradigms A Paradigm is (From Wikipedia) : –(in the vernacular) a pattern behind a set of typical examples of somethingpattern –(in philosophy of science) a Kuhnian paradigmKuhnianparadigm –(in experimental science) an experimental setupexperimental setup –(in computing) a paradigm is a style of programming, usually enforced by the programming language used.paradigmprogrammingprogramming language –(in linguistics) an inflection paradigminflection paradigm 1.We look at 3 programming paradigms and language support for them. 2.The three are: –imperative, –object-oriented, and –declarative language paradigms. 1.Several programming languages will be analysed.