Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Conditional Statement Body(Grammar for the body of a conditional statement might contain a '{' and a '}')
Conditional Statement Body [message #939363] Wed, 10 October 2012 17:41 Go to next message
A Jaro is currently offline A Jaro
Messages: 3
Registered: October 2012
Junior Member
I am trying to set up the Xtext grammar file so it can parse a few different styles of conditional statements. There are two if statements that I am trying which are very common in programming languages today below.

The first one is pretty straight forward; left-curly braces around the body. The second one is where there isn't left-curly braces and only one line of body is allowed.

The else portion seems to be working fine, but I can't get the 'if' portion to generate properly. I continue to get the error (where the '...' is various functions):

Decision can match input such as "..." using multiple alternatives: 1, 2

if (expression)
{
     body
}
else
{
     body
}


or

if (expression)
     one-line body
else
     one-line body



Here is what I have for the grammar:

IfStatement:
	'if' parexpression+=Expression+ 
		((=>'{' (ifstatement+=Statement|declaration+=Declare)* '}') | 
				ifstatement+=Statement)
	(=>	'else' ((=>'{' (elsestatement+=Statement|declaration+=Declare)* '}')|
				(elsestatement+=Statement|declaration+=Declare))
		)?
;


How can I make this one work?
Re: Conditional Statement Body [message #939605 is a reply to message #939363] Wed, 10 October 2012 22:59 Go to previous messageGo to next message
Henrik Lindberg is currently offline Henrik Lindberg
Messages: 2500
Registered: July 2009
Senior Member
One way is to make ExpressionBlock be an Expression.

Regards
- henrik

On 2012-10-10 19:41, A Jaro wrote:
> I am trying to set up the Xtext grammar file so it can parse a few
> different styles of conditional statements. There are two if statements
> that I am trying which are very common in programming languages today
> below.
> The first one is pretty straight forward; left-curly braces around the
> body. The second one is where there isn't left-curly braces and only
> one line of body is allowed.
>
> The else portion seems to be working fine, but I can't get the 'if'
> portion to generate properly. I continue to get the error (where the
> '...' is various functions):
>
> Decision can match input such as "..." using multiple alternatives: 1, 2
>
>
> if (expression)
> {
> body
> }
> else
> {
> body
> }
>
>
> or
>
>
> if (expression)
> one-line body
> else
> one-line body
>
>
>
> Here is what I have for the grammar:
>
>
> IfStatement:
> 'if' parexpression+=Expression+ ((=>'{'
> (ifstatement+=Statement|declaration+=Declare)* '}') |
> ifstatement+=Statement)
> (=> 'else' ((=>'{'
> (elsestatement+=Statement|declaration+=Declare)* '}')|
> (elsestatement+=Statement|declaration+=Declare))
> )?
> ;
>
>
> How can I make this one work?
Re: Conditional Statement Body [message #939998 is a reply to message #939363] Thu, 11 October 2012 08:06 Go to previous messageGo to next message
Sebastian Zarnekow is currently offline Sebastian Zarnekow
Messages: 2908
Registered: July 2009
Senior Member
The problem could be caused the cardinality in the parexpression
assignment. You allow multiple expressions there. Is that intentional?

I'd go for

'if' parexpression=Expression

instead of

'if' parexpression=Expression+

Regards,
Sebastian
--
Looking for professional support for Xtext, Xtend or Eclipse Modeling?
Go visit: http://xtext.itemis.com

Am 10.10.12 19:41, schrieb A Jaro:
> I am trying to set up the Xtext grammar file so it can parse a few
> different styles of conditional statements. There are two if statements
> that I am trying which are very common in programming languages today
> below.
> The first one is pretty straight forward; left-curly braces around the
> body. The second one is where there isn't left-curly braces and only
> one line of body is allowed.
>
> The else portion seems to be working fine, but I can't get the 'if'
> portion to generate properly. I continue to get the error (where the
> '...' is various functions):
>
> Decision can match input such as "..." using multiple alternatives: 1, 2
>
>
> if (expression)
> {
> body
> }
> else
> {
> body
> }
>
>
> or
>
>
> if (expression)
> one-line body
> else
> one-line body
>
>
>
> Here is what I have for the grammar:
>
>
> IfStatement:
> 'if' parexpression+=Expression+ ((=>'{'
> (ifstatement+=Statement|declaration+=Declare)* '}') |
> ifstatement+=Statement)
> (=> 'else' ((=>'{'
> (elsestatement+=Statement|declaration+=Declare)* '}')|
> (elsestatement+=Statement|declaration+=Declare))
> )?
> ;
>
>
> How can I make this one work?
Re: Conditional Statement Body [message #940489 is a reply to message #939998] Thu, 11 October 2012 17:30 Go to previous message
A Jaro is currently offline A Jaro
Messages: 3
Registered: October 2012
Junior Member
That seems like such a simple fix; to remove the cardinality. I thought I had added that for a specific reason, but it seems to work much better with your suggestion. Thank you very much for your help, you have saved me quite a bit of time.
Previous Topic:Migratiion problem
Next Topic:Xtext ecore cross references
Goto Forum:
  


Current Time: Sat Oct 25 07:23:19 GMT 2014

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

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