You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

TextX微波炉状态机模型解析报错,请求排查命令分组语法问题

微波炉状态机命令分组语法错误修复

问题描述

正在为微波炉构建状态机,包含烘焙、清洁两个功能,对应idle、baking、cleaning三个状态。进入状态时执行启动定时器、锁门等命令,尝试将这类命令分组并关联到状态动作,但触发语法错误,无法定位原因及修复方法。


元模型定义

StateMachine:
    'events'
        events+=Event
    'end'

    'commands'
        commands+=Command
    'end'

    ('commandgroups'
        commandGroupBlocks+=CommandGroupBlock        
    'end')? 

    states+=State
;

Keyword:
    'end' | 'events' | 'state' | 'actions' | 'commandgroups' | 'group' | 'groupend'
;

Event:
    name=SMID code=ID
;

Command:
    name=SMID code=ID
;

CommandGroupBlock:
    'group' name=SMID
        groupCommands+=[Command|SMID]       
    'groupend'
;

State:
    'state' name=ID
        ('actions' '{' actions+=[CommandGroupBlock|Command] '}')?
        (transitions+=Transition)?
    'end'
;

Transition:
    event=[Event|SMID] '=>' to_state=[State]
;

SMID:
    !Keyword ID
;

Comment:
    /\*(.|\n)*?\*/
;

烤箱模型代码

events
  bakeOn            E_BKON
  cleanOn           E_CLON
  cleanOff          E_CLOF
end

commands
  timerInitial      C_TMIN  
  timerRunning      C_TMRN
  lockDoor          C_D1LK
  unlockDoor        C_D1UL
  magnetronOn       C_MGON
  magnetronOff      C_MGOF
  internalLightOn   C_ILON
  internalLightOff  C_ILOF
  turnTableOn       C_TTON
  turnTableOff      C_TTOF
  cleanerOn         C_CLON    
  cleanerOff        C_CLOF
end

commandgroups
  group resetCommands
    unlockDoor
    timerInitial
    magnetronOff
    internalLightOff
    turnTableOff
    cleanerOff
  groupend
  group commonActiveCommands
    lockDoor  
    internalLightOn
    timerRunning  
  groupend
end

state idle
  actions {resetCommands}
  bakeOn => baking
  cleanOn => cleaning
end

state baking
  actions {commonActiveCommand magnetronOn turnTableOn}
end

state cleaning
  actions {commonActiveCommand cleanerOn turnTableOff}
  cleanOff => idle
end

报错信息

Traceback (most recent call last):
  File "state_machine.py", line 70, in <module>
    model = meta_model.model_from_file(sys.argv[1])
  File "/workspace/dsl-state-machine-oven/demo/lib/python3.8/site-packages/textx/metamodel.py", line 661, in model_from_file
    return self.internal_model_from_file(
  File "/workspace/dsl-state-machine-oven/demo/lib/python3.8/site-packages/textx/metamodel.py", line 709, in internal_model_from_file
    model = self._parser_blueprint.clone().get_model_from_str(
  File "/workspace/dsl-state-machine-oven/demo/lib/python3.8/site-packages/textx/model.py", line 372, in get_model_from_str
    self.parse(model_str, file_name=file_name)
  File "/workspace/dsl-state-machine-oven/demo/lib/python3.8/site-packages/arpeggio/__init__.py", line 1525, in parse
    self.parse_tree = self._parse()
  File "/workspace/dsl-state-machine-oven/demo/lib/python3.8/site-packages/textx/model.py", line 332, in _parse
    raise TextXSyntaxError(message=e.message,
textx.exceptions.TextXSyntaxError: /workspace/dsl-state-machine-oven/oven.sm:39:25: Expected ID => 'etCommands*}   bakeOn'

错误原因与修复方案

错误原因

语法错误的直接原因是命令组名称拼写错误:

  • 定义的命令组名称为commonActiveCommands(末尾带s),但在baking和cleaning状态的actions中误写为commonActiveCommand(缺少末尾s)。
  • 解析器无法找到匹配的命令组或命令,因此抛出语法错误。

修复步骤

将烤箱模型中两处拼写错误的命令组名称修正为正确的commonActiveCommands:

修正后的baking和cleaning状态代码:

state baking
  actions {commonActiveCommands magnetronOn turnTableOn}
end

state cleaning
  actions {commonActiveCommands cleanerOn turnTableOff}
  cleanOff => idle
end

验证说明

当前元模型的定义完全支持在状态actions中同时引用命令组和单个命令,多个元素用空格分隔即可,无需修改元模型代码。修正拼写错误后,模型即可正常解析。


内容的提问来源于stack exchange,提问作者Tech de Enigma

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.21 07:54:22