- Antlrworks is not good tool. It does not support semantic predicates.
- The documentation for using Antlr together with StringTemplate is not good enough.
- There are many ways to improve error reporting. A quick fix would be to override emitErrorMessage(String message) in the parser class and simply throw an exception with the provided message:
which you can test with the class:
After running the class above, you will see the following:
bart@hades:~/Programming/ANTLR/Demos/T$ java -cp antlr-3.3.jar org.antlr.Tool T.g
bart@hades:~/Programming/ANTLR/Demos/T$ javac -cp antlr-3.3.jar *.java
bart@hades:~/Programming/ANTLR/Demos/T$ java -cp .:antlr-3.3.jar Main
Parsing : (define x 5)
Parsing : (define x 5))
Parsing : (define x)
exception -> line 1:9 missing INT at ')'
Parsing : (define)
exception -> line 1:7 no viable alternative at input ')'
As you can see, the input (define x 5)) produces no exception! That is because the lexer has no problems with it (they're all valid tokens) and the parser is simply instructed to consume the definition rule:
definition
: '(' 'define' ( '(' variable def_formals ')' body ')'
| variable expression ')'
) ;
which it does. If you wanted an error because of the dangling ')', then you'd have the add the EOF token at the end of the rule:
definition
: '(' 'define' ( '(' variable def_formals ')' body ')'
| variable expression ')'
)
EOF;
definition
: '(' 'define' ( '(' variable def_formals ')' body ')'
| variable expression ')'
) ;
which it does. If you wanted an error because of the dangling ')', then you'd have the add the EOF token at the end of the rule:
definition
: '(' 'define' ( '(' variable def_formals ')' body ')'
| variable expression ')'
)
EOF;
No comments:
Post a Comment