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

22
CPSC 325 - Compiler Tutorial 4 Midterm Review

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

CPSC 325 - Compiler

Tutorial 4

Midterm Review

Deterministic Finite Automata (DFA)

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

DFA Example

q1 q2

1

0

0 1

δ 0 1

q1 q1 q2

q2 q1 q2

Transitionfunction:

Start:

Final:

DFA Example

S

q1

q2

r1

r2

a b

a

ab

b

b

a b

a

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.

NFA Example

q1 q2 q3 q4

0, 1

1 0, ε 1

0, 1

δ 0 1 ε

q1 {q1}{q1, q2}

q2 {q3} {q3}

q3 {q4}

q4 {q4} {q4}

What strings does this NFA accept?

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)

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 (2n).

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 q0 is start state of NFA, closure(q0) is start state of DFA. Each state of the DFA that contains a final state of the NF

A is a final state of the DFA. Transition function for DFA is: (S,c)=DFAedge(S, c) , wh

ich is the set of states that can be reached from S by consuming input symbol c.

NFA →DFA Example (1)

• NFA example (reg.ex.: 01(00|11)*10 )

0 1

0 0

1 1 1 0

εε

ε

ε

q0 q1 q2

q6 q7 q8

q3 q4 q5 q9 q10 q11ε

NFA →DFA Example (2)

State 0 1

S: q0 q1

q1 q2 q6 q3 q9

D: q2 q6 q3 q9 q7 q4 q10

q7 q8 q2 q6 q3 q9

q4 q10 q11 q5 q2 q6 q3 q9

q8 q2 q6 q3 q9 q7 q4 q10

F: q11

q5 q2 q6 q3 q9 q7 q4 q10

NFA →DFA Example (3)

0q0 q1

1

1q2q3q6q9

q7 q4q10

q11q2q3q6q8q9

q2q3q6q5q9

0 0 1

0,1

0,1

11

0

0 00

11

Exercise

• What language is accepted by following NFA?• Convert it to a DFA.

0 1

0

1 0

0

1

q0 q1 q2

q5 q6

q3 q4 q7 q81

0

1

1

1 0

1

A solution

• 01(00|11)*10

DFA:

0 1

0

1

0

q0 q1 q2

q5 q6

q3,q7q4

q8

1

0

1

0{}0,1 1

01

0

1

Regular Expression

*: denote 0 or more occurrence

+: denote 1 or more occurrence

| : OR

Example: the sentence contains string 0110(0|1)*0110(0|1)*

Converting Regular grammars to (N)FA

• (0|1)*0110(0|1)*

q0 q1 q2 q3 q40 1 1 0

0,1 0,1

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 t

he other A grammar without this property is ambiguou

s– Note that other grammars that generate the same

language may be unambigious

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

Classic Expression Grammar

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

Another Classic example

Grammar for conditional statements......

stmt ::= ifStmt | whileStmt

ifStmt ::= if ( cond ) stmt

| if ( cond ) stmt lese stmt

……

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”

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