1
votes

below is the grammar that i am using for a calculator language and my attempt at finding the follow set and the first set of the grammar.

I would love help in figuring out what i am doing wrong when trying to figure out these sets because I feel like i am not doing them correctly at all (at least for the follow sets)

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 set

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 Set

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) = {*, /, //, %, +, -}

1

1 Answers

2
votes

You have certain mistakes in First as well

first(p) = {id, input, print,e}

it will include epsilon * is missing in the next two -

first(fact_tail) = { *,/, //, %, e} first(mult_op) = {*, /, //, %}

fact_tail → mult_op fact fact_tail | ε

Iam assuming here you actually mean

fact_tail → mult_op factor fact_tail | ε

Follow

follow(stmt) = {id, input, print,$}

if you refer to

stmt_list → stmt stmt_list | ε

then stmt is followed by first of stmt_list which includes e so string generated will end, hence stmt is followed by $

follow(expr) = {(, id, number, ), input, print, , /, //, %}

I don't know how you got this, follow of expr is equal to follow of stmt and )

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)