自己设计的文法及其select集

来源:互联网 发布:win10查看mac地址 编辑:程序博客网 时间:2024/05/02 02:14

0.      program-> stmt-sequence | #
1. stmt-sequence-> statement  |  '{'mul-stmt'}'     
2.      mul-stmt-> '{' mul-stmt '}' mul-stmt | statement mul-stmt  |  none

3. statement ->if-stmt | while-stmt |assign-stmt; |cin-stmt; | cout-stmt; 
4.      if-stmt-> if ( exp ) stmt-sequence mid1
5.      mid1-> else stmt-sequence | none                         
6. while-stmt ->while ( exp ) stmt-sequence
7. assign-stmt-> id = exp

8. cin-stmt -> cin>>id
9. cout-stmt-> cout<<exp


10. exp->  logic1-exp { || logic1-exp } 
11. logic1-exp-> logic2-exp { && logic2-exp }
12. logic2-exp-> simple-exp mid2          
13. mid2 -> comp-op simple-exp | none      
14. comp-op-> <= | < | > | >= | == | !=

15. simple-exp->term { addop term }
16. addop -> + | -
17. term->factor { mulop factor }
18. mulop-> *| /
19. factor-> (exp) | id | num

 


select[stmt-sequence->statement ]=[if,while,id,cin,cout]
select[stmt-sequence->'{' mul-stmt'}' ]=['{']
select[mul-stmt-> '{' mul-stmt '}' mul-stmt ]=['{']
select[mul-stmt-> statement mul-stmt ]=[if,while,id,cin,cout]
select[mul-stmt-> none ]=['}']

select[statement ->if-stmt]=[ if ]
select[statement -> while-stmt]=[ while ]
select[statement -> assign-stmt]=[ id ]
select[statement -> cin-stmt]=[ cin ]
select[statement -> cout-stmt]=[ cout ]
select[if-stmt-> if ( exp ) stmt-sequence mid1]=[ if ]
select[mid1-> else stmt-sequence]=[ else ]

select[mid1-> none ]=[#,'{',if,while,id,cin,cout,'}']

由于stmt-sequence*->……mid2,#属于select集;由规则4知道要求if-stmt的follow集,由规则3知道要求statement
的follow集,由规则3知道要求stmt-sequence的follow集[else],由规则2知道要求mul-stmt的first集['{',if,while,id,cin,cout,'none']
和mul-stmt的follow集['}']。求出并集并把else剔除[#,'{',if,while,id,cin,cout,'}']即读到else优先选用mid1-> else stmt-sequence
产生式

select[while-stmt ->while ( exp ) stmt-sequence]=[while ]
select[assign-stmt-> id = exp]=[id ]
select[cin-stmt -> cin>>exp]=[cin ]
select[cout-stmt-> cout<<exp]=[cout ]
select[exp->logic1-exp{ || logic1-exp }]=[(,id,num ]
select[logic1-exp->logic2-exp{&&logic2-exp}]=[(,id,num]
select[logic2-exp-> simple-exp mid3]=[(,id,num ]
select[mid3 -> comp-op simple-exp]=[ <= , < , > , >= , == , != ]

select[mid3 -> none]=[ && , ||, ), ;]
由规则12知道要求logic2-exp的follow集[&&],同时由10,11知道要求logic1-exp的follow集[||],exp的follow集[),;],
求并集[ && , ||, ), ;]

select[comp-op-> <=]=[ <=]
select[comp-op-> <]=[ <]
select[comp-op-> >]=[ >]
select[comp-op-> >=]=[ >=]
select[comp-op-> ==]=[ ==]
select[comp-op-> !=]=[ !=]
select[simple-exp->term { addop term }]=[(,id,num ]
select[term->factor { mulop factor }]=[(,id,num ]
select[addop -> + ]=[ + ]
select[addop -> - ]=[ - ]
select[factor-> (exp) ]=[ ( ]
select[factor-> id]=[id]
select[factor-> num]=[num]