|
Algorithms_in_C 1.0.0
Set of algorithms implemented in C.
|
McNaughton–Yamada–Thompson algorithm More...
#include <assert.h>#include <stdio.h>#include <string.h>#include <stdlib.h>Data Structures | |
| struct | ASTNode |
| for assert() More... | |
| struct | transRule |
| Definition for a NFA state transition rule. More... | |
| struct | NFAState |
| Definition for a NFA state. More... | |
| struct | NFA |
| Definition for the NFA itself. More... | |
Functions | |
| struct ASTNode * | createNode (const char content) |
| creates and initializes a AST node | |
| void | destroyNode (struct ASTNode *node) |
| recursively destroys a AST | |
| char * | preProcessing (const char *input) |
| performs preprocessing on a regex string, making all implicit concatenations explicit | |
| struct ASTNode * | buildAST (const char *input) |
| recursively constructs a AST from a preprocessed regex string | |
| struct transRule * | createRule (struct NFAState *state, char c) |
| creates and initializes a transition rule | |
| void | destroyRule (struct transRule *rule) |
| destroys a transition rule object | |
| struct NFAState * | createState (void) |
| creates and initializes a NFA state | |
| void | destroyState (struct NFAState *state) |
| destroys a NFA state | |
| struct NFA * | createNFA (void) |
| creates and initializes a NFA | |
| void | destroyNFA (struct NFA *nfa) |
| recursively destroys a NFA | |
| void | addState (struct NFA *nfa, struct NFAState *state) |
| adds a state to a NFA | |
| void | addRule (struct NFA *nfa, struct transRule *rule, int loc) |
| adds a transition rule to a NFA | |
| void | postProcessing (struct NFA *nfa) |
| performs postprocessing on a compiled NFA, add circular empty character transition rules where it's needed for the NFA to function correctly | |
| void | transit (struct NFA *nfa, char input) |
| moves a NFA forward | |
| int | isAccepting (const struct NFA *nfa) |
| determines whether the NFA is currently in its accepting state | |
| int | isLiteral (const char ch) |
| helper function to determine whether a character should be considered a character literal | |
| size_t | indexOf (const char *str, char key) |
| utility function to locate the first occurrence of a character in a string while respecting parentheses | |
| char * | subString (const char *str, size_t begin, size_t end) |
| utility function to create a subString | |
| void | redirect (struct NFA *nfa, struct NFAState *src, struct NFAState *dest) |
| helper function to recursively redirect transition rule targets | |
| struct NFA * | compileFromAST (struct ASTNode *root) |
| int | contains (struct NFAState **states, int len, struct NFAState *state) |
| helper function to determine an element's presence in an array | |
| void | findEmpty (struct NFAState *target, struct NFAState **states, int *sc) |
| helper function to manage empty character transitions | |
| void | testHelper (const char *regex, const char *string, const int expected) |
| Testing helper function. | |
| static void | test (void) |
| Self-test implementations. | |
| int | main (void) |
| Main function. | |
McNaughton–Yamada–Thompson algorithm
From Wikipedia: In computer science, Thompson's construction algorithm, also called the McNaughton–Yamada–Thompson algorithm, is a method of transforming a regular expression into an equivalent nondeterministic finite automaton (NFA). This implementation implements the all three operations (implicit concatenation, '|' for union, '*' for Kleene star) required by the formal definition of regular expressions.
adds a transition rule to a NFA
| nfa | target NFA |
| rule | the rule to be added |
| loc | which state this rule should be added to |
adds a state to a NFA
| struct ASTNode * buildAST | ( | const char * | input | ) |
recursively constructs a AST from a preprocessed regex string
| input | regex |
helper function to determine an element's presence in an array
| states | target array |
| len | length of the target array |
| state | the element to search for |
1 if the element is present, 0 otherwise | struct NFA * createNFA | ( | void | ) |
creates and initializes a NFA
| struct ASTNode * createNode | ( | const char | content | ) |
creates and initializes a AST node
| content | data to initializes the node with |
creates and initializes a transition rule
| state | transition target |
| c | transition condition |
| struct NFAState * createState | ( | void | ) |
| void destroyNFA | ( | struct NFA * | nfa | ) |
recursively destroys a NFA
| nfa | pointer to the object to be deleted |
| void destroyNode | ( | struct ASTNode * | node | ) |
recursively destroys a AST
| node | the root node of the tree to be deleted |
| void destroyRule | ( | struct transRule * | rule | ) |
destroys a transition rule object
| rule | pointer to the object to be deleted |
| void destroyState | ( | struct NFAState * | state | ) |
helper function to manage empty character transitions
| target | target NFA |
| states | pointer to results storage location |
| sc | pointer to results count storage location |
| size_t indexOf | ( | const char * | str, |
| char | key | ||
| ) |
utility function to locate the first occurrence of a character in a string while respecting parentheses
| str | target string |
| key | the character to be located |
0 if it could not be found | int isAccepting | ( | const struct NFA * | nfa | ) |
| int isLiteral | ( | const char | ch | ) |
helper function to determine whether a character should be considered a character literal
| ch | the character to be tested |
1 if it is a character literal 0 otherwise | int main | ( | void | ) |
| void postProcessing | ( | struct NFA * | nfa | ) |
performs postprocessing on a compiled NFA, add circular empty character transition rules where it's needed for the NFA to function correctly
| nfa | target NFA |
| char * preProcessing | ( | const char * | input | ) |
performs preprocessing on a regex string, making all implicit concatenations explicit
| input | target regex string |
| char * subString | ( | const char * | str, |
| size_t | begin, | ||
| size_t | end | ||
| ) |
utility function to create a subString
| str | target string |
| begin | starting index, inclusive |
| end | ending index, inclusive |
|
static |
Self-test implementations.
| void testHelper | ( | const char * | regex, |
| const char * | string, | ||
| const int | expected | ||
| ) |
Testing helper function.
| regex | the regular expression to be used |
| string | the string to match against |
| expected | expected results |
| void transit | ( | struct NFA * | nfa, |
| char | input | ||
| ) |
moves a NFA forward