Grammar:
S → Aa | bAc | dc | bda
A → dAugmented Grammar:
S' → S
S → Aa | bAc | dc | bda
A → dCLR(1) Items and States:
I₀:
I₁: (on S)
I₂: (on A from I₀)
I₃: (on b from I₀)
I₄: (on d from I₀)
I₅: (on a from I₂)
I₆: (on A from I₃)
I₇: (on d from I₃)
I₈: (on c from I₄)
I₉: (on c from I₆)
I₁₀: (on a from I₇)
CLR(1) Parsing Table:
| State | a | b | c | d | $ | S | A |
|---|---|---|---|---|---|---|---|
| 0 | s3 | s4 | 1 | 2 | |||
| 1 | acc | ||||||
| 2 | s5 | ||||||
| 3 | s7 | 6 | |||||
| 4 | s8 | ||||||
| 5 | r1 | ||||||
| 6 | s9 | ||||||
| 7 | s10 | r5 | |||||
| 8 | r3 | ||||||
| 9 | r2 | ||||||
| 10 | r4 |
Production Rules:
Grammar:
A → Q
Q → St | Rr
S → sS | ε
R → qR | εStep-by-step analysis:
First Sets:
Follow Sets:
LL(1) Conditions Check:
For Q → St | Rr:
For S → sS | ε:
For R → qR | ε:
String "qqqr5" generation:
Conclusion: Yes, the grammar is LL(1) and "qqqr5" can be generated.
Grammar:
X → X + Y | Y
Y → Y d F | F
F → (X) | idFirst and Follow Functions:
First Sets:
Follow Sets:
LL(1) Grammar Check:
For X → X + Y | Y:
Conclusion: This grammar is NOT LL(1) due to left recursion in X → X + Y | Y.
Grammar:
E → T E'
E' → + T E' | ε
T → F T'
T' → * F T' | ε
F → (E) | idSequence of moves for "id+id*id":
| Stack | Input | Action |
|---|---|---|
| $E | id+id*id$ | E → TE' |
| $E'T | id+id*id$ | T → FT' |
| $E'T'F | id+id*id$ | F → id |
| $E'T'id | id+id*id$ | match |
| $E'T' | +id*id$ | T' → ε |
| $E' | +id*id$ | E' → +TE' |
| $E'T+ | +id*id$ | match |
| $E'T | id*id$ | T → FT' |
| $E'T'F | id*id$ | F → id |
| $E'T'id | id*id$ | match |
| $E'T' | *id$ | T' → *FT' |
| $E'T'F* | *id$ | match |
| $E'T'F | id$ | F → id |
| $E'T'id | id$ | match |
| $E'T' | $ | T' → ε |
| $E' | $ | E' → ε |
| $ | $ | accept |
Grammar with Semantic Rules:
PRODUCTION | SEMANTIC RULES
1) D → T L | L.in = T.type
2) T → int | T.type = integer
3) T → float | T.type = float
4) L → L₁, id | L₁.in = L.in; addType(id.lexeme, L.in)
5) L → id | addType(id.lexeme, L.in)Parse Tree for "float id1, id2, id3":
D
/ \
T L
(float) |
L , id3
/ |
L , id2
/ |
id1Dependency Graph:
Differences:
Algorithm:
Strategies:
Grammar with SDD:
E → E + T | E.val = E.val + T.val
E → T | E.val = T.val
T → T * F | T.val = T.val * F.val
T → F | T.val = F.val
F → (E) | F.val = E.val
F → digit | F.val = digit.lexvalParse Tree for "3*5 + 4n":
E (19)
/ | \
E + T (4)
/|\ |
T * F F
| | |
F 5 4
|
3Basic Block:
a = b + c
b = b - d
c = c + d
d = b + cDAG Representation:
Code:
void productionInt(int a[], int b[])
{
int prod=0, i=1;
do {
prod = prod * a[i]*b[i];
i++;
} while(i<=20);
printf("product=%d", prod);
}i) Three Address Code:
t1 = 0
t2 = 1
L1: t3 = t2 * 4
t4 = a[t3]
t5 = t2 * 4
t6 = b[t5]
t7 = t1 * t4
t8 = t7 * t6
t1 = t8
t9 = t2 + 1
t2 = t9
if t2 <= 20 goto L1
print t1ii) Basic Blocks:
iii) Flow Graph:
Entry → Block1 → Block2 → Block3
↑ ↓
←────────┘Basic Block: Sequence of consecutive statements with:
Flow Graph: Directed graph showing control flow between basic blocks.
i) Data type grammar use: synthesized attributes ii) Symbol table is generated in: semantic analysis phase iii) Operator precedence parser is: shift-reduce parser iv) Recursive descent parser can parse: LL(1) grammar
Expression: a + b * c / e + f * g
i) Quadruple:
(*, b, c, t1)
(/, t1, e, t2)
(+, a, t2, t3)
(*, f, g, t4)
(+, t3, t4, t5)ii) Triple:
(*, b, c)
(/, (0), e)
(+, a, (1))
(*, f, g)
(+, (2), (3))iii) Indirect Triple:
Instruction List: [0, 1, 2, 3, 4]
Triple:
(*, b, c)
(/, (0), e)
(+, a, (1))
(*, f, g)
(+, (2), (3))Definition: Local optimization technique that examines small sequences of instructions and replaces them with shorter or faster sequences.
Characteristics:
Example:
Before: MOV R1, R2
MOV R2, R1
After: MOV R1, R2Primary Tasks: