JavaCC expects nothing and found nothing -
i'm trying bottom of parser problem, begin, i'd know why happens:
encountered "" @ line 1, column 1. expecting 1 of:
here subset of grammar causes problem:
options { static = false; debug_parser = true; debug_lookahead = true; lookahead = 64; } parser_begin(myparser) package myparser; public class myparser { } parser_end(myparser) /* skip whitespace */ skip : { " " | "\t" | "\n" | "\r" } /* other symbols */ token : { < comma: "," > | < semicolon: ";" > | < left_paren: "(" > | < right_paren: ")" > | < left_bracket: "[" > | < right_bracket: "]" > | < left_brace: "{" > | < right_brace: "}" > } /* type specifiers */ token : { < type_specifier: "void" | "int" | "float" > } /* identifiers */ token [ignore_case] : { <identifier: ["_","a"-"z"] (["_","a"-"z","0"-"9"])* > } simplenode start() : {} { /* begin here */ declaration() { return jjtthis; } } void declaration() : {} { functionprototype() <semicolon> | initdeclaratorlist() <semicolon> } void functionprototype() : {} { functiondeclarator() <right_paren> } void functiondeclarator() : {} { functionheaderwithparameters() | functionheader() } void functionheaderwithparameters() : {} { /* recursive version: functionheader() parameterdeclaration() functionheaderwithparameters() <comma> parameterdeclaration() */ functionheader() parameterdeclaration() [functionheaderwithparametersprime()] } void functionheaderwithparametersprime() : {} { <comma> parameterdeclaration() [functionheaderwithparametersprime()] } void functionheader() : {} { fullyspecifiedtype() <identifier> <left_paren> } void fullyspecifiedtype() : {} { typespecifier() } void typespecifier() : {} { typespecifiernonarray() [arrayspecifier()] } void arrayspecifier() : {} { /* recursive version: <left_bracket> <right_bracket> | <left_bracket> constantexpression() <right_bracket> | arrayspecifier() <left_bracket> <right_bracket> | arrayspecifier() <left_bracket> constantexpression() <right_bracket> */ <left_bracket> <right_bracket> [arrayspecifierprime()] } void arrayspecifierprime() : {} { <left_bracket> <right_bracket> [arrayspecifierprime()] } void typespecifiernonarray() : {} { <type_specifier> | typename() } void typename() : {} { /* user defined type e.g struct or typedef */ <identifier> } void parameterdeclaration() : {} { parameterdeclarator() | parametertypespecifier() } void parametertypespecifier() : {} { typespecifier() } void parameterdeclarator() : {} { typespecifier() <identifier> | typespecifier() <identifier> arrayspecifier() } void initdeclaratorlist() : {} { /* recursive version: singledeclaration() | initdeclaratorlist() <comma> <identifier> | initdeclaratorlist() <comma> <identifier> arrayspecifier() | initdeclaratorlist() <comma> <identifier> arrayspecifier() <equal> initializer() | initdeclaratorlist() <comma> <identifier> <equal> initializer() */ singledeclaration() [initdeclaratorlistprime()] } void initdeclaratorlistprime() : {} { <comma> <identifier> [initdeclaratorlistprime()] | <comma> <identifier> arrayspecifier() [initdeclaratorlistprime()] } void singledeclaration() : {} { fullyspecifiedtype() | fullyspecifiedtype() <identifier> | fullyspecifiedtype() <identifier> arrayspecifier() }
function declarations working fine
void main(int a, int b);
but declaration statements, go in function body in full grammar, not working
float myvar;
the debug output parser , lookahead broken example follows
call: start call: declaration call: functionprototype(looking ahead...) call: functiondeclarator(looking ahead...) call: functionheaderwithparameters(looking ahead...) call: functionheader(looking ahead...) call: fullyspecifiedtype(looking ahead...) call: typespecifier(looking ahead...) call: typespecifiernonarray(looking ahead...) visited token: <<type_specifier>: "float" @ line 1 column 1>; expected token: <<type_specifier>> return: typespecifiernonarray(lookahead succeeded) call: arrayspecifier(looking ahead...) visited token: <<identifier>: "myvar" @ line 1 column 7>; expected token: <"["> return: arrayspecifier(lookahead failed) return: typespecifier(lookahead succeeded) return: fullyspecifiedtype(lookahead succeeded) visited token: <<identifier>: "myvar" @ line 1 column 7>; expected token: <<identifier>> visited token: <";" @ line 1 column 12>; expected token: <"("> return: functionheader(lookahead failed) return: functionheaderwithparameters(lookahead failed) call: functionheader(looking ahead...) call: fullyspecifiedtype(looking ahead...) call: typespecifier(looking ahead...) call: typespecifiernonarray(looking ahead...) visited token: <<type_specifier>: "float" @ line 1 column 1>; expected token: <<type_specifier>> return: typespecifiernonarray(lookahead succeeded) call: arrayspecifier(looking ahead...) visited token: <<identifier>: "myvar" @ line 1 column 7>; expected token: <"["> return: arrayspecifier(lookahead failed) return: typespecifier(lookahead succeeded) return: fullyspecifiedtype(lookahead succeeded) visited token: <<identifier>: "myvar" @ line 1 column 7>; expected token: <<identifier>> visited token: <";" @ line 1 column 12>; expected token: <"("> return: functionheader(lookahead failed) return: functiondeclarator(lookahead failed) return: functionprototype(lookahead failed) call: initdeclaratorlist(looking ahead...) call: singledeclaration(looking ahead...) call: fullyspecifiedtype(looking ahead...) call: typespecifier(looking ahead...) call: typespecifiernonarray(looking ahead...) visited token: <<type_specifier>: "float" @ line 1 column 1>; expected token: <<type_specifier>> return: typespecifiernonarray(lookahead succeeded) call: arrayspecifier(looking ahead...) visited token: <<identifier>: "myvar" @ line 1 column 7>; expected token: <"["> return: arrayspecifier(lookahead failed) return: typespecifier(lookahead succeeded) return: fullyspecifiedtype(lookahead succeeded) return: singledeclaration(lookahead succeeded) call: initdeclaratorlistprime(looking ahead...) visited token: <<identifier>: "myvar" @ line 1 column 7>; expected token: <","> visited token: <<identifier>: "myvar" @ line 1 column 7>; expected token: <","> return: initdeclaratorlistprime(lookahead failed) return: initdeclaratorlist(lookahead succeeded) visited token: <<identifier>: "myvar" @ line 1 column 7>; expected token: <";"> return: declaration return: start encountered "" @ line 1, column 1. expecting 1 of:
the problem seems in singledeclaration
production, expects
fullyspecifiedtype() | fullyspecifiedtype() <identifier>
where fullyspecifiedtype
either type constant (void, int, float) or identifier (to facilitate user-defined types).
if swap these lines around reads
fullyspecifiedtype() <identifier> | fullyspecifiedtype()
then functions intended. why order make difference?
first set looahead option 1, javacc warn lookahead conflicts.
your grammar has lookahead conflicts. javacc processor should report these warnings. should heed these warnings.
javacc, default, uses next token of input make choices. see documentation , faq details. take
void singledeclaration() : {} { fullyspecifiedtype() | fullyspecifiedtype() <identifier> | fullyspecifiedtype() <identifier> arrayspecifier() }
as example. there three-way choice made. choice can't made on basis of kind of next token, since token start first choice start second or third.
there 2 ways fix problem. best, usually, rewrite grammar.
void singledeclaration() : {} { fullyspecifiedtype() [ <identifier> [ arrayspecifier() ] ] }
the second use lookahead specifications.
void singledeclaration() : {} { lookahead(fullyspecifiedtype() <identifier> "[") fullyspecifiedtype() <identifier> arrayspecifier() | lookahead(fullyspecifiedtype() <identifier>) fullyspecifiedtype() <identifier> | fullyspecifiedtype() }
one more thing, start production should this:
simplenode start() : {} { /* begin here */ declaration() <eof> { return jjtthis; } }
Comments
Post a Comment