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.

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

CFGs and PDAs Sipser 2 (pages ). Long long ago…
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.
CFGs and PDAs Sipser 2 (pages ). Last time…
CS5371 Theory of Computation
1 、 Alphabet Non-empty set of symbols , usually expressed in  、 V or Other Upper-case Greece Letter 2 、 Symbol(Character) Elements in alphabet, finest.
Discussion #31/20 Discussion #3 Grammar Formalization & Parse-Tree Construction.
Normal forms for Context-Free Grammars
Chapter 3: Formal Translation Models
1 Context-Free Languages. 2 Regular Languages 3 Context-Free Languages.
COP4020 Programming Languages
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.
Context-free Grammars
Languages and Grammars MSU CSE 260. Outline Introduction: E xample Phrase-Structure Grammars: Terminology, Definition, Derivation, Language of a Grammar,
Problem of the DAY Create a regular context-free grammar that generates L= {w  {a,b}* : the number of a’s in w is not divisible by 3} Hint: start by designing.
Introduction Syntax: form of a sentence (is it valid) Semantics: meaning of a sentence Valid: the frog writes neatly Invalid: swims quickly mathematics.
CSE 3813 Introduction to Formal Languages and Automata Chapter 8 Properties of Context-free Languages These class notes are based on material from our.
BİL 744 Derleyici Gerçekleştirimi (Compiler Design)1 Syntax Analyzer Syntax Analyzer creates the syntactic structure of the given source program. This.
1 Section 3.3 Grammars A grammar is a finite set of rules, called productions, that are used to describe the strings of a language. Notational Example.
1 Chapter Construction Techniques. 2 Section 3.3 Grammars A grammar is a finite set of rules, called productions, that are used to describe the.
Context-free Grammars Example : S   Shortened notation : S  aSaS   | aSa | bSb S  bSb Which strings can be generated from S ? [Section 6.1]
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.
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.
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
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.
Chapter 5 Context-Free Grammars
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.
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.
CS 3240: Languages and Computation Context-Free Languages.
Discrete Structure Li Tak Sing( 李德成 ) Lectures
Introduction to Language Theory
Copyright © by Curt Hill Grammar Types The Chomsky Hierarchy BNF and Derivation Trees.
Chapter 4. Syntax Analysis (1). 2 Application of a production  A  in a derivation step  i   i+1.
1 Chapter 4 Grammars and Parsing. 2 Context-Free Grammars: Concepts and Notation A context-free grammar G = (Vt, Vn, S, P) –A finite terminal vocabulary.
Phrase-structure grammar A phrase-structure grammar is a quadruple G = (V, T, P, S) where V is a finite set of symbols called nonterminals, T is a set.
Grammar G = (V N, V T, P, S) –V N : Nonterminal symbols –V T : Terminal symbols V N  V T = , V N ∪ V T = V – P : a finite set of production rules α 
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.
CFG1 CSC 4181Compiler Construction Context-Free Grammars Using grammars in parsers.
Context Free Grammars.
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)
1 Pertemuan 7 & 8 Syntax Analysis (Parsing) Matakuliah: T0174 / Teknik Kompilasi Tahun: 2005 Versi: 1/6.
Context-Free Languages
Formal Languages and Grammars
Mathematical Foundations of Computer Science Chapter 3: Regular Languages and Regular Grammars.
Chapter 4: Syntax analysis Syntax analysis is done by the parser. –Detects whether the program is written following the grammar rules and reports syntax.
Transparency No. 1 Formal Language and Automata Theory Homework 5.
FORMAL LANGUAGES, AUTOMATA, AND COMPUTABILITY
Syntax Analysis By Noor Dhia Syntax analysis:- Syntax analysis or parsing is the most important phase of a compiler. The syntax analyzer considers.
Chapter 4. Syntax Analysis (1). 2 Application of a production  A  in a derivation step  i   i+1.
Lecture 17: Theory of Automata:2014 Context Free Grammars.
Chapter 2. Formal Languages Dept. of Computer Engineering, Hansung University, Sung-Dong Kim.
20 G M aaba acba aaba.. What is it about? Models of Language Generation Models of Language Recognition.
CONTEXT-FREE LANGUAGES
BCT 2083 DISCRETE STRUCTURE AND APPLICATIONS
Formal Language & Automata Theory
Context-Free Languages
Context-Free Grammars
5. Context-Free Grammars and Languages
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Chapter 4. Syntax Analysis (1)
Context-Free Grammars
Presentation transcript:

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 set of nonterminal symbols T is a finite nonempty set of terminal symbols disjoint from N P is a finite nonempty set of productions  is the sentential symbol or start symbol not in N or T

