Context Free Grammars CIS 361. Introduction Finite Automata accept all regular languages and only regular languages Many simple languages are non regular:

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.
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.
About Grammars CS 130 Theory of Computation HMU Textbook: Sec 7.1, 6.3, 5.4.
1 Introduction to Computability Theory Lecture5: Context Free Languages Prof. Amos Israeli.
CS5371 Theory of Computation
Foundations of (Theoretical) Computer Science Chapter 2 Lecture Notes (Section 2.1: Context-Free Grammars) David Martin With some.
Context-Free Grammars Lecture 7
January 14, 2015CS21 Lecture 51 CS21 Decidability and Tractability Lecture 5 January 14, 2015.
1 Foundations of Software Design Lecture 23: Finite Automata and Context-Free Grammars Marti Hearst Fall 2002.
Normal forms for Context-Free Grammars
1 Context-Free Languages. 2 Regular Languages 3 Context-Free Languages.
Specifying Languages CS 480/680 – Comparative Languages.
January 15, 2014CS21 Lecture 61 CS21 Decidability and Tractability Lecture 6 January 16, 2015.
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.
Context-free Grammars
1 Chapter 3 Context-Free Grammars and Parsing. 2 Parsing: Syntax Analysis decides which part of the incoming token stream should be grouped together.
FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY
Compiler Construction 1. Objectives Given a context-free grammar, G, and the grammar- independent functions for a recursive-descent parser, complete the.
1 Introduction to Parsing Lecture 5. 2 Outline Regular languages revisited Parser overview Context-free grammars (CFG’s) Derivations.
Chapter 4 Context-Free Languages Copyright © 2011 The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 1.
Lecture 16 Oct 18 Context-Free Languages (CFL) - basic definitions Examples.
CSE 3813 Introduction to Formal Languages and Automata Chapter 8 Properties of Context-free Languages These class notes are based on material from our.
Formal Languages Context free languages provide a convenient notation for recursive description of languages. The original goal of CFL was to formalize.
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.
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
Chapter 5 Context-Free Grammars
Grammars CPSC 5135.
PART I: overview material
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.
Dept. of Computer Science & IT, FUUAST Automata Theory 2 Automata Theory V Context-Free Grammars andLanguages.
Context Free Grammars. Context Free Languages (CFL) The pumping lemma showed there are languages that are not regular –There are many classes “larger”
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.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 11 Midterm Exam 2 -Context-Free Languages Mälardalen University 2005.
Context Free Grammars 1. Context Free Languages (CFL) The pumping lemma showed there are languages that are not regular –There are many classes “larger”
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.
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.
Chapter 8 Properties of Context-free Languages These class notes are based on material from our textbook, An Introduction to Formal Languages and Automata,
FORMAL LANGUAGES, AUTOMATA, AND COMPUTABILITY
Compiler Construction Lecture Five: Parsing - Part Two CSC 2103: Compiler Construction Lecture Five: Parsing - Part Two Joyce Nakatumba-Nabende 1.
1 Context-Free Languages & Grammars (CFLs & CFGs) Reading: Chapter 5.
Context-Free Languages & Grammars (CFLs & CFGs) (part 2)
5. Context-Free Grammars and Languages
CONTEXT-FREE LANGUAGES
Context-Free Languages & Grammars (CFLs & CFGs)
Context-Free Grammars: an overview
Formal Language & Automata Theory
Syntax Specification and Analysis
Context free grammar.
PARSE TREES.
Context-Free Languages
Context-Free Grammars
5. Context-Free Grammars and Languages
CHAPTER 2 Context-Free Languages
Context-Free Grammars 1
Finite Automata and Formal Languages
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
CFGs: Formal Definition
Presentation transcript:

Context Free Grammars CIS 361

Introduction Finite Automata accept all regular languages and only regular languages Many simple languages are non regular: and there is no finite automata that accepts them. - {a n b n : n = 0, 1, 2, …} - {w : w a is palindrome} context-free languages are a larger class of languages that encompasses all regular languages and many others, including the two above.

Context-Free Grammars Languages that are generated by context-free grammars are context-free languages Context-free grammars are more expressive than finite automata: if a language L is accepted by a finite automata then L can be generated by a context-free grammar Beware: The converse is NOT true

Context-Free Grammar Definition. A context-free grammar is a 4-tuple ( , NT, R, S), where:  is an alphabet (each character in  is called terminal) NT is a set (each element in NT is called nonterminal) R, the set of rules, is a subset of NT  (   NT)* S, the start symbol, is one of the symbols in NT If ( ,  )  R, we write production     is called a sentential form

CFGs: Alternate Definition many textbooks use different symbols and terms to describe CFG’s G = (V, , P, S) V= variablesa finite set  = alphabet or terminalsa finite set P= productionsa finite set S= start variableS  V Productions’ form, where A  V,  (V  ) * : A  

Derivations Definition. v is one-step derivable from u, written u  v, if: u = x  z v = x  z    in R Definition. v is derivable from u, written u  * v, if: There is a chain of one-derivations of the form: u  u 1  u 2  …  v

Context-Free Languages Definition. Given a context-free grammar G = ( , NT, R, S), the language generated or derived from G is the set: L(G) = {w : } S  * w Definition. A language L is context-free if there is a context-free grammar G = ( , NT, R, S), such that L is generated from G

