Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 44 – Jan. 28 Pumping lemma #2 Applications to compiling.

Similar presentations


Presentation on theme: "CS 44 – Jan. 28 Pumping lemma #2 Applications to compiling."— Presentation transcript:

1 CS 44 – Jan. 28 Pumping lemma #2 Applications to compiling

2 Adversary’s Revenge In Game #1, how did we win? Ex. { 0 n 1 n }. –Adversary chose p. –We chose s. –Adversary broke up s = xyz subject to constraints. –We were always able to pump and find words outside L. Game #2 strategy –Adversary’s constraints looser. The middle 3 parts have to be within p of each other. Can be anywhere in the string, not just in first p symbols of word.

3 { 1 n 2 n 3 n } Let p be any number. Choose s = 1 p 2 p 3 p. Let s = uvxyz such that |vxy| = 1. Where can v and y be? –All 1’s –All 2’s –All 3’s –Straddling 1’s and 2’s –Straddling 2’s and 3’s In every case, can we find a word not in L?

4 More examples { 1 i 2 j 3 k : i < j < k } –What should we chose as s? –How can the word be split up? { w w } –Let s = 0 p 1 p 0 p 1 p. Where can v & y be? –Same section of 0’s and 1’s. –In neighboring sections –Either v or y straddles a border.

5 Compiling Grammars are used to define programming language and check syntax. Phases of a compiler source code scanner stream of tokens parser parse tree

6 Scanning Scanner needs to know what to expect when eating your program. –identifiers –numbers –strings –comments Specifications for tokens can be expressed by regular expression (or regular grammar). While, scanning, we can be in different states, such as inside a comment.

7 Parser Purpose is to understand structure of program. All programming structures can be expressed as CFG. Simple example for + and – expr  expr + digit | expr – digit | digit digit  0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 How would we derive the string 9 – 5 + 2 ?

8 9 – 5 + 2 expr  expr + digit | expr – digit | digit digit  0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 expr expr +digit expr - digit 2 digit 5 9 Leftmost derivation: expr  expr + digit  expr – digit + digit  digit – digit + digit  9 – digit + digit  9 – 5 + digit  9 – 5 + 2 “parse tree”

9 Left & right recursion What is the difference between these 2 grammars? Which one is better? expr  expr + digit | expr – digit | digit digit  0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 expr  digit + expr | digit – expr | digit digit  0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 Let’s try 9 – 5 + 2 on both of these. The grammar must convey the order of operations! Operators may be left associative or right associative.

10 + - * / Question: How do we write grammar for all 4 operators? Can we do it this way… expr  expr + digit | expr – digit | expr * digit | expr / digit digit digit  0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 BTW, we can ignore “digits” and from now on just replace them with “num”, and understand it’s any single number.


Download ppt "CS 44 – Jan. 28 Pumping lemma #2 Applications to compiling."

Similar presentations


Ads by Google