Type aware parser; conditional syntactic predicates [message #1096609] |
Wed, 28 August 2013 16:24 |
Till F. Messages: 58 Registered: August 2012 |
Member |
|
|
Hi,
sorry for the bad title of my question... I coudn't find a better one...
I think my problem is well-known, but I couldn't find out how to handle this situation: I've got two different rules which are equally valid for the parser. Xtext/Antlr cannot handle this situation alone, but I think it should be possible (even though not in an early stage of parsing, since additional information about referenced types is required). Let me give an example:
DSLRoot:
types += (TypeOneClass | TypeTwoClass)*
('select' selections += AnyType ";")*
;
TypeOneClass:
'one' name=ID ';'
;
TypeTwoClass:
'two' name=ID ';'
;
TypeOne:
feature=[TypeOneClass|ID]
;
TypeTwo:
feature=[TypeTwoClass|ID]
;
AnyType returns Target:
TypeOne | TypeTwo
;
The problem is the last rule. The parser cannot differ between "TypeOne" and "TypeTwo" (because in both cases the ambiguous terminal "ID" is expected).
My first idea was to use a syntactic predicate to tell the parser that it should use TypeOne. I hoped that the parser would use rule TypeOne, but would fall-back to TypeTwo if the reference cannot be resolved.
AnyType returns Target:
(=> TypeOne) | TypeTwo
;
Unfortunately this doesn't work; the screenshots show what's happening: scoping works fine (this is a little bit unexpected), but any reference to a TypeTwoClass cannot be resolved (because only TypeOneClasses are accepted - this again is as feared).
How can I solve this issue? I thought that I might have to enable backtracking, but woudn't like this... And I fear that this won't work anyway.
/edit:
Of course, I could add terminals that allow easy distinguishing between the "TypeOne" and "TypeTwo" rules. But this would cause my language to become ugly. Another approach would be that I combine/merge "TypeOne" and "TypeTwo" in my grammer. But this would force me to implement scoping and resolving manually - the major reason why I'm using Xtext.
-
Attachment: scoping.png
(Size: 7.25KB, Downloaded 129 times) -
Attachment: resolving.png
(Size: 11.87KB, Downloaded 119 times)
[Updated on: Wed, 28 August 2013 16:32] Report message to a moderator
|
|
|
|
Re: Type aware parser; conditional syntactic predicates [message #1097068 is a reply to message #1096625] |
Thu, 29 August 2013 07:32 |
Till F. Messages: 58 Registered: August 2012 |
Member |
|
|
Hm... what a pitty. But after thinking about the problem for a while, I have to admit that it wouldn't be so good to solve it on the parser side, anyway.
However, what I would find great was to have the possibility to annotate my grammer in order to guide scoping. I've never seen something like this, so I doubt that it is possible. But are there any plans for the future? It could look something like this:
DSLRoot:
types += (TypeOneClass | TypeTwoClass)*
('select' selections += AnyTypeRef ';')*
;
TypeOneClass:
'one' name=ID ';'
;
TypeTwoClass:
'two' name=ID ';'
;
AnyTypeRef:
feature=[ (=>TypeOneClass | TypeTwoClass) | ID ]
;
The syntax used for feature of the AnyTypeRef rule is supposed to mean "Parser, expect the terminal ID here. Scoping, expect the name of a TypeOneClass here; if you cannt find such instance, expect the name of a TypeTwoClass". Thus, for the parser, the last rule would be equal to this:
I think this wouldn't be too hard to implement, would it?
|
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.03968 seconds