Skip to main content



      Home
Home » Modeling » TMF (Xtext) » Xtext recursive statement(Xtext recursive statement)
Xtext recursive statement [message #1732064] Thu, 12 May 2016 07:25 Go to next message
Eclipse UserFriend
Hi,

I have a rule:
OpenDecl: (exp+=Expression (','? exp+=Expression)*);

and expression rule:

Expression:
LogicalOr
;
LogicalOr returns Expression:
BitwiseOr ({LogicalOr.left=current} '||' right=BitwiseOr)*
;
BitwiseOr returns Expression:
LogicalAnd ({BitwiseOr.left=current} '|' right=LogicalAnd)*
;
LogicalAnd returns Expression:
BitwiseAnd ({LogicalAnd.left=current} '&&' right=BitwiseAnd)*
;
BitwiseAnd returns Expression:
Equivalency ({BitwiseAnd.left=current} '&' right=Equivalency)*
;
Equivalency returns Expression:
Relational ({Equivalency.left=current} ('=='|'!='|'=') right=Relational)*
;
Relational returns Expression:
PlusMinus ({Relational.left=current} ('>='|'<='|'>'|'<') right=PlusMinus)*
;
PlusMinus returns Expression:
DivMult ({PlusMinus.left=current} ('+'|'-') right=DivMult)*
;
DivMult returns Expression:
LogicalNot ({DivMult.left=current} ('/'|'*') right=LogicalNot)*
;
LogicalNot returns Expression:
OnesComplement ({LogicalNot.left=current} '!' right=OnesComplement)*
;
OnesComplement returns Expression:
Exponentiation ({OnesComplement.left=current} ('~') right=Exponentiation)*
;
Exponentiation returns Expression:
Concatenation ({Exponentialtion.left=current} '^' right=Concatenation)*
;
Concatenation returns Expression:
Primary ({Concatenation.left=current} '##' right=Primary)*
;
Primary returns Expression:
NUMBER|STRING|ID
;

When I am using any text at beginning of the line it is not showing error.

index.php/fa/25883/0/

I understand that the rule OpenDecl is allowing to add text freely but how can I restrict it not allow the text without a keyword of any rule?

[Updated on: Mon, 16 May 2016 05:56] by Moderator

Re: Xtext recursive statement [message #1732065 is a reply to message #1732064] Thu, 12 May 2016 07:26 Go to previous messageGo to next message
Eclipse UserFriend
But I can have expressions like this:

a=b ,test=test1+test2 ...
Re: Xtext recursive statement [message #1732069 is a reply to message #1732065] Thu, 12 May 2016 08:15 Go to previous messageGo to next message
Eclipse UserFriend
hi i do not understand your question. have a look at your Primary rule.
it exactly allows this
Re: Xtext recursive statement [message #1732071 is a reply to message #1732069] Thu, 12 May 2016 08:49 Go to previous messageGo to next message
Eclipse UserFriend
Yes. My Primary rule allows but, can I impose any condition to not allow ID but to allow ID = ID. I mean a line contains only ID (not expression) I need to show error.
Re: Xtext recursive statement [message #1732075 is a reply to message #1732071] Thu, 12 May 2016 08:56 Go to previous messageGo to next message
Eclipse UserFriend
is this a question on the parser? (the grammar)
or the validator (adding such constrains that go beyond the grammar)?
simply adding a check method to the validator class and checking if all OpenDecls expr are actually a Equivalency would do the job
Re: Xtext recursive statement [message #1732078 is a reply to message #1732075] Thu, 12 May 2016 09:05 Go to previous messageGo to next message
Eclipse UserFriend
My question is related parser not validator.

Suppose a line contains :

abcdef //should throw an error

I need a rule that the text should be allowed if it is preceded by a keyword or itself an expression like abcdef=1.
Re: Xtext recursive statement [message #1732079 is a reply to message #1732078] Thu, 12 May 2016 09:10 Go to previous messageGo to next message
Eclipse UserFriend
then you need another

Equivalency2 returns Expression:
Relational ({Equivalency.left=current} ('=='|'!='|'=') right=Relational)+
;
Re: Xtext recursive statement [message #1732081 is a reply to message #1732079] Thu, 12 May 2016 09:10 Go to previous messageGo to next message
Eclipse UserFriend
and use this one instead of

OpenDecl: (exp+=Equivalency2 (','? exp+=Equivalency2)*);
Re: Xtext recursive statement [message #1732082 is a reply to message #1732081] Thu, 12 May 2016 09:11 Go to previous messageGo to next message
Eclipse UserFriend
p.s.:
but anyway: you dont have and should not check EVERYTHING in the grammar
Re: Xtext recursive statement [message #1732107 is a reply to message #1732082] Thu, 12 May 2016 12:11 Go to previous messageGo to next message
Eclipse UserFriend
It's not working .
Re: Xtext recursive statement [message #1732108 is a reply to message #1732107] Thu, 12 May 2016 12:14 Go to previous messageGo to next message
Eclipse UserFriend
What do you mean by not working
Re: Xtext recursive statement [message #1732110 is a reply to message #1732108] Thu, 12 May 2016 12:19 Go to previous messageGo to next message
Eclipse UserFriend
I have used
OpenDecl: (exp+=Equivalency2 (','? exp+=Equivalency2)*);

and

Equivalency2 returns Expression:
Relational ({Equivalency.left=current} ('=='|'!='|'=') right=Relational)+
;

It is throwing error. Maybe something going wrong with my grammar that's it is not working.

Re: Xtext recursive statement [message #1732111 is a reply to message #1732110] Thu, 12 May 2016 12:23 Go to previous messageGo to next message
Eclipse UserFriend
How to know which line causing error?

I added
parserGenerator = {
options = {
ignoreCase = true
}
debugGrammar = true
}
but no difference in error in console.

[Updated on: Thu, 12 May 2016 13:22] by Moderator

Re: Xtext recursive statement [message #1732112 is a reply to message #1732110] Thu, 12 May 2016 12:23 Go to previous messageGo to next message
Eclipse UserFriend
please share a complete grammar i can copy and paste
Re: Xtext recursive statement [message #1732113 is a reply to message #1732112] Thu, 12 May 2016 12:30 Go to previous messageGo to next message
Eclipse UserFriend
I have attached in the previous message
Re: Xtext recursive statement [message #1732114 is a reply to message #1732113] Thu, 12 May 2016 12:51 Go to previous messageGo to next message
Eclipse UserFriend
the grammar was working before? reeally?

your grammar with all the optionality simply is ambigous. ia it an existing language you are building for? if not: get rid of the optional commas etc
and you have stuff in that is hard to check on parser level. thus doing the equivalency check in the validator would have been 1000000 times easier.

especially your primary is expremely bogus. nearly all stuff in it is ambigous.
it dont get why you always do the strange thing for (something.)

you can try to solve it with predicates. but i dont know if the resulting ast will be what you are looking fo

OpenDecl: (exp+=Equivalency2 (=>','? exp+=Equivalency2)*);
Re: Xtext recursive statement [message #1732116 is a reply to message #1732114] Thu, 12 May 2016 12:56 Go to previous messageGo to next message
Eclipse UserFriend
I am building grammar for existing language which it allows so many things which is not possible in java

[Updated on: Thu, 12 May 2016 13:21] by Moderator

Re: Xtext recursive statement [message #1732117 is a reply to message #1732116] Thu, 12 May 2016 13:00 Go to previous messageGo to next message
Eclipse UserFriend
That attachment contains all the possible statements for the existing language. Can you please tell me Am I going in wrong direction?
Re: Xtext recursive statement [message #1732118 is a reply to message #1732117] Thu, 12 May 2016 13:19 Go to previous messageGo to next message
Eclipse UserFriend
sry i have not the time to digg that deep to get rid of all warnings.
is there any exisiting antlr grammar for jpl?

if i use
OpenDecl: (exp+=Expression (=>','? exp+=Expression)*);

the grammar compliles fine.
so what about using a validator ?!?
Re: Xtext recursive statement [message #1732119 is a reply to message #1732118] Thu, 12 May 2016 13:22 Go to previous messageGo to next message
Eclipse UserFriend
Thank you.
Re: Xtext recursive statement [message #1732121 is a reply to message #1732119] Thu, 12 May 2016 13:26 Go to previous messageGo to next message
Eclipse UserFriend
I didn't use validator. If I want to use validator I can written validation for a rule right? But how can I check a line which contains text
Re: Xtext recursive statement [message #1732123 is a reply to message #1732121] Thu, 12 May 2016 13:29 Go to previous messageGo to next message
Eclipse UserFriend
hi i dont understand this questions.

in the validator you validate the ast.

in your case you want to validate a OpenDecl.

thus you would have a

@Check
def void checkOpenDecl(OpenDecl decl) {
.... // validate if the given decl in correct here
}
Re: Xtext recursive statement [message #1732135 is a reply to message #1732116] Thu, 12 May 2016 14:38 Go to previous messageGo to next message
Eclipse UserFriend
Hi

If it is an existing language, there is almost certainly an existing
grammar which should not be too hard to translate to Xtext. Look for it
/ buy it.

Regards

Ed Willink

On 12/05/2016 17:56, RamaRao Nandamuri wrote:
> I am building grammar for existing language which it allows so many
> things which is not possible in java
Re: Xtext recursive statement [message #1732289 is a reply to message #1732123] Sun, 15 May 2016 00:35 Go to previous messageGo to next message
Eclipse UserFriend
Hi,

I am using validator to get the value entered for OpenDecl like
grammar:
OpenDecl: (openDecl+=Expression (','? openDecl+=Expression)*);

Validator Code:
@Check
def void checkOpenDecl(OpenDecl decl) {
println("OPen Declaration Test :::::::::::::::"+decl.openDecl)
}

the output I got:
OPen Declaration Test :::::::::::::::[com.prolifics.jpl.jPLEditor.impl.OneDeclImpl@1d03261 (number: null, jplString: null) (isEmpty: false, isArray: false, maxOccurs: null, isRange: false, range: null, hasSize: false, varsize: null, str: null, isArrayVar: false, hasExp: false, isString: false)]


I tried a lot but no luck. Could you please tell me how to get the value entered in editor?
Re: Xtext recursive statement [message #1732290 is a reply to message #1732289] Sun, 15 May 2016 02:06 Go to previous messageGo to next message
Eclipse UserFriend
Nodemodelutil will give you access to the nodes which you can ask for its text.
But as I said you want to inspect the ast by having a look at the open decl s expr parts
Re: Xtext recursive statement [message #1732291 is a reply to message #1732290] Sun, 15 May 2016 03:19 Go to previous messageGo to next message
Eclipse UserFriend
I tried this:

@Check
def void checkOpenDecl(OneDecl decl) {
val node = NodeModelUtils.findActualNodeFor(decl)
for (n : node.asTreeIterable) {
val ge = n.grammarElement
println("Grammar Element JPL ::::::::::")
}
}

for the grammar
OneDecl:{OneDecl} name=JPLID (
(isEmpty ?= '(' ')')? &
(isArray ?= '[' (maxOccurs+=NUMBER) ']')? &
(isRange ?= '[' (range+=RANGE) ']')? &
(hasSize ?= '(' (varsize+=NUMBER|jplId+=JPLID|str+=STRING) (',' (varsize+=NUMBER|jplId+=JPLID|str+=STRING))* ')')? &
(isArrayVar ?= '[' (jplId+=JPLID) ']')? &
(hasExp ?= '[' (exp+=Expression) ']')? &
(isString ?= '(' (str+=STRING) ')')?
)
;

But will it give keyword preceded by the above grammar?

Example: float a(10)

a(10) works based on above grammar rule. How can I check a(10) is preceded by any keyword or not?
Re: Xtext recursive statement [message #1732294 is a reply to message #1732291] Sun, 15 May 2016 03:38 Go to previous message
Eclipse UserFriend
What is the grammar around that float thing. Btw you usually don't use the node model for such a check. All relevant information is in the ast if the grammar is properly crafted.
Previous Topic:How to write xtext grammar for if statement
Next Topic:name auto generation
Goto Forum:
  


Current Time: Mon Jul 14 13:06:43 EDT 2025

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

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

Back to the top