Give the predictive parsing table for the following grammar:
E -> T E’ (Do not forget to consider $)
E’ -> + T E’ | e ( e stand for empty string)
T -> F T’
T’ -> * F T’ | e
F -> ( E ) | id | num
Answer:
Construction of the predictive parsing table involves the following steps.
(1) Identifying FIRST(α): FIRST(α) is the set
of terminal symbols that begin the strings derivable from a string
of terminal and nonterminal symbols α in a grammar.
If α can derive ε, then ε is also in FIRST(α).
The algorithm to compute FIRST(X):
1. If X is a terminal, then FIRST(X) = { X }.
2. If X → ε is a production, then add ε to FIRST(X).
3. If X → Y1 Y2 ... Yk is a
production for k ≥ 1, and
for some i ≤ k, Y1Y2 ...
Yi-1 derives the empty string, and a is in
FIRST(Yi), then add a to
FIRST(X).
If Y1Y2 ... Yk derives the empty string,
then add ε to FIRST(X).
(2) Identifying FOLLOW(A): FOLLOW(A) is the set
of terminals that can appear immediately to the right of A
in some sentential form in a grammar.
Let us assume the string to be parsed is terminated by an
end-of-string endmarker $. Then if A can be the rightmost
symbol in some sentential form, the right endmarker $ is also in
FOLLOW(A).
Algorithm to compute FOLLOW(A) for all nonterminals A of a grammar:
1. Place $ in FOLLOW(S) where S is the start symbol of the grammar.
2. If A → αBβ is a production, then add every terminal symbol a in FIRST(β) to FOLLOW(B).
3. If there is a production A → αB, or a
production A → αBβ, where FIRST(β) contains
ε,
then add every symbol in FOLLOW(A) to
FOLLOW(B).
Consider the grammar:
E -> T E’
E’ -> + T E’ | ε
T -> F T’
T’ -> * F T’ | ε
F -> ( E ) | id | num
Step1: Find their FIRST and FOLLOW sets:
Now create a parsing table with rows are non-terminals and the columns are terminals, as shown below:
Thank you.
Give the predictive parsing table for the following grammar: E -> T E’ (Do not forget...
4.- [11 points] Give the predictive parsing table for the following grammar: (Do not forget to consider $) (ε stand for empty string) E → TE E' →+TE' E TFT T →*FT'TE F → (E) | id num
(Do not forget to consider $) (€ stand for empty string) E → TE E' →+TE' E T → FT T' → *FT' € F→ (E) id num 5.- ( 11 points) Armed with a predictive parsing table of question 4, it is easy to write a recursive- descent parser. Write, using pseudocode or C-syntax, the fragment of parser to parse l' and E'.
Given the grammar E -> E + T | T T -> T * F | F F -> E | id Parsing the string id*id results in: Id * id F * id T * id T * F T E What kind of parsing is this? What would the parsing look like if it were the other method?
7- Show a complete LR(0) and SLR(1) parsers, including the canonical collection of LR(0) and parsing table, using the following grammar E-→ E + T / T T-, T F / F l a l b Is this grammar LR(0) or SLR(1)? Why? 7- Show a complete LR(0) and SLR(1) parsers, including the canonical collection of LR(0) and parsing table, using the following grammar E-→ E + T / T T-, T F / F l a l b Is...
Question 1. (15 points) Consider the following LL (1) grammar with starting symbol S s→(ST) F → id F → num a) Compute the First and Follow sets of all non-terminals (5 points) b) Construct the LL (1) parsing table for the grammar (5 points) c) Show step-by-step (content of stack and input string, as well as the production taken) how the following string is parsed: ((20+30 + a)) (5 points)
Build a LR parsing table for the following grammar: F → f
2. Consider the following context free grammar with terminals (), +, id, num, and starting symbol S. S (ST) F-id Fnum a. Compute the first and follow set of all non-terminals (use recursion or iteration, show all the steps) Show step-by-step (the parsing tree) how the following program is parsed: (num+num+id)) b.
Q6) Consider the following grammar for arithmetic expressions. F ? (E) l i Using top-down parsing, find a leftmost derivation in this grammar for the expression i/i + . Show your work. 10 Points
create a c++ code for all possible states for bottom parsing for the following grammar expression: E E->E+T E->T T->T*F T->F F->(E) F->i
Recursive Descent Parsing Consider the following BNF grammar: A -> I = E E -> P O P | P O -> + | - | * | / | ** P -> I | L | UI | UL | (E) U -> + | - | ! I -> C | CI C -> a | b | ... | y | z L -> D | DL D -> 0 | 1 | ... | 8 |...