Ambiguity in Access Expressions

Monkey Forums/Monkey Programming/Ambiguity in Access Expressions

Tibit(Posted 2011) [#1]
Hi!

I would really like some input on a challenge I'm working on.

My aim is to make a SyntaxPraser that can parse Monkey code.

However I can't think my way out of how to solve that Monkey does not require parenthesis on function and method calls.

Let us take an if-statement for an example:

If expression statement

expression can be replaced by a function call, ex MyFunction 20,10
statement can be replaced by a function call also, ex MyFunction 5,5
One trixy thing is that each parameter to the Function is in itself an expression, and can be a function call in turn.

How do I parse this?

I mean if THEN where required for one-liners, easy. If parenthesis where required on functions, easy.

I don't think this compiles, but should it not in theory?
If MyFunction MyFunction MyFunction 10 , 10 , MyFunction, 10

However this compiles:
Field MyField:= new MyClass 10,10

I'm confused on how to boil this down to a LLParsable expression! Help!

Which are the rules for when parenthesis are required?

EDIT
Can it be that this is an impossible problem to solve? That a complete parsing of Monkey is impossible if not assuming a certain code-standard?Though with my current grammar I can't even parse all of mojo correctly.


Samah(Posted 2011) [#2]
If MyFunction MyFunction MyFunction 10 , 10 , MyFunction, 10

No, because the return value for MyFunction is being used. You can only call a function without parentheses if you're discarding the return value. Personally I think parentheses should be mandatory, as should the Then keyword. But this is coming from a background where languages actually have a rigid structure. :)

If you can write a valid grammar for Monkey, you should be able to get an AST fairly simply.


Samah(Posted 2011) [#3]
Can it be that this is an impossible problem to solve?

If trans can correctly parse it, it's 100% possible.

Bah... double post...


ziggy(Posted 2011) [#4]
welcome to the monkey parsing world! hehehe If you tokenize everything and cassify each token it's not that complicated. I would suggest you to take a look to the parser class in the trans module of the monkey parser.


marksibly(Posted 2011) [#5]
Hi,

> However this compiles:
> Field MyField:= new MyClass 10,10

It doesn't here! Perhaps a bug...?

Brackets are only optional for 'statement' function calls (well, and for functions that don't take any parameters - but that's really a semantic issue as to the parser these just look like plain identifiers).

To parse bracketless statement functions involves a bit of token 'look ahead' though, which may or may not be possible with an off-the-shelf parser.

Please feel free to email me if you want to discuss any compiler issues in more detail without boring the pants off everyone here!


Tibit(Posted 2011) [#6]
Ops I didn't post my last reply, anyway here it goes again:

Yeah I have a tokenized everything, got a class for almost every concept in Monkey, way over a 100 classes :)

You can only call a function without parentheses if you're discarding the return value.

Is it as simple as this? That would be awesome!

@Mark, oh well my bad! Let me find one from the Monkey source instead..

Here we go:
Function PlaySound( sound:Sound,channel=0,flags=0 )
If sound.sample device.PlaySample sound.sample,channel,flags
End
* As I have it now, my parser complains that after sound.sample it found a , that it did not expect

Mark thanks for the offer! I will send you a mail and save the sanity of people here :)