Implement NFA construction for empty expression and literals

This commit is contained in:
2024-10-26 22:24:21 +01:00
parent 5d980cf64b
commit 2ac92f62f1
5 changed files with 128 additions and 1 deletions

45
lib/construct.c Normal file
View File

@@ -0,0 +1,45 @@
/*
* Copyright (c) Camden Dixie O'Brien
* SPDX-License-Identifier: AGPL-3.0-only
*/
#include "construct.h"
#include <assert.h>
#include <stdlib.h>
static void construct_literal(char literal, fsa_t *out)
{
fsa_init(out);
const int id = fsa_add_state(out);
fsa_add_rule(out, id, out->initial, literal);
out->initial = id;
}
static void construct_term(const regex_term_t *term, fsa_t *out)
{
switch (term->type) {
case REGEX_TERM_EMPTY:
fsa_init(out);
break;
case REGEX_TERM_LITERAL:
construct_literal(term->literal, out);
break;
case REGEX_TERM_SUBEXPR:
return;
case REGEX_TERM_WILDCARD:
case REGEX_TERM_CLASS:
assert(false);
}
}
static void construct_sequence(const regex_sequence_t *seq, fsa_t *out)
{
construct_term(&seq->contents[0], out);
}
void construct(const regex_t *regex, fsa_t *out)
{
construct_sequence(&regex->contents[0], out);
}

14
lib/construct.h Normal file
View File

@@ -0,0 +1,14 @@
/*
* Copyright (c) Camden Dixie O'Brien
* SPDX-License-Identifier: AGPL-3.0-only
*/
#ifndef CONSTRUCT_H
#define CONSTRUCT_H
#include "fsa.h"
#include "regex.h"
void construct(const regex_t *regex, fsa_t *out);
#endif