Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » language with unstructured macros
language with unstructured macros [message #805251] Thu, 23 February 2012 09:49 Go to next message
Vlad Dumitrescu is currently offline Vlad Dumitrescu
Messages: 318
Registered: July 2009
Location: Gothenburg
Senior Member
Hi,

My problem is that the language has C-like macros. The values for the macros need not be well-formed expressions and in the worst case the resulting code that we have to parse loses all structure. Is there some trick to handle this kind of situation? I mean, maybe not the worst case, but as much as possible.

best regards,
Vlad
Re: language with unstructured macros [message #805276 is a reply to message #805251] Thu, 23 February 2012 10:20 Go to previous messageGo to next message
Henrik Lindberg is currently offline Henrik Lindberg
Messages: 2498
Registered: July 2009
Senior Member
On 2012-23-02 15:49, Vlad Dumitrescu wrote:
> Hi,
>
> My problem is that the language has C-like macros. The values for the
> macros need not be well-formed expressions and in the worst case the
> resulting code that we have to parse loses all structure. Is there some
> trick to handle this kind of situation? I mean, maybe not the worst
> case, but as much as possible.
>

In this case I think you should follow the advice of Iron Maiden - "Run
to the hills - run for your lives"...

Seriously - correctly dealing with macros as a MAJOR pita.

You need to become an expert in lexing.

Regards
- henrik
Re: language with unstructured macros [message #805532 is a reply to message #805276] Thu, 23 February 2012 16:46 Go to previous messageGo to next message
Vlad Dumitrescu is currently offline Vlad Dumitrescu
Messages: 318
Registered: July 2009
Location: Gothenburg
Senior Member
Thanks Henrik,

Quote:
Seriously - correctly dealing with macros as a MAJOR pita.


I knew this was the case, but hoped somewhat irrationally that there was some magic that would ease the pain.

I'm glad that the generated parser has good error recovery and the disturbance does not spread through the whole file. If I could ignore the related errors in Eclipse, it could be good enough.

regards,
Vlad
Re: language with unstructured macros [message #806410 is a reply to message #805532] Fri, 24 February 2012 18:43 Go to previous messageGo to next message
Henrik Lindberg is currently offline Henrik Lindberg
Messages: 2498
Registered: July 2009
Senior Member
On 2012-23-02 22:46, Vlad Dumitrescu wrote:
> Thanks Henrik,
>
> Quote:
>> Seriously - correctly dealing with macros as a MAJOR pita.
>
>
> I knew this was the case, but hoped somewhat irrationally that there was
> some magic that would ease the pain.
>
Nope, it is really, really awful and difficult to get right, and you
have to override things here, there and everywhere.

> I'm glad that the generated parser has good error recovery and the
> disturbance does not spread through the whole file. If I could ignore
> the related errors in Eclipse, it could be good enough.
>

Nope, no, sorry - if not very hard, it is at least a LOT of work.
I would forget about trying to solve this at the grammar level, it is a
lexing and textual read/write and presentation matter - basically the
lexer needs to evaluate the macros to turn the macros into valid
expressions in the language as this is what the parser needs. At the
same time you need to present things in the editor where the macros are
not expanded.

--- in file "x.header"
#define x = public class

--- in file mystuff
#include x.header
x foo {}
class bar extends foo {}

There is no support for this in Xtext at all. If you really need this, I
suggest talking to the guys at Itemis and pay for help.

Regards
- henrik
Re: language with unstructured macros [message #806961 is a reply to message #806410] Sat, 25 February 2012 14:48 Go to previous message
Vlad Dumitrescu is currently offline Vlad Dumitrescu
Messages: 318
Registered: July 2009
Location: Gothenburg
Senior Member
Hi!

Quote:
> I'm glad that the generated parser has good error recovery and the
> disturbance does not spread through the whole file. If I could ignore
> the related errors in Eclipse, it could be good enough.
>

Nope, no, sorry - if not very hard, it is at least a LOT of work.
I would forget about trying to solve this at the grammar level, it is a
lexing and textual read/write and presentation matter - basically the
lexer needs to evaluate the macros to turn the macros into valid
expressions in the language as this is what the parser needs. At the
same time you need to present things in the editor where the macros are
not expanded.


Fortunately, real-world uses of macros are not very wild and crazy, so there are only a few cases that can happen - mostly that any expression or sequence of expressions can be represented by a macro instance. Unfortunately, this is still enough to make the grammar ambiguous.

I tried to let only the basic structural features of the language remain in place and let the rest be possible to be anything, but it doesn't look too bright. It might work with a backtracking parser, though.

Thanks a gain for the support!
regards,
Vlad

Previous Topic:Terminals vs Datatypes vs Rules
Next Topic:Parsing block expressions vs. scope
Goto Forum:
  


Current Time: Tue Jul 22 05:23:13 EDT 2014

Powered by FUDForum. Page generated in 0.02041 seconds