2011-12-08 43 views
1

我不习惯功能性编程,并且很难为每个循环找出一个简单的方法。对于每个XSL - 变量?递归?

我有这个手,我需要在xsl中使用相同的算法。

let the input be a string S consisting of n characters: a1 ... an. 
let the grammar contain r nonterminal symbols R1 ... Rr. 
This grammar contains the subset Rs which is the set of start symbols. 
let P[n,n,r] be an array of booleans. Initialize all elements of P to false. 
for each i = 1 to n 
    for each unit production Rj -> ai 
    set P[i,1,j] = true 
for each i = 2 to n -- Length of span 
    for each j = 1 to n-i+1 -- Start of span 
    for each k = 1 to i-1 -- Partition of span 
     for each production RA -> RB RC 
     if P[j,k,B] and P[j+k,i-k,C] then set P[j,i,A] = true 
if any of P[1,n,x] is true (x is iterated over the set s, where s are all the indices for Rs) then 
    S is member of language 
else 
    S is not member of language 

我能够做一些,但我觉得卡住,走错了路。

我了解到我不能拥有类似数组的东西。即使我创建一个变量,就像一个数组,我无法更新它。

所以我开始做一些递归(通过调用模板),但我仍然认为,如果这是正确的方式。如果还有另一个为什么要实现那些没有递归嵌套每个。

+2

你是对的:从程序算法的描述逆向工程一个功能程序是很困难的。抛弃算法通常要好得多,并从描述算法的目标开始。 –

回答

0

在XSLT中,如果你避免使用xsl:for-each,你会好得多。

您可能希望看到完全用XSLT 2.0编写的一般LR(1)解析器,该解析器在2007年中实现。这是FXSL,可以在这里观看部分:http://fxsl.cvs.sourceforge.net/viewvc/fxsl/fxsl-xslt2/f/func-lrParse.xsl?view=markup

我已经写了两个相当复杂的解析器一个是JSON(见http://fxsl.cvs.sourceforge.net/viewvc/fxsl/fxsl-xslt2/f/func-json-document.xsl?revision=1.11&view=markup

和另一个,要复杂得多,是的XPath 2.0(语法有209条规则)。

一个基本工具是一个修改后的YACC,它生成特定LR(1)文法的解析表作为XML文档。然后将它们与输入字符串一起提供给一般LR(1)解析器的XSLT实现。