Professional Documents
Culture Documents
FLCD Course2
FLCD Course2
FLCD Course2
S. Motogna - LFTC
Symbol Table
Definition = contains all information collected during compiling
regarding the symbolic names from the source program
Variants:
- Unique symbol table – contains all symbolic names
- distinct symbol tables: IT (identifiers table) + CT (constants
table)
S. Motogna - LFTC
ST organization
Remark: search and insert
S. Motogna - LFTC
Hash table
• K = set of keys (symbolic names)
• A = set of positions (|A| = m; m –prime number)
h:K→A
h(k) = (val(k) mod m) + 1
Toy hash function to use at
lab:
Sum of ASCII codes of chars
• Conflicts: k1 ≠ k2 , h(k1) = h(k2)
S. Motogna - LFTC
Visibility domain (scope) Example:
Int main(){
… int a;
• Each scope – separate ST void f()
• Structure -> inclusion tree {float a;
… int h() {…}
Hierachical structure of STs: }
…
main void g()
{char a;
f g …
}
}
h
S. Motogna - LFTC
Formal Languages
- basic notions-
S. Motogna - FL&CD
Examples of languages
• natural (ex. English, Romanian)
• programming (ex. C,C++, Java, Python)
• formal
S. Motogna - FL&CD
Example
a boy has a dog • A→ α = rule
• S,P,V,N,Q,C,B = nonterminal symbols
S→PV • a, boy,dog,has = terminal symbols
P→ a N
N→ boy or N→ dog Remarks
(N→boy|dog) 1. Sentence = word, sequence (contains only
V → QC terminal symbols) ; denoted w.
Q → has
C → BN 2. S⇒PV⇒a NV⇒a NQC⇒a N has C -
sentential form
B→a
In general : w=a1a2. . . an
3. The rule guarantees syntactical correctness,
but not the semantical correctness (A dog has
a boy)
S. Motogna - FL&CD
Grammar
• Definition: A (formal) grammar is a 4-tuple: G=(N,Σ,P,S)
with the following meanings:
• N – set of nonterminal symbols and |N| < ∞
• Σ - set of terminal symbols (alphabet) and |Σ|<∞
• P – finite set of productions (rules), with the propriety:
P⊆(N∪Σ)∗ N(N∪Σ)∗ x (N∪Σ)∗
• S∈N – start symbol /axiom A* = transitive and
Remarks : reflexive closure =
{a,aa,aaa,…} {a0}
1. (α,β)∈P is a production denoted α→β
A = {a}
2. N ∩ Σ = ∅ A+ = {a,aa,aaa,…}
X0 =𝜀
S. Motogna - FL&CD
Binary relations defined on (N ∪ Σ)∗
• Direct derivation
α ⇒ β , α,β ∈ (N ∪ Σ)∗ if α=x1xy1 , β=x1yy1 and x→y∈P
(x is transformed in y)
• k derivation
k
α ⇒ β ,α,β ∈ (N ∪ Σ)∗
sequence of k direct derivations α⇒α1 ⇒α2 ⇒...⇒αk−1 ⇒β, α,α1,α2,...αk−1,β∈(N∪Σ)∗
• + derivation
+ k
α ⇒ β if ∃ k>0 such that α ⇒ β (there exists at least one direct derivation)
• * derivation
* k * + 0
α ⇒ β if ∃ k≥0 such that α ⇒ β namely, α⇒ β⇔α⇒ β OR α⇒ β (α=β)
S. Motogna - FL&CD
Definition: Language generated by a grammar G=(N,Σ,P,S) is:
* w}
L(G)={w∈Σ∗ | S ⇒
Remarks: L1 = {a,b,aa}
L2 = {c,d,cd}
L1L2 = {ac,ad,acd,bc,bd,bcd,aac,aad,aacd}
1. S⇒* α,α∈(N∪Σ)∗ = sentential form
* w, w∈Σ∗ = word / sequence
S⇒
2. Operations defined for languages (sets) :
L1∪L2 , L1∩L2 , L1-L2 , 𝐿 (complement) , L+=⋃!"# 𝐿! , L∗=⋃!$# 𝐿!
Concatenation: L=L1L2 = {w1w2 | w1∈L1 , w2∈L2}
3. |w|=0 (empty word - denoted ε)
Definition: Two grammar G1 and G2 are equivalent if they generate the same language
L(G1)=L(G2)
S. Motogna - FL&CD
Chomsky hierarchy(based on form α → β ∈ P)
• type 0 : no restriction
• type 1 : context dependent grammar (x1Ay1 → x1γy1)
• type 2 : context free grammar (A → α ∈ P ,where A∈N and α∈(N ∪ Σ)∗
)
• type 3 : regular grammar ( A → aB|a ∈ P)
Remark :
type 3 ⊆ type 2 ⊆ type 1 ⊆ type 0
S. Motogna - FL&CD
Notations
o A,B,C,... – nonterminal symbols
o S ∈ N – start symbol
o a,b,c,... ∈ Σ – terminal symbol
o α,β,γ ∈ (N ∪ Σ)∗ - sentential forms
o ε – empty word
ox,y,z,w ∈ Σ∗ - words
o X,Y,U,... ∈ (N ∪ Σ) – grammar symbols (nonterminal or terminal)
S. Motogna - FL&CD
u
d a
a n a
r c a
a
Problem: The door to the tower is closed by the Red Dragon, using a
complicated machinery. Prince Charming has managed to steal the
plans and is asking for your help. Can you help him determining all
the person names that can unlock the door