SlideShare ist ein Scribd-Unternehmen logo
1 von 28
CMSC 723 / LING 645: Intro to Computational Linguistics September 8, 2004: Monz Regular Expressions and  Finite State Automata (J&M 2) Prof. Bonnie J. Dorr Dr. Christof Monz TA: Adam Lee
Regular Expressions  and  Finite State Automata ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Regular Expressions ,[object Object],[object Object],[object Object]
Regular Expressions ,[object Object],[object Object]
Regular Expressions ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],* + Stephen Cole Kleene
Regular Expressions ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Disjunction, Grouping, Precedence ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Perl Commands While ( $line=<STDIN> ){ if ( $line =~ /the/ ){ print “MATCH: $line”; } }
Writing correct expressions ,[object Object],/the/ / [tT] he/ /  [tT] he  / / [^a-zA-Z] [tT] he [^a-zA-Z] / / ( ^ | [^a-zA-Z] ) [tT] he [^a-zA-Z] /
A more complex example ,[object Object],/ $ [0-9]+/ / $ [0-9]+ [0-9][0-9] / /  $ [0-9]+ ( [0-9][0-9] )?  / /  $ [0-9][0-9]?[0-9]? ( [0-9][0-9] )?  / /  [0-9]+  * ([MG]Hz|[Mm]egahertz|  [Gg]igahertz)  / /  [0-9]+  * (Mb|[Mm]egabytes ? )  / /  [0-9]([0-9]+)  * (Gb|[Gg]igabytes ? )  /
Advanced operators should be _
Substitutions and Memory ,[object Object],s/colour/color/ s/colour/color/g s/([Cc]olour)/$1olor/ /the (.*)er they were, the $1er they will be/ /the (.*)er they (.*), the $1er they $2/ Substitute as many times as possible! Case insensitive matching s/colour/color/i ,[object Object]
Eliza  [Weizenbaum, 1966] ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Eliza-style regular expressions ,[object Object],[object Object],[object Object],[object Object],Step 1: replace first person with second person references s/  I(’m| am)    /YOU ARE/g s/  my    /YOUR/g S/  mine    /YOURS/g Step 2: use additional regular expressions to generate replies Step 3: use scores to rank possible transformations
Finite-state Automata ,[object Object],[object Object],[object Object]
Finite-state Automata (Machines) /^baa+!$/ q 0 q 1 q 2 q 3 q 4 b a a ! a state transition final state baa! baaa! baaaa! baaaaa! ...
Input Tape REJECT a b a ! b q 0 0 1 2 3 4 b a a ! a
Input Tape ACCEPT b a a a q 0 q 1 q 2 q 3 q 3 q 4 ! 0 1 2 3 4 b a a ! a
Finite-state Automata ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
State-transition Tables 4: 3 2 1 0 State Ø 3 Ø 4 3 Ø Ø Ø Ø Ø 2 Ø Ø Ø 1 ! a b Input
D-RECOGNIZE function  D-RECOGNIZE ( tape ,  machine )  returns  accept or reject   index     Beginning of tape   current-state     Initial state of machine   loop   if  End of input has been reached  then   if  current-state is an accept state  then   return  accept   else   return  reject   elsif   transition-table [current-state, tape[index]]  is empty  then   return  reject   else   current-state    transition-table [current-state, tape[index]]   index     index  + 1 end
Adding a failing state q 0 q 1 q 2 q 3 q 4 b a a ! a q F a ! b ! b ! b b a !
Adding an “all else” arc q 0 q 1 q 2 q 3 q 4 b a a ! a q F = = = =
Languages and Automata ,[object Object],[object Object],[object Object],[object Object]
Languages and Automata ,[object Object],[object Object]
Using NFSAs to accept strings ,[object Object],[object Object],[object Object]
Using NFSAs Input Ø 4 Ø Ø Ø ! 4: 3 2 1 0 State Ø 2,3 Ø Ø Ø Ø Ø Ø Ø Ø 2 Ø Ø Ø 1  a b
Readings for next time ,[object Object]

