pythonabstract-syntax-treeparser-generatorebnfgrako

How to process the stuctured language file in python


I have a big structured language file like this:

TASK SchM_Task {
    TYPE = AUTO;
    SCHEDULE = NON;
    PRIORITY = 160;
    ACTIVATION = 1;
    TIMING_PROTECTION = FALSE;
    AUTOSTART = FALSE;
    EVENT = SchM_Event;
    RESOURCE = SystemS_Resource;
    StackSize = 1024;
    NotUsingSchedule = FALSE;
}: "BSW task for calling of bsw runnables";
ALARM SchM_Alarm {
    COUNTER = SystemTimer;
    ACTION = SETEVENT
    {
       TASK = SchM_Task;
       EVENT = SchM_Event;
    };
    AUTOSTART = FALSE
    {
       StaticAlarm = FALSE;
    };
};
RESOURCE SystemS_Resource {
    RESOURCEPROPERTY = INTERNAL;
}: "Via this resource the cooperativ behavior can be set";
EVENT SchM_Event {
   MASK = AUTO;
};
ISR CanIsr_1 {
   CATEGORY = 2;
   TIMING_PROTECTION = FALSE;
   EnableNesting = TRUE;
   InterruptLevel = 30;
   InterruptSource = CAN1IRQ;
   StackSize = 1024;
   UseSpecialFunctionName = FALSE;
}: "CAN Send/Receive (main CAN)";

I am really new to python and for scripting. How to parser this text file to AST using python. On search in web I have found grako in python may be an option. Can you please explain how the grako works with a sample code. Thanks ahead.


Solution

  • The language seems simple enough. You should read a bit about parsing to understand what you have to do, whatever the programming language.

    PyParsing is popular among Python programmers. I think that Grako (I'm the author) is too complex for a beginner to parsing.