java - Problems With SableCC Grammar File -


i seem having issues sablecc generating relevant lexer, node , parse stuff automatically generates grammar file. i'm not implementing abstract syntax tree @ moment.

when try run sablecc grammar file below, following error:

[41,33] redefinition of afunctionhead.id. have know idea problem is, seems in productions area. perhaps missing something?

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; 

this explained in sablecc documentation, aka Étienne gagnon's master's thesis:

unlike alternatives, elements have obvious candidate name identifier of element itself. work, long element not appear twice in same alternative. in case current version sablecc requires name @ least 1 of 2 elements. (for backward compatibility, 1 occurrence of repeated element can remain unnamed). sablecc issue error if not enough names provided. element names specified prefixing element identifier between square brackets followed colon.

in other words, can't use id twice in production function_head without giving @ least 1 of them name (whether or not you're generating ast).

try this:

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

Comments

Popular posts from this blog

javascript - how to protect a flash video from refresh? -

android - Associate same looper with different threads -

visual studio 2010 - Connect to informix database windows form application -