Weitere ähnliche Inhalte

Was ist angesagt?

Regular Expression (Regex) Fundamentals
Regular Expression (Regex) FundamentalsRegular Expression (Regex) Fundamentals
Regular Expression (Regex) FundamentalsMesut Günes
 
Perl File Handling and Regex
Perl File Handling and RegexPerl File Handling and Regex
Perl File Handling and RegexJayant Parida
 
CONTEXT FREE GRAMMAR
CONTEXT FREE GRAMMAR CONTEXT FREE GRAMMAR
CONTEXT FREE GRAMMAR Zahid Parvez
 
Introduction to Regular Expressions
Introduction to Regular ExpressionsIntroduction to Regular Expressions
Introduction to Regular ExpressionsJesse Anderson
 
Using Regular Expressions in Grep
Using Regular Expressions in GrepUsing Regular Expressions in Grep
Using Regular Expressions in GrepDan Morrill
 
Regular Expressions 101 Introduction to Regular Expressions
Regular Expressions 101 Introduction to Regular ExpressionsRegular Expressions 101 Introduction to Regular Expressions
Regular Expressions 101 Introduction to Regular ExpressionsDanny Bryant
 
Regular Expressions 101
Regular Expressions 101Regular Expressions 101
Regular Expressions 101Raj Rajandran
 
Introduction to regular expressions
Introduction to regular expressionsIntroduction to regular expressions
Introduction to regular expressionsBen Brumfield
 
Introduction to Regular Expressions
Introduction to Regular ExpressionsIntroduction to Regular Expressions
Introduction to Regular ExpressionsMatt Casto
 
3.2 javascript regex
3.2 javascript regex3.2 javascript regex
3.2 javascript regexJalpesh Vasa
 
Regular expressions
Regular expressionsRegular expressions
Regular expressionsBrij Kishore
 

Was ist angesagt? (20)

Regular Expression (Regex) Fundamentals
Regular Expression (Regex) FundamentalsRegular Expression (Regex) Fundamentals
Regular Expression (Regex) Fundamentals
 
Grep
GrepGrep
Grep
 
Perl File Handling and Regex
Perl File Handling and RegexPerl File Handling and Regex
Perl File Handling and Regex
 
CONTEXT FREE GRAMMAR
CONTEXT FREE GRAMMAR CONTEXT FREE GRAMMAR
CONTEXT FREE GRAMMAR
 
Context free grammar
Context free grammarContext free grammar
Context free grammar
 
Regular Expressions
Regular ExpressionsRegular Expressions
Regular Expressions
 
Learning Grep
Learning GrepLearning Grep
Learning Grep
 
Introduction to Regular Expressions
Introduction to Regular ExpressionsIntroduction to Regular Expressions
Introduction to Regular Expressions
 
Using Regular Expressions in Grep
Using Regular Expressions in GrepUsing Regular Expressions in Grep
Using Regular Expressions in Grep
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 
Regular Expressions 101 Introduction to Regular Expressions
Regular Expressions 101 Introduction to Regular ExpressionsRegular Expressions 101 Introduction to Regular Expressions
Regular Expressions 101 Introduction to Regular Expressions
 
Minimizing DFA
Minimizing DFAMinimizing DFA
Minimizing DFA
 
Regular Expressions 101
Regular Expressions 101Regular Expressions 101
Regular Expressions 101
 
Introduction to regular expressions
Introduction to regular expressionsIntroduction to regular expressions
Introduction to regular expressions
 
Introduction to Regular Expressions
Introduction to Regular ExpressionsIntroduction to Regular Expressions
Introduction to Regular Expressions
 
XPath
XPathXPath
XPath
 
3.2 javascript regex
3.2 javascript regex3.2 javascript regex
3.2 javascript regex
 
