2

I use ANTLR in the Eclipse environment. I want to pass an attribute (n.text) to another rule (description) and also use a semantic predicate in the latter rule to validate the input in relation to n.text. Here is my code:

useCaseSpecification 
    :   n=useCase '='
        description[$n.text]
    ;

useCase
    : ucID=('UC' INTEGER)? ucName
    ;

ucName
    :   caren io
    ;

caren
    :   'create' | 'creates' | alter | read | 'erase' | 'erases' | notify
    ;

/* ..more code */

description[String str]
    :   'Description' ':' primaryActor (useCase {str==$useCase.text}?) /* more grammar */
    ;

I tried many alternatives for the semantic predicate expression, such as {str.equals($useCase.text)}, but nothing. It seems that the parser does not make the validation.

When I run the interpreter with an example, it allows every input of useCase type. For example, if the input is:

create a Prescription = 
Description: a doctor create a Prescription /* ... */

that should be correct.

If the input, is:

create a Prescription = 
Description: a doctor create a Rrrrescription /* ... */

that should be wrong.

Bart Kiers
  • 166,582
  • 36
  • 299
  • 288
Marinos
  • 21
  • 2

2 Answers2

2

Do not rely on the interpreter (either ANTLRWorks' interpreter, or the one of Eclipse's plugin). The interpreter does not take any predicates into account.

Also, not sure what target you're using, but realize that {str==$useCase.text}? is wrong in case you're using Java (== compares the identity of objects, use equals(...) instead).

For example, parsing two the same letters can be done as follows:

grammar T;

parse
 : Letter same[$Letter.text] EOF
 ;

same [String s]
 : Letter {$Letter.text.equals(s)}?
 ;

Letter : 'a'..'z' | 'A'..'Z';

Parsing "AB" will result in an exception:

enter image description here

while parsing "AA" does not:

enter image description here

The trees above are generated using ANTLRWorks' debugger, which works like a charm. Note that the debugger ignores the language=XYZ option: it will debug your grammar with Java as the target language. So any embedded code other that Java will cause problems!

Be aware that cramming too much of these semantic checks in your grammar will result in a hard to maintain jumble of mixed grammar rules and code. These kind of checks are usually performed after the parser created a (abstract) parse tree: that means liberally matching useCases and then validating them when iterating over the tree created by the parser.

Bart Kiers
  • 166,582
  • 36
  • 299
  • 288
  • Thank you, Bart. Maybe something is wrong with how I configured ANTLR in Eclipse, or something else with the javacc? I used a new project with your example, and I get the same result like what you showed with your first graph (just in the case where parsing "AA", it returns 'A' instead of 'B', but next to 'A' still is 'FailedPredicateException'). What am I doing wrong please? Thank you – Marinos Jul 23 '12 at 10:18
  • I have also tried your example with ANTLRWorks, but I get the same result. – Marinos Jul 23 '12 at 10:27
  • @Marinos, then you're also trying with ANTLRWorks' interpreter: which you shouldn't (check my answer). No, I can't help you with Eclipse: I don't use it (in combination with ANTLR). – Bart Kiers Jul 23 '12 at 10:29
  • so how can I test the grammar please and get the results like in your graphs? – Marinos Jul 23 '12 at 10:31
  • OK, that's clear with the debugger, but I am new to it, and sorry I have to make more questions, such as 'how should I use the debugger?" Can I just put the whole test in the debugger? For example, these are the first two sentences of the test: /*sentence1*/ create a Prescription = /*sentence2*/ Description: a doctor create a Prescription with the help of a patient. A pharmacist is an intended recipient of a Prescription. – Marinos Jul 23 '12 at 10:38
  • it worked, thanks again, Bart! I will post a few more questions later, and I hope you will be available...although I don't know the time zone of your location :) – Marinos Jul 23 '12 at 11:40
  • @Marinos, I'll be away for the rest of the day: I'll probably have a look tomorrow. – Bart Kiers Jul 23 '12 at 11:49
  • @Marinos, Take a look [here](http://stackoverflow.com/questions/8343488/antlr-ide-in-eclipse-doesnt-work/10887515#10887515) for configuring the ANTLR IDE in Eclipse. Then look [here](http://stackoverflow.com/questions/10838068/testing-antlr-grammar/10886842#10886842) for using ANTLR in Eclipse. – cb4 Jul 24 '12 at 03:16
  • Well, can't seem to edit my comment. Just wanted to add that if you create a new question for "so how can I test the grammar please and get the results like in your graphs?" I can help you out with that. – cb4 Jul 24 '12 at 04:08
  • _"The interpreter does not take any predicates into account."_, did you mean that nor the lexer or parser will evaluate the given predicates? I'm just interesting on ANTLR. So, am I right? –  Oct 08 '17 at 20:34
  • 1
    @HydroSan I was talking about the interpreter in ANTLRWorks (a feature in the old ANTLR IDE) – Bart Kiers Oct 08 '17 at 20:48
0

Since you are using Eclipse, I probably should have posted this as an answer instead of a comment. Look at How To Configure ANTLR IDE and then Debug/Test with ANTLR IDE. Appreciate the upvote if you already found these helpful.

Community
  • 1
  • 1
cb4
  • 6,689
  • 7
  • 45
  • 57