aboutsummaryrefslogtreecommitdiff
Regular expressions

Regular expressions
Matching links to Google documents

1. Task

Construct a regular expression, then a non-deterministic finite state machine, and determinize it (transitions can be specified using ranges). Implement a program that checks the entered text using the finite state machine implementation (output options: the string matches, does not match, characters are not from the alphabet). It is also necessary to implement a function for randomly generating a valid string based on the resulting finite state machine.

Examples of correct strings:

https://docs.google.com/document/d/1poUMiPBYm/edit?usp=sharing&sd=true
https://docs.google.com/document/d/1poUMiPm-wfYTyTf6NFtPwbWEae_/preview
http://docs.google.com/document/d/1po-MiPBwUfY5MUTyTf6NFtPwbWEae_/copy
docs.google.com/document/d/1po-UMiPBYmfY5MlaksjdshUTywTf6NFtPwbWEae_/export?
docs.google.com/document/d/1po-UMiPBYmfY5MUsdfTywTf6NFtPwbWEae_
docs.new
http://docs.new/
https://docs.new//

There are 3 possible protocol specifications:

  • an empty string
  • http​://
  • https​://

There are 2 different inputs:

  • docs.new – only the characters /.
  • docs.google.com, then:
    1. must be /document/d/,
    2. must be a document identifier
    3. an optional path command after /
    4. optional options after ?

2. Documentation

The exported report can be found at this link.

3. Showcase

3.1. Non-deterministic finite state machine

The full non-deterministic finite state machine can be found at this link.

fsm-undetermined-1.png

Figure 1: Non-deterministic finite state machine (part 1/2)

fsm-undetermined-2.png

Figure 2: Non-deterministic finite state machine (part 2/2)

3.2. Deterministic finite state machine

The full deterministic finite state machine can be found at this link.

fsm-determined-1.png

Figure 3: Deterministic finite state machine (part 1/3)

fsm-determined-2.png

Figure 4: Deterministic finite state machine (part 2/3)

fsm-determined-3.png

Figure 5: Deterministic finite state machine (part 3/3)

3.3. Program

