本文介绍了规则由于递归规则调用而具有非LL(*)决策的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一条错误(如标题中所述),但有一条我不知道如何解决的规则.

I have an error (like said in the title) with one rule that i dont know how resolve.

我写了以下规则:

FunctionArguments returns FunctionArgs::IFunctionArguments :
    FunctionArgumentsNormal
    | FunctionArgumentsForIter
    ;

FunctionArgumentsNormal returns FunctionArgs::IFunctionArguments :
    {FunctionArgs::FunctionArguments} args+=Expression (',' args+=Expression)*
    | {FunctionArgs::FunctionArguments} argNames+=NamedArguments (',' argNames+=NamedArguments)*
    ;

FunctionArgumentsForIter returns FunctionArgs::IFunctionArguments :
    {FunctionArgs::FunctionArgumentsIterator} exp=Expression 'for' iterators=ForIterator
    ;

您能帮我通过左分解此表达式来解决该问题还是请提供其他解决方案?

Could you help me to resolve it by left-factoring this expression or give any others solutions please ?

推荐答案

在LL语法中,您不能有左递归.问题是LL解析器可以选择进行这样的推导:FunctionArguments-> FunctionArgumentsNormal-> FunctionArguments-> FunctionArgumentsNormal ...您的语法包含所谓的间接左递归.您可以在此Wikipedia文章中找到一个示例,该示例还包含有关如何解决它的解决方案:左-递归.一个不错的起点是(非常简单)以一种非常简单的方式编写语法,而无需在示例中包含所有注释和内容.如果您具有简单形式的语法:

In a LL grammar you can have no left-recursion. The problem is that a LL parser can choose to make a derivation like this:FunctionArguments -> FunctionArgumentsNormal -> FunctionArguments -> FunctionArgumentsNormal ...Your grammar contains what is called indirect left recursion. You can find an example in this wikipedia-article which also contains a solution as to how you can fix it: Left-Recursion.A good place to start is by (I suppose you already have done this) writing your grammar in a very simple way without all the annotations and things included in your example. If you have the grammar on the simple form:

S -> A | B
B -> "terminal1" B
  |  b
A -> a "terminal2"

进行必要的重写要容易得多.

it is much easier to do the necessary rewriting.

这篇关于规则由于递归规则调用而具有非LL(*)决策的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 13:13