validation - ANTLR-based rule engine in java -
i writing antlrv4 grammar implement simple rule engine parse fix messages, , specify action taken when rule violated.
this grammar stands:
grammar ruledefinition; ruleset: rule+; rule : 'tag(' int ')' numberop (int | float| string) (action_director action)?; id : [a-za-z]+ ; // match identifiers int : [0-9]+; // match integers float : '0'..'9'+('.'('0'..'9')*)? ; // match float newline :'\r'? '\n' ; // return newlines parser (end-statement signal) ws : [ \t\n\r]+ -> skip ; // toss out whitespace number_op : eq|gr|ge|ls|le|ne; numberop : eq|gr|ge|ls|le|ne; eq : '='; gr: '>'; ge: '>='; ls: '<'; le: '<='; ne: '!='; action_director : '->'; action: 'warn' | 'error'; string : '"' (' '..'~')* '"';
the problem generated code unable correctly parse when rule contains action_director (->), error "mismatched input 'error' expecting action"
parsing successful for:
tag(9)>0
parsing fails for:
tag(9)>0 -> error
any pointers on how correct above highly appreciated.
look @ 3 lines:
warn: 'warn'; error: 'error'; action: warn|error;
these lexer rules (upper case start character). lexer responsible split input tokens of determined type. 'error' can have 1 token type , antlr decides error
(two rules error
, action
match, , error
is first defined).
to resolve transform lexer rules parser rules (lower case start character):
rule : 'tag' '(' int ')' numberop (int | float| string) (action_director action)*; .... action : warn | error; numberop : eq|gr|ge|ls|le|ne; stringop : eq|ne; ...
parser rules compose tokens instead of joining them. means action can warn
or error
.
Comments
Post a Comment