Symbol Table
Symbol Table
Symbol Table
Definition: A symbol table is a data structure used by a compiler to store
information about variables, functions, objects, classes, etc.
Purpose:
Performs shift (push input symbol to stack) or reduce (replace handle on stack with
LHS of a production)
Shift id → stack: id
Reduce id → E
Shift + → stack: E +
Shift id → stack: E + id
Reduce id → E
Shift * → stack: E + E *
Shift id → stack: E + E * id
Reduce id → E
Reduce E * E → E
Reduce E + E → E
Infix: A + B
Postfix: A B +
Prefix: + A B
String: 4 * 5 + 7 - 2
Postfix: 4 5 * 7 + 2 -
Prefix: - + * 4 5 7 2
markdown
Copy
Edit
-
/ \
+ 2
/ \
* 7
/ \
4 5
Prefix Tree (Top-Down Order):
markdown
Copy
Edit
-
/ \
+ 2
/ \
* 7
/ \
4 5
4. Operator Grammar & Leading/Trailing
Operator Grammar: A grammar that does not have ε-productions or two adjacent non-
terminals.
Leading(X):
If X → a..., add a
Trailing(X):
If X → ...a, add a
Example:
Grammar:
E → E + T | T
T → T * F | F
F → (E) | id
Leading(E) = { '(', id }
Trailing(E) = { ')', id }
Example:
kotlin
Copy
Edit
Production: E → E1 + T
Synthesized: E.val = E1.val + T.val
Inherited: T.inh = E1.type