UNIX - Class6 - sed - Detail
UNIX - Class6 - sed - DetailUNIX - Class6 - sed - Detail
UNIX - Class6 - sed - Detail
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 
Lex analysis
Lex analysisLex analysis
Lex analysis
 

Andere mochten auch

Deterministic Finite Automata
Deterministic Finite AutomataDeterministic Finite Automata
Deterministic Finite AutomataShiraz316
 
Properties of Regular Expressions
Properties of Regular ExpressionsProperties of Regular Expressions
Properties of Regular ExpressionsShiraz316
 
Lecture 03 lexical analysis
Lecture 03 lexical analysisLecture 03 lexical analysis
Lecture 03 lexical analysisIffat Anjum
 
Theory of automata and formal language
Theory of automata and formal languageTheory of automata and formal language
Theory of automata and formal languageRabia Khalid
 
Finite Automata
Finite AutomataFinite Automata
Finite AutomataShiraz316
 
Regular expression with DFA
Regular expression with DFARegular expression with DFA
Regular expression with DFAMaulik Togadiya
 
Finite State Automata
Finite State AutomataFinite State Automata
Finite State Automata孟賢 施
 
Lec 3 ---- dfa
Lec 3  ---- dfaLec 3  ---- dfa
Lec 3 ---- dfaAbdul Aziz
 
Theory of Automata
Theory of AutomataTheory of Automata
Theory of AutomataFarooq Mian
 
Regular language and Regular expression
Regular language and Regular expressionRegular language and Regular expression
Regular language and Regular expressionAnimesh Chaturvedi
 
Lecture: Regular Expressions and Regular Languages
Lecture: Regular Expressions and Regular LanguagesLecture: Regular Expressions and Regular Languages
Lecture: Regular Expressions and Regular LanguagesMarina Santini
 
5.state diagrams
5.state diagrams5.state diagrams
5.state diagramsAPU
 
Nondeterministic Finite Automat
Nondeterministic Finite AutomatNondeterministic Finite Automat
Nondeterministic Finite AutomatAdel Al-Ofairi
 

Andere mochten auch (17)

Deterministic Finite Automata
Deterministic Finite AutomataDeterministic Finite Automata
Deterministic Finite Automata
 
Properties of Regular Expressions
Properties of Regular ExpressionsProperties of Regular Expressions
Properties of Regular Expressions
 
Tests
TestsTests
Tests
 
Lecture 03 lexical analysis
Lecture 03 lexical analysisLecture 03 lexical analysis
Lecture 03 lexical analysis
 
Theory of automata and formal language
Theory of automata and formal languageTheory of automata and formal language
Theory of automata and formal language
 
Finite Automata
Finite AutomataFinite Automata
Finite Automata
 
Regular expression with DFA
Regular expression with DFARegular expression with DFA
Regular expression with DFA
 
Theory of computing pdf
Theory of computing pdfTheory of computing pdf
Theory of computing pdf
 
Finite State Automata
Finite State AutomataFinite State Automata
Finite State Automata
 
Optimization of dfa
Optimization of dfaOptimization of dfa
Optimization of dfa
 
Lec 3 ---- dfa
Lec 3  ---- dfaLec 3  ---- dfa
Lec 3 ---- dfa
 
Theory of Automata
Theory of AutomataTheory of Automata
Theory of Automata
 
Regular language and Regular expression
Regular language and Regular expressionRegular language and Regular expression
Regular language and Regular expression
 
Lecture: Regular Expressions and Regular Languages
Lecture: Regular Expressions and Regular LanguagesLecture: Regular Expressions and Regular Languages
Lecture: Regular Expressions and Regular Languages
 
5.state diagrams
5.state diagrams5.state diagrams
5.state diagrams
 
Finite Automata
Finite AutomataFinite Automata
Finite Automata
 
Nondeterministic Finite Automat
Nondeterministic Finite AutomatNondeterministic Finite Automat
Nondeterministic Finite Automat
 