CFGs & CFLs: Example 1 {a n b n | n  0} One of our canonical non-RLs. S   | a S b Formally: G = ({S}, {a,b}, {S  , S  a S b}, S)

CFGs & CFLs: Example 2 all strings of balanced parentheses A core idea of most programming languages. Another non-RL. P   | ( P ) | P P ? ?

CFGs & CFLs: Lessons Both examples used a common CFG technique, “wrapping” around a recursive variable. S  a S bP  ( P )

CFGs & CFLs: Example 3 {a m b n c m+n | m,n  0} ? ? Rewrite as {a m b n c n c m | m,n  0}: S  S’ | a S c S’   | b S’ c

CFGs & CFLs: Non-Example {a n b n c n | n  0} Can’t be done; CFL pumping lemma later. Intuition: Can count to n, then can count down from n, but forgetting n. I.e., a stack as a counter. Will see this when using a machine corresponding to CFGs.

Parse Tree A parse tree of a derivation is a tree in which: Each internal node is labeled with a nonterminal If a rule A  A 1 A 2 …A n occurs in the derivation then A is a parent node of nodes labeled A 1, A 2, …, A n S a S a S S e b

Parse Trees Sample derivations: S  AB  AAB  aAB  aaB  aabB  aabb S  AB  AbB  Abb  AAbb  Aabb  aabb S  A | A B A   | a | A b | A A B  b | b c | B c | b B These two derivations use same productions, but in different orders. This ordering difference is often uninteresting. Derivation trees give way to abstract away ordering differences. Root label = start node. Each interior label = variable. Each parent/child relation = derivation step. Each leaf label = terminal or . All leaf labels together = derived string = yield. S AB AABb aab

Leftmost, Rightmost Derivations Definition. A left-most derivation of a sentential form is one in which rules transforming the left-most nonterminal are always applied Definition. A right-most derivation of a sentential form is one in which rules transforming the right-most nonterminal are always applied

Leftmost & Rightmost Derivations Sample derivations: S  AB  AAB  aAB  aaB  aabB  aabb S  AB  AbB  Abb  AAbb  Aabb  aabb S  A | A B A   | a | A b | A A B  b | b c | B c | b B S AB AABb aab These two derivations are special. 1 st derivation is leftmost. Always picks leftmost variable. 2 nd derivation is rightmost. Always picks rightmost variable.

Left / Rightmost Derivations In proofs… Restrict attention to left- or rightmost derivations. In parsing algorithms… Restrict attention to left- or rightmost derivations. E.g., recursive descent uses leftmost; yacc uses rightmost.

Derivation Trees Infinitely many others possible. S AB AAb a a bA S A AA AAbA a  a bA S AB AABb aab S  A | A B A   | a | A b | A A B  b | b c | B c | b B w = aabb Other derivation trees for this string? ? ?

Ambiguous Grammar S  A S  B S  AB A  aA B  bB A  e B  e Definition. A grammar G is ambiguous if there is a word w  L(G) having are least two different parse trees Notice that a has at least two left-most derivations

Ambiguity CFG ambiguous  any of following equivalent statements:  string w with multiple derivation trees.  string w with multiple leftmost derivations.  string w with multiple rightmost derivations. Defining ambiguity of grammar, not language.

Ambiguity & Disambiguation Given an ambiguous grammar, would like an equivalent unambiguous grammar. Allows you to know more about structure of a given derivation. Simplifies inductive proofs on derivations. Can lead to more efficient parsing algorithms. In programming languages, want to impose a canonical structure on derivations. E.g., for 1+2  3. Strategy: Force an ordering on all derivations.

Disambiguation: Example 1 Exp  n | Exp + Exp | Exp  Exp What is an equivalent unambiguous grammar? Exp  Term | Term + Exp Term  n | n  Term Uses operator precedence left-associativity ? ?

Disambiguation What is a general algorithm? There are CFLs that are inherently ambiguous Every CFG for this language is ambiguous. E.g., {a n b n c m d m | n  1, m  1}  {a n b m c m d n | n  1, m  1}. So, can’t necessarily eliminate ambiguity! None exists! ? ?

CFG Simplification Can’t always eliminate ambiguity. But, CFG simplification & restriction still useful theoretically & pragmatically. Simpler grammars are easier to understand. Simpler grammars can lead to faster parsing. Restricted forms useful for some parsing algorithms. Restricted forms can give you more knowledge about derivations.

CFG Simplification: Example How can the following be simplified? S  A B S  A C D A  A a A  a A  a A A  a C   D  d D D  E E  e A e F  f f 1) Delete: B useless because nothing derivable from B. 2) Delete either A  Aa or A  aA. 3) Delete one of the idential productions. 4) Delete & also replace S  ACD with S  AD. 5) Replace with D  eAe. 6) Delete: E useless after change #5. 7) Delete: F useless because not derivable from S. ? ?

CFG Simplification Eliminate ambiguity. Eliminate “useless” variables. Eliminate  -productions: A . Eliminate unit productions: A  B. Eliminate redundant productions. Trade left- & right-recursion.

Trading Left- & Right-Recursion Left recursion:A  A  Right recursion: A   A Most algorithms have trouble with one, In recursive descent, avoid left recursion.