如何在python中处理结构化语言文件

时间:2015-02-26 15:35:44

标签: python abstract-syntax-tree parser-generator ebnf grako

我有一个像这样的大型结构化语言文件:

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)";

我是python和脚本的新手。如何使用python将此文本文件解析为AST。在网上搜索我发现python中的grako可能是一个选项。你能解释一下grako如何使用示例代码。谢谢。

1 个答案:

答案 0 :(得分:3)

语言似乎很简单。您应该阅读一些关于parsing的内容,以了解您必须做什么,无论编程语言如何。

PyParsing在Python程序员中很受欢迎。我认为Grako(我是作者)太复杂了,不适合初学者解析。