Question

Using the grammar given construct the First, Follow, and Predict table.

def id ParamList Block MethodDecl ParamList ParamList Param ParamListTail ParamListTail Param ParamListTail ParamList Tai

10 marks] Prove that this grammar is not the FIRST, FOLLOW, and PREDICT sets LL(1). Hint: You can do this by constructing

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer:

The solution having explanation and step by step to understand better and fully.
The given grammar is

MethodDecl -> 'def' id '(' paramList ')' Block
paramList -> c
paramList -> Param paramListTail
paramListTail -> c
paramListTail -> ',' param paramListTail
param -> id
Block -> '{' Statements '}'
Statements -> c
Statements -> Statement Statements
Statement -> IfStatement
Statement -> ReturnStatment
IfStatement -> 'if' '(' Expression
Else -> c
Else -> 'else' Block
Else -> 'else' IfStatement
ReturnStatment -> 'return' Expression
Expression -> integer
Expression -> id
Expression -> id '(' ArgList ')'
ArgList -> c
ArgList -> Expression ArgListTail
ArgListTail -> c
ArgListTail -> ',' Arg ArgListTail
Arg -> Expression


FIRST AND FOLLOW of above grammar are below :

NON-TERMINALS FIRST

MethodDecl def
paramList c|id
paramListTail c|,
Param id
Block $
Statements c|if|return
Statement if|return
IfStatement if
Else c|else
ReturnStatment return
Expression integer|id
ArgList c|integer|id
ArgListTail c|,
Arg integer|id

NON-TERMINALS FOLLOW

MethodDecl $
paramList )
paramListTail )
Param c|,
Block {
Statements }
Statement c|if|return
IfStatement c|if|return|$|if
Else $|if
ReturnStatment c|if|return
Expression c|if|return|$|if| c|,|)
ArgList )
ArgListTail )
Arg $

Follow of start symbol is always $

So a grammar will be in LL(1) if there Won't be any more than one entry in any cell of a prsing table.
so from above grammar, let's say we want to get 'id' from expression. As FIRST of expression is id|integer.
so on looking grammar, there is TWO entries to get the id from expression.

One entry : Expression -> id
other entry: Expression -> id '(' ArgList ')'

so the compiler will be confused which production rule to expand in parsing table to get 'id'.
Hence there are conflicts between these two production rules.
Therefore the above grammar WON'T be in LL(1).

Now To make the above grammar LL(1),
try to remove the Left recursion and Indirect Left recursion as well.
left recursion means production looks like: E -> E+T
so here Left recursion E to E.
similarly, There is an Indirect Left recursion. so after modification to these both, the grammar will be in LL(1).

Hit like/ upvote if you find the answer useful. Your response is important to us and is much needed.

Add a comment
Know the answer?
Add Answer to:
Using the grammar given construct the First, Follow, and Predict table. def id 'ParamList ' Block...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT