递归下降解析器LL(4)与示例

时间:2015-03-12 17:36:58

标签: java parsing recursive-descent

我想定义一种语法,语法生成的语言需要LL(4)递归下降解析器。语法不需要复杂,只要它满足要求吗?

语法的if语句可以如下

if lookahead ∈ FIRST(Something) then
code for Something ...
else if lookahead ∉ FOLLOW(Something
?
) then
ERROR;
Something
*
can be implemented as a while loop:
while lookahead ∈ FIRST(Something) do
code for Something ...
if lookahead ∉ FOLLOW(Something
*
) then
ERROR;
and Something
+
can be implemented as a repeat loop:
repeat
if lookahead ∉ FIRST(Something) then
ERROR;
code for Something ...
until lookahead ∈ FOLLOW(Something
+
);

2 个答案:

答案 0 :(得分:1)

不完全退化的示例语法(使用LR(1)解析器轻松解析,顺便说一句):

s: a | b | ID;
a: "x" "x" "x" "(" s "," s ")"
b: "x" "x" "x" "[" s "]"

答案 1 :(得分:1)

第181页的Grune和Jacobs的

Parsing Techniques(也可用online)显示了LL(k)的LL(k + 1)语言的示例:

S ::= a S A 
    | 
A ::= aᵏ b S 
    | c

因此,该语法描述了LL(4)语言

S ::= a S A 
    | 
A ::= a a a b S 
    | c

语法实际上是强-LL(4)和LALR(4),但LL(3)和LR(3)都不是。

对于LL(4)语法,这个将执行:

S ::= a a a 
    | A a a a a
A ::=

同样强-LL(4)和LALR(4),但LL(3)和LR(3)都不是。