state state_transition_table[STATE_COUNT][ALPHABET_SYMBOL_COUNT] = {
  [ 1] = {
    [C2A ('h')] =  2,           // https?://
    [C2A ('d')] = 10,           // docs\.
  },
  [ 2] = { [C2A ('t')] =  3 },
  [ 3] = { [C2A ('t')] =  4 },
  [ 4] = { [C2A ('p')] =  5 },
  [ 5] = { [C2A ('s')] =  6, [C2A (':')] =  7 },
  [ 6] = { [C2A (':')] =  7 },
  [ 7] = { [C2A ('/')] =  8 },
  [ 8] = { [C2A ('/')] =  9 },
  [ 9] = { [C2A ('d')] = 10 },
  [10] = { [C2A ('o')] = 11 },
  [11] = { [C2A ('c')] = 12 },
  [12] = { [C2A ('s')] = 13 },
  [13] = { [C2A ('.')] = 14 },
  [14] = {
    [C2A ('g')] = 15,           // (g)oogle\.com
    [C2A ('n')] = 74,           // (n)ew
  },
  [15] = { [C2A ('o')] = 16 },
  [16] = { [C2A ('o')] = 17 },
  [17] = { [C2A ('g')] = 18 },
  [18] = { [C2A ('l')] = 19 },
  [19] = { [C2A ('e')] = 20 },
  [20] = { [C2A ('.')] = 21 },
  [21] = { [C2A ('c')] = 22 },
  [22] = { [C2A ('o')] = 23 },
  [23] = { [C2A ('m')] = 24 },
  [24] = { [C2A ('/')] = 25 },
  [25] = { [C2A ('d')] = 26 },
  [26] = { [C2A ('o')] = 27 },
  [27] = { [C2A ('c')] = 28 },
  [28] = { [C2A ('u')] = 29 },
  [29] = { [C2A ('m')] = 30 },
  [30] = { [C2A ('e')] = 31 },
  [31] = { [C2A ('n')] = 32 },
  [32] = { [C2A ('t')] = 33 },
  [33] = { [C2A ('/')] = 34 },
  [34] = { [C2A ('d')] = 35 },
  [35] = { [C2A ('/')] = 36 },
  [36] = { [C2A ('-')] = 37, [C2A ('0')] = 37, [C2A ('1')] = 37, [C2A ('2')] = 37, [C2A ('3')] = 37, [C2A ('4')] = 37, [C2A ('5')] = 37, [C2A ('6')] = 37, [C2A ('7')] = 37, [C2A ('8')] = 37, [C2A ('9')] = 37, [C2A ('A')] = 37, [C2A ('B')] = 37, [C2A ('C')] = 37, [C2A ('D')] = 37, [C2A ('E')] = 37, [C2A ('F')] = 37, [C2A ('G')] = 37, [C2A ('H')] = 37, [C2A ('I')] = 37, [C2A ('J')] = 37, [C2A ('K')] = 37, [C2A ('L')] = 37, [C2A ('M')] = 37, [C2A ('N')] = 37, [C2A ('O')] = 37, [C2A ('P')] = 37, [C2A ('Q')] = 37, [C2A ('R')] = 37, [C2A ('S')] = 37, [C2A ('T')] = 37, [C2A ('U')] = 37, [C2A ('V')] = 37, [C2A ('W')] = 37, [C2A ('X')] = 37, [C2A ('Y')] = 37, [C2A ('Z')] = 37, [C2A ('_')] = 37, [C2A ('a')] = 37, [C2A ('b')] = 37, [C2A ('c')] = 37, [C2A ('d')] = 37, [C2A ('e')] = 37, [C2A ('f')] = 37, [C2A ('g')] = 37, [C2A ('h')] = 37, [C2A ('i')] = 37, [C2A ('j')] = 37, [C2A ('k')] = 37, [C2A ('l')] = 37, [C2A ('m')] = 37, [C2A ('n')] = 37, [C2A ('o')] = 37, [C2A ('p')] = 37, [C2A ('q')] = 37, [C2A ('r')] = 37, [C2A ('s')] = 37, [C2A ('t')] = 37, [C2A ('u')] = 37, [C2A ('v')] = 37, [C2A ('w')] = 37, [C2A ('x')] = 37, [C2A ('y')] = 37, [C2A ('z')] = 37 },
  [37] = { [C2A ('-')] = 37, [C2A ('0')] = 37, [C2A ('1')] = 37, [C2A ('2')] = 37, [C2A ('3')] = 37, [C2A ('4')] = 37, [C2A ('5')] = 37, [C2A ('6')] = 37, [C2A ('7')] = 37, [C2A ('8')] = 37, [C2A ('9')] = 37, [C2A ('A')] = 37, [C2A ('B')] = 37, [C2A ('C')] = 37, [C2A ('D')] = 37, [C2A ('E')] = 37, [C2A ('F')] = 37, [C2A ('G')] = 37, [C2A ('H')] = 37, [C2A ('I')] = 37, [C2A ('J')] = 37, [C2A ('K')] = 37, [C2A ('L')] = 37, [C2A ('M')] = 37, [C2A ('N')] = 37, [C2A ('O')] = 37, [C2A ('P')] = 37, [C2A ('Q')] = 37, [C2A ('R')] = 37, [C2A ('S')] = 37, [C2A ('T')] = 37, [C2A ('U')] = 37, [C2A ('V')] = 37, [C2A ('W')] = 37, [C2A ('X')] = 37, [C2A ('Y')] = 37, [C2A ('Z')] = 37, [C2A ('_')] = 37, [C2A ('a')] = 37, [C2A ('b')] = 37, [C2A ('c')] = 37, [C2A ('d')] = 37, [C2A ('e')] = 37, [C2A ('f')] = 37, [C2A ('g')] = 37, [C2A ('h')] = 37, [C2A ('i')] = 37, [C2A ('j')] = 37, [C2A ('k')] = 37, [C2A ('l')] = 37, [C2A ('m')] = 37, [C2A ('n')] = 37, [C2A ('o')] = 37, [C2A ('p')] = 37, [C2A ('q')] = 37, [C2A ('r')] = 37, [C2A ('s')] = 37, [C2A ('t')] = 37, [C2A ('u')] = 37, [C2A ('v')] = 37, [C2A ('w')] = 37, [C2A ('x')] = 37, [C2A ('y')] = 37, [C2A ('z')] = 37,
           [C2A ('/')] = 38,
           [C2A ('?')] = 71,    // \?.*
  },
  [38] = {
    [C2A ('c')] = 39,           // (c)opy
    [C2A ('e')] = 42,           // (e)dit
    [C2A ('m')] = 45,           // (m)obilebasic
    [C2A ('p')] = 55,           // (p)review
    [C2A ('t')] = 61,           // (t)emplate/preview
    [C2A ('v')] = 58,           // (v)iew
  },
  [39] = { [C2A ('o')] = 40 },  // (co)py
  [40] = { [C2A ('p')] = 41 },
  [41] = { [C2A ('y')] = 70 },  // ?
  [42] = { [C2A ('d')] = 43 },  // (ed)it
  [43] = { [C2A ('i')] = 44 },
  [44] = { [C2A ('t')] = 70 },  // ?
  [45] = { [C2A ('o')] = 46 },  // (mo)bilebasic
  [46] = { [C2A ('b')] = 47 },
  [47] = { [C2A ('i')] = 48 },
  [48] = { [C2A ('l')] = 49 },
  [49] = { [C2A ('e')] = 50 },
  [50] = { [C2A ('b')] = 51 },
  [51] = { [C2A ('a')] = 52 },
  [52] = { [C2A ('s')] = 53 },
  [53] = { [C2A ('i')] = 54 },
  [54] = { [C2A ('c')] = 70 },  // ?
  [55] = { [C2A ('r')] = 56 },  // (p)review
  [56] = { [C2A ('e')] = 57 },  // view
  [57] = { [C2A ('v')] = 58 },  // (template/)(pre)(v)iew
  [58] = { [C2A ('i')] = 59 },
  [59] = { [C2A ('e')] = 60 },
  [60] = { [C2A ('w')] = 70 },  // ?
  [61] = { [C2A ('e')] = 62 },  // (te)mplate
  [62] = { [C2A ('m')] = 63 },
  [63] = { [C2A ('p')] = 64 },
  [64] = { [C2A ('l')] = 65 },
  [65] = { [C2A ('a')] = 66 },
  [66] = { [C2A ('t')] = 67 },
  [67] = { [C2A ('e')] = 68 },
  [68] = { [C2A ('/')] = 69 },
  [69] = { [C2A ('p')] = 55 },  // (p)review
  [70] = { [C2A ('?')] = 71 },  // .*
  [71] = { [C2A ('!')] = 71, [C2A ('"')] = 71, [C2A ('#')] = 71, [C2A ('$')] = 71, [C2A ('%')] = 72, [C2A ('&')] = 71, [C2A ('\'')] = 71, [C2A ('(')] = 71, [C2A (')')] = 71, [C2A ('*')] = 71, [C2A ('+')] = 71, [C2A (',')] = 71, [C2A ('-')] = 71, [C2A ('.')] = 71, [C2A ('/')] = 71, [C2A ('0')] = 71, [C2A ('1')] = 71, [C2A ('2')] = 71, [C2A ('3')] = 71, [C2A ('4')] = 71, [C2A ('5')] = 71, [C2A ('6')] = 71, [C2A ('7')] = 71, [C2A ('8')] = 71, [C2A ('9')] = 71, [C2A (':')] = 71, [C2A (';')] = 71, [C2A ('<')] = 71, [C2A ('=')] = 71, [C2A ('>')] = 71, [C2A ('?')] = 71, [C2A ('@')] = 71, [C2A ('A')] = 71, [C2A ('B')] = 71, [C2A ('C')] = 71, [C2A ('D')] = 71, [C2A ('E')] = 71, [C2A ('F')] = 71, [C2A ('G')] = 71, [C2A ('H')] = 71, [C2A ('I')] = 71, [C2A ('J')] = 71, [C2A ('K')] = 71, [C2A ('L')] = 71, [C2A ('M')] = 71, [C2A ('N')] = 71, [C2A ('O')] = 71, [C2A ('P')] = 71, [C2A ('Q')] = 71, [C2A ('R')] = 71, [C2A ('S')] = 71, [C2A ('T')] = 71, [C2A ('U')] = 71, [C2A ('V')] = 71, [C2A ('W')] = 71, [C2A ('X')] = 71, [C2A ('Y')] = 71, [C2A ('Z')] = 71, [C2A ('[')] = 71, [C2A ('\\')] = 71, [C2A (']')] = 71, [C2A ('^')] = 71, [C2A ('_')] = 71, [C2A ('`')] = 71, [C2A ('a')] = 71, [C2A ('b')] = 71, [C2A ('c')] = 71, [C2A ('d')] = 71, [C2A ('e')] = 71, [C2A ('f')] = 71, [C2A ('g')] = 71, [C2A ('h')] = 71, [C2A ('i')] = 71, [C2A ('j')] = 71, [C2A ('k')] = 71, [C2A ('l')] = 71, [C2A ('m')] = 71, [C2A ('n')] = 71, [C2A ('o')] = 71, [C2A ('p')] = 71, [C2A ('q')] = 71, [C2A ('r')] = 71, [C2A ('s')] = 71, [C2A ('t')] = 71, [C2A ('u')] = 71, [C2A ('v')] = 71, [C2A ('w')] = 71, [C2A ('x')] = 71, [C2A ('y')] = 71, [C2A ('z')] = 71, [C2A ('{')] = 71, [C2A ('|')] = 71, [C2A ('}')] = 71, [C2A ('~')] = 71 },
  [72] = { [C2A ('0')] = 73, [C2A ('1')] = 73, [C2A ('2')] = 73, [C2A ('3')] = 73, [C2A ('4')] = 73, [C2A ('5')] = 73, [C2A ('6')] = 73, [C2A ('7')] = 73, [C2A ('8')] = 73, [C2A ('9')] = 73, [C2A ('A')] = 73, [C2A ('B')] = 73, [C2A ('C')] = 73, [C2A ('D')] = 73, [C2A ('E')] = 73, [C2A ('F')] = 73, [C2A ('a')] = 73, [C2A ('b')] = 73, [C2A ('c')] = 73, [C2A ('d')] = 73, [C2A ('e')] = 73, [C2A ('f')] = 73 },
  [73] = { [C2A ('0')] = 71, [C2A ('1')] = 71, [C2A ('2')] = 71, [C2A ('3')] = 71, [C2A ('4')] = 71, [C2A ('5')] = 71, [C2A ('6')] = 71, [C2A ('7')] = 71, [C2A ('8')] = 71, [C2A ('9')] = 71, [C2A ('A')] = 71, [C2A ('B')] = 71, [C2A ('C')] = 71, [C2A ('D')] = 71, [C2A ('E')] = 71, [C2A ('F')] = 71, [C2A ('a')] = 71, [C2A ('b')] = 71, [C2A ('c')] = 71, [C2A ('d')] = 71, [C2A ('e')] = 71, [C2A ('f')] = 71 },
  [74] = { [C2A ('e')] = 75 },  // (ne)w
  [75] = { [C2A ('w')] = 76 },  // /*
  [76] = { [C2A ('/')] = 76 },  // /*
};

