Professional Documents
Culture Documents
COMPILER
COMPILER
1. Solution:
Given Grammar:
S_S#A/A
A_>A&B/B
B_>ID
Semantic Rules:
B _> ID: The value of B is the actual numeric value of the identifier (ID).
S#A
/ \
S A
/ \
A B
/ \
B ID
/ \
1 0
S_>AS{printf(1)}
S_>AB{printf(2)}
A_>a{printf(3)}
B_>bC{printf(4)}
B_>db{printf(5)}
C_>c{printf(6)}
Top-Down Parsing:
Bottom-Up Parsing:
Types of SDT
1. S-attributed SDT :
If an SDT uses only synthesized attributes, it is called as S-attributed SDT.
S-attributed SDTs are evaluated in bottom-up parsing, as the values of the parent
nodes depend upon the values of the child nodes.
Semantic actions are placed in rightmost place of RHS.
2. L-attributed SDT:
If an SDT uses both synthesized attributes and inherited attributes with a restriction
that inherited attribute can inherit values from left siblings only, it is called as L-
attributed SDT.
Attributes in L-attributed SDTs are evaluated by depth-first and left-to-right parsing
manner.
Semantic actions are placed anywhere in RHS.
Ease of implementation
Separation of concerns
Efficient code generation
Limited expressiveness
Inflexibility
Limited error recovery
4. Type Checking:
Type checking is a crucial phase in the compilation process of a programming language. It
is the process of verifying and enforcing the consistency of types in a program according to
the language's type system rules. The primary goal of type checking is to ensure that
operations performed on variables and expressions are semantically valid and conform to
the intended usage of types.
Type checking ensures that operations and assignments involve compatible data types,
preventing mismatches such as adding a string to an integer.
Ensures that function calls match the expected function signatures in terms of the number
and types of arguments and the return type.
Prevents errors related to array bounds, illegal memory access, and pointer type mismatches
to enhance program security.
Simplification for Code Generation: Breaks down complex language constructs into
simpler, more uniform operations, simplifying the subsequent phase of code generation for
the target machine.
Different Forms of Intermediate Code: Intermediate code can take various forms, such as
Three-Address Code (TAC) or Static Single Assignment (SSA) form, each suited for
different optimization strategies.