Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "1 Context-Free Grammars Formalism Derivations Backus-Naur Form Left- and Rightmost Derivations."— Presentation transcript:

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

2 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 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 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 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 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 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 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 9 Derivations – Formalism uWe say  A  =>  if A ->  is a production. uExample: S -> 01; S -> 0S1. uS => 0S1 => 00S11 => 000111.

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

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

12 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 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 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 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 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 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 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 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 20 Example: Optional Elements uGrammar for if-then-else can be replaced by: S -> iCtSA A -> ;eS | ε

21 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 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 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 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 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 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 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 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.


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

Similar presentations


Ads by Google