You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Let's get started by generating a regular expression checker, testing whether a string is of the language of `/(a|b)*cd?/` using *lxa*.
23
+
24
+
> Tips: You will see there are concepts of *NFAs* and *DFAs* in the example code. Don't be worried about that since using *lxa* does not require the prerequisite knowledge of [NFAs (Non-deterministic Finite Automata)](https://en.wikipedia.org/wiki/Nondeterministic_finite_automaton) and [DFAs (Deterministic Finite Automata)](https://en.wikipedia.org/wiki/Deterministic_finite_automaton). It's not hard for you to build your own lexical analyzer or regular expression tools following this guide. Understanding those concepts helps you acquire a deeper understanding of the *lxa*'s principle though.
25
+
26
+
The expression of `(a|b)*cd?` consists of three parts, which also consist of smaller units, and so on. The following describes all the parts of the entire expression.
27
+
28
+
The entire expression is the concatenation of the following three expressions
29
+
30
+
-`(a|b)*`
31
+
32
+
- which is the closure of `(a|b)`
33
+
34
+
- which is the union of `a` and `b`
35
+
36
+
- A single character of `c`
37
+
38
+
-`d?`
39
+
40
+
- The concatenation of single character `d` and empty string (We mark empty string as `ε` (epsilon)
41
+
42
+
1. First, we need to create *states* for each part of the expression and combine them together.
const final =newConcatState(concat_with_c, d_or_empty);
74
+
```
75
+
2.GenerateaDFAfortesting.
76
+
77
+
```ts
78
+
import { NFA } from 'lxa';
79
+
const dfa = new NFA(final).toDFA();
80
+
81
+
dfa.test('aaac'); // true
82
+
dfa.test('abcd') // true
83
+
dfa.test('bbbcd') // true
84
+
dfa.test('ad') // false
85
+
```
86
+
Itisverbosetounionorconcatenatemultiplestatesbecauseweneedtonestthosestatesinaverydeephierarchy, especiallywhentheexpressioniscomplicated. Wehaveprovidedyouwithtwoutilfunctions [`concatMultipleStates()`](#concatmultiplestates), [`unionMultipleStates()`](#unionmultiplestates) tounionorconcatenatemultiplestatessuchthatwedon't have to nest them all.
-`inputType`iseithera`string`typeor the [`epsilon`](#epsilon) object
116
+
- `accepted` indicates whether the current state is accepted or not. If the current state is accepted and there is no more input string, the whole regular expression is accepted. Refer to the the explanation for *NFAs* and *DFAs* for more details about the *accepted* state. Default to `false`.
0 commit comments