Ähnlich wie Lecture2 B

Advanced Regular Expressions Redux
Advanced Regular Expressions ReduxAdvanced Regular Expressions Redux
Advanced Regular Expressions ReduxJakub Nesetril
 
Parsing Expression Grammars
Parsing Expression GrammarsParsing Expression Grammars
Parsing Expression Grammarsteknico
 
Mod 2_RegularExpressions.pptx
Mod 2_RegularExpressions.pptxMod 2_RegularExpressions.pptx
Mod 2_RegularExpressions.pptxRaviAr5
 
Automata theory - CFG and normal forms
Automata theory - CFG and normal formsAutomata theory - CFG and normal forms
Automata theory - CFG and normal formsAkila Krishnamoorthy
 
Talk Unix Shell Script
Talk Unix Shell ScriptTalk Unix Shell Script
Talk Unix Shell ScriptDr.Ravi
 
Theory of computing
Theory of computingTheory of computing
Theory of computingRanjan Kumar
 
language , grammar and automata
language , grammar and automatalanguage , grammar and automata
language , grammar and automataElakkiyaS11
 
Talk Unix Shell Script 1
Talk Unix Shell Script 1Talk Unix Shell Script 1
Talk Unix Shell Script 1Dr.Ravi
 
Theory of computing presentation
Theory of computing presentationTheory of computing presentation
Theory of computing presentationMd. Touhidur Rahman
 
Introduction to Regular Expressions RootsTech 2013
Introduction to Regular Expressions RootsTech 2013Introduction to Regular Expressions RootsTech 2013
Introduction to Regular Expressions RootsTech 2013Ben Brumfield
 
PUSH DOWN AUTOMATA VS TURING MACHINE
PUSH DOWN AUTOMATA VS TURING MACHINEPUSH DOWN AUTOMATA VS TURING MACHINE
PUSH DOWN AUTOMATA VS TURING MACHINEAbhishek Shivhare
 
Theory of Computation Regular Expressions, Minimisation & Pumping Lemma
Theory of Computation Regular Expressions, Minimisation & Pumping LemmaTheory of Computation Regular Expressions, Minimisation & Pumping Lemma
Theory of Computation Regular Expressions, Minimisation & Pumping LemmaRushabh2428
 
Introduction to Perl
Introduction to PerlIntroduction to Perl
Introduction to PerlSway Wang
 
RegularLanguageProperties [Autosaved].pptx
RegularLanguageProperties [Autosaved].pptxRegularLanguageProperties [Autosaved].pptx
RegularLanguageProperties [Autosaved].pptxRaviAr5
 
Regular Expressions: JavaScript And Beyond
Regular Expressions: JavaScript And BeyondRegular Expressions: JavaScript And Beyond
Regular Expressions: JavaScript And BeyondMax Shirshin
 

Ähnlich wie Lecture2 B (20)

Advanced Regular Expressions Redux
Advanced Regular Expressions ReduxAdvanced Regular Expressions Redux
Advanced Regular Expressions Redux
 
Parsing Expression Grammars
Parsing Expression GrammarsParsing Expression Grammars
Parsing Expression Grammars
 
Lec1.pptx
Lec1.pptxLec1.pptx
Lec1.pptx
 
Mod 2_RegularExpressions.pptx
Mod 2_RegularExpressions.pptxMod 2_RegularExpressions.pptx
Mod 2_RegularExpressions.pptx
 
To lec 03
To lec 03To lec 03
To lec 03
 
Automata theory - CFG and normal forms
Automata theory - CFG and normal formsAutomata theory - CFG and normal forms
Automata theory - CFG and normal forms
 
Talk Unix Shell Script
Talk Unix Shell ScriptTalk Unix Shell Script
Talk Unix Shell Script
 
Theory of computing
Theory of computingTheory of computing
Theory of computing
 
language , grammar and automata
language , grammar and automatalanguage , grammar and automata
language , grammar and automata
 
