[英]How to write a rule to define a method in ANTLR
I am creating a DSL with ANTLR and I want to define the following syntax我正在用 ANTLR 创建一个 DSL,我想定义以下语法
// study without parameters
study()
// study with a single parameter
study(x = 1)
// study with several parameters
study(x = 1, x = 2)
here my grammer ,it allows the following input : study(x=1x=2)
这里是我的语法,它允许以下输入: study(x=1x=2)
study: 'study' '(' ( assign* | ( assign (',' assign)*) ) ')' NEWLINE;
assign: ID '=' (INT | DATA );
INT : [0-9]+ ;
DATA : '"' ID '"' | '"' INT '"';
ID : [a-zA-Z]+ ;
Your grammar allows study(x=1x=2)
because assign*
matches x=1x=2
.您的语法允许study(x=1x=2)
因为assign*
匹配x=1x=2
。 If you don't want to allow input like that, you should remove the assign*
alternative.如果您不想允许这样的输入,您应该删除assign*
替代项。 To allow empty parameter lists, you can just make everything between the parentheses optional:要允许空参数列表,您可以将括号之间的所有内容设为可选:
study: 'study' '(' (assign (',' assign)*)? ')' NEWLINE;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.