Unit 03 CD
Unit 03 CD
BCS-602
Instructor: Md. Shahid
Unit-03
In Context-Free Grammar (CFG), especially for arithmetic expressions, we often divide expressions into
parts like:
Non-Terminal Meaning
Expression (E) A full arithmetic expression that can have additions and subtractions (+, -)
Term (T) A part of an expression that deals with multiplications and divisions (*, /)
Factor (F) The most basic units — like identifiers (id), numbers, or parenthesized
expressions ( E )
Assignment expression grammar— It defines the rules for how an assignment statement (like x = 5)
is structured in a programming language.
S → id = E // Assignment statement
E→E+T|T // Expressions
T → id | num // Terms
Parse Tree:
/|\
id = E
/|\
E + T
| |
T T
| |
id num
Note— Limitations of the parser are addressed by the semantic analyser (3rd phase of compiler).
Semantic Analysis
Semantic analysis is a crucial phase in the compilation process, following syntax analysis. While
parsers only check the syntax of a program (i.e., whether the program is written according to the
grammar rules), they do not verify if the program makes sense logically. This is where semantic
analysis comes in — it ensures that the program is not only syntactically correct but also
semantically meaningful, i.e., it complies with the rules of the programming language's semantics.
Problem: A parser only checks syntax, not semantics (the meaning of the code).
Analogy:
Example: int x = "Meerut"; → Syntax valid, but semantically wrong because we cannot
assign a string value to an integer variable.
Solution: Need attributes (e.g., type, val) and rules to enforce meaning.
Checks Needed:
T --> T * F| F
F → num
(a) Write a Syntax-Directed Definition (SDD) to compute the value of an arithmetic
expression based on the given grammar.
(b) Using the SDD, draw the annotated parse tree for evaluating the expression 3 + 2 * 5.
Answer— a. SDD
3+2*5 E = 13 ( answer )
E.val= 3 + T.val=10
F. val= 3 F. val=2
Attributes: These are metadata attached to grammar symbols (like E, T, id) to store
computed information (e.g. type, value, scope) during semantic analysis. They are defined
in SDD using semantic rules. Attributes may be of any kind: numbers, types, table
references, or strings.
Two Major Types of Attributes
1. Synthesized Attributes: Computed from the children nodes' attributes in the parse tree.
Example:
If we have a rule:
Production Semantic Rule
E → E₁ + T E.value = E₁.value + T.value
Here, E.value is synthesized because it is computed from E₁.value and T.value. The parent’s
attribute (E.value) depends on its children’s attributes (E₁.value and T.value) in the parse tree.
2. Inherited Attributes: Inherited attributes pass information from a parent node or left sibling
to a child node. Such attributes pass type information, scope information, function parameter
types, etc.
Example: production: A → B C
Semantic Rules:
1. B. i = A. i [ The inherited attribute i of B is assigned the value of i from its parent node A.]
2. C. i = B.s [ The inherited attribute i of C is assigned the value of s from its left sibling B.]
Q. In Syntax-Directed Definitions (SDDs), what are the two main types of attributes
used to propagate semantic information? Give one example of each type.
Types of SDD
SDDs are classified into two main types based on attribute dependency flow:
1. S-attributed SDD: An SDD where all attributes are synthesized (computed from
children to parents).
Key Features:
Production:
A→BC
Semantic Rules:
1. A.s = B.b
Answer—
This SDD is not L-attributed because the inherited attribute B.i depends on C.c (a right
sibling in the production A → B C).
Key Violation:
In L-attributed SDDs, an inherited attribute (like B.i) can only depend on:
Since B.i = f(C.c, A.s) requires C.c (a right sibling), it breaks the left-to-right evaluation
rule of L-attributed definitions.
Result:
P.i = A.i + 2
Q.i = P.i + A.i
A.s = P.s + Q.s
Rule 2:
Production: A → XY
Semantic rules:
Answer—
Rule 1 Analysis:
Production: A → PQ
Semantic rules:
P.i = A.i + 2 → P inherits from parent A. (Allowed in L-attributed)
Q.i = P.i + A.i → Q inherits from left sibling P and parent A. (Allowed in L-attributed)
A.s = P.s + Q.s → A synthesizes from children P and Q. (Always allowed)
Conclusion: Rule 1 is L-attributed.
Rule 2 Analysis:
Production: A → XY
Semantic rules:
X.i = A.i + Y.s
Here, X is depending on Y.s (right sibling Y). It is not allowed in L-attributed SDD (inherited
attributes should depend only on parent or left siblings, not right siblings).
Y.i = X.s + A.i
Y depends on X.s (left sibling X) and parent A. (Allowed)
Q. Differentiate between Syntax-Directed Definitions (SDD) and Syntax Directed Translation (SDT)
with one key point each.
Q. Write syntax directed definition for the given assignment statement used for expression
evaluation:
S → id = E
E→E+E
E→E*E
E → (E)
E → id
Answer:
Or
Write SDD to produce three-address code for Boolean expressions.
Or
Write down SDD of three-address code for Boolean expressions.
Or
Answer: B, B₁, and B₂ are non-terminals in the grammar (they can be expanded further).
Example:
B₁ might expand to (x > 0).
B₂ might expand to (y < 10).
B becomes (x > 0) || (y < 10).
Boolean Expression— It is a logical statement that evaluates to either true or false. Example: (5
> 3) → true, (10 == 20) → false
Note – This PDF covers only the ST2 syllabus. Complete notes for Unit 3 will be provided after
ST2 concludes.