xtext:表达式/因子/术语语法 [英] xtext: expression/factor/term grammar
问题描述
这一定是互联网上某个知名示例之一,但我似乎找不到它.
This has got to be one of those well-known examples that's somewhere on the internet, but I can't seem to find it.
我正在尝试学习XText,我认为计算器表达式解析器将是一个好的开始.但是我的语法出现语法错误:
I'm trying to learn XText and I figured a calculator expression parser would be a good start. But I'm getting syntax errors in my grammar:
Expression:
Term (('+'|'-') Term)*;
Term:
Factor (('*'|'/') Factor)*;
Factor:
number=Number | variable=ID | ('(' expression=Expression ')');
在表达式"和术语"行中出现此错误:
I get this error in the Expression and Term lines:
Multiple markers at this line
- Cannot change type twice within a rule
- An unassigned rule call is not allowed, when the 'current'
was already created.
有什么作用?我怎样才能解决这个问题?以及什么时候在语法中有instanceName=Rule
与Rule
条目?
What gives? How can I fix this? And when do I have instanceName=Rule
vs. Rule
entries in a grammar?
推荐答案
我下载了与eclipse集成的xtext,它带有一个计算器示例,该示例可以完成您希望的算术运算.根据我的收集,您将需要为令牌分配关联性.这种语法对我来说很好:
I downloaded xtext integrated with eclipse and it comes with a calculator example which does approximately what you wish called arithmetics. From what I can gather you will need to assign an associativity to your tokens. This grammar runs fine for me:
Expression:
Term (({Plus.left=current}'+'|{Minus.left=current}'-') right=Term)*;
Term:
Factor (({Multiply.left=current} '*'| {Division.left=current}'/') right=Factor)*;
Factor:
number=NUMBER | variable=ID | ('(' expression=Expression ')');
The example grammar they have for arithmetics can be viewed here. It includes a bit more than your, like function calls, but the basics are the same.
这篇关于xtext:表达式/因子/术语语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!