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

如何解决MaxScript歧义语法解析:fn_call与case_clause匹配冲突

MaxScript解析器歧义问题排查

我正在为MaxScript编写解析器,该脚本包含多处歧义语句。示例语法定义如下:

program: expr* EOF ;

expr: simple_expr | case_expr ;

// fn_call must be first, if not, the program matches a sequence of operands...

simple_expr : fn_call | operand | ...;

// case val of (1:true; 2:false; 3:someVal, default:false)
case_expr :
'('
   case_clause (Nl case_clause)*
')'
;

case_clause: case_clause_def expr ;

// no whitespace nor newlines here
case_clause_def: factor ':';

// calle arg arg param:val param:val....
// grammar reference says 'until an eol or lower precedence token'
// No new lines allowed between the call args or parameters

fn_call
  : caller = (var_name | accessor) (args+=operand | params+=param))+
  | caller = (var_name | accessor) '()'
  ;

param: param_name expr;

// no whitespace nor newlines here
param_name: var_name ':' ;

operand: factor | accessor;

factor
  : var_name
  | number
  | ...
  ;

...

Nl : {this.lineTerminatorAhead()}?
  ;

当前无法正确处理“不允许换行”的要求(怀疑是谓词未在左侧被忽略,或是无法与量词配合工作),导致<fn_call>与<case_clause>之间出现语法歧义。

测试代码

case mode of (
  1: 5
  2: someVal
  default: 10
)

预期解析结果

case expr: case mode of (1:5; 2: someVal; default:10)

实际解析结果

case expr: case mode of (1:5; 2:someVal; default:10); fn_call: someVal default:10

内容的提问来源于stack exchange,提问作者Atelier Bump

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.21 02:34:56