Talk Unix Shell Script 1
Talk Unix Shell Script 1Talk Unix Shell Script 1
Talk Unix Shell Script 1
 
Theory of computing presentation
Theory of computing presentationTheory of computing presentation
Theory of computing presentation
 
Lesson 04
Lesson 04Lesson 04
Lesson 04
 
Introduction to Regular Expressions RootsTech 2013
Introduction to Regular Expressions RootsTech 2013Introduction to Regular Expressions RootsTech 2013
Introduction to Regular Expressions RootsTech 2013
 
PUSH DOWN AUTOMATA VS TURING MACHINE
PUSH DOWN AUTOMATA VS TURING MACHINEPUSH DOWN AUTOMATA VS TURING MACHINE
PUSH DOWN AUTOMATA VS TURING MACHINE
 
Theory of Computation Regular Expressions, Minimisation & Pumping Lemma
Theory of Computation Regular Expressions, Minimisation & Pumping LemmaTheory of Computation Regular Expressions, Minimisation & Pumping Lemma
Theory of Computation Regular Expressions, Minimisation & Pumping Lemma
 
Introduction to Perl
Introduction to PerlIntroduction to Perl
Introduction to Perl
 
NLP_KASHK:Finite-State Automata
NLP_KASHK:Finite-State AutomataNLP_KASHK:Finite-State Automata
NLP_KASHK:Finite-State Automata
 
RegularLanguageProperties [Autosaved].pptx
RegularLanguageProperties [Autosaved].pptxRegularLanguageProperties [Autosaved].pptx
RegularLanguageProperties [Autosaved].pptx
 
Perl Presentation
Perl PresentationPerl Presentation
Perl Presentation
 
Regular Expressions: JavaScript And Beyond
Regular Expressions: JavaScript And BeyondRegular Expressions: JavaScript And Beyond
Regular Expressions: JavaScript And Beyond
 

Lecture2 B

  • 1. CMSC 723 / LING 645: Intro to Computational Linguistics September 8, 2004: Monz Regular Expressions and Finite State Automata (J&M 2) Prof. Bonnie J. Dorr Dr. Christof Monz TA: Adam Lee
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8. Perl Commands While ( $line=<STDIN> ){ if ( $line =~ /the/ ){ print “MATCH: $line”; } }
  • 9.
  • 10.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16. Finite-state Automata (Machines) /^baa+!$/ q 0 q 1 q 2 q 3 q 4 b a a ! a state transition final state baa! baaa! baaaa! baaaaa! ...
  • 17. Input Tape REJECT a b a ! b q 0 0 1 2 3 4 b a a ! a
  • 18. Input Tape ACCEPT b a a a q 0 q 1 q 2 q 3 q 3 q 4 ! 0 1 2 3 4 b a a ! a
  • 19.
  • 20. State-transition Tables 4: 3 2 1 0 State Ø 3 Ø 4 3 Ø Ø Ø Ø Ø 2 Ø Ø Ø 1 ! a b Input
  • 21. D-RECOGNIZE function D-RECOGNIZE ( tape , machine ) returns accept or reject index  Beginning of tape current-state  Initial state of machine loop if End of input has been reached then if current-state is an accept state then return accept else return reject elsif transition-table [current-state, tape[index]] is empty then return reject else current-state  transition-table [current-state, tape[index]] index  index + 1 end
  • 22. Adding a failing state q 0 q 1 q 2 q 3 q 4 b a a ! a q F a ! b ! b ! b b a !
  • 23. Adding an “all else” arc q 0 q 1 q 2 q 3 q 4 b a a ! a q F = = = =
  • 24.
  • 25.
  • 26.
  • 27. Using NFSAs Input Ø 4 Ø Ø Ø ! 4: 3 2 1 0 State Ø 2,3 Ø Ø Ø Ø Ø Ø Ø Ø 2 Ø Ø Ø 1  a b
  • 28.