CPSC 325 - Compiler Tutorial 4 Midterm Review. Deterministic Finite Automata (DFA) Q: finite set of...

download CPSC 325 - Compiler Tutorial 4 Midterm Review. Deterministic Finite Automata (DFA) Q: finite set of states £: finite set of “lettersâ€‌ (input alphabet)

of 22

  • date post

    14-Dec-2015
  • Category

    Documents

  • view

    218
  • download

    3

Embed Size (px)

Transcript of CPSC 325 - Compiler Tutorial 4 Midterm Review. Deterministic Finite Automata (DFA) Q: finite set of...

  • Slide 1

CPSC 325 - Compiler Tutorial 4 Midterm Review Slide 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. Slide 3 DFA Example q1 q2 1 0 0 1 01 q1 q2 q1q2 Transition function: Start: Final: Slide 4 DFA Example S q1 q2 r1 r2 a b a ab b b ab a Slide 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. Slide 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? Slide 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) Slide 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 ). Slide 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. Slide 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 Slide 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 Slide 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 Slide 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 Slide 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 Slide 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)* Slide 16 Converting Regular grammars to (N)FA (0|1)*0110(0|1)* q0q1q2 q3q4 01 10 0,1 Slide 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 Slide 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 Slide 19 Classic Expression Grammar expr ::= term + expr | term expr | term term ::= factor * term | factor / term | factor factor ::= int | ( expr ) int ::= 0 | 1 | 2 | | 9 Slide 20 Another Classic example Grammar for conditional statements...... stmt ::= ifStmt | whileStmt ifStmt ::= if ( cond ) stmt | if ( cond ) stmt lese stmt Slide 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 Slide 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