Grammars.

Slides:



Advertisements
Similar presentations
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.
Advertisements

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.
Context-Free Grammars Sipser 2.1 (pages 99 – 109).
1 Introduction to Computability Theory Lecture5: Context Free Languages Prof. Amos Israeli.
Context-Free Grammars Sipser 2.1 (pages 99 – 109).
CS5371 Theory of Computation
CSC 3130: Automata theory and formal languages Andrej Bogdanov The Chinese University of Hong Kong Context-free.
Costas Busch - RPI1 Grammars. Costas Busch - RPI2 Grammars Grammars express languages Example: the English language.
1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition.
1 Lecture 29 Context Free Grammars –Examples of “real-life” grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language.
Courtesy Costas Busch - RPI
Transparency No. P2C1-1 Formal Language and Automata Theory Part II Pushdown Automata and Context-Free Languages.
Chapter 3: Formal Translation Models
1 Context-Free Languages. 2 Regular Languages 3 Context-Free Languages.
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.
Prof. Busch - LSU1 Context-Free Languages. Prof. Busch - LSU2 Regular Languages Context-Free Languages.
Chapter 4 Context-Free Languages Copyright © 2011 The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 1.
Context-Free Grammars
Languages & Strings String Operations Language Definitions.
Lecture 16 Oct 18 Context-Free Languages (CFL) - basic definitions Examples.
CS/IT 138 THEORY OF COMPUTATION Chapter 1 Introduction to the Theory of Computation.
Context-free Grammars Example : S   Shortened notation : S  aSaS   | aSa | bSb S  bSb Which strings can be generated from S ? [Section 6.1]
1 Context-Free Languages Not all languages are regular. L 1 = {a n b n | n  0} is not regular. L 2 = {(), (()), ((())),...} is not regular.  some properties.
Lecture # 19. Example Consider the following CFG ∑ = {a, b} Consider the following CFG ∑ = {a, b} 1. S  aSa | bSb | a | b | Λ The above CFG generates.
Grammars CPSC 5135.
Languages & Grammars. Grammars  A set of rules which govern the structure of a language Fritz Fritz The dog The dog ate ate left left.
1 Context-Free Languages. 2 Regular Languages 3 Context-Free Languages.
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.
Context-Free Grammars Chapter 11. Languages and Machines.
Chapter 3 Describing Syntax and Semantics
Context Free Grammars CFGs –Add recursion to regular expressions Nested constructions –Notation expression  identifier | number | - expression | ( expression.
CS 3240 – Chapter 5. LanguageMachineGrammar RegularFinite AutomatonRegular Expression, Regular Grammar Context-FreePushdown AutomatonContext-Free Grammar.
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.
Formal Languages and Grammars
Context Free Grammars & Parsing CPSC 388 Fall 2001 Ellen Walker Hiram College.
Chapter 1 INTRODUCTION TO THE THEORY OF COMPUTATION.
COP4620 – Programming Language Translators Dr. Manuel E. Bermudez
PROGRAMMING LANGUAGES
CONTEXT-FREE LANGUAGES
Context-Free Grammars: an overview
Context-Free Languages
Formal Language & Automata Theory
Grammars Module 03.2 COP4020 – Programming Language Concepts Dr. Manuel E. Bermudez.
Language and Grammar classes
The chomsky hierarchy Module 03.3 COP4020 – Programming Language Concepts Dr. Manuel E. Bermudez.
CSE 105 theory of computation
Compiler Construction
Context-Free Languages
Context-Free Grammars and Languages
Context-Free Grammars
Relationship to Left- and Rightmost Derivations
CSE322 LEFT & RIGHT LINEAR REGULAR GRAMMAR
Regular Grammar.
Context-Free Grammars (CFG’s)
CS21 Decidability and Tractability
CHAPTER 2 Context-Free Languages
CSE 311: Foundations of Computing
Chapter Twelve: Context-Free Languages
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Theory of Computation Lecture #
Chapter 2 Context-Free Language - 02
Teori Bahasa dan Automata Lecture 9: Contex-Free Grammars
CSE 105 theory of computation
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.
Context-Free Grammars
CSCI 432 Computer Science Theory
CSE 105 theory of computation
Presentation transcript:

Grammars

What is a grammar? Roughly speaking, a grammar is a set of rules that produce sentences (strings). For example, a simplistic grammar for the English language is the following: <SENTENCE> → <NOUN PHRASE><VERB> <NOUN PHRASE> → <ARTICLE><NOUN> <ARTICLE> → a | the <NOUN> → girl | dog <VERB> → barks | runs |sleeps

Production of the sentence “the dog barks” <SENTENCE> → <NOUN PHRASE> <VERB> <NOUN PHRASE> → <ARTICLE> <NOUN> <ARTICLE> → a | the <NOUN> → girl | dog <VERB> → barks | runs |sleeps <SENTENCE> → <NOUN PHRASE> <VERB> → <ARTICLE> <NOUN> <VERB> → the <NOUN> <VERB> → the dog <VERB> → the dog barks

Production of the sentence “a girl runs” <SENTENCE> → <NOUN PHRASE> <VERB> <NOUN PHRASE> → <ARTICLE> <NOUN> <ARTICLE> → a | the <NOUN> → girl | dog <VERB> → barks | runs |sleeps <SENTENCE> → <NOUN PHRASE> <VERB> → <ARTICLE> <NOUN> <VERB> → a <NOUN> <VERB> → a girl <VERB> → a girl runs

Production of the sentence “a girl runs” <SENTENCE> → <NOUN PHRASE> <VERB> <NOUN PHRASE> → <ARTICLE> <NOUN> <ARTICLE> → a | the <NOUN> → girl | dog <VERB> → barks | runs |sleeps <SENTENCE> → <NOUN PHRASE> <VERB> → <ARTICLE> <NOUN> <VERB> → a <NOUN> <VERB> → a girl <VERB> → a girl runs Variables Terminals

Grammars (more formally) A grammar is a quadruple (V, T, S, P) with V being a set of Variables T being a set of Terminals S in V being the start variable P being a set of productions rules

Derivation A string of terminals can be derived by the grammar if, starting from the start variable and substituting parts of the so far produced string (following the production rules) we can produce the string of terminals. The language that a grammar produces is the set of strings of terminals which can be derived by the grammar.

Another grammar S → aSb | ε Which language does this grammar produce?

Another grammar S → aSb | ε Which language does this grammar produce?

Another grammar S → aSb | ε Which language does this grammar produce?

Another grammar S → aSb | ε Which language does this grammar produce?

Another grammar S → aSb | ε Which language does this grammar produce? S → aSb → ab

Another grammar S → aSb | ε Which language does this grammar produce? → aaSbb

Another grammar S → aSb | ε Which language does this grammar produce? → aaSbb → aabb

Another grammar S → aSb | ε Which language does this grammar produce? → aaSbb → aaaSbbb

Another grammar S → aSb | ε Which language does this grammar produce? → aaSbb → aaaSbbb → aaabbb

Another grammar S → aSb | ε Which language does this grammar produce? → aaSbb → aaaSbbb … → anSbn

Another grammar S → aSb | ε Which language does this grammar produce? S → aSb → ab S → aSb → aaSbb → aabb S → aSb → aaSbb → aaaSbbb → aaabbb S → aSb → aaSbb → … → anSbn → anbn

Another grammar S → aSb | ε Which language does this grammar produce? S → aSb → ab S → aSb → aaSbb → aabb S → aSb → aaSbb → aaaSbbb → aaabbb S → aSb → aaSbb → … → anSbn → anbn L = {anbn : n ≥ 0}

L() : “proper opening and closing (, )” Strings in the language: Strings not in the language:

L() : “proper opening and closing (, )” Strings in the language: (), (()), ()(), ((()())()) Strings not in the language: (, )(, (()((

A grammar for the language L() : “proper opening and closing (, )” S → (S) | SS | ε Example: Derivation of the string ((()())()) S → (S) → (SS) → ((S)S) → ((SS)S) → (((S)S)S) → (((ε)S)S) → ((()S)S) → ((()(S))S) → ((()(ε))S) → ((()S)S) → ((()())(S)) → ((()())(ε)) → ((()())())

Try it yourself Find a grammar that produces the language LR = {wwR : w in {a,b}*}

Try it yourself Find a grammar that produces the language LR = {wwR : w in {a,b}*} S → aSa | bSb | ε

Context free grammars All the grammars that we saw so far are called Context Free grammars. The CF grammars have a special rule form: The production rules must be of the form A ⟶ α A is in V α is in (V∪T)*

Context Free Languages A language which is produced by a context free grammar is called context free language. Example of CF languages: L = {anbn : n ≥ 0} L() = set of strings of proper opening and closing (, ) LR = {wwR : w in {a,b}*}

Context Free Languages A language which is produced by a context free grammar is called context free language. Example of CF languages: L = {anbn : n ≥ 0} S → aSb | ε L() = set of strings of proper opening and closing (, ) S → (S) | SS | ε LR = {wwR : w in {a,b}*} : S → aSa | bSb | ε

Parse Trees A parse tree for a string of terminals is a tree with the following properties: The root is the start variable Each intermediate node has as children from left to right all the variables and terminals in the sequence they appear in the right part of a derivation rule in which this variable appears in the left part. All the leafs are terminals appearing in the sequence they are in the string.

Parse Trees Consider the grammar: S → (S) → SS → ε A parse tree for (()()) is the following

Parse Trees Consider the grammar: S → (S) → SS → ε A parse tree for (()()) is the following S

Parse Trees Consider the grammar: S → (S) → SS → ε A parse tree for (()()) is the following S S ( )

Parse Trees Consider the grammar: S → (S) → SS → ε A parse tree for (()()) is the following S S ( ) S S

Parse Trees Consider the grammar: S → (S) → SS → ε A parse tree for (()()) is the following S S ( ) S S ( S ) ( S )

Parse Trees Consider the grammar: S → (S) → SS → ε A parse tree for (()()) is the following S S ( ) S S ( S ) ( S ) ε ε

Parse Trees Consider the grammar: S → (S) → SS → ε A parse tree for (()()) is the following S S ( ) S S ( S ) ( S ) ε ε

Ambiguity For the previous language there is a unique parse tree for every string in the language. This is not always the case!!! Consider the language for mathematical expressions: E → E + E | E • E | (E) | N N → 0 | 1 | … | 9

Ambiguity This language is ambiguous because the string 2+2•2 has two different derivation trees. E E + E N E • E 2 N N 2 2

Ambiguity This language is ambiguous because the string 2+2•2 has two different derivation trees. E E • E N E + E 2 N N 2 2

Ambiguity The problem with that is that the same string can have two different meanings according to the way it is parsed. Example: 2 + 2•2 can denote: 2 + (2 • 2) = 6 –first parse tree (2 + 2) • 2 = 8 –second parse tree This is bad because there are two different ways to evaluate the same expression.

Ambiguity We can design an unambiguous grammar for the same language: E → E + T | T T → | T • F | F F → (E) | N N → 0 | 1 | … | 9

Unique parse tree for 2+2•2

Closure under the reg. operations Context Free languages are closed under the regular operations: Union ∪ Concatenation ∘ Star *

CF languages are closed under ∪ Assume that we have two CF languages L1, L2. There are CF grammars (V1, T1, S1, R1) and (V2, T2, S2, R2) producing them. Create a CF grammar for the union: Add a new variable S and the rules S ⟶ S1 | S2. Make sure that the sets of variables are completely disjoint. If not change the names of common symbols. The grammar for the union is (V1 ∪ V2, T1 ∪ T2, S, R1 ∪ R2).

Example Say we want to find a grammar for the union of L = {anbn : n ≥ 0} and LR = {wwR : w in {a,b}*} G1: S → aSb | ε and G2: S → aSa | bSb | ε The grammar for L ∪ LR is: S → S1 | S2 S1 → aS1b | ε S2 → aS2a | bS2b | ε

Try it yourself Prove that CF languages are closed under ∘

Try it yourself Prove that CF languages are closed under ∘ Same as before. Just add a new start variable S and a new rule S → S1S2

CF languages are closed under * Assume that L is a CF language. There is a CF grammar (V, T, S, R) for L. Create a CF grammar for L*: Create a new start variable S’ Add the rules S’ ⟶ S’S’| S | ε