Using Lark to parse a message definition file then generate C code

56 views Asked by At

So I'm definitely new to the concept and application of ENBF grammar and Lark in python. I'm having a bit of success but also am stuck on how to optimally do the next step of this project. To start let me explain what I'm trying to do.

I have a wireless communications protocol that has been built for a custom educational robotic platform. There is a miniscule library that was built to easily package, serialize, deserialize messages. But the problem is to make a new message there is a significant amount of boiler plate that needs to be written. So as a solution I want to move the message definitions into a separate file type very similar to how ROS/ROS2 do it (but in my case I don't need a message to be capable of containing another message in it as of now).

Here's an example.msg:

name TWIST
id 123
float variableone
float variabletwo

I'm able to use Lark to parse this text file and generate tokens that separate out the important elements. In this case the first line is the msg name (which is TWIST), next a numerical id, and in this case there are two members which are of float type.

I'm using this grammar file to parse it:

start: (msgname | msgid | member)+
msgname: "name" MSG_NAME
msgid: "id" MSG_ID
member: DATATYPE MEMBER_NAME

DATATYPE: "float"|"int"|"bool"
MSG_NAME: WORD
MEMBER_NAME: WORD
MSG_ID: INT

%import common ( INT, WORD, LETTER, NUMBER, ESCAPED_STRING, WS)
%ignore WS

The question comes with generating text that will generate proper C code in the following format:

struct **MSG_NAME** {
    **MSG_NAME**(const Packet&);

    int id=**123**; //MSG_ID
    **float field_1**; //member
    **float field_2**; //member
};

Not sure how to put bold inside a code quote, but I've put asterisks around the elements that need to be filled out dynamically for each message, the rest is the same for all messages.

I know that I can manually go through the output tree that Lark has provided me but was wondering if there is a more clever solution that would be capable of using ANOTHER grammar to define this struct in C and then somehow use the example.msg grammar to auto fill where necessary. Is this possible? And if so what features of Lark should I look at?

I have a repository of what I've tested so far (just the parser) if needed. I'm just very new to Lark and not aware of many of it's features, and the examples in Lark don't seem to be helpful for my use case but I imagine this is mostly my inexperience with Lark.

0

There are 0 answers