将BNF表格转换为CNF表格

时间:2014-03-24 06:05:50

标签: algorithm parsing compiler-construction context-free-grammar chomsky-normal-form

我已经实现了cyk算法来检查一个字符串,无论它是否使用java以CNF形式给出的语法。所以,如果我认为以下是语法,

S->AB|BC
A->BA|a
B->CC|b
C->AB|a

然后我可以验证像abba这样的字符串。但现在我的语法如下,以BNF格式

<query> ::= <definestream>|<executionquery>

<definestream>::= define stream <streamname><attributename><type> {<attributename><type>}

<executionquery>::=<input> <output> [<projection>]

<input> ::= from <streams>

<output> ::= ((insert [<outputtype>]into <streamname>)| (return [<outputtype>]))

<streams> ::= <stream>[#<window>]| <stream>#<window> [unidirectional]<join> [unidirectional] <stream>#<window>
on <condition>within <time>| [every] <stream> ><stream> … <stream>within <time>
| <stream>, <stream>, <stream>within <time>

<stream> ::= <streamname><conditionlist>

<projection> ::= (<externalcall><attributelist>)|<attributelist>
[group by <attributename>][having <condition>]

<externalcall>::= call <name> ( <paramlist>)

<conditionlist>::= {‘[’<condition>’]’}

<attributelist>::=(<attributename>[as <referencename>])| ( <function>(<paramlist>)as   <referencename>)

<outputtype>::= expiredevents| currentevents| allevents

<paramlist>::= {<expression>}

<condition> ::= ( <condition> (and|or) <condition> )|(not <condition>)
|( <expression> (==|!=|>=|<=|>|<|contains) <expression> )

<expression> ::= ( <expression> (+||/|*|%)<expression> )|<attributename>|<
int>|<long>|<double>|<float>|<string>

我需要将其转换为CNF格式。我已经搜索并发现应该完成以下任务以将任何CFG转换为cnf形式。 1.删​​除空值,然后删除单位产品。但他们所展示的例子是为了遵循以下语法而做的,

S→ASA|aB        
A→B|S            
B→b|ε

但是在BNF格式中有很多其他语法,我不知道如何转换它。并且很难识别上述BNF语法的终端。任何人都可以解释一下这个程序,所以我可以继续这个。提前致谢

0 个答案:

没有答案