[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Generating the input-parser with flex

From: Viktor Engelmann
Subject: Generating the input-parser with flex
Date: Sat, 22 Jan 2022 13:03:44 +0100
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.5.0

Hello everyone,

I looked into the gperf input-parser and found it quite excessive to have over 1000 lines of code to parse only 24 variables. So I started writing a flex-file for the task and managed to get it quite functional with only ~100 lines of code (the input.l contains some additional code to make it show what it's trying to do instead of actually doing it. Also it doesn't support the struct-feature yet).

Flex uses regular expressions everywhere, so it was quite easy, to even enforce
certain patterns for the variables. For example the hash-function-name can
only be set to something that matches [a-zA-Z_][a-zA-Z0-9_]* (the pattern for C/C++ identifiers. So something that isn't a valid function-name will be caught
already before it gets to the compiler and causes errors there.

I haven't tested the speed, but since the original input-parser compares every line to every variable separately, I guess it should be faster (although this shouldn't
matter much, because there are only 24 variables anyways).

I also wrote a less strict parser in another flex file. This one allows multiple syntaxes
for the same functionality. For example you can use %FOO = BAR or %FOO BAR
or %define FOO BAR or %define FOO = BAR interchangeably.
This lexer has only ~50 lines of code.

you can translate the flex-files using

    flex -o input.cpp input.l
    flex -o input2.cpp input2.l

and compile the resulting codes using

    g++ -o input input.cpp
    g++ -o input2 input2.cpp

What are your thoughts on this?

Kind regards


Attachment: input.l
Description: Text document

Attachment: input2.l
Description: Text document

reply via email to

[Prev in Thread] Current Thread [Next in Thread]