|
View:
New views
1 Messages
—
Rating Filter:
Alert me
|
|
|
Problem: semantic lookahead calling boolean functionsProblem: semantic lookahead calling boolean functions
At certain points in my JavaCC grammar (Kleene.jjt), I have to do fancy semantic lookahead at a choice point in a production called expression(). I'm trying to do the semantic lookahead by calling boolean functions, as shown below. mustBeRegexp() looks ahead for token types that signal that the parser should be committed to parsing a regexp() (a regular expression). mustBeNumexp() similarly looks ahead for token types that signal that the parser should be committed to parsing a numexp() (an arithmetic expression). // one boolean function, used for semantic lookahead below JAVACODE boolean mustBeRegexp() { int i ; // skip over any LPAREN tokens (which can appear as groupers only // in regexp() and numexp()) for (i = 1; getToken(i).kind == LPAREN; i++) {} Token t = getToken(i) ; if ( t.kind == CHAR || t.kind == ANY || t.kind == ESCAPE_CHAR || t.kind == ESCAPE_HEX_PYTHON || t.kind == ESCAPE_HEX_PERL || t.kind == NET_FUNC_ID_BEFORE_ARGLIST // tokenizer found $&foo and '(' || t.kind == NET_ID // $foo || t.kind == LEFT_DOUBLE_QUOTE // start of double-quoted string || t.kind == LSQUARE // start of [abc] || t.kind == LSQUARE_CIRCUMFLEX // start of [^abc] || t.kind == MULTICHAR_SYMBOL // '[Noun]' || t.kind == LONGEST_DIRECTIVE // longest{ } -> left _ right || t.kind == SHORTEST_DIRECTIVE // shortest{ } -> left _ right || t.kind == LANGLE // start of a weight <0.3> || t.kind == TILDE_OP // regular negation/complement || t.kind == TILDE_OP_ASCII || t.kind == EPSILON || t.kind == EPSILON_ASCII ) { return true ; } else { return false ; } } // a second boolean function, used for semantic lookahead below JAVACODE boolean mustBeNumexp() { int i ; // skip over any LPAREN tokens for (i = 1; getToken(i).kind == LPAREN; i++) {} Token t = getToken(i) ; if ( t.kind == NUM_FUNC_ID_BEFORE_ARGLIST || t.kind == NUM_ID || t.kind == HEX_INT_LITERAL || t.kind == DEC_FLOAT_LITERAL || t.kind == DEC_INT_LITERAL || t.kind == EXCLAMATION_MARK_OP || t.kind == PLUS_SIGN_OP || t.kind == MINUS_SIGN_OP ) { return true ; } else { return false ; } } And here's the production with the choice point. The two options with semantic lookahead, calling the boolean functions above, are at the top. void expression() : {} { // semantic lookahead calls boolean function LOOKAHEAD( { mustBeRegexp() } ) regexp() // semantic lookahead calls boolean function | LOOKAHEAD( { mustBeNumexp() } ) numexp() | LOOKAHEAD( { getToken(1).kind == NET_FUNC_ID_NO_ARGLIST } ) net_func_exp() | LOOKAHEAD( { getToken(1).kind == NET_FUNC_FUNC_ID_BEFORE_ARGLIST || getToken(1).kind == NET_FUNC_LAMBDA } ) // then must be at least a net_func_func_call or a net_func_lambda_exp, // both of which are types of net_func_exp net_func_exp() ( LOOKAHEAD( { getToken(1).kind == LPAREN } ) // then there's an arg_list, and so must be // a net_func_call, a kind of regexp ( arg_list() #net_func_call(2) ) #regexp(1) )? | LOOKAHEAD( { getToken(1).kind == NET_FUNC_FUNC_ID_NO_ARGLIST } ) net_func_func_exp() ...... // etc } // END OF CODE SAMPLE When I try to compile this, I get the following error message when javac tries to compile the generated Kleene.java file. Kleene.java:13263: unreported exception ParseException; must be caught or declared to be thrown jj_semLA = mustBeRegexp(); Kleene.java:13268: unreported exception ParseException; must be caught or declared to be thrown jj_semLA = mustBeNumexp(); // END OF ERROR MESSAGE If I comment out the lines LOOKAHEAD( { mustBeRegexp() }) and LOOKAHEAD( { mustBeNumexp() }) then the error messages go away. I've tried declaring that mustBeRegexp() and mustBeNumexp() "throw ParseException", and I've tried putting try{} catch{} blocks in the expression() production. But I keep getting these error messages. Can anyone help me see where I'm going wrong? Thanks, Ken ****************************** Kenneth R. Beesley, D.Phil. P.O. Box 540475 North Salt Lake, UT 84054 USA --------------------------------------------------------------------- To unsubscribe, e-mail: users-unsubscribe@... For additional commands, e-mail: users-help@... |
| Free embeddable forum powered by Nabble | Forum Help |