Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 461 – Sept. 19 Last word on finite automata… –Scanning tokens in a compiler –How do we implement a “state” ? Chapter 2 introduces the 2 nd model of.

Similar presentations


Presentation on theme: "CS 461 – Sept. 19 Last word on finite automata… –Scanning tokens in a compiler –How do we implement a “state” ? Chapter 2 introduces the 2 nd model of."— Presentation transcript:

1 CS 461 – Sept. 19 Last word on finite automata… –Scanning tokens in a compiler –How do we implement a “state” ? Chapter 2 introduces the 2 nd model of computation

2 Application of FA Compilers! When a compiler looks at your program, it does the following: –Lexical analysis (scanning: break up into tokens) –Syntax analysis –Semantic analysis –Code generation –Optimization Next char Read / Comment Found “/=“ Div by number / * = num

3 Chapter 2 How can we represent a language? –English description √ –FA drawing (state diagram) √ –Transition table √ –Regular expression √ –Recursive definition! A context-free grammar (CFG) is a compact way of writing a recursive definition.

4 Expr vs. grammar Basic idea to creating a grammar: –Base case: what is smallest word in language? (Sometimes we need > 1 base case.) –Recursive case: how to create bigger words? What do we append to the beginning or end of word to create more words? Ex. 101* in the form of a grammar: –Shortest word is 10. –Create more words by appending 1s to the right end. S  10 S  S1

5 example Let’s try (00)*10 as a grammar: –Shortest word is 10. –Bigger words? Append 00 to the front. S  10 S  00S –These rules can be combined to say S  10 | 00S

6 examples Let’s try 01*0(10)*. –Uh-oh, how do you pump in the middle? If you need concatenation, define the language in parts. The first part will handle 01* and the second part 0(10)*. S  AB A  0 | A1 B  0 | B10 (10*1)* could be written this way: S  ε | SAB A  1 | A0 B  1

7 Formal definition A context-free grammar has 3 things –Set of terminal symbols (alphabet plus ε) –Set of nonterminal symbols (i.e. variables in grammar) –Set of rules or “productions” for our recursive definitions The rules have the following format: nonterminal  concat of 1+ terminals/nonterminals Often we have several rules for the same nonterminal, so they are joined by “|” meaning “or.”

8 More power CFGs are more powerful than FAs or regular expressions because they allow us to define non-regular languages! For example, 0*1* and { 0 n 1 n } S  ABS  ε | 0S1 A  ε | A0 B  ε | B1 Wow, maybe non-regular languages are easier! With CFGs, we now have a 2 nd class of languages, the context-free languages.

9 Models # 1# 2 Model nameFinite automaton (FA) Pushdown automaton (PDA) Recognizes what? Regular languages Context-free languages Text rep’nRegular expression Context-free grammar

10 2 nd model Encompasses regular & non-regular languages. PDA is like FA, but also has a stack. –More on PDAs in section 2.2 later. Section 2.1: CFGs –Important because this is how programming languages are defined. –Goals: 1. Given a grammar, can we generate words; 2. Given a set, can we write a grammar for it.

11 Deriving words Here is a CFG. What is it defining? S  AB A  ε | 1A B  ε | 1B0

12 More practice { 0 i 1 j : i > j } 0*11*0(0 + 1)* (000)*11 1*0 (1 + 01*0)* (a + ba) b*a [bb + (a + ba) b*a]*


Download ppt "CS 461 – Sept. 19 Last word on finite automata… –Scanning tokens in a compiler –How do we implement a “state” ? Chapter 2 introduces the 2 nd model of."

Similar presentations


Ads by Google