簡體   English   中英

Antlr非LL(*)決策

[英]Antlr non-LL(*) decision

我在為編程語言創建部分ANTLR語法時遇到了一些麻煩。

type聲明的第二部分發生時,我得到了錯誤:

public type
    :   ID ('.' ID)* ('?')? -> ^(R__Type ID ID* ('?')?)
    |   '(' type (',' type)*  ')' ('?')? -> ^(R__Type type* ('?')?)
    ;

我正在嘗試匹配:

  • System.String這樣的行(可以正常工作)
  • 一個元組,例如(System.String, System.Int32)

該錯誤在樹的上方稍稍出現,並指出:

[fatal] rule statement has non-LL(*) decision due to recursive rule invocations reachable from alts 1,2. Resolve by left-factoring or using syntactic predicates or using backtrack=true option.

我究竟做錯了什么?

是的,通過編輯處理變量聲明的規則,我設法在樹的前面修復了這個問題:

'my' ID (':' type '=' constant | ':' type | '=' constant) -> ^(R__VarDecl ID type? constant?)

這樣它就可以像:

'my' ID
(
   ':' type ('=' constant)?
|   '=' constant
) -> ^(R__VarDecl ID type? constant?)

我從這里的語法謂詞示例中得到了這個主意:

https://wincent.com/wiki/ANTLR_predicates

幸運的是,我最終不需要謂詞!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM