automata theory december 2001 npdapart 3:. 2 npda example example: a calculator for reverse polish...

16
Automata Theory December 2001 NPDA Part 3:

Upload: kevin-blake

Post on 28-Mar-2015

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Automata Theory December 2001 NPDAPart 3:. 2 NPDA example Example: a calculator for Reverse Polish expressions Infix expressions like: a + log((b + c)/d)

Automata Theory

December 2001

NPDAPart 3:

Page 2: Automata Theory December 2001 NPDAPart 3:. 2 NPDA example Example: a calculator for Reverse Polish expressions Infix expressions like: a + log((b + c)/d)

2

NPDA example

Example: a calculator for “Reverse Polish” expressions

Infix expressions like:

a + log((b + c)/d)

are very familiar but can not be simply evaluated from left to right. Operators like + occur in between their operands.

Postfix expressions like:

b c + d / log a +

are less familiar but can be simply evaluated from left to right. Operators always occur immediately after their operands.

Page 3: Automata Theory December 2001 NPDAPart 3:. 2 NPDA example Example: a calculator for Reverse Polish expressions Infix expressions like: a + log((b + c)/d)

3

“Reverse Polish” calculator

A deterministic push-down automaton that evaluates postfix expressions (like an HP calculator) has the following moves:

( q0 is the only state).

1. (q0, ax, ) (q0, x, a) where a is any number,

2. (q0, $x, ab) (q0, x, c) where a,b,c are numbers, $ is a binary operator such as

+, –, * or /, and the result c in infix notation is c = a $ b,

3. (q0, #x, a) (q0, x, c) where a,c are numbers, # is a unary operator such as

negate, sin, cos, log or exp, and the result c is given by c = #(a).

Page 4: Automata Theory December 2001 NPDAPart 3:. 2 NPDA example Example: a calculator for Reverse Polish expressions Infix expressions like: a + log((b + c)/d)

4

“Reverse Polish” calculator

(q0, ax, ) (q0, x, a) a number in the input stream (or a variable representing a number) is copied to the top of the stack, so it joins onto on the left.

(q0, $x, ab) (q0, x, c) a binary operator in the input stream causes the top two numbers on the stack to be pulled and the operator applied to them (note that this is order-dependent if the operator is – or /) and the result pushed onto the stack.

(q0, #x, a) (q0, x, c) a unary operator in the input stream is applied to the number on top of the stack and the result replaces that number.

Page 5: Automata Theory December 2001 NPDAPart 3:. 2 NPDA example Example: a calculator for Reverse Polish expressions Infix expressions like: a + log((b + c)/d)

5

“Reverse Polish” calculator

In postfix expressions there is no need for brackets but the unary minus (sign change) must be distinguished from the binary minus (subtraction).

E.g.,

(8 + 3 / (-4)) * (6 - 2*5)

becomes

8 3 4 negate / + 6 2 5 * – *

and evaluates to –29.

Page 6: Automata Theory December 2001 NPDAPart 3:. 2 NPDA example Example: a calculator for Reverse Polish expressions Infix expressions like: a + log((b + c)/d)

6

Equivalence of context-free grammars and NPDAs

Given any context-free grammar G = (N,T,P,S) there is an equivalent

NPDA = (Q,T,V,g) with

Q = {q0}

V = N T with v0 = S

and moves given by:

1. (q0, x, A) (q0, x, ) for each rule of form A P

2. (q0, ax, a) (q0, x, ) for each terminal symbol a T

Page 7: Automata Theory December 2001 NPDAPart 3:. 2 NPDA example Example: a calculator for Reverse Polish expressions Infix expressions like: a + log((b + c)/d)

7

Equivalence of context-free grammars and NPDAs

Thus, if a non-terminal symbol A is on top of the stack, it must be replaced by all the symbols on the right-hand-side of a rule which has A on the left-hand-side, but the choice of rule is not determined. If a terminal symbol a is on top of the stack and if it matches the next input symbol then both are dropped. If there is a mismatch between the next input symbol and a terminal symbol on top of the stack then no move is possible and the machine is stuck,

i.e. (q0, ax, b) were a, b T and a b.

It is assumed that the machine always makes the right choices of rule and therefore avoids getting stuck. If there is no way of avoiding it then the string has to be rejected.

Page 8: Automata Theory December 2001 NPDAPart 3:. 2 NPDA example Example: a calculator for Reverse Polish expressions Infix expressions like: a + log((b + c)/d)

8

Propositional Logic Example

Grammar:

Automaton:

(the start symbol for this grammar)

1.

2.

Consider the formula . Successive operations on the stack which lead to this formula being accepted are shown below.

N{F,T,A}

T p r {&, , ,~,(,), , , }

P

p r

{F T F&T F T F T (Formula)T A ~A (Term)

A (F) (Atom)

Q q{ }0

V N T v with F0

( , , ) ( , , ) ( , , ) ( , , ) ( , , )q x q x q x q x q x0 0 0 0 0F T F&T F T F T

( , , ) ( , , ) ( , ,~ )q x q x q x0 0 0T A A

( , , ) ( , ,( ) ) ( , , ) ( , , )q x q x q x p q x r0 0 0 0A F

( , , ) ( , , )q axa q x a T0 0 for all

~ ( )p p r

Page 9: Automata Theory December 2001 NPDAPart 3:. 2 NPDA example Example: a calculator for Reverse Polish expressions Infix expressions like: a + log((b + c)/d)

9

Propositional Logic - operations

F

F v T

T A

~ A ( F )

p F T

T

A

A

r

p

Input Remaining Stack~ p (p r)~ p (p r)~ p (p r)~ p (p r)

p (p r)p (p r)

(p r)(p r)(p r)(p r)p r)p r)p r)p r)p r)

