Download presentation

Presentation is loading. Please wait.

Published byLarissa Garrett Modified over 3 years ago

1
CPSC 325 - Compiler Tutorial 4 Midterm Review

2
Deterministic Finite Automata (DFA) Q: finite set of states Σ: finite set of “letters” (input alphabet) δ: Q × Σ → Q (transition function) q 0 : start state (in Q) F: set of accept states (subset of Q) Acceptance: input consumed with the automaton in a final state.

3
DFA Example q1 q2 1 0 0 1 δ01 q1 q2 q1q2 Transition function: Start: Final:

4
DFA Example S q1 q2 r1 r2 a b a ab b b ab a

5
Non-deterministic Finite Automata (NFA) Transition function is different δ: Q × Σ ε → P(Q) P(Q) is the powerset of Q (set of all subsets) Σ ε is the union of Σ and the special symbol ε (denoting empty) String is accepted if there is at least one path leads to an accept state, and input is consumed.

6
NFA Example q1q2q3q4 0, 1 1 0, ε1 0, 1 δ01ε q1{q1}{q1, q2} q2{q3} q3{q4} q4{q4} What strings does this NFA accept?

7
Converting an NFA to a DFA For set of states S, closure(S) is the set of states that can be reached from S without consuming any input. For a set of states S, DFAedge(S, c) is the set of states that can be reached from S by consuming input symbol c; algorithm: for each state s in S add closure(δ(s,c)) to DFAedge(S,c)

8
NFA to DFA conversion (cont ’ d) A state of the DFA after reading a given input letter represents the set of states that the NFA might have reached with the same input letter. Each state of the DFA that contains a final state of the NFA is a final state of the DFA. Number of states of the DFA exponential (in the worst case) in the number of states of the NFA (2 n ).

9
NFA to DFA conversion (formally) If Q is set of states for NFA, P(Q) is set of states for DFA. Σ is same for both automata. If q 0 is start state of NFA, closure(q 0 ) is start state of DFA. Each state of the DFA that contains a final state of the NFA is a final state of the DFA. Transition function for DFA is: (S,c)=DFAedge(S, c), which is the set of states that can be reached from S by consuming input symbol c.

10
NFA →DFA Example (1) NFA example (reg.ex.: 01(00|11)*10 ) 01 00 1110 ε ε ε ε q0q0 q1q1 q2q2 q6q6 q7q7 q8q8 q3q3 q4q4 q5q5 q9q9 q 10 q 11 ε

11
NFA →DFA Example (2) State01 S: q 0 q1q1 q1q1 q 2 q 6 q 3 q 9 D: q 2 q 6 q 3 q 9 q7q7 q 4 q 10 q7q7 q 8 q 2 q 6 q 3 q 9 q 4 q 10 q 11 q 5 q 2 q 6 q 3 q 9 q 8 q 2 q 6 q 3 q 9 q7q7 q 4 q 10 F: q 11 q 5 q 2 q 6 q 3 q 9 q7q7 q 4 q 10

12
NFA →DFA Example (3) 0 q0q0 q1q1 1 1 q2q3q6q9q2q3q6q9 q7q7 q 4 q 10 q 11 q2q3q6q8q9q2q3q6q8q9 q2q3q6q5q9q2q3q6q5q9 00 1 0,1 1 1 0 00 0 1 1

13
Exercise What language is accepted by following NFA? Convert it to a DFA. 01 0 10 0 1 q0q0 q1q1 q2q2 q5q5 q6q6 q3q3 q4q4 q7q7 q8q8 1 0 1 1 1 0 1

14
A solution 01(00|11)*10 DFA: 01 0 1 0 q0q0 q1q1 q2q2 q5q5 q6q6 q 3, q 7 q4q4 q8q8 1 0 1 0 {} 0,1 1 0 1 0 1

15
Regular Expression *: denote 0 or more occurrence +: denote 1 or more occurrence | : OR Example: the sentence contains string 0110 (0|1)*0110(0|1)*

16
Converting Regular grammars to (N)FA (0|1)*0110(0|1)* q0q1q2 q3q4 01 10 0,1

17
Ambiguity Grammar G is unambiguous iff every sentence in L(G) has a unique leftmost (or rightmost) derivation – Fact: unique leftmost or unique rightmost implies the other A grammar without this property is ambiguous – Note that other grammars that generate the same language may be unambigious

18
Solutions The grammar has no notion of precedence or associatively Solution: – Create a non-terminal for each level of precedence – Isolate the corresponding part of the grammar – Force the parser to recognize higher precedence sub expressions first

19
Classic Expression Grammar expr ::= term + expr | term – expr | term term ::= factor * term | factor / term | factor factor ::= int | ( expr ) int ::= 0 | 1 | 2 | … | 9

20
Another Classic example Grammar for conditional statements...... stmt ::= ifStmt | whileStmt ifStmt ::= if ( cond ) stmt | if ( cond ) stmt lese stmt ……

21
Solving Ambiguity Fix the grammar to separate if statements with else clause and if statement with no else - add lots of non-terminals Use some ad-hoc rule in parser - “else matches closest unpaired if”

22
Reading for Mid-term Converting NFA to DFA Converting Regular grammars to (N)FA Parsing bottom up or top down General phases of a compiler Chomsky Type 0-3 languages Left recursion causing problem for top down parser and solution to it empty rules causing problems for bottom-up parsers … others Input

Similar presentations

OK

Pushdown Automata Chapter 12. Recognizing Context-Free Languages Two notions of recognition: (1) Say yes or no, just like with FSMs (2) Say yes or no,

Pushdown Automata Chapter 12. Recognizing Context-Free Languages Two notions of recognition: (1) Say yes or no, just like with FSMs (2) Say yes or no,

© 2018 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Run ppt on html editor Ppt on current account deficit graph Ppt on rural tourism in india Ppt on pi in maths what does mode Ppt on power supply Human body systems for kids ppt on batteries Ppt on world diabetes day theme Ppt on any topic in hindi Ppt on rain detector alarm Open ppt on ipad