« Return to Thread: If statement vs short if statement

Re: If statement vs short if statement

by Alex James-2 :: Rate this Message:

Reply to Author | View in Thread

Unfortunately, statements can be broken up on multiple lines so I  
would have to define an <EOL> token after every possible input except  
for this situation.
I'm thinking I might have to go for the deep complicated look ahead  
option.
I've been playing around with it and have come up with this (it  
doesn't work but it's a starting point):

<IF> condition()
       
                        ((statement())+ (<ELSE> (statement())+)? <ENDIF>
                |
                        statement() )

How would I use lookahead in this situation? At the moment it chooses  
the long if incorrectly.


On 17 May 2009, at 20:29, Nicolas Piguet wrote:

> Can statements be broken up on multiple lines? If they can't, the  
> easiest
> solution you have is probably to treat end of lines just like any  
> other token
> in your grammar instead of ignoring them.
>
> then you could just define your statement and if as
>
> Statement = ( Whatever | IfStatement | WhileStatement | Declaration  
> | ...)
> <EOL>+
> IfStatement = <IF> condition (LongIf | Statement)
> LongIf = <EOL> Statement+ (<ELSE> statement+)? <ENDIF>
>
> The advantage of this is that your parser won't need to do any deep,
> complicated lookahead to decide which branch to take, so it should  
> be fast
> enough.
>
> Are there any other cases that have a short form (say while and for  
> loops, for
> example)?
>
> On Sunday 17 May 2009 11:03:22 pm Alex James wrote:
>> Hi,
>>
>> I'm quite to new JavaCC and need a little help with the parsing of an
>> if statement in the language I'm trying to replicate.
>>
>> The language has the following definition for if statements:
>>
>> <if> condition
>>   statement+
>> (<else>
>>   statement+)?
>> <endif>
>>
>> It also has a short if statement:
>>
>> <if> condition statement
>>
>> Trouble is I can't get my parser to recognise when the short if
>> statement has been used as there is nothing to distinguish it from  
>> the
>> long if statement. The language I'm trying to replicate generally
>> ignores end of line characters but seems to take them into account
>> when recognising the short if statement. Is there any way I can  
>> ignore
>> end of line characters except in this one circumstance?
>>
>> Any help would be greatly appreciated.
>>
>> Cheers,
>>
>> Alex
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@...
>> For additional commands, e-mail: users-help@...
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@...
> For additional commands, e-mail: users-help@...
>


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@...
For additional commands, e-mail: users-help@...

 « Return to Thread: If statement vs short if statement