简体   繁体   中英

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

I have CF-grammar. It rules is as follows:

S->a|AS

A->AB|a|b

B->b

I want to parse these rules using Regular Expressions.

My Regular Expression:

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

For: "A->AB|a|b" result:

0: A->AB|a|b

1: A

2: b

but I whant this:

0: A->AB|a|b

1: A

2: AB

3: a

4: b

Regular Expressions aren't sufficiently powerful for the task, but are used for instance in EBFN to enhance expressiveness of the grammar. 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). At first glance your grammar should be LL(1), ie requires 1 token lookahead.

You could just split every rule with ->|\\| to get the desired list.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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