Using the grammar given construct the First, Follow, and Predict table.
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.
Using the grammar given construct the First, Follow, and Predict table. def id 'ParamList ' Block...
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 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 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...
(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 | 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 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)...