我正在尝试定义一个简单的函数式语言语法,我几乎完成了我的定义,但我无法克服以下歧义。
[14:43:53] warning(200): mygrammar.g:14:11: Decision can match input such as "ATOM" using multiple alternatives: 1, 2 As a result, alternative(s) 2 were disabled for that input
[14:43:53] warning(200): mygrammar.g:14:11: Decision can match input such as "ID" using multiple alternatives: 1, 2 As a result, alternative(s) 2 were disabled for that input
以下是我认为的相关规则,ATOM 和 ID 的绘图几乎相同:
program : (statement'.')* ;
statement : assignment
| expression;
assignment : func '->' statement ((','statement)=> ',' statement)*
| ID '->' expression
| ATOM '->' ( string | number );
func : (ID '(' args ')')=> ID '(' args ')';
term : func
| '(' expression ')'
| number
| string
| ID
| ATOM ;
ATOM : ('A'..'Z'|'_')+;
ID : ('a'..'z'|'_')('a'..'z'|'A'..'Z'|'0'..'9'|'_')*;
这是来自 ANTLRWorks 的语法树
id 替代品 http://www.vertigrated.com/images/id_alternatives.png
这是我试图支持解析的粗略稻草人。
hypotenuse(a,b) ->
sqr(x) -> x * x,
sqr(sqr(a) + sqr(b)).
print(hypotnenuse(2,3)).
所以我需要能够支持statements
嵌套functions
我的赋值运算符在哪里->
,这是一种单一的赋值语言
.
我的语句结束标记在哪里
这甚至可以用 ANTLR3 解析吗?