简体   繁体   中英

Context free grammar assistance

I need to find the context free grammar of 10 n 10 n 1 .

So far I have S→1S1S1∣0S∣ε .

When you want to "match up" different tokens in the input so that you know there are the same number of each, you're going to need a recursive rule that matches those tokens at the start and end of the rule, with a base case that matches what is in the middle. Other tokens that need to be before or after the matching tokens will necessarily require other rules.

So in your case, you have a "core" that you're trying to match 0 n 10 n so you need a recursive rule match 0 at the beginning and end

A → 0 A 0 | 1

Now you need to wrap that to get the tokens before and after the core

S → 1 A 1

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