1 Context-Free Grammars Formalism Derivations Backus-Naur Form Left- and Rightmost Derivations.

Slides:



Advertisements
Similar presentations
1 Parse Trees Definitions Relationship to Left- and Rightmost Derivations Ambiguity in Grammars.
Advertisements

Context-Free Grammars
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 Chomsky Normal Form of CFG’s Definition Purpose Method of Constuction.
ISBN Chapter 3 Describing Syntax and Semantics.
CS5371 Theory of Computation
Conversion of CFG to PDA Conversion of PDA to CFG
Normal Forms for CFG’s Eliminating Useless Variables Removing Epsilon
Chapter 3 Describing Syntax and Semantics Sections 1-3.
Chapter 3 Describing Syntax and Semantics Sections 1-3.
January 14, 2015CS21 Lecture 51 CS21 Decidability and Tractability Lecture 5 January 14, 2015.
Chapter 3 Describing Syntax and Semantics Sections 1-3.
Normal forms for Context-Free Grammars
Dr. Muhammed Al-Mulhem 1ICS ICS 535 Design and Implementation of Programming Languages Part 1 Fundamentals (Chapter 4) Compilers and Syntax.
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.
Decision Properties of Regular Languages
Properties of Context-Free Languages
(2.1) Grammars  Definitions  Grammars  Backus-Naur Form  Derivation – terminology – trees  Grammars and ambiguity  Simple example  Grammar hierarchies.
1 Syntax and Semantics The Purpose of Syntax Problem of Describing Syntax Formal Methods of Describing Syntax Derivations and Parse Trees Sebesta Chapter.
1 Introduction to Parsing Lecture 5. 2 Outline Regular languages revisited Parser overview Context-free grammars (CFG’s) Derivations.
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.
CS 355 – PROGRAMMING LANGUAGES Dr. X. Topics Introduction The General Problem of Describing Syntax Formal Methods of Describing Syntax.
Context-free Grammars Example : S   Shortened notation : S  aSaS   | aSa | bSb S  bSb Which strings can be generated from S ? [Section 6.1]
1 Chapter 3 Describing Syntax and Semantics. 3.1 Introduction Providing a concise yet understandable description of a programming language is difficult.
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 Parse Trees Definitions Relationship to lm and rm Derivations Ambiguity in Grammars.
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
CS Describing Syntax CS 3360 Spring 2012 Sec Adapted from Addison Wesley’s lecture notes (Copyright © 2004 Pearson Addison Wesley)
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.
CS 3240: Languages and Computation Context-Free Languages.
C H A P T E R TWO Syntax and Semantic.
ISBN Chapter 3 Describing Syntax and Semantics.
1 Syntax In Text: Chapter 3. 2 Chapter 3: Syntax and Semantics Outline Syntax: Recognizer vs. generator BNF EBNF.
CMSC 330: Organization of Programming Languages Context-Free Grammars.
CFG1 CSC 4181Compiler Construction Context-Free Grammars Using grammars in parsers.
Introduction to Parsing
Chapter 3 Describing Syntax and Semantics
Context Free Grammars CFGs –Add recursion to regular expressions Nested constructions –Notation expression  identifier | number | - expression | ( expression.
Syntax The Structure of a Language. Lexical Structure The structure of the tokens of a programming language The scanner takes a sequence of characters.
ISBN Chapter 3 Describing Syntax and Semantics.
Context Free Grammars 1. Context Free Languages (CFL) The pumping lemma showed there are languages that are not regular –There are many classes “larger”
1 Parse Trees Definitions Relationship to Left- and Rightmost Derivations Ambiguity in 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.
Chapter 3 Context-Free Grammars Dr. Frank Lee. 3.1 CFG Definition The next phase of compilation after lexical analysis is syntax analysis. This phase.
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 4 Syntax. SPECIFYING SYNTAX Programming languages must be very well defined – there’s no room for ambiguity. Language designers must use formal.
9/15/2010CS485, Lecture 2, Fall Lecture 2: Introduction to Syntax (Revised based on the Tucker’s slides)
C H A P T E R T W O Syntax and Semantic. 2 Introduction Who must use language definitions? Other language designers Implementors Programmers (the users.
Copyright © 2006 Addison-Wesley. All rights reserved.1-1 ICS 410: Programming Languages Chapter 3 : Describing Syntax and Semantics Syntax.
Lecture 6: Context-Free Languages
Spring 16 CSCI 4430, A Milanova 1 Announcements HW1 will be out this evening Due Monday, 2/8 Submit in HW Server AND at start of class on 2/8 A review.
Chapter 3 – Describing Syntax CSCE 343. Syntax vs. Semantics Syntax: The form or structure of the expressions, statements, and program units. Semantics:
Formal Language & Automata Theory
Context free grammar.
PARSE TREES.
Lecture 14 Grammars – Parse Trees– Normal Forms
Context-Free Grammars
Context-Free Grammars 1
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
COMPILER CONSTRUCTION
Presentation transcript:

1 Context-Free Grammars Formalism Derivations Backus-Naur Form Left- and Rightmost Derivations

2 Informal Comments uA context-free grammar is a notation for describing languages. uIt is more powerful than finite automata or RE’s, but still cannot define all possible languages. uUseful for nested structures, e.g., parentheses in programming languages.

3 Informal Comments – (2) uBasic idea is to use “variables” to stand for sets of strings (i.e., languages). uThese variables are defined recursively, in terms of one another. uRecursive rules (“productions”) involve only concatenation. uAlternative rules for a variable allow union.

4 Example: CFG for { 0 n 1 n | n > 1} uProductions: S -> 01 S -> 0S1 uBasis: 01 is in the language. uInduction: if w is in the language, then so is 0w1.

5 CFG Formalism uTerminals = symbols of the alphabet of the language being defined. uVariables = nonterminals = a finite set of other symbols, each of which represents a language. uStart symbol = the variable whose language is the one being defined.

6 Productions uA production has the form variable -> string of variables and terminals. uConvention: wA, B, C,… are variables. wa, b, c,… are terminals. w…, X, Y, Z are either terminals or variables. w…, w, x, y, z are strings of terminals only. w , , ,… are strings of terminals and/or variables.

7 Example: Formal CFG uHere is a formal CFG for { 0 n 1 n | n > 1}. uTerminals = {0, 1}. uVariables = {S}. uStart symbol = S. uProductions = S -> 01 S -> 0S1

8 Derivations – Intuition uWe derive strings in the language of a CFG by starting with the start symbol, and repeatedly replacing some variable A by the right side of one of its productions. wThat is, the “productions for A” are those that have A on the left side of the ->.

9 Derivations – Formalism uWe say  A  =>  if A ->  is a production. uExample: S -> 01; S -> 0S1. uS => 0S1 => 00S11 =>

10 Iterated Derivation u=>* means “zero or more derivation steps.” uBasis:  =>*  for any string . uInduction: if  =>*  and  => , then  =>* .

11 Example: Iterated Derivation uS -> 01; S -> 0S1. uS => 0S1 => 00S11 => uSo S =>* S; S =>* 0S1; S =>* 00S11; S =>*

12 Sentential Forms uAny string of variables and/or terminals derived from the start symbol is called a sentential form. uFormally,  is a sentential form iff S =>* .

13 Language of a Grammar uIf G is a CFG, then L(G), the language of G, is {w | S =>* w}. wNote: w must be a terminal string, S is the start symbol.  Example: G has productions S -> ε and S -> 0S1. uL(G) = {0 n 1 n | n > 0}. Note: ε is a legitimate right side.

14 Context-Free Languages uA language that is defined by some CFG is called a context-free language. uThere are CFL’s that are not regular languages, such as the example just given. uBut not all languages are CFL’s. uIntuitively: CFL’s can count two things, not three.

15 BNF Notation uGrammars for programming languages are often written in BNF (Backus-Naur Form ). uVariables are words in ; Example:. uTerminals are often multicharacter strings indicated by boldface or underline; Example: while or WHILE.

16 BNF Notation – (2) uSymbol ::= is often used for ->. uSymbol | is used for “or.” wA shorthand for a list of productions with the same left side. uExample: S -> 0S1 | 01 is shorthand for S -> 0S1 and S -> 01.

17 BNF Notation – Kleene Closure uSymbol … is used for “one or more.” uExample: ::= 0|1|2|3|4|5|6|7|8|9 ::= … wNote: that’s not exactly the * of RE’s. uTranslation: Replace  … with a new variable A and productions A -> A  | .

18 Example: Kleene Closure uGrammar for unsigned integers can be replaced by: U -> UD | D D -> 0|1|2|3|4|5|6|7|8|9

19 BNF Notation: Optional Elements uSurround one or more symbols by […] to make them optional. uExample: ::= if then [; else ]  Translation: replace [  ] by a new variable A with productions A ->  | ε.

20 Example: Optional Elements uGrammar for if-then-else can be replaced by: S -> iCtSA A -> ;eS | ε

21 BNF Notation – Grouping uUse {…} to surround a sequence of symbols that need to be treated as a unit. wTypically, they are followed by a … for “one or more.” uExample: ::= [{; }…]

22 Translation: Grouping uYou may, if you wish, create a new variable A for {  }. uOne production for A: A -> . uUse A in place of {  }.

23 Example: Grouping L -> S [{;S}…] uReplace by L -> S [A…] A -> ;S wA stands for {;S}.  Then by L -> SB B -> A… | ε A -> ;S wB stands for [A…] (zero or more A’s).  Finally by L -> SB B -> C | ε C -> AC | A A -> ;S wC stands for A….

24 Leftmost and Rightmost Derivations uDerivations allow us to replace any of the variables in a string. uLeads to many different derivations of the same string. uBy forcing the leftmost variable (or alternatively, the rightmost variable) to be replaced, we avoid these “distinctions without a difference.”

25 Leftmost Derivations uSay wA  => lm w  if w is a string of terminals only and A ->  is a production. uAlso,  =>* lm  if  becomes  by a sequence of 0 or more => lm steps.

26 Example: Leftmost Derivations uBalanced-parentheses grammmar: S -> SS | (S) | () u S => lm SS => lm (S)S => lm (())S => lm (())() uThus, S =>* lm (())() uS => SS => S() => (S)() => (())() is a derivation, but not a leftmost derivation.

27 Rightmost Derivations uSay  Aw => rm  w if w is a string of terminals only and A ->  is a production. uAlso,  =>* rm  if  becomes  by a sequence of 0 or more => rm steps.

28 Example: Rightmost Derivations uBalanced-parentheses grammmar: S -> SS | (S) | () u S => rm SS => rm S() => rm (S)() => rm (())() uThus, S =>* rm (())() uS => SS => SSS => S()S => ()()S => ()()() is neither a rightmost nor a leftmost derivation.