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
  • Consider the following grammar: (//some alternative rules are listed on separate lines without using symbol |):...

    Consider the following grammar: (//some alternative rules are listed on separate lines without using symbol |): stmt −> assignment −> subr call assignment −> id := expr subr call −> id ( arg list ) expr −> primary expr tail expr tail −> op expr −> ε primary −> id −> subr call −> ( expr ) op −> + | - | * | / arg list −> expr args tail args tail −> , arg list −> ε...

  • Question 3: Given the following grammar: assign → id := expr expr → expr + term \ term term -term *factor lfactor factor-(expr) id Using the above grammar, show a leftmost derivation (first five step...

    Question 3: Given the following grammar: assign → id := expr expr → expr + term \ term term -term *factor lfactor factor-(expr) id Using the above grammar, show a leftmost derivation (first five steps) for the following assignment statement: A ((A B)+ C) a. [3 marks] b. Using the above grammar, show a rightmost derivation (first five steps) for the following assignment statement: A:-A+B+C)+A [3 marks] Draw the abstract syntax tree for each of the above statements [4 marks]...

  • Please read the last line (provide parse table and predict sets.) Suppose an elevator is controlled...

    Please read the last line (provide parse table and predict sets.) Suppose an elevator is controlled by two commands: ↑ to move the elevator up one floor and ↓ to move the elevator down one floor. Assume that the building is arbitrarily tall and that the elevator starts at floor x. Write an LL(1) grammar that generates arbitrary command sequences that (1) never cause the elevator to go below floor x and (2) always return the elevator to floor x...

  • Given the following grammar, FIRST and FOLLOW sets and LL Parser table, trace the parse of...

    Given the following grammar, FIRST and FOLLOW sets and LL Parser table, trace the parse of the string “fd xcor + ycor rt ycor” and draw the parse tree. FIRST(S) = FIRST(B) = FIRST(D) = { fd, rt } FIRST(A) = { fd, rt, ε } FIRST(E) = { xcor, ycor } FIRST(F) = { +, -, ε } FOLLOW(S) = { $ }                                      S --> BA FOLLOW(A) = { $ }                                      A --> BA | ε FOLLOW(B) = {...

  • Write a parser program for cSub using the method of recursive descent. The main program here...

    Write a parser program for cSub using the method of recursive descent. The main program here will effectively be the main program of the compiler as a whole. The input to the program will be a Csub source file, specified on the command line. The program will construct a parse tree for the program, with one interior node for every nonterminal in the derivation of the program, and one leaf node for each of the id, num, and real tokens...

  • (10] Eliminate left recursion from the grammar A Ba |Aa c B Bb | Ab 1...

    (10] Eliminate left recursion from the grammar A Ba |Aa c B Bb | Ab 1 d A Ad IB A BA ASJAE Consider the following grammar G: S'S S (S)S|e fa) (10] Construct the collection of the sets of LR(0) items (b) [5] When constructing the action table of SLR parser of G what are the rules to determine the parsing actions? That is, what is the rule for a shift action at state /? What is the rule...

  • Please help me with the coding for LL(1)!! The given grammar was: P → PL |...

    Please help me with the coding for LL(1)!! The given grammar was: P → PL | L L → N; | M; | C N → print E M → print "W" W → TW | ε C → if E {P} | if E {P} else {P} E → (EOE) | V (note: this has a variable O) O → + | - | * V → 0 | 1 | 2 | 3 (note: this has a terminal...

  • Designing functions Reminder: When designing functions, follow the given steps to reinforce good software development p...

    Designing functions Reminder: When designing functions, follow the given steps to reinforce good software development practices and problem solving strategies: a) Start by writing the documentation for the function. Use docstrings shown in lecture and lab. b) Write test calls to the function in your main function for the different cases of input the function will encounter. This will help you understand the behavior of the function and later behave as tests for your function. c) Define the function d)...

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