第5章上下文无关文法:
设计文法:
做题的时候发现了一个正则表达式到文法的算法 R规则
根据正则式推导右线性文法_右线性文法表达ab*_Pluto °的博客-CSDN博客
举例
设计文法的关键在于理解递归性,文法是一个迭代器
1.The set {| i ≠ j or j ≠ k}, that is, the set of strings of a's followed by b's followed by c's, such that there are either a different number of a's and b's or a different number of b's and c's, or both.

上方的做法是错误的做法 因为没有考虑i=j=k的情况。

修改之后的结果
2.The set {|i=j|j=k} is generated by G = ( { S , T , U , A , C } , { a , b , c } , P , S )) with production P:

3.请为语言L = {}设计文法 

4.设计0、1构成的串的文法,0 1数量不相等.
5. The set of all strings 0’s and 1’s that are not of the form ww, that is , not equal to any string repeated.
6.The set of all strings with twice as many 0's as 1's.
                                 
7.设计 Lj≥2i = {| j ≥ 2i} 的文法. 
- S→AB
- A→aAbb I 空串
- B→Bb I 空串
8. L= {  | n 
0 and n
3} 
9.L(0 0* 11* 22* 00* 11* 22* 00* 11* 22*) Hint : The language defined by the regular expression.
 分析:看起来像是012202012然后每一个的子字符可以重复,
- 相当于三次方 
10. L = { |} 
explaination:分成两种情况:
                                        
   

11.L ={ }

验证某个串是否在文法中:派生 使用树来判断字符串是否在文法中
12.构造右线性文法:
 (1)
 (1)
     
 (2)
 (3)
(4) 正则语言是(a+b)*(aa+bb)*(a+b)* 所以根据R规则
 
13. Design CFG for strings in {0,1} * in which the number of 0s is greater than or equal to the number of 1s.
14.Design CFG for L = {w ∈ {(, )} ∗ | w is a string of balanced parentheses}.(括号匹配)
15.L ={ } 感觉构造PDA然后转换比较简单
16 L ={}的CFG
17.L={} 的CFG
文法的歧义性
1.判断一个语言具有歧义性,画出两种树

2.让你消除歧义性:重新设计文法。
文法的化简
消除无用的符号


一定要注意顺序
- 先消除产生的
- 再消除可达的
- 否则消除不干净

 先消除非产生的 
再消除非可达的


为啥要化简:自动机不能识别空产生式,多一步生成的操作 注意顺序的问题,有利于优化编译器的识别。
消除空产生式:

 


消除单元产生式


文法化简可靠顺序

消除无用符号


CNF范式

算法:

 
PDA转换成CFG



泵引理























