[antlr-interest] Exception handling doubts for C language.
Meena Vinod
spprtmail09 at yahoo.com
Tue Feb 10 00:26:46 PST 2009
Hello,
I need help on the following grammar file:
---------------------------
grammar Test;
options
{
output = AST;
language = C;
ASTLabelType = pANTLR3_BASE_TREE;
}
tokens
{
T_COMMAND;
T_ACTION;
T_OPT;
}
validate_cmd: command EOF -> ^(T_COMMAND command);
command: action1 | action2;
action1: action1_name (action1_opt)* ;
action2: action2_name (action2_opt)* ;
action1_name: ACTION1 -> ^(T_ACTION ACTION1) ;
action2_name: ACTION2 -> ^(T_ACTION ACTION2) ;
action1_opt: tsep action1_tree -> ^(T_OPT action1_tree);
action1_tree: (common_options | wait_option)+;
action2_opt: tsep action2_tree -> ^(T_OPT action2_tree);
action2_tree: (common_options)+;
common_options: version_option | help_option ;
wait_option: WAIT tsep Digit+;
version_option: VERSION_OPTION;
help_option: HELP_OPTION tsep Literal+;
ACTION1 : 'copy';
ACTION2 : 'cut';
VERSION_OPTION: '-v'('ersion')?;
HELP_OPTION: '-h'('elp')?;
WAIT : '-w'('ait')?;
SEMI: ';' ;
fragment DIGIT : ('0'..'9');
fragment ALPHA : ('a'..'z' | 'A'..'Z')*;
Literal : DIGIT | ALPHA;
Digit : DIGIT+;
tsep : WSP;
WSP : ('\u0009' | ' ')+;
-------------------------------------------------------------------------------
When I enter the command "copy -w aa", where the -w is expecting a digit, I get an early exit exception.
My doubts include the following:
1. How do I handle exceptions when my language option is "C"?
2. More specifically, can I have the "wait_option" rule handle the exception when a digit is not supplied
equivalent of
if wait_option: WAIT tsep Digit+, then continue
else wait_option: WAIT tsep <not a digit>, then flag error!
I dont want to do soemthing like
wait_option: WAIT tsep Digit+
{ //Success
}
|
WAIT tsep NonDigit+
{ //Failure
}
;
with the fear that my source code might get bigger.
3. Can I have a common error block, that is whenever any parser has a problem, it executes a common function?
Thanks a ton,
Swift
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20090210/4c160af7/attachment.html
More information about the antlr-interest
mailing list