Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Help with grammar(How best to write this grammar)
Help with grammar [message #1742824] Wed, 07 September 2016 22:42 Go to next message
Waqas Ilyas is currently offline Waqas IlyasFriend
Messages: 80
Registered: July 2009
Member
Problem #1

Lets say I have three rules a, b and c. And I want to allow any sequence of these rules, except that b and c should appear only once. How best to write such a grammar?

Here is what I came up with:
root:
  a+=a*
  b=b
  a+=a*
  c=c
  a+=a*
;
a: 'a';
b: 'b';
c: 'c';


Is there a better way to write root grammar? b and c still have to be in a strict order, which is not ideal.

Problem #2
Have a look at this grammar:
root:
    any+=any*
    x=x
    any+=any*
;

any:
    name=ID
    '{'
        any+=any*
    '}'
;

x:
    name='x' '{' y=y '}'
;

y:
    name='y' '{' z=z '}'
;

z:
    name='z' '{' any+=any* '}'
;


Using this grammar I expected to be able to write a language like below:
a {
    b {
        
    }
    
    c {
        y {
            
        }
    }
}

x {
    y {
        z {
            the_end {}
        }
    }
}


However, I get an error due to the node "y" appearing under "c". Why is that? Is it because now that "y" has been used as a terminal in one of the rules, it cannot appear anywhere else in the language?

How to fix this grammar?
Re: Help with grammar [message #1742835 is a reply to message #1742824] Thu, 08 September 2016 05:14 Go to previous messageGo to next message
Karsten Thoms is currently offline Karsten ThomsFriend
Messages: 747
Registered: July 2009
Location: Dortmund, Germany
Senior Member

To #1:
root:
  a+=a*
  (
    (b=b a+=a* c=c)
    |
    (c=c a+=a* b=b)
  )
  a+=a*
;

a: 'a';
b: 'b';
c: 'c';


To#2:
It is not only that 'y' is a terminal rule, this could be solved by a datatype rule that allows 'y' as a valid identifier instead of using ID directly. The other problem is that any's ID could also be 'y' and there is no other chance to distinguish it in your grammar. Usually you could solve that with syntactic predicates, but not in your case.


Need professional support for Xtext, EMF, Eclipse IDE?
Go to: http://devhub.karakun.com
Twitter : @kthoms
Blog : www.karsten-thoms.de
Re: Help with grammar [message #1743040 is a reply to message #1742835] Fri, 09 September 2016 20:03 Go to previous message
Waqas Ilyas is currently offline Waqas IlyasFriend
Messages: 80
Registered: July 2009
Member
Thanks a lot Karsten for the reply. It helps a lot.

For the second problem, I presume a better way would be to implement the special hierarchy of x/y/z through validators, instead of grammar rules. And the grammar should instead be simplified like:
root:
    any+=any+
;

any:
    name=ID
    '{'
        any+=any*
    '}'
;
Previous Topic:ParseHelper accepts illegal input
Next Topic:Problems parsing ruby on rails files with xtext
Goto Forum:
  


Current Time: Tue Apr 20 02:28:04 GMT 2021

Powered by FUDForum. Page generated in 0.02012 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top