Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » arbitrary number of parantheses(father(john,tim). vs father((john,tim)))
arbitrary number of parantheses [message #767414] Sat, 17 December 2011 23:35 Go to next message
Mokhtar Alshubei is currently offline Mokhtar AlshubeiFriend
Messages: 121
Registered: November 2011
Location: Germany
Senior Member
Hello guys,

i have in my grammar to make the following statements accepted:

father(tim,john).
father((tim,john)).     //or even 
father(((((tim,john))))). 


that is i want to allow any number of pairs of parantheses. My rule for that is:

Father:
functor='father' '(' son=ID ',' father=ID ')' '.'
;


i tried these two definitions:
functor='father' '('+ son=ID ',' father=ID ')'+ '.' //with + operator
;

or:
functor='father' '('+ son=ID ',' father=ID =>')'+ '.' //with + op and syntactic predicate
;

the first solution gives warnings "Decision can be matched by different alternatives..." and the other solution with the syntactic predicate "=>" gives no warnings but doesnt work as expected. for example it accepts the following illegal statement:
father((tim,john)//with one missing )


Regards,
Mokhtar
Re: arbitrary number of parantheses [message #767435 is a reply to message #767414] Sun, 18 December 2011 01:04 Go to previous messageGo to next message
Henrik Lindberg is currently offline Henrik LindbergFriend
Messages: 2509
Registered: July 2009
Senior Member
It looks like you are on the wrong track.
There are several problems.

Since the parentheses are unassigned you would not know how many there were.
Since there is no matching, the opening and closing number of
parentheses may be different e.g. father ((((((((((tim,john)

So problem goes undetected by grammar, and you can not validate it.

Not sure why you get the problems you get when you specify a + after the
parentheses, probably because you have other rules as well. Hard to say
without seeing the entire grammar.

Have you looked at any of the Expression examples?

What does the rest of your language look like? Do you have many
different functions? Lists, datatypes, expressions like +-*/, etc. is it
lisp like?

Hard to suggest something that will work without knowing more.

As an example, is it legal to state father((tim),(john)),
father(father(tim,john), fred) etc.

Just curious, why Father entity has feature 'son', what about daughters?

And, are you trying to link individuals by their ID/Name? If so, the
approach would again be different.

FamilyModel : statements += Statement ('.' statements += Statment)* ;
Statement : Person | Function ;

Person : Male | Female ;
Male : 'male' name = ID;
Female : 'female' name = ID;

Function : Father | Mother ;
Father : 'father' '(' child = [Person] ',' father = [Male] ')' ;
Mother : 'mother' '(' child = [Person] ',' mother = [Female] ')' ;

male john.
male tim.
female mary.
father (tim, john).
father (mary, john).

etc...

Hope that gives you some ideas...
Regards
- henrik

On 2011-18-12 24:35, Xtexter wrote:
> Hello guys,
>
> i have in my grammar to make the following statements accepted:
>
> father(tim,john).
> father((tim,john)). //or even father(((((tim,john))))).
>
> that is i want to allow any number of pairs of parantheses. My rule for
> that is:
>
> Father:
> functor='father' '(' son=ID ',' father=ID ')' '.'
> ;
>
> i tried these two definitions:
> functor='father' '('+ son=ID ',' father=ID ')'+ '.' //with + operator
> ;
> or:
> functor='father' '('+ son=ID ',' father=ID =>')'+ '.' //with + op and
> syntactic predicate
> ;
> the first solution gives warnings "Decision can be matched by different
> alternatives..." and the other solution with the syntactic predicate
> "=>" gives no warnings but doesnt work as expected. for example it
> accepts the following illegal statement:
> father((tim,john)//with one missing )
>
> Regards,
> Mokhtar
Re: arbitrary number of parantheses [message #767626 is a reply to message #767435] Sun, 18 December 2011 14:19 Go to previous messageGo to next message
Mokhtar Alshubei is currently offline Mokhtar AlshubeiFriend
Messages: 121
Registered: November 2011
Location: Germany
Senior Member
yes. it is actually prolog like, therefore father(x,y). and father((x),(y)). or even father(((x)),(((y)))). must be accepted because prolog interpreter will evaluate them the same. This for example father((x,y)). should also be accepted but it is considered as another fact with same functor father and one arity which is the two arguments inserted between the parantheses-(never mind as in grammar level we dont care about the semantic). So my concern is to allow any number of parantheses. You have right, with the + it should work and it does but with the typical warning i mentioned above. below please find the real grammar for that (note that the previous was demo):
grammar org.xtext.example.mydsl.MyDsl with org.eclipse.xtext.common.Terminals

generate myDsl "http://www.xtext.org/example/mydsl/MyDsl"
import "http://www.eclipse.org/emf/2002/Ecore" as ecore

Model:	
	('module' '(' name = ID ')')?
	elements+=AbstractElement*;
AbstractElement:
	Element|UseModule
	;

	
UseModule:
	'use_module' '(' importURI = STRING ')'
;	
Element:
	Rule;
		
Rule:
	head= Literal (':-'body=Literal ((','|';')bodys+=Literal)*)? '.' 
;

Args:
	ts+=Term(','ts+=Term)*
;
Literal:
	 functor = ID '('+ args=Args ')'+
Term:
	s=STRING|id=ID|int=INT|lit=Literal
	;


please notice that the problem comes from the lit=Literal within the Term rule. when i commented it, no warning comes BUT i need the Term to be also either a Literal!
if you are confused from my verbose words, i would say in brief: based on the grammer above i need to allow any nr of "( )" to surround any Term.

many thanks,
Mokhtar
Re: arbitrary number of parantheses [message #767662 is a reply to message #767626] Sun, 18 December 2011 16:08 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14716
Registered: July 2009
Senior Member
hi,

what about something like

Args:
	ts+=Term(','ts+=Term)*
; 

Literal:
	 functor = ID '(' args=Args ')';
Term:
	'(' Term ')' |s=STRING|id=ID|int=INT|lit=Literal
	;


~Christian


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: arbitrary number of parantheses [message #767665 is a reply to message #767662] Sun, 18 December 2011 16:22 Go to previous messageGo to next message
Mokhtar Alshubei is currently offline Mokhtar AlshubeiFriend
Messages: 121
Registered: November 2011
Location: Germany
Senior Member
Thank you Christain! this works fine for the Term, still i need to apply same thing to Args. that is,
father(X,Y):- (male(X),person(Y)).
or
father(X,Y):- male(X),(person(Y),not(true)).

so far what works is like:
father(X,(Y)):- male(((X))),person((Y)).
or even
father(X,(Y)):- (male(((X)))),person((Y)). //parentheses surround male(..)

but not surrounding this "Term,Term,..."
regards,
Mokhtar

[Updated on: Sun, 18 December 2011 16:27]

Report message to a moderator

Re: arbitrary number of parantheses [message #767671 is a reply to message #767665] Sun, 18 December 2011 16:33 Go to previous messageGo to next message
Mokhtar Alshubei is currently offline Mokhtar AlshubeiFriend
Messages: 121
Registered: November 2011
Location: Germany
Senior Member
@correction@ sorry the statement
father(X,(Y)):- (male(((X)))),person((Y)). //parentheses surround male(..)
won't work still. it will be solved if i can allow arbitrary () around Args rule...
Re: arbitrary number of parantheses [message #767683 is a reply to message #767671] Sun, 18 December 2011 17:20 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14716
Registered: July 2009
Senior Member
Hi,

what about a straiht forward solution:

Args:
	ts+=Term(','ts+=Term)*
;

ArgsLiteral:
	=>'(' ArgsLiteral ')' | Args
;
 

Literal:
	 functor = ID '(' args=ArgsLiteral ')';
Term:
	'(' Term ')' |s=STRING|id=ID|int=INT|lit=Literal
	;


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: arbitrary number of parantheses [message #767688 is a reply to message #767683] Sun, 18 December 2011 17:33 Go to previous messageGo to next message
Mokhtar Alshubei is currently offline Mokhtar AlshubeiFriend
Messages: 121
Registered: November 2011
Location: Germany
Senior Member
Thank you! It worked but with mistakes. For example the following are accepted
father(a,b)
father((a,b))
father(a,(b))
		

but not
father((a),b) 
Re: arbitrary number of parantheses [message #767695 is a reply to message #767688] Sun, 18 December 2011 17:59 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14716
Registered: July 2009
Senior Member
Hi,

I'd change the grammar to

Args:
	ts+=Term(','ts+=Term)*
;

ArgsLiteral:
	Args | '(' ArgsLiteral ')'
;
 

Literal:
	 functor = ID '(' args=ArgsLiteral ')';
Term:
	'(' t=Term ')' |s=STRING|id=ID|int=INT|lit=Literal
	;


and enable backtracking in both antlr fragments in the workflow


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: arbitrary number of parantheses [message #767704 is a reply to message #767695] Sun, 18 December 2011 18:14 Go to previous messageGo to next message
Mokhtar Alshubei is currently offline Mokhtar AlshubeiFriend
Messages: 121
Registered: November 2011
Location: Germany
Senior Member
I did it but i am sorry there is still error in compilation
Re: arbitrary number of parantheses [message #767705 is a reply to message #767704] Sun, 18 December 2011 18:17 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14716
Registered: July 2009
Senior Member
How does your wf look like?

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: arbitrary number of parantheses [message #767707 is a reply to message #767705] Sun, 18 December 2011 18:20 Go to previous messageGo to next message
Mokhtar Alshubei is currently offline Mokhtar AlshubeiFriend
Messages: 121
Registered: November 2011
Location: Germany
Senior Member
Ooh that is embarrassing i just uncommented this
fragment = parser.antlr.XtextAntlrGeneratorFragment {
              options = {
                  backtrack = true
              }
            }

and ran the wf but fatal error came.
10q for helping
Re: arbitrary number of parantheses [message #767708 is a reply to message #767707] Sun, 18 December 2011 18:21 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14716
Registered: July 2009
Senior Member
I said: and enable backtracking in both antlr fragments in the workflow

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: arbitrary number of parantheses [message #767711 is a reply to message #767708] Sun, 18 December 2011 18:33 Go to previous messageGo to next message
Mokhtar Alshubei is currently offline Mokhtar AlshubeiFriend
Messages: 121
Registered: November 2011
Location: Germany
Senior Member

Oooki so this
options = {
                  backtrack = true
              }
should be added to this fragment
fragment = parser.antlr.XtextAntlrUiGeneratorFragment { }


sorry i was searching for something to uncomment :0. didnt expect to understand the instructions of workflow DSL.
But what is the disadvantages of enabling backtrack?
Thank you it is working. i will be bothering again i promise

Mokhtar

[Updated on: Sun, 18 December 2011 18:35]

Report message to a moderator

Re: arbitrary number of parantheses [message #767732 is a reply to message #767711] Sun, 18 December 2011 20:01 Go to previous messageGo to next message
Meinte Boersma is currently offline Meinte BoersmaFriend
Messages: 434
Registered: July 2009
Location: Leiden, Netherlands
Senior Member
W.r.t. backtracking: see 7th paragraph (2nd one under first grammar fragment) in http://dslmeinte.wordpress.com/2011/12/05/using-syntactic-predicates-in-xtext-part-1/

Re: arbitrary number of parantheses [message #767817 is a reply to message #767626] Mon, 19 December 2011 01:40 Go to previous messageGo to next message
Henrik Lindberg is currently offline Henrik LindbergFriend
Messages: 2509
Registered: July 2009
Senior Member
Do you not want the number of parentheses preserved in the model?
If they are not preserved, then some operations on the model (a
semantic quick fix) will also simplify the expressions and remove all
superfluous parentheses. This may come as a surprise to the user.

I think you should preserve them, and that you need to write the grammar
using left factorization. You should not need backtracking.

Below is an (completely untried) example what it could look like.
You probably need to work on the 'returns Term' - not sure I got that
right everywhere.

If the grammar is too lenient and allows expressions that are not
semantically correct, you can always catch that during validation.

You can count the number of parentheses and issue a warning "Superfluous
Parentheses" and offer a quick fix to simplify the expression.

The grammar below is a better start if you are planning to add more
operators, or to allow variations on cat(tom), 42(life), etc. (see
comments in the grammar).

Hope this helps, and that I did not make too many typos ;)

I did not include support for the form father((tom, john)).
It may be enough to just modify Parenthesizes to something like:

Parenthesized returns Term
: '(' terms += Term (',' terms+= Term)* ')';
;

If it is ok that it always wraps the Term in a list.
Otherwise you can use the rewrite rules (as shown elswhere), to make it
produce either a single parenthesized term, or a list.
Did not really think it through if that introduces some other conflict -
but you will see as you experiment.

Good Luck.

Regards
- henrik

Model : ('module' '(' name = ID ')')? elements += AbstractElement*
;

AbstractElement
: Element
| UseModule
;

UseModule
: 'use_module' '(' importURI = STRING ')'
;

Element : Rule '.'
;

// This produces either an instance of Fact, or
// instance of Rule, depending on if consequence operator
// :- is seen after the Fact or not. If seen, the Rule.head = current
// assigns the seen Fact to Rule.head, and Rule becomes the new current.
//
// i.e. cat(tom). is a valid rule (i.e. shorthand for cat(tom) :- true.)
// if this was Prolog.
//
Rule returns Term
: Fact
({Rule.head = current}
':-'
bodies += Term
(separator = (',' | ';') bodies += Term)*
)?
;


Term returns Term
: Fact
| Literal
| Parenthesized
;

// Fact produces either a LiteralId, or Fact depending on if a '(' is
// seen after the LiteralId.
//
Fact returns Term
: LiteralId
({Fact.functor = current}
'(' args += Term ',' args += Term)* ')'
)?
;

// Note, LiteralId is not part of Literal, because it probably not
// wanted to be able to state: 47(tom), or "Hello World"(tom).
//
Literal returns Term
: LiteralString
| LiteralInt
;

LiteralInt returns Term
: value = INT
;

LiteralId returns Term
: value = ID
;

LiteralString returns Term
: value = STRING
;

Parenthesized returns Term
: '(' term = Term ')';
;



On 2011-18-12 15:19, Xtexter wrote:
> yes. it is actually prolog like, therefore father(x,y). and
> father((x),(y)). or even father(((x)),(((y)))). must be accepted because
> prolog interpreter will evaluate them the same. This for example
> father((x,y)). should also be accepted but it is considered as another
> fact with same functor father and one arity which is the two arguments
> inserted between the parantheses-(never mind as in grammar level we dont
> care about the semantic). So my concern is to allow any number of
> parantheses. You have right, with the + it should work and it does but
> with the typical warning i mentioned above. below please find the real
> grammar for that (note that the previous was demo):
>
> grammar org.xtext.example.mydsl.MyDsl with
> org.eclipse.xtext.common.Terminals
>
> generate myDsl "http://www.xtext.org/example/mydsl/MyDsl"
> import "http://www.eclipse.org/emf/2002/Ecore" as ecore
>
> Model:
> ('module' '(' name = ID ')')?
> elements+=AbstractElement*;
> AbstractElement:
> Element|UseModule
> ;
>
>
> UseModule:
> 'use_module' '(' importURI = STRING ')'
> ;
> Element:
> Rule;
>
> Rule:
> head= Literal (':-'body=Literal ((','|';')bodys+=Literal)*)? '.' ;
>
> Args:
> ts+=Term(','ts+=Term)*
> ;
> Literal:
> functor = ID '('+ args=Args ')'+
> Term:
> s=STRING|id=ID|int=INT|lit=Literal
> ;
>
>
> please notice that the problem comes from the lit=Literal within the
> Term rule. when i commented it, no warning comes BUT i need the Term to
> be also either a Literal!
> if you are confused from my verbose words, i would say in brief: based
> on the grammer above i need to allow any nr of "( )" to surround any Term.
>
> many thanks,
> Mokhtar
Re: arbitrary number of parantheses [message #771595 is a reply to message #767817] Tue, 27 December 2011 17:53 Go to previous messageGo to next message
Mokhtar Alshubei is currently offline Mokhtar AlshubeiFriend
Messages: 121
Registered: November 2011
Location: Germany
Senior Member
Thank you!
Re: arbitrary number of parantheses [message #771617 is a reply to message #767732] Tue, 27 December 2011 19:03 Go to previous message
Mokhtar Alshubei is currently offline Mokhtar AlshubeiFriend
Messages: 121
Registered: November 2011
Location: Germany
Senior Member
Dear Mr. Meinte Boersma,

i read the part in your article about backtracking as last choice we should resort to. But still in same article you propose the syntactic predicate which is good but i can't find it solving any problem for which enabling backtracking would solve it.
I wanted to raise this question as we solved my problem of parantheses starting with being excited where to put => then we ended up with backtrack=true eventually!

Regards,
Mokhtar
PS: this is the article i meant:
http://dslmeinte.wordpress.com/2011/12/05/using-syntactic-predicates-in-xtext-part-1/
Previous Topic:Help please, ContentAssist with HTML support !
Next Topic:Unable to link alternatives [newbie]
Goto Forum:
  


Current Time: Wed Sep 25 08:19:06 GMT 2024

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

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

Back to the top