[antlr-interest] assign a value to a token

Marinos Georgiades marinos at studyhood.com
Wed Jul 18 06:08:46 PDT 2012


I would like to assign the value of a token to a variable, so as I will be able to use that variable through my grammar (I am sorry if I don't use the right terminology).

 For example, I have the following rules (I am quoting part of them for simplification):

useCaseSpecification
:'UseCase:' ucName '='
description
statePre
.......
;

ucName
:caren io
;

caren
: 'create' | 'alter' | 'read'
;

io
:ioName ioType
;

ioName
: ARTICLE NOUN
;

description
:'Description' ':' ucName ......
;

statePre
:io 'is 'at' state 'state' '.' 'Triggered' 'by' 'UseCase' ':' ucName
;

A test syntax can go like this:

UseCase: create Prescription = 
Description: create Prescription
Examination is at ....... Triggered by UseCase: Create Examination

In this example, ucName is assigned the values 'create Prescription' for line 1 and 2, and the value 'Create Examination' for line 3. However, someone can write something else, e.g. 'read Book' for line 2. And I don't want it to be like this. I want the ucName of the rule 'description' to be the same with the ucName of the rule useCaseSpecificaton. And I want the ucName of the rule statePre to be different from the others.
From what I read , there must be a way with the use of Java code, but I haven't managed to make it work. I would really appreciate if someone could help me.

Thank you,

Marinos


More information about the antlr-interest mailing list