r) r)r)r))

FF TT T

~A TA Tp T

TTA

( F )F)

F T)T T)A T)p T)

T)T)A)r))

Page 10: Automata Theory December 2001 NPDAPart 3:. 2 NPDA example Example: a calculator for Reverse Polish expressions Infix expressions like: a + log((b + c)/d)

10

Algebraic expressions example

P = { E E + T | E – T | T (q0,x,E) (q0,x,E+T) etc.

T T * F | T / F | F (q0,x,T) (q0,x,T*F) etc.

F ( E ) | a | b | . . . } (q0,x,F) (q0,x,(E)) etc.

Also,

(q0,ax,a) (q0,x, ) for all a.

Start symbol is E.

Now let’s consider the expression: a + b * (c + d).

Page 11: Automata Theory December 2001 NPDAPart 3:. 2 NPDA example Example: a calculator for Reverse Polish expressions Infix expressions like: a + log((b + c)/d)

11

Algebraic expressions example

E

E + T

T

F

a

T * F

F

b E

E( )

+ T

T

F

c

F

d

Input remaining Stack

a + b * (c + d)

+ b * (c + d)b * (c + d)

* (c + d)(c + d)

c + d)

+ d) d)

)

EE + TT + TF + Ta + T

+ TT

T * FF * Fb * F

* FF

( E )E)

E + T)T + T)F + T)c + T+ T)

T)F)d)

)

Page 12: Automata Theory December 2001 NPDAPart 3:. 2 NPDA example Example: a calculator for Reverse Polish expressions Infix expressions like: a + log((b + c)/d)

12

Algebraic expressions example

The sequence of rules used by the automaton generates the parse tree, from which assembly code can be inferred.

The automaton is assumed to make the correct choice of rule at each point

- a choice is necessary since it is non-deterministic.

Page 13: Automata Theory December 2001 NPDAPart 3:. 2 NPDA example Example: a calculator for Reverse Polish expressions Infix expressions like: a + log((b + c)/d)

13

Non-determinism of NPDA

This non-determinism of NPDA is unsatisfactory in practice.

For finite automata,

• from an arbitrary NFA (equivalent to a regular grammar) we can always construct an equivalent DFA (see last lecture).

But for push-down automata this is not true:

• from an arbitrary NPDA (equivalent to a context-free grammar) we can not always construct an equivalent deterministic push-down automaton.

Page 14: Automata Theory December 2001 NPDAPart 3:. 2 NPDA example Example: a calculator for Reverse Polish expressions Infix expressions like: a + log((b + c)/d)

14

Possible solutions

1.Use a second stack to record the choices made. The system can then go back and try alternatives when the parse fails — leads to the “top-down backtrack parsing” algorithm, very inefficient because a lot of work is duplicated.

2.Embed the non-determinism into a richer set of states using the NFA DFA procedure - this leads to the “LR parsing” algorithm which is very efficient and widely used.

Page 15: Automata Theory December 2001 NPDAPart 3:. 2 NPDA example Example: a calculator for Reverse Polish expressions Infix expressions like: a + log((b + c)/d)

15

Summary of Grammars and Automata

1.Computer languages (and much of natural language) can be modelled by context-free grammars (CFG).

2.From the CFG we can infer an equivalent non-deterministic push-down automaton (NPDA), and enhance this into a deterministic parsing algorithm (such as LR).

3.A compiler does this, along witha finite automaton (regular grammar) for identifying

numbers, variable names, procedure names, standard functions, keywords etc., within program text,

a code generator/optimiser for producing executables.

4.A natural-language interface does this along with dialogue management and knowledge-base inference systems.

Page 16: Automata Theory December 2001 NPDAPart 3:. 2 NPDA example Example: a calculator for Reverse Polish expressions Infix expressions like: a + log((b + c)/d)

16