2014-09-10 39 views
0

我似乎遇到了SableCC生成相關詞法分析器,節點和解析通常從語法文件自動生成的東西的問題。我目前沒有實現抽象語法樹。SableCC語法文件的問題

當我嘗試與下面的語法文件運行SableCC,我得到以下錯誤:

[41,33] AFunctionHead.Id的重新定義。我知道這個問題是什麼,但它似乎是生產領域的東西。我可能錯過了什麼?

Package Grammar_Specification; 

Helpers 

    digit = ['0'..'9']; 
    letter = (['a'..'z'] | ['A'..'Z']); 
    underscore = '_'; 
    plus = '+'; 
    minus = '-'; 
    mult = '*'; 
    div = '/'; 
    equals = '='; 
    l_par = '('; 
    r_par = ')'; 
    l_curly = '{'; 
    r_curly = '}'; 
    unicode_input_character = [0..0xffff]; 
    lf = 0x000a; 
    cr = 0x000d; 
    line_terminator = lf | cr | cr lf; 
    input_character = [unicode_input_character - [cr + lf]]; 
    not_star = [input_character - '*'] | line_terminator;    
    not_star_not_slash = [input_character - ['*' + '/']] | line_terminator; 
    multi_line_comment = '/*' not_star+ '*'+ (not_star_not_slash not_star* '*'+)* '/'; 
    line_comment = '//' input_character* line_terminator?; 

Tokens 

    func = 'FUNC'; 
    id = (letter(letter | digit | underscore)* | underscore(letter | digit | underscore)*); 
    float_literal = minus? digit (digit)* ('.' (digit)*)? (('e' | 'E') (plus | minus)? digit (digit)*)?; 
    whitespace = (' ' | '\t' | '\n' | '\r')+; 
    comment = multi_line_comment | line_comment; 

Productions 

    program = function_decl*statement*; 

    function_decl = function_head function_body; 

    function_head = func id l_par id r_par; 

    function_body = l_curly statement* r_curly; 

    statement = id equals expression; 

    expression = expression plus term | 
      expression minus term | 
      term; 

    term = term mult factor | 
     term div factor | 
     factor; 

    factor = l_par expression r_par | 
      identifier l_par expression r_par | 
      float_literal | 
      id; 

回答

2

這在SableCC documentation解釋,又名艾蒂安加格農的碩士論文:

Unlike alternatives, elements have an obvious candidate for name which is the identifier of the element itself. This will work, as long as an element does not appear twice in the same alternative. In that case the current version SableCC requires a name for at least one of the two elements. (For backward compatibility, one occurrence of the repeated element can remain unnamed). SableCC will issue an error if not enough names are provided. Element names are specified by prefixing the element with an identifier between square brackets followed by a colon.

換句話說,你不能在生產中使用id兩次function_head沒有給他們的至少一個一個名字(不管你是否生成一個AST)。

嘗試這樣:

function_head = func id l_par [parameter]:id r_par; 
+0

非常感謝:)我認爲我設法理清的問題:d – Curiosity 2014-09-10 19:06:01