ANSWER :
S -> F
S -> (ST)
T -> ?
T -> +FT
F -> id
F -> num
a)
FIRST function :
FIRST(S) = FIRST(F)
FIRST(S) = {(}
FIRST(T) = {?}
FIRST(T) = {?,+}
FIRST(F) = {id,num}
FIRST(S) = {(,id,num}
FOLLOW function :
FOLLOW(S) = {$,FIRST(T)} = {$,+, FIRST())} = {$,+,)}
FOLLOW(T) = {)}
FOLLOW(F) = {FOLLOW(S)} = {$,+,)}
FOLLOW(F) = {$,+,),FIRST(T)} = {$,+,),FOLLOW(T)} = {$,+,)}
FOLLOW(F) = {$,+,)}
Table for first and follow function :
FIRST |
FOLLOW |
|
S |
{(,id,num} |
{$,+,)} |
T |
{?,+} |
{)} |
F |
{id,num} |
{$,+,)} |
b)
Parse Table :
( |
) |
+ |
id |
num |
$ |
|
S |
S->(ST) |
S->F |
S->F |
|||
T |
T->? |
T->+FT |
||||
F |
F->id |
F->num |
We have given a program ((num+num+id))
Parse Tree :
2. Consider the following context free grammar with terminals (), +, id, num, and starting symbol...
Consider the following LL(1) grammar with terminals (, ), +, id, num, and starting symbol S. Compute the first and follow set of all the nonterminals. S → (ST) F→id
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)
Consider the following context-free grammar with terminals {a, b, c, d} and start symbol S. S → W | X | Y | Z W → AW D | X | Y | Z X → BXD | Z Y → AY C | Z Z → BZC | ε A → a B → b C → c D → d (a) Give a derivation tree with input string: aaaabccddd (b) What language does this CFG recognize? Give a...
Write a context-free grammar that generates the same language as regular expression which is ab*|c+ (Describe the four components of context-free grammar which are start symbol(S), non-terminals(NT), terminals(T), and set of production rules(P))
Write a Context-Free grammar in either one of the following way: 1. Use recursion method to define grammar inductively, 2. Use semantic meanning for non-terminals method For the following language: strings have equal numbers of 0 and 1. For example your language will accept following strings 01, 0101, 010101, 000111, 001011, but will reject 010, 00011, 001, 11000, ... . Also show that grammar you created is ambiguos or not by using parse tree approach
Consider following note that are terminals. E rightarrow e + t | t t rightarrow ID | j x rightarrow e, E | E Eliminate left Then perform left factoring for the grammar Compute the first set for all symbols in the grammar Compute the follow set for non-terminal in the grammar. Can you please explain how to get first follow set? Thanks.
Problem 5 Suppose G is a context-free grammar, 2 is its set of terminals, and A is a variable in G (a) Describe an algorithm that determines whether A** w for some we *. (b) Argue that your algorithm is correct.
Given the following ambiguous context free grammar (3x20) 1. (a) Explain why the grammar is ambiguous (b) Find an equivalent unambiguous context-free grammar. (c) Give the unique leftmost derivation and derivation tree for the string s generated from the unambiguous grammar above. 2. Construct non-deterministic pushdown automata to accept the following language (20) 3. Convert the following CFG into an cquivalent CFG in Chomsky Normal Form (CNF) (20)-
Consider the following context free grammar. Write an attribute grammar that specifies the calculation rules, i.e. how the value of E or T is calculated. There is no need to perform type checking. We assume all types are matched correctly. Please clearly specify the type of attribute(s) you introduce, i.e. synthesized, inherited, or intrinsic. E ::= E + T | T T ::= T * F | F F ::= NUM NUM ::= 1 | 2 | 3 | 4...
Use a general algorithm to construct a (non-deterministic) pushdown automaton that corresponds to the following context-free grammar with the starting variable S: S → Aab, A → Sba; S → ε. Show, step by step, how the word baab will be accepted by this automaton. Its derivation in the given grammar is straightforward: S → Aab → Sbaab → baab.