1

In my grammar I have Variables and Objects (Objects are just a name (ID)). Now I want that a variable can be a reference to another declared variable or an object.
So I want to parse the following input:

var = 3;
var2 = var;
objVar = MyObject;

I've implemented like that:

Declaration:
    name = ID "=" Content ";"
;
    Content:
        INT
        | reference = [Declaration]
        | object = ID

But the parser can't differentiate between a simple ID and a reference to a declaration (because name is also an ID).
Is there a way to solve my problem?
I tried syntactic predicates but then I can as well delete either the reference or the object from my rule because the parser only see an ID and not a reference. In ANTLR I would use semantic predicates but as far as I know they don't exist in Xtext.
Is there a way to let the parser recognize if the ID is a reference or not and then let it either choose the ID to match the reference or the object?

Greeting Krzmbrzl

Raven
  • 2,951
  • 2
  • 26
  • 42

1 Answers1

0

This is not possible with Xtext. You have to add at least one more token before or after the reference attribute xor the object attribute. I would suggest s.th. like new:

Content:
    INT
    | reference=[Declaration]
    | 'new' object=ID
;

Now, the parser can decide wheter an ID creates a new object or is the cross reference to an other declaration.

Joko
  • 600
  • 3
  • 15
  • That are really bad news becaus I am implementing this eclipse-plugin for an existing language therefore I can't simply add a token... – Raven Jun 01 '15 at 17:50