Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material has been reproduced and communicated to you by or on behalf of Monash University.

Similar presentations


Presentation on theme: "COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material has been reproduced and communicated to you by or on behalf of Monash University."— Presentation transcript:

1 COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material has been reproduced and communicated to you by or on behalf of Monash University pursuant to Part VB of the Copyright Act 1968 (the Act). The material in this communication may be subject to copyright under the Act. Any further reproduction or communication of this material by you may be the subject of copyright protection under the Act. Do not remove this notice.

2 Context Free Grammars CSE2303 Formal Methods I Lecture 10

3 Overview Recursive Definitions Context Free Grammars Parse Trees Ambiguity

4 Arithmetic Expressions 1. All integers are Arithmetic Expressions 2. If A and B are Arithmetic Expressions so are: (i) A + B (ii) A - B (iii) A * B (iv) A / B (v) (A)

5 Production Rules AE  integer AE  AE + AE AE  AE - AE AE  AE * AE AE  AE / AE AE  (AE) S  A A  integer A  A + A A  A - A A  A * A A  A / A A  (A)

6 Backus Normal Form S  A A  integer | A + A | A - A | A * A | A / A | (A)

7 Definitions A string is in EQUAL if it has an equal number of a ’s and b ’s. E.g. {  ab ba aabb abab abba baba … } An a-type string has one more a than b. A b-type string has one more b than a.

8 Definition of EQUAL S   S  aB S  bA A  a A  aS A  bAA B  b B  bS B  aBB A string is in EQUAL if it is – , or –an, a followed by a string of type-b, or –it is a b followed by a string of type-a. A string is of type-a if it is –an a, or –an a followed by a string in EQUAL, or –a b followed by two strings of type-a. A string is of type-b if it is –an b, or –a b followed by a string in EQUAL, or –an a followed by two strings of type-b.

9 Production Rules S   S  bA S  aB A  a A  aS A  bAA B  b B  bS B  aBB Non-terminals Terminals

10 A Context Free Grammar consists of: 1. An alphabet –The letters are called terminals 2. A set of symbols –We call these symbols nonterminals –One of these symbols is S 3. A finite set of production rules of the form: One nonterminal  finite string of terminals and/or nonterminals Note: One rule must contain S on the left side.

11 S  aS | Sa |  1. S  Sa 2. S  aS 3. S   S  Sa (Rule 1)  aSa (Rule 2)  aaSa (Rule 2)  aaSaa (Rule 1)  aa  aa (Rule 3) = aaaa

12 Parse Tree S  Sa  aSa  aaSa  aaSaa  aa  aa = aaaa S Sa Sa Sa Sa 

13 EQUAL 1.S   2.S  bA 3.S  aB 4.A  a 5.A  aS 6.A  bAA 7.B  b 8.B  bS 9.B  aBB S  bA (Rule 2)  baS (Rule 5)  baaB (Rule 3)  baaaBB (Rule 9)  baaaBb (Rule 7)  baaabSb (Rule 8)  baaabbAb (Rule 2)  baaabbab (Rule 4)

14 Parse Tree S  bA  baS  baaB  baaaBB  baaaBb  baaabSb  baaabbAb  baaabbab S bA aS aB aBB bbS bA a

15 Definition The language generated by a CFG –Consists of those strings which can be produced from the start symbol S using the production rules. A language generated by a CFG is called a Context Free Language (CFL).

16 Ambiguity Include Your Children When Baking Cookies Police Begin Campaign To Run Down Jaywalkers Miners Refuse to Work After Death Juvenile Court to Try Shooting Defendant Kids Make Nutritious Snacks Stolen Painting Found by Tree Local High School Dropouts Cut in Half

17 Time flies S nounverb Timeflies S noun Timeflies

18 S  aS | Sa |  S Sa Sa Sa Sa  S a Sa Sa Sa S 

19 Arithmetic Expressions S  A A  integer | A + A | A - A | A * A | A / A | (A) S A 4 +AA A*A 23 S A 4 *AA A+A 2 3

20 Definition A CFG is called ambiguous if there is one word it generates which has two different parse trees. If a CFG is not ambiguous it is called unambiguous.

21 Alternative 1. S  Sa 2. S  aS 3. S   1. S  aS 2. S  

22 Arithmetic Expressions S  A A  integer A  A + A A  A - A A  A * A A  A / A A  (A) S  E E  E + T| E - T| T T  T * F | T / F | F F  integer | (E)

23 4 + 2*3 S E 4 +ET T*F 2 3 S  E E  E + T | E – T | T T  T * F | T / F | F F  integer | (E) F F T

24 Revision Context Free Grammars –Definition. How to use them. Parse Trees –Definition. How to make them. Ambiguity –Definition Preparation Read –Text Book p415-423, p282-285


Download ppt "COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material has been reproduced and communicated to you by or on behalf of Monash University."

Similar presentations


Ads by Google