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 |
Mokhtar Alshubei 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 |
Henrik Lindberg 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 |
Mokhtar Alshubei 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 #767665 is a reply to message #767662] |
Sun, 18 December 2011 16:22 |
Mokhtar Alshubei 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 #767711 is a reply to message #767708] |
Sun, 18 December 2011 18:33 |
Mokhtar Alshubei 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 #767817 is a reply to message #767626] |
Mon, 19 December 2011 01:40 |
Henrik Lindberg 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
|
|
| | |
Goto Forum:
Current Time: Wed Sep 25 08:19:06 GMT 2024
Powered by FUDForum. Page generated in 0.05044 seconds
|