0

I want to create a goto expression as follows

//label
 <bb 2> :

//goto
goto <bb 2>;

The following grammar works fine for a simple ID. I have no idea how to reference the <ID INT> in the goto expression.

Goto returns Goto:
    {Goto}
    'goto' goto+=[Label]  ';'
;

LabelDef returns LabelDef:
    {LabelDef}
    label+= Label ':'
    ;

Label returns Label:
    {Label}
    name= ID
    ;

Do have any idea how to that?

2 Answers2

1

the feature you are looking for is a DataType rule

Goto returns Goto:
    {Goto}
    'goto' goto+=[Label|IDandINT]  ';'
;

LabelDef returns LabelDef:
    {LabelDef}
    label+= Label ':'
    ;

Label returns Label:
    {Label}
    name= IDandINT
    ;
IDandINT: ID INT;

you may also introduce / customize DefaultTerminalConverters/IValueConverter for the datatype rule to normalize whitespace

Christian Dietrich
  • 11,778
  • 4
  • 24
  • 32
  • Thank you for your answer, when generating I wasn't having the result I was looking for because of xtext. I discovered that when I created a new project. The following sub-grammar works fine. `Goto returns Goto: {Goto} 'goto' goto = [Label|QualifiedName] ';' '[INV]' ; LabelDef returns LabelDef: {LabelDef} name = Label ; Label returns Label: {Label} name = QualifiedName ':' ; QualifiedName: '<' ID INT '>' ; ` – Soulimane Kamni Dec 08 '21 at 14:32
0

I think you want a terminal that is essentially "ID INT" and then use it to crossreference your Label. I think this is going to be a lot of work just to be able to allow "spaces" in your labels. Why not simply rely on terminal "ID" and users may name them "bb2" if they wish?

user1292456
  • 778
  • 4
  • 12