Languages & Grammars. Grammars  A set of rules which govern the structure of a language Fritz Fritz The dog The dog ate ate left left.

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

1 Parsing The scanner recognizes words The parser recognizes syntactic units Parser operations: Check and verify syntax based on specified syntax rules.
1 Context-Free Languages. 2 Regular Languages 3 Context-Free Languages.
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.
1 Introduction to Computability Theory Lecture5: Context Free Languages Prof. Amos Israeli.
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.
Courtesy Costas Busch - RPI1 Context-Free Languages.
Chapter 3: Formal Translation Models
1 Context-Free Languages. 2 Regular Languages 3 Context-Free Languages.
Specifying Languages CS 480/680 – Comparative Languages.
Lecture 9UofH - COSC Dr. Verma 1 COSC 3340: Introduction to Theory of Computation University of Houston Dr. Verma Lecture 9.
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.
1 Context-Free Languages. 2 Regular Languages 3 Context-Free Languages.
Fall 2005Costas Busch - RPI1 Context-Free Languages.
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.
Chapter 4 Context-Free Languages Copyright © 2011 The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 1.
Context-Free Grammars
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.
Lecture 16 Oct 18 Context-Free Languages (CFL) - basic definitions Examples.
نظریه زبان ها و ماشین ها فصل دوم Context-Free Languages دانشگاه صنعتی شریف بهار 88.
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]
CSCI 2670 Introduction to Theory of Computing September 21, 2004.
Design contex-free grammars that generate: L 1 = { u v : u ∈ {a,b}*, v ∈ {a, c}*, and |u| ≤ |v| ≤ 3 |u| }. L 2 = { a p b q c p a r b 2r : p, q, r ≥ 0 }
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:
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.
PART I: overview material
Context-Free Grammars – Derivations Lecture 15 Section 2.1 Mon, Sep 24, 2007.
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.
Dept. of Computer Science & IT, FUUAST Automata Theory 2 Automata Theory V Context-Free Grammars andLanguages.
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.
Lecture 11 Theory of AUTOMATA
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.
CS 3240 – Chapter 5. LanguageMachineGrammar RegularFinite AutomatonRegular Expression, Regular Grammar Context-FreePushdown AutomatonContext-Free Grammar.
Re-enter Chomsky More about grammars. 2 Parse trees S  A B A  aA | a B  bB | b Consider L = { a m b n | m, n > 0 } (one/more a ’s followed by one/more.
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.
1 Chapter 6 Simplification of CFGs and Normal Forms.
Introduction Finite Automata accept all regular languages and only regular languages Even very simple languages are non regular (  = {a,b}): - {a n b.
Chapter 5 Context-free Languages
Context-Free Languages
CSC312 Automata Theory Lecture # 26 Chapter # 12 by Cohen Context Free Grammars.
1 Context-Free Languages. 2 Regular Languages 3 Context-Free Languages.
Regular Grammars Reading: 3.3. What we know so far…  FSA = Regular Language  Regular Expression describes a Regular Language  Every Regular Language.
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.
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.
Compiler Construction Lecture Five: Parsing - Part Two CSC 2103: Compiler Construction Lecture Five: Parsing - Part Two Joyce Nakatumba-Nabende 1.
Theory of Languages and Automata By: Mojtaba Khezrian.
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:
Lecture 17: Theory of Automata:2014 Context Free Grammars.
CONTEXT-FREE LANGUAGES
Formal Language & Automata Theory
PARSE TREES.
Context-Free Languages
REGULAR LANGUAGES AND REGULAR GRAMMARS
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
CFGs: Formal Definition
Presentation transcript:

Languages & Grammars

Grammars  A set of rules which govern the structure of a language Fritz Fritz The dog The dog ate ate left left

Formal Grammar Notation  G = {V,T,S,P} –V are variables ( ) –T are terminals (Fritz) –S is the start variable ( ) –P are the production rules  Let W be a string of variables and terminals  W Y means that W can be transformed into Y using the production rules

Languages + Grammars  A grammar defines a language  Many grammars can define the same language  Grammars that generate the same language are equivalent.

Formal languages  Forget words; letters only  S is usually start symbol  What language?  S -> aA |  S -> aA |  A -> bS

Exercises... or Puzzles?  Write a grammar that generates: L={a n b m, n<2, m<=2}  Write a grammar that generates: L={a n b m, n>0, m>=0}  Write a grammar that generates all strings on {a,b}* with exactly 2 a ’ s  Write a grammar that generates: L={a n b n, n>0}  Write a grammar that generates:  L = {w  {a}*: |w| mod 4 = 0}

Mathy Questions  Suppose a grammar for L1 has start symbol S1 and a grammar for L2 has start symbol S2. What grammar describes –L1 U L2? –L1L2? –L1*?  Can you prove that your answer is correct?  Can you prove your neighbor ’ s answer is wrong?

Recursion  Generate a+ –S -> aS | a  Generate a* –S -> aS | –S -> aS |

Impose Order  Language: a + b + –S -> AB –A -> aA | a –B -> bB | b

Relationship between symbols  Language: a n b n n>0 –S -> aSb | ab

Context-Free Grammars  A grammar is context-free if all production rules have only one non- terminal on the left-hand side A -> aSa A -> AB A -> a  Not context-free: ABB -> aaSB

Members of CFLs  To see if a string is a member of a CFL, replace every non-terminal with the right side of one of its production rules.  S -> AB A -> aaA | λ B -> Bb | λ  Derivation of string aab: S -> AB -> aaAB -> aaB -> aaBb -> aab  So, aab is a sentence in the language, and aaAB is a sentential form.

Theory  Not all Context-free languages are regular.  Example: a n b n  Can you write a CF grammar?  Can you write an automaton?

Theory  BUT: All regular languages are also context-free.  How could we prove this? CF Reg

Automata to Grammar  States are non-terminals  Alphabet letters are the terminals  Start state corresponds to Start Symbol  Final States go to lambda

 S -> aA  A -> aA | aS | bB | C  B -> bC  C -> λ

Derivations  Leftmost derivation always replaces the leftmost variable in the sentential form next.  Rightmost derivation always replaces the one on the right.  Derivations can be shown using a derivation tree.

Example S -> aAB A -> bBb B -> A | λ Derive the string abbbb Leftmost: S-> a A B -> a bBb B -> a bAb B -> a b bBb b B -> a b b b b B -> a b b b b Rightmost:

Derivation Trees  Ordered tree in which: –Interior nodes are left-hand sides of rules (variables) –Children of a node are right-hand sides –Root is start symbol –Leaves are terminals  Reading the leaves from left to right is the yield of the tree (a sentence in the language)

Example:  S -> aAB A -> bBb B -> A | λ  Show derivation tree for derivation of abbbb S a A B b B b λ A b B b λ

Ambiguity  For any string in a context-free language, there may be more than one derivation tree that produces it. This is ambiguity.  Programming Languages cannot have ambiguity.  Try to find equivalent, unambiguous grammars, but it can ’ t always be done.  If there is any unambiguous grammar for a language, it is unambiguous.  If the language has no unambiguous grammar, the language is inherently ambiguous.

Exercises  Grammar: –S -> Sa | SSb | b | –S -> Sa | SSb | b |  Give rightmost derivation of string bbaaba  Draw derivation tree  Show that the grammar is ambiguous