简体   繁体   English

使用正则表达式解析规则CF语法(如何使用模板)

[英]Parse rules CF-grammar using Regular Expressions (how using templates)

I have CF-grammar. 我有CF语法。 It rules is as follows: 它的规则如下:

S->a|AS S-> a | AS

A->AB|a|b A-> AB | a | b

B->b B-> b

I want to parse these rules using Regular Expressions. 我想使用正则表达式解析这些规则。

My Regular Expression: 我的正则表达式:

\\b([AZ])->(?:([A-Za-z]+)\\|?)+ \\ b([AZ])->(?:([A-Za-z] +)\\ |?)+

For: "A->AB|a|b" result: 对于:“ A-> AB | a | b”结果:

0: A->AB|a|b 0:A-> AB | a | b

1: A 1:A

2: b 2:b

but I whant this: 但我想这样:

0: A->AB|a|b 0:A-> AB | a | b

1: A 1:A

2: AB 2:AB

3: a 3:

4: b 4:b

Regular Expressions aren't sufficiently powerful for the task, but are used for instance in EBFN to enhance expressiveness of the grammar. 正则表达式的功能不足以完成任务,但是正则表达式在EBFN中用于增强语法的表达能力。 You could consider a topdown parser (shaped via recursive calls) to parse your input. 您可以考虑使用自上而下的解析器(通过递归调用进行成形)来解析您的输入。 That's easy to implement in all languages that allows mutually recursive calls. 在所有允许相互递归调用的语言中,这很容易实现。 It requires a grammar with some restrictions (see Wikipedia about this, if you are interested). 它要求语法有一些限制(如果您有兴趣,请参见Wikipedia)。 At first glance your grammar should be LL(1), ie requires 1 token lookahead. 乍一看,您的语法应为LL(1),即需要1个令牌超前。

You could just split every rule with ->|\\| 您可以使用->|\\|拆分所有规则 to get the desired list. 获得所需的列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM