Hello,
Is the following rewrite correct?
variable-access = entire-variable | component-variable | identified-variable | buffer-variable
entire-variable = variable-identifier
component-variable = indexed-variable | field-designator
identified-variable = pointer-variable, '^'
buffer-variable = file-variable, '^'
indexed-variable = array-variable, `,', index-expression, < `,', index-expression > `]'
field-designator = record-variable, ` .', field-specifier | field-designator-identifier
array-variable = variable-access record-variable = variable-access pointer-variable = variable-access file-variable = variable-access field-specifier = field-identifier
REWRITTEN
variable access = entire variable | ( array variable, '[' index expression, < ',', index expression > ']' | record variable, '.' field specifier | field designator identifier | pointer variable, '^' | file variable, '^' | field specifier ), { '^' | '[' index expression, { ',', index expression }, ']' | '.', field specifier | field designator identifier }
Thanks,
Paul Isaacs
On 23 Feb 2017, at 05:12, Paul Isaacs paul@redpineinstruments.org wrote:
Hello,
Is the following rewrite correct?
[…]
Maybe, but I can’t see how…
But why rewrite at this point? For performance?
Best, Bastiaan.
On 23/02/17 06:26 AM, Bastiaan Veelo wrote:
[â¦] Maybe, but I canât see how⦠But why rewrite at this point? For performance? Best, Bastiaan.
Hello Bastiaan,
Thanks for the reply.
I think that variable-access is left recursive. Therefore, with a recursive descent parser, I have to rewrite.
It looks like the basic form of varaible-access is:
identifier { suffix } where suffix varies for record/array/dereference and doesn't exist for entire variable.
Regards,
Paul
On 23 Feb 2017, at 19:53, Paul Isaacs paul@redpineinstruments.org wrote:
On 23/02/17 06:26 AM, Bastiaan Veelo wrote:
[…] Maybe, but I can’t see how… But why rewrite at this point? For performance? Best, Bastiaan.
I think that variable-access is left recursive. Therefore, with a recursive descent parser, I have to rewrite.
Ah, understood. Indeed, there are no less than six left-recursive cycles through variable-access:
VariableAccess <- ComponentVariable <- IndexedVariable <- StringVariable VariableAccess <- ComponentVariable <- FieldDesignator <- RecordVariable VariableAccess <- ComponentVariable <- IndexedVariable <- ArrayVariable VariableAccess <- IdentifiedVariable <- PointerVariable VariableAccess <- BufferVariable <- FileVariable VariableAccess <- SubstringVariable <- StringVariable
In addition to the three through funtion-access and four through constant-access:
FunctionAccess <- ComponentFunctionAccess <- IndexedFunctionAccess <- ArrayFunction FunctionAccess <- ComponentFunctionAccess <- RecordFunctionAccess <- RecordFunction FunctionAccess <- SubstringFunctionAccess <- StringFunction ConstantAccess <- ConstantAccessComponent <- IndexedConstant <- ArrayConstant ConstantAccess <- ConstantAccessComponent <- IndexedConstant <- StringConstant ConstantAccess <- ConstantAccessComponent <- FieldDesignatedConstant <- RecordConstant ConstantAccess <- ConstantAccessComponent <- SubstringConstant <- StringConstant
I can’t force my brain to rewrite these rules so that all these left-recursions are eliminated. I’m not sure it can be done, as recursion may pass through different cycles for successive recursions. It is a real nightmare.
There is a technique for preventing infinite recursion in recursive descent parsers due to left-recursive grammars. It is based on memoizing the match for each recursion. After a certain number of recursions the length of the match stops growing (actually starts out short again). The crux is to break out of the recursion when the length of the match has reached its maximum. See the following post on one of the D forums, and the pointers therein (including literature references):
https://forum.dlang.org/post/jmpkcjmremoypbpcxqdu@forum.dlang.org https://forum.dlang.org/post/jmpkcjmremoypbpcxqdu@forum.dlang.org
This, also, is not straight forward, but I think I have managed to implement this successfully in Pegged.
Best, Bastiaan.
On 23/02/17 18:53, Paul Isaacs wrote:
On 23/02/17 06:26 AM, Bastiaan Veelo wrote:
[â¦] Maybe, but I canât see how⦠But why rewrite at this point? For performance? Best, Bastiaan.
Hello Bastiaan,
Thanks for the reply.
I think that variable-access is left recursive. Therefore, with a recursive descent parser, I have to rewrite.
It looks like the basic form of varaible-access is:
identifier { suffix } where suffix varies for record/array/dereference and doesn't exist for entire variable.
Regards,
Paul
Gpc mailing list Gpc@gnu.de https://www.g-n-u.de/mailman/listinfo/gpc
Hi Paul,
Here is a link to a Pascal recursive decent parser. It seems to handle basic variable access OK.
Regards, Peter
https://github.com/tangentstorm/pascal/blob/master/super/parse.p