I'm working on a parser and require custom errors to be thrown for every keyword. My code is the following.
SKIP: { " " | "\t" | "\n" | "\r" }
TOKEN: { "DEF" | "MAIN" | <NAME: (["A"-"Z"])+> | <PARAM: (["a"-"z"])+> | <NUM: (["0"-"9"])+> }
void Start(): {} {(Def() Func())+ <EOF>}
void Def(): {} {"DEF" | { throw new ParseException("expected keyword DEF"); }}
void Func(): {} {"MAIN" | Name() Param() | { throw new ParseException("Expected MAIN or NAME PARAM"); }}
void Name(): {} {<NAME> | { throw new ParseException("invalid function name"); }}
void Param(): {} { <PARAM> | { throw new ParseException("invalid PARAM"); }}
The Start()
function is giving me an error and tells me that Expansion within "(...)+" can be matched by empty string error
. I think the problem is in the Name() Param()
part of Func()
but I do not know how to change this while still throwing custom error messages. Can anyone provide some pointers?
While I agree with the comment from user207421, you could maybe do the following
void oneOrMoreThings() : {} {
(Thing() | (throw new ParseException( ... ) ; }
( Thing() )*
}
将 DEF 设为可选,然后检查它是否已找到,如果没有则引发异常。
Start(): {Token tk=null;} {tk="DEF"? {if (tk==null) throw ...} "MAIN" etc
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.