Professional Documents
Culture Documents
Course 1112
Course 1112
Course 1112
Push-Down Automata
(PDA)
S.Motogna - FL&CD
Intuitive Model
S.Motogna - LFTC
Definition
• A push-down automaton (APD) is a 7-tuple M = (Q,𝞢,𝞒,𝞭,q0,Z0,F)
where:
• Q – finite set of states
• 𝞢 - alphabet (finite set of input symbols)
• 𝞒 – stack alphabet (finite set of stack symbols)
• 𝞭 : Q x (𝞢 U {𝜺}) x 𝞒 →𝒫(Qx 𝞒*) –transition function
• q0 ∈Q – initial state
• Z0 ∈ 𝞒 – initial stack symbol
• F ⊆Q – set of final states
S.Motogna - LFTC
Push-down automaton
Transition is determined by:
• Current state
• Current input symbol
• Head of stack
S.Motogna - LFTC
Configurations and transition / moves
• Configuration:
(q, x, 𝞪) ∈Q x 𝜮* x 𝜞*
where:
• PDA is in state q
• Input band contains x
• Head of stack is 𝞪
• Initial configuration (q0, w, Z0)
S.Motogna - LFTC
Configurations and moves(cont.)
S.Motogna - LFTC
Representations
• Enumerate
• Table
• Graphic
S.Motogna - LFTC
Construct PDA
• L = {0n1n| n ≥ 1}
• States, stack, moves?
1. States:
• Initial state:q0 – beginning and process symbols ‘0’
• When first symbol ‘1’ is found – move to new state => q1
• Final: final state q2
2. Stack:
• Z0 – initial symbol
• X – to count symbols:
• When reading a symbol ’0’ – push X in stack
• When reading a symbol ‘1’ – pop X from stack
S.Motogna - LFTC
Exemple 1 (enumerate)
M = ({q0,q1,q2}, {0,1}, {Z0,X},𝜹,q0,Z0,{q2})
𝜹(q0,0,Z0) = (q0,XZ0)
𝜹(q0,0,X) = (q0,XX)
𝜹(q0,1,X) = (q1,𝜺)
𝜹(q1,1,X) = (q1,𝜺) Empty stack
𝜹(q1,𝜺,Z0) = (q2,Z0) 𝜹(q1,𝜺,Z0) = (q1, 𝜺)
⊢ (q1, 𝜺, 𝜺)
(q0,0011,Z0) ⊢ (q0,011,XZ0) ⊢ (q0,11,XXZ0) ⊢ (q1,1,XZ0) ⊢(q1, 𝜺, Z0) ⊢ (q2, 𝜺,
Z0)
S.Motogna - LFTC
Final state
Exemple 1 (table)
0 1 𝜺
Z0 q0,XZ0
q0 X q0,XX q1,𝜺
Z0 q2,Z0 (q1, 𝜺)
q1 X q1,𝜺
Z0
q2 X
push
pop
0, X➝XX
0, Z0➝XZ0 1, X➝𝜺
1, X➝𝜺 𝜺, Z0➝Z0
q0 q1 q2
S.Motogna - LFTC
Properties
Theorem 1: For any PDA M, there exists a PDA M’ such that
L 𝜺(M) = Lf(M’)
Theorem 2: For any PDA M, there exists a context free grammar such
that
L 𝜺(M) = L(G)
Theorem 3: For any context free grammar there exists a PDA M such
that
L(G) = L 𝜺(M)
S.Motogna - LFTC
HW
• Parser:
• Descendent recursive
• LL(1)
• LR(0), SLR, LR(1)
Corresponding PDA
S.Motogna - LFTC
Structure of compiler
Source program
analysis
scanning
parsing
Sequence of
tokens
semantic analysis
Syntax tree
generate intermediary
Annotated code synthesis
abstract syntax
tree optimize
Intermediary intermediary code
code
Optimized generate object Object
code program
intermediary
code
S. Motogna - LFTC
Semantic analysis
• Parsing – result: syntax tree (ST)
Example
S.Motogna - FL&CD
Semantic analysis
• Attach meanings to syntactical constructions of a program
• What:
• Identifiers -> values / how to be evaluated
• Statements -> how to be executed
• Declaration -> determine space to be allocated and location to be stored
• Examples:
• Type checkings
• Verify properties
• How:
• Attribute grammars
• Manual methods
S.Motogna - FL&CD
Attribute grammar
• Syntactical constructions (nonterminals) – attributes
∀ 𝑋 ∈ 𝑁 ∪ Σ: 𝐴(𝑋)
∀ 𝑝 ∈ 𝑃: 𝑅(𝑝)
S.Motogna - FL&CD
Definition
AG = (G,A,R) is called attribute grammar where:
S.Motogna - FL&CD
Example 1
• G = ({N,B},{0,1}, P, N}
P: N -> NB
N1.v = 2* N2.v + B.v
N -> B N.v = B.v
B -> 0 B.v = 0
B -> 1 B.v = 1
S.Motogna - FL&CD
Evaluate attributes
• Traverse the tree: can be an infinite cycle
S.Motogna - FL&CD
Steps
• What? - decide what you want to compute (type, value, etc.)
• Decide attributes:
• How many
• Which attribute is defined for which symbol
• Attach evaluation rules:
• For each production – which rule/rules
S.Motogna - FL&CD
Example 2 (L-attribute grammar)
Decl -> DeclTip ListId ListId.type = DeclTip.type
ListId -> Id Id.type = ListId.type
ListId2.type = ListId1.type
ListId -> ListId, Id
Id.type = ListId1.type
S.Motogna - FL&CD
Example 3 (S-attribute grammar)
ListDecl -> ListDecl; Decl ListDecl1.dim = ListDecl2.dim + Decl.dim
ListDecl -> Decl ListDecl.dim = Decl.dim
Decl -> Type ListId Decl.dim = Type.dim * ListId.no
Type -> int Type.dim = 4
Type.dim =8
Type -> long
ListId.no = 1
ListId -> Id ListId1.no = ListId2.no + 1
ListId -> ListId, Id
S.Motogna - FL&CD
Manual methods
• Symbolic execution
• Using control flow graph, simulate on stack how the program will behave
• [Grune – Modern Compiler Design]
S.Motogna - FL&CD
Course 12
S.Motogna - FL&CD
Structure of compiler
Source program
analysis
scanning
parsing
Sequence of
tokens
semantic analysis
Parse tree
generate intermediary
Annotated syntax code synthesis
tree
optimize
Intermediary intermediary code
code
Optimized generate object Object
code program
intermediary
code
S. Motogna - LFTC
Generate intermediary code
Limbaj 1 Maºina 1
Limbaj 2 Maºina 2
Cod
. intermediar .
. .
. .
Limbaj m Maºina n
S. Motogna - LFTC
Representations of intermediary code
• Annotated tree: intermediary code is generated in semantic analysis
• Polish postfix form:
• No parenthesis
• Operators appear in the order of execution
• Ex.: MSIL
S. Motogna - LFTC
3 address code
= sequence of simple format statements, close to object code, with the
following general form:
Represented as:
- Quadruples
- Triples
- Indirected Triples
S. Motogna - LFTC
• Quadruples:
< op > < arg1 > < arg2 > < result >
• Triples:
< op > < arg1 > < arg2 >
S. Motogna - LFTC
Special cases:
1. Expressions with unary operator: < result >=< op >< arg2 >
2. Assignment of the form a := b => the 3 addresss code is a = b (no operatorand no 2nd
argument)
3. Unconditional jump: statement is goto L, where L is the label of a 3 address code
4. Conditional jump: if c goto L: if c is evaluated to true then unconditional jump to
statement labeled with L, else (if c is evaluated to false), execute the next statement
5. Function call p(x1, x2, ..., xn) – sequence of statements: param x1, param x2 ,
param xn, call p, n
6. Indexed variables: < arg1 >,< arg2 >,< result > can be array elements of the form a[i]
7. Pointer, references: &x, ∗x
S. Motogna - LFTC
Example: b∗b−4∗a∗c
op arg1 arg2 rez
* b b t1
* 4 a t2
* t2 c t3
- t1 t3 t4
nr op arg1 arg2
(1) * b b
(2) * 4 a
(3) * (2) c
(4) - (1) (3)
S. Motogna - LFTC
Example 2
If (a<2) then a=b else a=b*b
S.Motogna - FL&CD
Optimize intermediary code
• Local optimizations:
• Perform computation at compile time – constant values
• Eliminate redundant computations
• Eliminate inaccessible code – if…then...else...
• Loop optimizations:
• Factorization of loop invariants
• Reduce the power of operations
S. Motogna - LFTC
comune C § B şi D + C § B.
D:=D+C*B
A:=D+C*B
Eliminate redundant
C:=D+C*Bcomputations
Secvenţa corespunzătoare de cod cu trei adrese, reprezentat ca tripl
este:
Example:
D:=D+C*B (1) * C B
A:=D+C*B (2) + D (1)
C:=D+C*B (3) := (2) D
(4) * C B
(5) + D (4)
(6) := (5) A
(7) * C B
(8) + D (7)
(9) := (8) C
S.Motogna - FL&CD
constă
constă ı̂n ı̂n a scoate
a scoate această
această instrucţiune
instrucţiune ı̂naintea
ı̂naintea ciclului,
ciclului, ea exe
ea executân
astfel
astfel o singură
o singură dată.
dată.
Factorization of loop invariants
Exemplul
Exemplul 6.3.
6.3. O secvenţă
O secvenţă de program
de program ceis aconţine
ceWhat
conţine un invarian
un invariant
loop invariant? de
ı̂nainte
ı̂nainte şi după
şi după optimizare:
optimizare:
for(i=0,
for(i=0, i<=n,i++)
i<=n,i++) x=y+z;
x=y+z;
{ x=y+z;
{ x=y+z; for(i=0,
for(i=0, i<=n,i++)
i<=n,i++)
a[i]=i*x}
a[i]=i*x} { a[i]=i*x}
{ a[i]=i*x}
Reducerea
Reducerea puterii
puterii operaţiilor
operaţiilor
Această optimizare
Această optimizareare are
ca scop ı̂nlocuirea
ca scop operaţiilor
ı̂nlocuirea costisitoare
operaţiilor costisit
exemplu ı̂nmulţirea)
exemplu cu operaţii
ı̂nmulţirea) maimai
cu operaţii ieftine (adunarea)
ieftine
S. Motogna - LFTC (adunarea)ı̂n definirea
ı̂n defin
V1:
P = a[0] V2:
V3
P=a[n]
For i=1 to n
3 solutions P = P*v + a[n-i]
S.Motogna - FL&CD
valoareacalculată
valoarea calculată
la la iteraţia
iteraţia ° 1.
i °i 1.
Exemplul
Exemplul 6.4.
6.4. Considerând
Considerând
Reduce the power of operations ciclul
ciclul următor,
următor, ı̂n ı̂n
carecare
v v
esteeste
un un
inv
dedeciclu,
ciclu,elelpoate
poate fi optimizat
fi optimizat astfel:
astfel:
i<=n,i++)
for(i=k,i<=n,i++)
for(i=k, t1=k*v;
t1=k*v;
t=i*v;
{ {t=i*v; for(i=k,
for(i=k, i<=n,i++)
i<=n,i++)
. . ..}.} { t=t1;
{ t=t1;
t1=t1+v;...}
t1=t1+v;...}
S. Motogna - LFTC