Formal Grammar, cont. Each production in P has the form  A    where , , and  are in (N  T)* and may possibly be empty, and A= , or A is in N. Read: If A has  to its left and  to its right, then A can be replaced by , in that context. If  and  are empty, then the production is A  , and A can be replaced by , context- free.

Example G = (N,T,P,  ), where N = { A } T = { 0, 1 } P = {  ,   A, A  0A1, A  01 }

Some Terminologies Formal grammar – phrase structure grammar Generating a sentence – successive rewriting of sentential forms through the use of productions of the grammar, starting with . Derivation of a sentence – the sequence of sentential forms needed to generate the sentence from .

Sentential Form Let G be a formal grammar. A string of symbols in (N  T)*  {  } is called a sentential form. Examples 00A11 is a sentential form 0A100A is another sentential form  is a sentential form is a sentential form

Immediately Derived Let G be a formal grammar. If    is a production of G, and  =  and  ’ =  are sentential forms, then we say that  ’ is immediately derived from  by the rule   , and we write   . Example: From 0A1 we can immediately derive 00A11 by the rule A  0A1. We write 0A1  00A11.

Derivation If w1, w2, w3, …, wn is a sequence of sentential forms such that w1  w2  w3  …  wn then we say that wn is derivable from w1, and we write w1  * wn. The above sequence is called a derivation of wn from w1.

Example derivation   A  0A1  00A11  Thus the sentential form is derivable from .  . Thus the empty string is derivable from .

Language of a Grammar The language L(G) generated by a formal grammar G is the set of terminal strings derivable from . L(G) = { w  T* |   * w } If w  L(G) we say that w is a string, sentence or word in the language generated by G.

Example In the previous example G, L(G) = {, 01, 0011, , … } = { 0 k 1 k | k  0 }.

Example 2 G = (N, T, P,  ) N = {A, B} T = {0, 1} P = {   | A | B; A  1 | 0A ; B  0 | 1B; } L(G) = {, 0 k 1, 1 k 0 | k  0 }

Types of Grammars T 0 unrestricted T 1 context sensitive T 2 context free T 3 regular W set of all strings

T 0 Unrestricted Grammars Each production in P has the form  A    where , , and  are in (N  T)* and may possibly be empty, and A= , or A  N. If  =, then |  A  | > |  |, and grammar G is called contracting.

T 1 Context Sensitive Grammar Each production in P has the form  A      where , ,   (N  T)* and  and  may possibly be empty,  , and A= , or A  N.

T 1 Example   A A  aABC ; A  abC CB  * BC bB  bb bC  bc cC  cc L(G) = {a k b k c k | k  1}

T 2 Context-Free Grammar Each production in P has the form A     where   (N  T)* - { }, and A= , or A  N.

T 2 Examples Double parentheses language   A ; A  AA ; A  ( ) | (A) | [ ] | [A] ; L(G) = ? Matching pair language   A ; A  aAb | ab ; L(G) = { a k b k | k  1 }

T 3 Regular Grammar Left Linear: Each production in P has the form: A  Ba | a ;   Right Linear: Each production in P has the form: A  aB | a ;   Here A  N or A= , B  N, a  T

T 3 Example Even parity grammar:   A | ; A  0 | 0A | 1B ; B  1 | 0B | 1A ; L(G) = Strings with even number of 1s

T 3 Example Strings of 1s followed by zeroes:   A | B | ; A  1 | 1A | 1B ; B  0 | 0B ; L(G) = Prefix of 1s (possibly empty) followed by suffix of 0s (possibly empty)

Derivation Trees The derivation tree T corresponding to a derivation  = w 1  w 2  w 3  …  w n is an ordered tree whose root is , and whose leaves are terminal symbols. If a step in the derivation is:    1  2 …  k  then the subtree rooted at  will have children  1,  2, …, and  k.

Derivation Tree Example   A  0A1  00A11  A A01 A01

Leftmost derivations A leftmost derivation is a derivation in which at each step, only the left-most nonterminal symbol is replaced by a production of the grammar.

Ambiguity A grammar G is ambiguous if for some string w in L(G), w has more than one distinct leftmost derivation. If for each string w in L(G), w has exactly one leftmost derivation, then the grammar is called unambiguous.

Example of Ambiguous Grammar N = { A } ; T = { a, x, y } ;   A ; A  a | AyA | AxA ;

Example of Unambiguous Grammar N = { A, B } ; T = { a, x, y } ;   A ; A  B | AyB ; B  a | Bxa ;