2012-02-25 97 views
1

這已經是在互聯網上某個着名的例子之一,但我似乎無法找到它。xtext:expression/factor/term grammar

我想學習XText,我覺得計算器表達式解析器會是一個好的開始。但我發現了語法錯誤在我的語法:

Expression: 
    Term (('+'|'-') Term)*; 

Term: 
    Factor (('*'|'/') Factor)*; 

Factor: 
    number=Number | variable=ID | ('(' expression=Expression ')'); 

我在表達和期限線得到這個錯誤:

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=RuleRule條目?

回答

2

我下載了與eclipse集成的xtext,它附帶了一個計算器例子,它可以完成所謂的算術運算。從我可以收集的信息中,您需要爲您的令牌分配關聯性。這個語法對我來說運行良好:

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 ')'); 

他們的例子語法用於算法可以被看作here。它包含了比你更多的功能調用,但基本是一樣的。

+0

這非常有幫助!你能否指引我參考一下描述這個'{Plus.left = current}'語法的含義? – 2012-02-25 17:11:51

+0

我真的不知道,我以前從未真正嘗試過xtext。但它在我看來它生成一個名爲'Plus'的接口,它構建了分析樹。 .left將當前令牌與顯然分配的操作的左側部分相關聯:http://www.eclipse.org/Xtext/documentation/2_0_0/020-grammar-language.php#syntax – Dervall 2012-02-25 17:19:56

相關問題