简体   繁体   English

上下文无关的语法帮助

[英]Context free grammar assistance

I need to find the context free grammar of 10 n 10 n 1 . 我需要找到10 n 10 n 1的上下文无关文法。

So far I have S→1S1S1∣0S∣ε . 到目前为止,我有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 因此,在您的情况下,您有一个试图匹配0 n 10 n的“核心”,因此您需要在开头和结尾处匹配0的递归规则

A → 0 A 0 | A→0 A 0 | 1 1

Now you need to wrap that to get the tokens before and after the core 现在,您需要将其包装以获取核心之前和之后的令牌

S → 1 A 1 S→1 A 1

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

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