3

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.

  • Some how i have managed to parse the file using grako. So now i have AST and JSON data structures. But now i want to edit the AST and re generate the file back. will grako do the same or any other python tool is needed. – Ravi Shankar Mar 06 '15 at 08:55

1 Answers1

3

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.

Apalala
  • 8,159
  • 3
  • 26
  • 47
  • 1
    Some how i have managed to parse the file using grako. So now i have AST and JSON data structures. But now i want to edit the AST and re generate the file back. will grako do the same or any other python tool is needed. – Ravi Shankar Mar 06 '15 at 08:55
  • 1
    Grako ``AST``s have their idiosyncrasies, but, indeed, they can be modified. If you choose to do so, take a look at the ``grako.codegen`` module and the bundled examples (antlr2grako) for Grako's standard way of doing translation (which doesn't have to be to a different language). You can also use the JSON with Python's ``json`` module to have the AST represented in standard data structures with which you can do whatever you want. – Apalala Mar 06 '15 at 12:25
  • 1
    I have used the grako.codegen to generate the file from AST. But generated file is not indented properly. And also not able to edit the AST tree. Can share some samples to achieve the above. – Ravi Shankar Mar 11 '15 at 10:07
  • 1
    Use ``grako.util.asjson()`` to convert the AST to standard Python data types. Then you can manipulate the structure at will. – Apalala Mar 12 '15 at 14:25
  • 1
    Thanks for suggesting pyparsing. Regardless of what parsing library one uses, I always recommend that developers write at least a rough [BNF](http://en.wikipedia.org/wiki/Backus%E2%80%93Naur_Form). It doesn't have to be rigorous BNF syntax, any kind of plan for the parser is better than nothing. You can review the BNF itself for consistency and ambiguity, which are good to get resolved before you even start writing the parser. The BNF will also help you track your progress while writing the parser, including the very valuable "bells-and-whistles detection" and "knowing when you are done". – PaulMcG May 04 '15 at 13:32