Using the appropriate conditions on the grammar productions, explain when a grammar is context sensitive, when it is context free, and when it is a regular grammar.
• Context sensitive grammars -> every production is of the form a -> b where |a| <= |b|
• Context free grammars -> they are context sensitive grammars, where every production has in the left part one non-terminal A -> BaA
Regular grammars -> they are context-free grammars, where all productions have on of the forms: A -> a or A -> aB
Explain what is a synthesized attribute in a syntax-directed definition (SDD)
A synthesized attribute in an SDD is an attribute whose value at a node N is defined in terms of attributes at the children of N and at N itself.
What is a compiler and a interpreter?
What are the phases of a compiler?
What is the difference by passing an element by value or by reference?
Describe the terms left-recursive, nullable, useless.
What is a calling graph?
Edge from A to B, whenever A calls B (directly or indirectly)
What is an indirect calling graph?
What is a token (in lexical analysis)?
What are the two ways to handle reserved words?
What is Lex and describe how it works.
Describe the components of a parse tree in Syntax Analysis.
- Each internal node ○ Is marked by a non-terminal ○ Represents the application of a production rule - Each leaf ○ Is marked by a terminal - All the leafs give the input string
What are the two main methods for constructing a parse tree:
- The top-down approach ○ Start from the root (labelled with the start symbol) ○ Continue down to the leafs - The bottom-up approach ○ Start from the leafs Continue up to the root
How can we resolve disambiguity?
• Impose rules defining the relative precedence of operators
○ When we have two different operators (e.g. + and *)
Operator * has a higher precedence than +
Describe the components of an abstract parse tree.
Describe recursive descent parsing.
Describe predictive parsing.
• Special case of recursive -descent parsing
• It uniquely determines the steps of each procedure -> therefore no backtracking is required
• Runs in linear time
Not all grammars can be parsed by predictive parsing
What are LL(K) grammars?
Eliminate left recursion-
A → ABd / Aa / a
B → Be / b
A → aA’
A’ → BdA’ / aA’ / ∈
B → bB’
B’ → eB’ / ∈
Do left factoring in the following grammar-
S → aAd / aB
A → a / ab
B → ccd / ddc
S → aS’
S’ → Ad / B
A → aA’
A’ → b / ∈
B → ccd / ddc
Consider the following grammar and eliminate left recursion-
S → Aa / b
A → Ac / Sd / ∈
Step-01:
First let us eliminate left recursion from S → Aa / b
This is already free from left recursion.
Step-02:
Substituting the productions of S in A → Sd, we get the following grammar-
S → Aa / b
A → Ac / Aad / bd / ∈
Step-03:
Now, eliminating left recursion from the productions of A, we get the following grammar-
S → Aa / b
A → bdA’ / A’
A’ → cA’ / adA’ / ∈
Consider the following grammar and eliminate left recursion-
X → XSb / Sa / b
S → Sb / Xa / a
Step-01:
First let us eliminate left recursion from X → XSb / Sa / b
Eliminating left recursion from here, we get-
X → SaX’ / bX’
X’ → SbX’ / ∈
Now, given grammar becomes-
X → SaX’ / bX’
X’ → SbX’ / ∈
S → Sb / Xa / a
Step-02:
Substituting the productions of X in S → Xa, we get the following grammar-
X → SaX’ / bX’
X’ → SbX’ / ∈
S → Sb / SaX’a / bX’a / a
Step-03:
Now, eliminating left recursion from the productions of S, we get the following grammar-
X → SaX’ / bX’
X’ → SbX’ / ∈
S → bX’aS’ / aS’
S’ → bS’ / aX’aS’ / ∈
Do left factoring in the following grammar-
S → a / ab / abc / abcd
Step-01:
S → aS’
S’ → b / bc / bcd / ∈
Again, this is a grammar with common prefixes.
Step-02:
S → aS’
S’ → bA / ∈
A → c / cd / ∈
Again, this is a grammar with common prefixes.
Step-03:
S → aS’
S’ → bA / ∈
A → cB / ∈
B → d / ∈
This is a left factored grammar.