先计算再遵循语法集
Calculating first and follow set of grammar
下面是我在计算器语言中使用的语法,以及我试图找到后续集和第一组语法的尝试。
我很乐意帮助找出我在尝试弄清楚这些集合时做错了什么,因为我觉得我根本没有正确地做它们(至少对于后续集合)
语法
program → stmt_list $$$
stmt_list → stmt stmt_list | ε
stmt → id = expr | input id | print expr
expr → term term_tail
term_tail → add op term term_tail | ε
term → factor fact_tail
fact_tail → mult_op fact fact_tail | ε
factor → ( expr ) | number | id
add_op → + | -
mult_op → * | / | // | %
第一组
first(p) = {id, input, print}
first(stmt_list) = {id, input, print, e}
first(s) = {id, input, print}
first(expr) = {(, id, number}
first(term_tail) = {+, -, e}
first(term) = {(, id, number}
first(fact_tail) = {, /, //, %, e}
first(factor) = {(, id, number}
first(add_op) = {+, -}
first(mult_op) = {, /, //, %}
关注集合
follow(p) = {$}
follow(stmt_list) = {$}
follow(stmt) = {id, input, print}
follow(expr) = {(, id, number, ), input, print, , /, //, %}
follow(term_tail) = {), (, id, number, print, input}
follow(term) = {+, -}
follow(factor) = {, /, //, %}
follow(add_op) = {}
follow(mult_op) = {}
follow(fact_tail) = {*, /, //, %, +, -}
你的First也有一定的错误
first(p) = {id, input, print,e}
它将包括 epsilon
* 后面两个缺失 -
first(fact_tail) = { *,/, //, %, e} first(mult_op) = {*, /, //, %}
fact_tail → mult_op fact fact_tail | ε
我在这里假设你的意思是
fact_tail → mult_op factor fact_tail | ε
关注
follow(stmt) = {id, input, print,$}
如果你参考
stmt_list → stmt stmt_list | ε
然后 stmt 之后是 stmt_list 的第一个,其中包括 e 所以生成的字符串将结束,因此 stmt 之后是 $
follow(expr) = {(, id, number, ), input, print, , /, //, %}
我不知道你是怎么得到的,expr 的跟随等于 stmt 的跟随和 )
follow(expr) = {id, ), input, print,$}
follow(term_tail) is equal to follow(expr)
follow(term) = {+,-,),id,input,print,$}
follow(fact_tail) is equal to follow(term)
follow(factor) = first(fact_tail)
follow(add_op) = first(term)
follow(mult_op) = first(factor)
下面是我在计算器语言中使用的语法,以及我试图找到后续集和第一组语法的尝试。
我很乐意帮助找出我在尝试弄清楚这些集合时做错了什么,因为我觉得我根本没有正确地做它们(至少对于后续集合)
语法
program → stmt_list $$$
stmt_list → stmt stmt_list | ε
stmt → id = expr | input id | print expr
expr → term term_tail
term_tail → add op term term_tail | ε
term → factor fact_tail
fact_tail → mult_op fact fact_tail | ε
factor → ( expr ) | number | id
add_op → + | -
mult_op → * | / | // | %
第一组
first(p) = {id, input, print}
first(stmt_list) = {id, input, print, e}
first(s) = {id, input, print}
first(expr) = {(, id, number}
first(term_tail) = {+, -, e}
first(term) = {(, id, number}
first(fact_tail) = {, /, //, %, e}
first(factor) = {(, id, number}
first(add_op) = {+, -}
first(mult_op) = {, /, //, %}
关注集合
follow(p) = {$}
follow(stmt_list) = {$}
follow(stmt) = {id, input, print}
follow(expr) = {(, id, number, ), input, print, , /, //, %}
follow(term_tail) = {), (, id, number, print, input}
follow(term) = {+, -}
follow(factor) = {, /, //, %}
follow(add_op) = {} follow(mult_op) = {}
follow(fact_tail) = {*, /, //, %, +, -}
你的First也有一定的错误
first(p) = {id, input, print,e}
它将包括 epsilon * 后面两个缺失 -
first(fact_tail) = { *,/, //, %, e} first(mult_op) = {*, /, //, %}
fact_tail → mult_op fact fact_tail | ε
我在这里假设你的意思是
fact_tail → mult_op factor fact_tail | ε
关注
follow(stmt) = {id, input, print,$}
如果你参考
stmt_list → stmt stmt_list | ε
然后 stmt 之后是 stmt_list 的第一个,其中包括 e 所以生成的字符串将结束,因此 stmt 之后是 $
follow(expr) = {(, id, number, ), input, print, , /, //, %}
我不知道你是怎么得到的,expr 的跟随等于 stmt 的跟随和 )
follow(expr) = {id, ), input, print,$}
follow(term_tail) is equal to follow(expr)
follow(term) = {+,-,),id,input,print,$}
follow(fact_tail) is equal to follow(term)
follow(factor) = first(fact_tail)
follow(add_op) = first(term)
follow(mult_op) = first(factor)