Verilator

Форк
0
/
generate_dictionary 
77 строк · 2.2 Кб
1
#!/usr/bin/env python3
2
# pylint: disable=C0103,C0114,C0115,C0116,C0321
3
######################################################################
4
# DESCRIPTION: Fuzzer dictionary generator
5
#
6
# Copyright 2019-2019 by Eric Rippey. This program is free software; you
7
# can redistribute it and/or modify it under the terms of either the GNU Lesser
8
# General Public License Version 3 or the Perl Artistic License Version 2.0.
9
# SPDX-License-Identifier: LGPL-3.0-only OR Artistic-2.0
10
######################################################################
11

12
# Attempts to pull a list of keywords out of the Flex input
13
# These are then put in a dictionary of "interesting" sequences
14
# This will be used to help the fuzzer pick interesting inputs more quickly.
15

16
from subprocess import getstatusoutput
17
from os import system
18

19

20
def take_while(f, a):
21
    # any(a) => (a->bool)->[a]->[a]
22
    # Does the same think as Haskell's takewhile.
23
    out = []
24
    for elem in a:
25
        if f(elem):
26
            out.append(elem)
27
        else:
28
            return out
29
    return out
30

31

32
def skip_while(f, a):
33
    # any(a) => (a->bool)->[a]->[a]
34
    # Basically, the opposite thing from skipwhile
35
    while len(a) and f(a[0]):
36
        a = a[1:]
37
    return a
38

39

40
def print_lines(a):
41
    # printable(a) => [a]->void
42
    for elem in a:
43
        print(elem)
44

45

46
def write_file(filename, contents):
47
    # str->str->void
48
    with open(filename, "w", encoding="utf8") as fh:
49
        fh.write(contents)
50

51

52
def parse_line(s):
53
    # str->maybe str
54
    if len(s) == 0: return None
55
    part = skip_while(lambda x: x != '"', s)
56
    if len(part) == 0 or part[0] != '"': return None
57
    literal_part = take_while(lambda x: x != '"', part[1:])
58
    return ''.join(filter(lambda x: x != '\\', literal_part))
59

60

61
def main():
62
    status, output = getstatusoutput('flex -T ../../src/verilog.l')
63
    assert status == 0
64

65
    lines = output.splitlines()
66
    lines = take_while(lambda x: 'beginning dump of nfa' not in x, lines)
67
    tokens = set(filter(lambda x: x, map(parse_line, lines)))
68

69
    dirname = 'dictionary'
70
    r = system('mkdir -p ' + dirname)
71
    assert r == 0
72
    for i, token in enumerate(tokens):
73
        write_file(dirname + '/' + str(i), token)
74

75

76
if __name__ == '__main__':
77
    main()
78

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.