Chapter 5 ContextFree Grammars Comments Formalism Derivations Leftmost

  • Slides: 15
Download presentation
Chapter 5: Context-Free Grammars Comments Formalism Derivations Leftmost and Rightmost Derivations 1

Chapter 5: Context-Free Grammars Comments Formalism Derivations Leftmost and Rightmost Derivations 1

Comments about CFGs • Context-free grammar: another way to define languages. • It is

Comments about CFGs • Context-free grammar: another way to define languages. • It is more powerful than finite automata or regular expressions • Played an important role in complier technology • Particularly parsers (functions that discover the structure of programs) 2

More comments about CFGs Two basis things to learn about CFGs: • How CFGs

More comments about CFGs Two basis things to learn about CFGs: • How CFGs defines languages • How to derive the strings in those languages Use “variables” to stand for sets of strings. Define variables recursively Recursive rules are called “productions” 3

Example: CFG for { 0 n 1 n | n > 1} S is

Example: CFG for { 0 n 1 n | n > 1} S is a variable Productions of S: S -> 01 S -> 0 S 1 Basis: 01 is in the language (n=1). Induction: if w is in the language, then 0 w 1 is in the language 4

CFG Formalism • Terminals = symbols of the alphabet of the language being defined.

CFG Formalism • Terminals = symbols of the alphabet of the language being defined. • Variables = a finite set of symbols, each of which represents a language (i. e. a set of strings) • Start symbol = the variable whose language is the one being defined. • Production = replacement rules of the form variable -> string of variables and terminals. 5

Formal CFG definitions Formal definition of the CFG for {0 n 1 n|n>1} Terminals:

Formal CFG definitions Formal definition of the CFG for {0 n 1 n|n>1} Terminals: {0, 1} Variables: {S} Start symbol: S Productions: S->01|0 S 1 short-hand for S -> 01 S -> 0 S 1 6

Derivations We derive strings in the language of a CFG by starting with the

Derivations We derive strings in the language of a CFG by starting with the start symbol, and repeatedly replacing some variable, A for example, by the right side of one of its productions. “productions of A” are replacement rules for A Derivation is complete when all variables are expressed as terminals Denote each step in a derivation by => Example : A => if A -> is a production 7

Examples of derivations: derive string 000111 in { 0 n 1 n | n

Examples of derivations: derive string 000111 in { 0 n 1 n | n > 1} Productions are S -> 01; S -> 0 S 1. 8

Examples of derivations: derive string 000111 in { 0 n 1 n | n

Examples of derivations: derive string 000111 in { 0 n 1 n | n > 1} Productions are S -> 01; S -> 0 S 1. S => 0 S 1 => 00 S 11 => 000111 Replace S by 0 S 1 as many times as needed Complete by replacing S by 01 9

Formal definition of L(CFG) S=> means “one derivation step of S” S=>* means “zero

Formal definition of L(CFG) S=> means “one derivation step of S” S=>* means “zero or more steps of S” Formal definition of L(CFG) on alphabet T L(CFG) = {w in T* | S =>* w} w must be a terminal string S must be the start symbol of derivation of w Not all CFL are regular Example: L = {0 n 1 n | n > 0} Not all languages are CFL’s. 10

Sentential Forms Any string of variables and/or terminals derived from the start symbol is

Sentential Forms Any string of variables and/or terminals derived from the start symbol is called a sentential form. Formally, is a sentential form iff S =>*. 11

Leftmost and Rightmost Derivations allow us to replace any of the variables in a

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

leftmost/rightmost notation w. A =>lm w using production A -> denotes a single leftmost

leftmost/rightmost notation w. A =>lm w using production A -> denotes a single leftmost step. =>*lm denotes zero or more leftmost steps by which becomes . Similarly for rightmost derivations 13

Example: Leftmost/Rightmost Derivations Balanced-parentheses grammar: S -> SS | (S) | () perform S

Example: Leftmost/Rightmost Derivations Balanced-parentheses grammar: S -> SS | (S) | () perform S =>*lm (())() perform S =>*rm (())() 14

Example: Leftmost/Rightmost Derivations Balanced-parentheses grammar: S -> SS | (S) | () perform S

Example: Leftmost/Rightmost Derivations Balanced-parentheses grammar: S -> SS | (S) | () perform S =>*lm (())() perform S =>*rm (())() S =>lm SS =>lm (S)S =>lm (())() S =>rm S() =>rm (S)() =>rm (())() 15