state set_of_final_states[FINAL_STATE_COUNT] = { 37, 38, 70, 71, 76 };
$ ./stats
                  INITIAL_STATE:     1
                    STATE_COUNT:    77
              FINAL_STATE_COUNT:     5
          ALPHABET_SYMBOL_COUNT:    94
sizeof   state_transition_table:  7238
sizeof      set_of_final_states:     5
sizeof bitarray_of_final_states:    10
sizeof                      fsm:  7253

3.3.1. Matching

$ ./fsm-match
Usage: ./fsm-match string
$ ./fsm-match "asdf 123"
error: symbols not from alphabet (starting from ` ' at column 4)
$ ./fsm-match http//docs.google.com/document/d/id
info: string doesn't match
$ ./fsm-match http://docs.google.com/document/d/id
info: string matches

3.3.2. Generating

$ ./fsm-generate --usage
Usage: ./fsm-generate [options]
Options:
  --usage              Print this usage string.
  --count <count>      Number of strings to generate.
  --seed <seed>        Seed number for srand.
$ ./fsm-generate
http://docs.new/
$ ./fsm-generate --count 10
docs.new
http://docs.google.com/document/d/CAJ06Aavs/edit?6GDAb%Ab11C>%95l~t^a;u!!=c:[@M96
https://docs.google.com/document/d/0pxlMmMHOsguaGdY7Tn8dTEWxpYYURJnXH_?+
docs.new/
https://docs.new/
docs.google.com/document/d/XpY2gwCiwje89vUy56pB5EtL/template/preview
http://docs.google.com/document/d/KkqvwK2?_UvWwW_mD4G;&sie<G/YUJf5|Zjt2Gc
https://docs.google.com/document/d/AT9e0qzp_VlzDecx4VaZ62vSl6FlY
https://docs.google.com/document/d/7JI5C/mobilebasic
docs.google.com/document/d/cnHxOjANGjVUqKKCdgCXOBSP/view

4. Unlicense

This is free and unencumbered software released into the public domain.

Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.

In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

For more information, please refer to https://unlicense.org/

Date: 2025H1

Author: Martin Michalec

Created: 2026-11-08 Sun 08:50