|
|
|
|
|
Re: Strange left recursive rules [message #1242790 is a reply to message #1242752] |
Mon, 10 February 2014 04:59 |
Xi Lin Messages: 21 Registered: January 2014 |
Junior Member |
|
|
Henrik Lindberg wrote on Sun, 09 February 2014 22:33
The problems here are:
* When looking at an Atomic and the next token is ID, it does not know
if this is a Atomic with value = ID, or a FunctionCall since the
function call also starts with an ID.
* Second problem is that LL(*) parsing does not have precedence on
operators and it does not know if to "shift or reduce" (in other parsing
technologies). This is something that must be manually resolved by the
grammar author when using LL. This is called "left factoring". You get
this in the FunctionCall rule which boils down to ID '(' ID
i.e. something like this does not work in a LL parser
Expression
: Expression '+' Expression
| Expression '-' Expression
...
;
Thanks for you reply.
1. For the first problem, i think that Xtext is working in a greedy match way that it should not be problem. I write a test as follow:
Atomic
:
FunctionCall
| TheOtherFunctionCall
;
FunctionCall
:
name=ID '(' arg=Atomic ')'
;
TheOtherFunctionCall
:
name=ID '[' arg=Atomic ']'
;
And it works well. So i think it may not be the core reason?
2. For the second problem, i know about using current to solve that Expression problem. But i still think maybe it is not the cause of problem? Because for the posted problematic grammar, if it enter the FunctionCall rule, it should know how to parse it.
See an example as follow:
This is a rule of ID '(' ID ')' and only FunctionCall rule can be applied on it and not other Atomic rule can be used in my thought.
In the posted grammar, if i remove Map from Atomic rule, everything is OK. So i think the problem is happened in Map rule and FunctionCall, but i cannot figure out in which case will it be ambiguous.
|
|
|
Re: Strange left recursive rules [message #1243177 is a reply to message #1242790] |
Mon, 10 February 2014 16:24 |
Henrik Lindberg Messages: 2509 Registered: July 2009 |
Senior Member |
|
|
On 2014-10-02 5:59, Xi Lin wrote:
> Henrik Lindberg wrote on Sun, 09 February 2014 22:33
>> The problems here are:
>>
>> * When looking at an Atomic and the next token is ID, it does not know
>> if this is a Atomic with value = ID, or a FunctionCall since the
>> function call also starts with an ID.
>>
>> * Second problem is that LL(*) parsing does not have precedence on
>> operators and it does not know if to "shift or reduce" (in other
>> parsing technologies). This is something that must be manually
>> resolved by the grammar author when using LL. This is called "left
>> factoring". You get this in the FunctionCall rule which boils down to
>> ID '(' ID
>>
>> i.e. something like this does not work in a LL parser
>>
>> Expression
>> : Expression '+' Expression
>> | Expression '-' Expression
>> ...
>> ;
>
>
> Thanks for you reply.
>
> 1. For the first problem, i think that Xtext is working in a greedy
> match way that it should not be problem. I write a test as follow:
>
>
> Atomic
> :
> FunctionCall
> | TheOtherFunctionCall
> ;
>
> FunctionCall
> : name=ID '(' arg=Atomic ')'
> ;
>
> TheOtherFunctionCall
> :
> name=ID '[' arg=Atomic ']'
> ;
>
> And it works well. So i think it may not be the core reason?
>
> 2. For the second problem, i know about using current to solve that
> Expression problem. But i still think maybe it is not the cause of
> problem? Because for the posted problematic grammar, if it enter the
> FunctionCall rule, it should know how to parse it.
>
> See an example as follow:
>
> getName(xi)
>
> This is a rule of ID '(' ID ')' and only FunctionCall rule can be
> applied on it and not other Atomic rule can be used in my thought.
>
> In the posted grammar, if i remove Map from Atomic rule, everything is
> OK. So i think the problem is happened in Map rule and FunctionCall, but
> i cannot figure out in which case will it be ambiguous.
As I said, you have to go and read up on how it actually works. I read a
great explanation in an ANTLr book ("The Definitive ANTLR reference")
how LL* parser works. It is not immediately intuitive when having worked
with other parser technologies.
Also look at other Xtext based grammars and how various programming
language constructs were implemented (calls, maps, etc).
- henrik
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.03329 seconds