CPSC 325 - Compiler Tutorial 4 Midterm Review. Deterministic Finite Automata (DFA) Q: finite set of states Σ: finite set of “letters” (input alphabet)

Slides:



Advertisements
Similar presentations
4b Lexical analysis Finite Automata
Advertisements

Nondeterministic Finite Automata CS 130: Theory of Computation HMU textbook, Chapter 2 (Sec 2.3 & 2.5)
C O N T E X T - F R E E LANGUAGES ( use a grammar to describe a language) 1.
CFGs and PDAs Sipser 2 (pages ). Long long ago…
1 Introduction to Computability Theory Lecture4: Regular Expressions Prof. Amos Israeli.
1 Introduction to Computability Theory Lecture3: Regular Expressions Prof. Amos Israeli.
CS5371 Theory of Computation
Costas Busch - RPI1 Context-Free Languages. Costas Busch - RPI2 Regular Languages.
1 Foundations of Software Design Lecture 23: Finite Automata and Context-Free Grammars Marti Hearst Fall 2002.
Fall 2004COMP 3351 Context-Free Languages. Fall 2004COMP 3352 Regular Languages.
CPSC Compiler Tutorial 3 Parser. Parsing The syntax of most programming languages can be specified by a Context-free Grammar (CGF) Parsing: Given.
CSE 413 Programming Languages & Implementation Hal Perkins Autumn 2012 Context-Free Grammars and Parsing 1.
8/19/2015© Hal Perkins & UW CSEC-1 CSE P 501 – Compilers Parsing & Context-Free Grammars Hal Perkins Winter 2008.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 7 Mälardalen University 2010.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 5 Mälardalen University 2005.
Nondeterministic Finite Automata CS 130: Theory of Computation HMU textbook, Chapter 2 (Sec 2.3 & 2.5)
Context-free Grammars Example : S   Shortened notation : S  aSaS   | aSa | bSb S  bSb Which strings can be generated from S ? [Section 6.1]
Lecture # 3 Chapter #3: Lexical Analysis. Role of Lexical Analyzer It is the first phase of compiler Its main task is to read the input characters and.
1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 5 Mälardalen University 2010.
Grammars CPSC 5135.
Lexical Analysis Constructing a Scanner from Regular Expressions.
4b 4b Lexical analysis Finite Automata. Finite Automata (FA) FA also called Finite State Machine (FSM) –Abstract model of a computing entity. –Decides.
COMP3190: Principle of Programming Languages DFA and its equivalent, scanner.
Bernd Fischer RW713: Compiler and Software Language Engineering.
11 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 7 School of Innovation, Design and Engineering Mälardalen University 2012.
Context-Free Languages. Regular Languages Context-Free Languages.
Costas Busch - LSU1 Parsing. Costas Busch - LSU2 Compiler Program File v = 5; if (v>5) x = 12 + v; while (x !=3) { x = x - 3; v = 10; } Add v,v,5.
using Deterministic Finite Automata & Nondeterministic Finite Automata
Grammar Set of variables Set of terminal symbols Start variable Set of Production rules.
CS 154 Formal Languages and Computability February 11 Class Meeting Department of Computer Science San Jose State University Spring 2016 Instructor: Ron.
Mid-Terms Exam Scope and Introduction. Format Grades: 100 points -> 20% in the final grade Multiple Choice Questions –8 questions, 7 points each Short.
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,
Deterministic Finite Automata Nondeterministic Finite Automata.
COMP3190: Principle of Programming Languages DFA and its equivalent, scanner.
Lecture 2 Compiler Design Lexical Analysis By lecturer Noor Dhia
1/29/02CSE460 - MSU1 Nondeterminism-NFA Section 4.1 of Martin Textbook CSE460 – Computability & Formal Language Theory Comp. Science & Engineering Michigan.
Introduction to Parsing
6. Pushdown Automata CIS Automata and Formal Languages – Pei Wang.
Parsing & Context-Free Grammars
Context-Free Grammars: an overview
CS 404 Introduction to Compiler Design
G. Pullaiah College of Engineering and Technology
Programming Languages Translator
CS510 Compiler Lecture 4.
Lexical analysis Finite Automata
Non Deterministic Automata
Syntax Specification and Analysis
Table-driven parsing Parsing performed by a finite state machine.
Simplifications of Context-Free Grammars
CS314 – Section 5 Recitation 3
Two issues in lexical analysis
Chapter 2 FINITE AUTOMATA.
Department of Software & Media Technology
Parsing & Context-Free Grammars Hal Perkins Autumn 2011
(Slides copied liberally from Ruth Anderson, Hal Perkins and others)
REGULAR LANGUAGES AND REGULAR GRAMMARS
CSE322 CONSTRUCTION OF FINITE AUTOMATA EQUIVALENT TO REGULAR EXPRESSION Lecture #9.
Non-Deterministic Finite Automata
Recognition of Tokens.
Non Deterministic Automata
CHAPTER 2 Context-Free Languages
Finite Automata.
4b Lexical analysis Finite Automata
Parsing Costas Busch - LSU.
Introduction to Parsing
Introduction to Parsing
4b Lexical analysis Finite Automata
Parsing & Context-Free Grammars Hal Perkins Summer 2004
Chapter # 5 by Cohen (Cont…)
Parsing & Context-Free Grammars Hal Perkins Autumn 2005
Presentation transcript:

CPSC Compiler Tutorial 4 Midterm Review

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.

DFA Example q1 q δ01 q1 q2 q1q2 Transition function: Start: Final:

DFA Example S q1 q2 r1 r2 a b a ab b b ab 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 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?

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

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.

NFA →DFA Example (1) NFA example (reg.ex.: 01(00|11)*10 ) ε ε ε ε q0q0 q1q1 q2q2 q6q6 q7q7 q8q8 q3q3 q4q4 q5q5 q9q9 q 10 q 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

NFA →DFA Example (3) 0 q0q0 q1q1  1 1 q2q3q6q9q2q3q6q9 q7q7 q 4 q 10 q 11 q2q3q6q8q9q2q3q6q8q9 q2q3q6q5q9q2q3q6q5q ,

Exercise What language is accepted by following NFA? Convert it to a DFA q0q0 q1q1 q2q2 q5q5 q6q6 q3q3 q4q4 q7q7 q8q

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

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)* q0q1q2 q3q ,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 the other A grammar without this property is ambiguous – 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