PROGRAMMING LANGUAGES

Slides:



Advertisements
Similar presentations
Grammar types There are 4 types of grammars according to the types of rules: – General grammars – Context Sensitive grammars – Context Free grammars –
Advertisements

Natural Language Processing - Formal Language - (formal) Language (formal) Grammar.
Translator Architecture Code Generator ParserTokenizer string of characters (source code) string of tokens abstract program string of integers (object.
Grammars, Languages and Parse Trees. Language Let V be an alphabet or vocabulary V* is set of all strings over V A language L is a subset of V*, i.e.,
COGN1001: Introduction to Cognitive Science Topics in Computer Science Formal Languages and Models of Computation Qiang HUO Department of Computer.
Chapter Chapter Summary Languages and Grammars Finite-State Machines with Output Finite-State Machines with No Output Language Recognition Turing.
Chapter 3: Formal Translation Models
Lecture 9UofH - COSC Dr. Verma 1 COSC 3340: Introduction to Theory of Computation University of Houston Dr. Verma Lecture 9.
Context-Free Grammars Chapter 3. 2 Context-Free Grammars and Languages n Defn A context-free grammar is a quadruple (V, , P, S), where  V is.
Languages and Grammars MSU CSE 260. Outline Introduction: E xample Phrase-Structure Grammars: Terminology, Definition, Derivation, Language of a Grammar,
1 INFO 2950 Prof. Carla Gomes Module Modeling Computation: Languages and Grammars Rosen, Chapter 12.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.
Languages & Strings String Operations Language Definitions.
Modeling Computation Rosen, ch. 12.
Introduction Syntax: form of a sentence (is it valid) Semantics: meaning of a sentence Valid: the frog writes neatly Invalid: swims quickly mathematics.
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.
Grammars CPSC 5135.
Discrete Structure Li Tak Sing( 李德成 ) Lectures
CSNB143 – Discrete Structure Topic 11 – Language.
Lecture # 5 Pumping Lemma & Grammar
Regular Grammars Chapter 7. Regular Grammars A regular grammar G is a quadruple (V, , R, S), where: ● V is the rule alphabet, which contains nonterminals.
Regular Grammars Chapter 7 1. Regular Grammars A regular grammar G is a quadruple (V, , R, S), where: ● V is the rule alphabet, which contains nonterminals.
Introduction Finite Automata accept all regular languages and only regular languages Even very simple languages are non regular (  = {a,b}): - {a n b.
CSC312 Automata Theory Lecture # 26 Chapter # 12 by Cohen Context Free Grammars.
Lecture 16: Modeling Computation Languages and Grammars Finite-State Machines with Output Finite-State Machines with No Output.
Formal Languages and Grammars
Discrete Structures ICS252 Chapter 5 Lecture 2. Languages and Grammars prepared By sabiha begum.
Transparency No. 1 Formal Language and Automata Theory Homework 5.
Models of Computation by Dr. Michael P. Frank, University of Florida Modified and extended by Longin Jan Latecki, Temple University Rosen 7 th ed., Ch.
1 A well-parenthesized string is a string with the same number of (‘s as )’s which has the property that every prefix of the string has at least as many.
Formal grammars A formal grammar is a system for defining the syntax of a language by specifying sequences of symbols or sentences that are considered.
Lecture 17: Theory of Automata:2014 Context Free Grammars.
Chapter 2. Formal Languages Dept. of Computer Engineering, Hansung University, Sung-Dong Kim.
Lecture #2 Advanced Theory of Computation. Languages & Grammar Before discussing languages & grammar let us deal with some related issues. Alphabet: is.
General Information on Context-free and Probabilistic Context-free Grammars İbrahim Hoça CENG784, Fall 2013.
20 G M aaba acba aaba.. What is it about? Models of Language Generation Models of Language Recognition.
Modeling Arithmetic, Computation, and Languages Mathematical Structures for Computer Science Chapter 8 Copyright © 2006 W.H. Freeman & Co.MSCS SlidesAlgebraic.
Describing Syntax and Semantics Chapter 3: Describing Syntax and Semantics Lectures # 6.
PROGRAMMING LANGUAGES
BCT 2083 DISCRETE STRUCTURE AND APPLICATIONS
Introduction to Formal Languages
Discrete Mathematics and its Applications Rosen 7th ed., Ch. 13.1
Context-Free Grammars: an overview
L-systems L-systems are grammatical systems introduced by Lyndenmayer to describe biological developments such as the growth of plants and cellular organisms.
Classification of Languages
Complexity and Computability Theory I
Natural Language Processing - Formal Language -
CS314 – Section 5 Recitation 3
CSE322 Chomsky classification
Context-Free Languages
Context-Free Grammars and Languages
A HIERARCHY OF FORMAL LANGUAGES AND AUTOMATA
Compilers CSCI/CMPE 3334 David Egle.
Regular Grammar.
Chapter 7 Regular Grammars
Discrete Mathematics and its Applications Rosen 6th ed., Ch. 12.1
Models of Computation by Dr. Michael P
CS21 Decidability and Tractability
CHAPTER 2 Context-Free Languages
Finite Automata and Formal Languages
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Discrete Mathematics and its Applications Rosen 7th ed., Ch. 13.1
Discrete Mathematics and its Applications Rosen 7th ed., Ch. 13.1
Theory of Computation Lecture #
Models of Computation by Dr. Michael P
Discrete Mathematics and its Applications Rosen 8th ed., Ch. 13.1
COSC 3340: Introduction to Theory of Computation
Formal Languages Context free languages provide a convenient notation for recursive description of languages. The original goal of formalizing the structure.
Models of Computation by Dr. Michael P
Languages and Grammer In TCS
Presentation transcript:

PROGRAMMING LANGUAGES CS 222 LECTURE 04 PROGRAMMING LANGUAGES

Grammar It is often convenient to specify languages in terms of grammars. The advantage in doing so arises mainly from the usage of a small number of rules for describing a language with a large number of sentences. For instance, the possibility that an English sentence consists of a subject phrase followed by a predicate phrase can be expressed by a grammatical rule of the form

Grammar <sentence>  <noun-phrase><verb-phrase> <noun-phrase>  <article><adjective><noun> <noun-phrase>  <article><noun> <verb-phrase>  <verb><adverb> | <verb> <article>  a | the <adjective>  large | hungry <noun>  rabbit | mathematician <verb>  eat | hops <adverb>  quickly | wildly

Grammar From these rules we can form valid sentences using a series of replacements until no more rules can be used. Example, the sentence is “the large rabbit hops quickly” we start with the rule : <sentence>  <noun-phrase><verb-phrase>  <article><adjective><noun> ><verb-phrase>  the<adjective><noun> ><verb-phrase>

Grammar the large<noun> ><verb-phrase> the large rabbit <verb-phrase> the large rabbit <verb><adverb> the large rabbit hops <adverb> the large rabbit hops quickly Let try this sentence : “a hungry mathematician eats wildly”

Grammar Let move to the formal definition of grammar Definition : A grammar < ,N,P,S> consists of four parts: 1. A finite set  of terminal symbols , the alphabet of the language, that areassembled to make up the sentences in the language. 2. A finite set N of nonterminal symbols , each of which represents some collection of subphrases of sentences. 3. A finite set P of productions or rules that describe how each nonterminal is defined in terms of terminal symbols and nonterminals. The choice of nonterminals determines the phrases of the language to which we ascribe meaning. 4. A distinguished nonterminal S, the start symbol , that specifies the principal category being defined—for example, sentence or program.

Classification of grammar Chomsky classified grammars according to the structure of their productions,suggesting four forms of particular usefulness, calling them type 0 through type 3 Type 0 :The most general grammars, the unrestricted grammars , require only that at least one nonterminal occur on the left side of a rule where α and β are strings of symbols in and α is not the empty string, and is a specially designated start symbol.

Type 0 : Example Let  = { a, b }, N = { A, B, S } , S = { S } P = S  ABa , A  BB , B  ab , AB  b

Classification of grammar Type 1 : When we add the restriction that the right side contains no fewer symbols than the left side, we get the context-sensitive grammars—for example, a rule of the form αβ → αγβ where α,β ∈ (N U Σ)+ (i.e., α and β are strings of nonterminals and terminals) and γ ∈ (N U Σ)+ (i.e., γ is a nonempty string of nonterminals and terminals).

Type 1 : Example P = S  aSbc S  aBC aB  ab bC  bc cb  bc  = { a,b,c } , N ={ S,B,C } P = S  aSbc S  aBC aB  ab bC  bc cb  bc This type 1 grammar is generate string anbncn where n  1

Classification of grammar Type 2 : The context-free grammars prescribe that the left side be a single nonterminal producing rules of the form A → w where A ∈ N (i.e., A is a single nonterminal), w ∈ (N U Σ)* (i.e., w are strings of nonterminals and terminals)

Type 2 : Example  = { a,b } , N ={ S,A} P = S  bSbb S  A A  aA This type 2 grammar is generate string

Classification of grammar Type 3 : The most restrictive grammars, the regular grammars , allow only a terminal or a terminal followed by one nonterminal on the right side—that is, rules of the form A → a - where A is a non-terminal in N and a is a terminal in Σ A → aB - where A and B are in N and a is in Σ A →  - where A is in N and  denotes the empty string, i.e. the string of length 0.

Type 3 : Example P = S → aS S → bA A →  A → cA  = { a,b,c } , N ={ S,A } P = S → aS S → bA A →  A → cA This type 3 grammar is generate string a*bc*

Derivations The cfg G that generates the language consisting of strings over ={a,b} with a even number of a G = (,N,P,S) , N={S,A} , ={a,b} P : S AA A  AAA | bA | Ab | a we want to generate string ababaa, we will write its derivation. So, we have two types of derivation which are leftmost and rightmost derivation.

Derivations S=> AA => aA => aAAA => abAAA => abaAA => ababAA => ababaA => ababaa => AAAA => Aa => AAAa => AAbAa => AAbaa => AbAbaa => Ababaa Leftmost Left most Rightmost

CFG Example The cfg G that generates the language consisting of strings over ={0,1} G = (,N,P,S) , N={A,B} , ={0,1} , S={A} P : A B |  B  0A1 we want to generate string 000111, write its leftmost or rightmost derivation.

Derivations Leftmost derivation A => B => 0A1 => 0B1 => 000111

Some Pascal production rules <expression>  <simple expression> <simple expression>  <term> | <sign><term> | <simple expression><adding operator><term> <adding operator>  + | - <multiplying operator>  * | / | div | mod <term>  <factor> | <term><multiplying operator><factor> <factor>  <identifier> | <unsigned constant> | (<expression>)

<unsigned constant>  <unsigned number> <unsigned number>  <unsigned integer> | <unsigned real> <unsigned integer>  <digit><unsigned integer> | <digit > <identifier>  <letter><identifier tail> <identifier tail>  <letter><identifier tail>| <digit><identifier tail> |  <sign>  + | - <digit> 0 | 1 | 2 |…| 9 <letter>  a | b | c | … | z

Example : Uses Pascal production rules to find the leftmost derivation that given the constant 65. Derivation is <expression>  <simple expression>  <term>  <factor>  <unsigned constant>  <unsigned number>  <unsigned integer>  <digit><unsigned integer>  6 <unsigned integer>  6<digit>  65

Try this Uses Pascal production rules to find the leftmost derivation that given -250. Uses Pascal production rules to find the leftmost derivation that given 5 * ( x + 15 ) Uses Pascal production rules to find the leftmost derivation that given ( (x – 10) div 2 )