Download presentation

Presentation is loading. Please wait.

Published byKenyon McAllister Modified about 1 year ago

1
Translator Architecture Code Generator ParserTokenizer string of characters (source code) string of tokens abstract program string of integers (object code) ? Parser

2
Here’s the Plan Design a context-free grammar to specify (syntactically) valid BL programs Use the grammar to implement a recursive descent parser (i.e., an algorithm to parse BL programs and construct the corresponding Program object)

3
Specifying Syntax with CFGs Context-free grammars (cfg) Some syntactically valid real constants: E E-93

4
Rewrite Rules for Real Constants . |. |. E | E + | E - | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 can be rewritten as or

5
Four Components of a CFG Nonterminal symbols,,, Terminal symbols 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, E, +, -,. Start symbol Rewrite rules on previous slide

6
A Derivation of 5.6E10 We begin with the start symbol… … and choose one of its rewrite rules: . |. |.

7
A Derivation of 5.6E10 . Now we choose one of the nonterminal symbols… … and choose one of its rewrite rules: |

8
A Derivation of 5.6E10 . Again we choose one of the nonterminal symbols… … and choose one of its rewrite rules: 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9

9
A Derivation of 5.6E10 . 5. 5. 6 5. 6 E 5. 6 E 1 5. 6 E 1 0 If it’s possible to find such a derivation, we write: 5.6E10 *

10
A Derivation Tree for 5.6E10. E

11
Find a Derivation Tree for 5.E3 The derivation tree: Can you find a derivation tree for.6E10?. E 35 Why not?

12
Language Generated by a CFG Definition: Let G = (Nonterminals, Terminals,, Rewrite Rules) be a context-free grammar. The language generated (or specified) by G is denoted L(G) and is defined as: * L(G) = {x: string of Terminals ( x)}

13
Another Example: A CFG for Boolean Expressions T | F | NOT ( ) | ( AND ) | ( OR )

14
CFG for Boolean Expressions What are the nonterminal symbols? What are the terminal symbols? What is the start symbol? How many rewrite rules? T, F, AND, OR, NOT, (, ) 5

15
A Derivation Tree for NOT((T OR F)) NOT() OR() T F

16
Find a Derivation Tree for ((T OR NOT (F)) AND T) AND() T OR() TNOT() F

17
A Famous Context-free Grammar | | ( ) | + | - * | DIV | MOD | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9

18
What’s So Special About This CFG? Find a derivation tree for * * 2

19
What’s So Special Continued… Find the derivation tree for (4 + 6) * 2 4 * 2 () 6 +

20
How About These Rewrite Rules? | ( ) | + | - | * | DIV | MOD | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9

21
New Rules Continued… Find a derivation tree for * * 2

22
New Rules Continued… How about this tree for * 2? 2 * 4 + 6

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google