Implement Regular Expression Matching Using Finite Automaton

Source: Internet
Author: User

Q: Is there a regular expression abc * d in the main string? E matching. We will use the finite automatic machine method to search for it. The simplest DFA of this regular expression is as follows:

Shows the status conversion table.

The two-dimensional array is defined in the program as follows:

# Define STATES_NUMBER5 # define LETTER_NUMBER5 // indicates abc * d? E's simplest DFA status conversion table (-1 indicates not acceptable) int trans_table [STATES_NUMBER] [LETTER_NUMBER] = {1,-1,-1,-1,-1,-1, -1, 2,-1,-1,-1,-1,-1,-1, 2, 3, 4,-1,-1,-1,-1, 4, -1,-1,-1,-1,-1 };
The algorithm is a brute-force match. Among them, the is_matched function imitates the library function strstr in C language (the source code in Linux is highly robust ).

The procedure is as follows:

/*************************************** *********************************** Created: 2014/03/08 filename: main. cauthor: Justme0 (http://blog.csdn.net/justme0) purpose: Simulate DFA, search for the pattern abc * d in the master string? E, check whether a match exists ************************************ **************************************/# include
 
  
# Include
  
   
# Include
   
    
# Include # define MAX_LENGTH100 # define STATES_NUMBER5 # define LETTER_NUMBER5 // indicates abc * d? E's simplest DFA status conversion table (-1 indicates not acceptable) // you can find whether the table can be matched without greedy match, therefore, the last row in the acceptance status will not actually be accessed to int trans_table [STATES_NUMBER] [LETTER_NUMBER] = {1,-1,-1,-1,-1,-1, 2,-1,-1,-1,-1,-1,-1, 2, 3, 4,-1,-1,-1,-1,-1, -1,-1,-1,-1};/*** whether it is the acceptance status */int is_end (const int state) {return STATES_NUMBER-1 = state ;} /*** Whether the state accepts letter */int is_acceptable (const int state, const char letter) {int column = letter-'A'; assert (0 <= sta Te & state <STATES_NUMBER); if (! (0 <= column & column <LETTER_NUMBER) {return 0;} return-1! = Trans_table [state] [column];} int move (const int state, const char letter) {int column = letter-'A'; return trans_table [state] [column]; // is_acceptable and move are redundant. To be improved}/*** if the primary string matches the pattern, 1 is returned, otherwise 0 */int is_matched (const char * const str) {const char * head = NULL; // head is the position of the current mode header in str const char * p = NULL; // p indicates that the main string int state = 0; // state indicates the mode for (head = str; '\ 0 '! = * Head; ++ head) {state = 0; for (p = head; '\ 0 '! = * P &&(! Is_end (state) & is_acceptable (state, * p); ++ p) {state = move (state, * p);} if (is_end (state )) {return 1 ;}} return 0 ;}int main (int argc, char ** argv) {char str [MAX_LENGTH]; int ans; FILE * in_stream = freopen ("test.txt", "r", stdin); if (NULL = in_stream) {printf ("Can not open file! \ N "); exit (1) ;}while (EOF! = Scanf ("% s", str) {scanf ("% d", & ans); assert (ans = is_matched (str )); if (is_matched (str) {printf ("find abc * d? E matches \ n ");} else {printf (" abc * d not found? E match \ n ") ;}} fclose (in_stream); return 0 ;}/ * test.txt abe 1 abee 1 abde 1 eabcce 1bb33_aabcabdee 1-* + 68 1271a 0abc 0b 0. 0eab 0 eabcccd 0 abdff 0 & * % & (* 0 */
   
  
 

There are several feelings:

1. The AC automation that is often used in ACM is different from the Trie tree implementation.

2. As mentioned above, it uses a brute-force match. That is to say, the pattern is not matched this time, moving one character forward and re-matching. I think there should be a KMP-like algorithm, you can slide multiple characters without matching them.

3. The library that provides regular expressions is used to search for any regular expression in the main string. It is supported in many languages and added to C ++ 11, you may be able to view the source code. The method used in the http://blog.csdn.net/xiaozhuaixifu/article/details/9875423 in this article is relatively simple, direct matching, the implementation principle of the library may be similar to it.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.