Download presentation

Presentation is loading. Please wait.

Published byKevin Blake Modified over 2 years ago

1
Automata Theory December 2001 NPDAPart 3:

2
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.

3
3 Reverse Polish calculator A deterministic push-down automaton that evaluates postfix expressions (like an HP calculator) has the following moves: ( q 0 is the only state). 1. (q 0, ax, ) (q 0, 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).

4
4 Reverse Polish calculator (q 0, ax, ) (q 0, 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.

5
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 negate / * – * and evaluates to –29.

6
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 v 0 = S and moves given by: 1. (q 0, x, A ) (q 0, x, ) for each rule of form A P 2. (q 0, ax, a ) (q 0, x, ) for each terminal symbol a T

7
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. (q 0, 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.

8
8 Propositional Logic Example Grammar: Automaton: (the start symbol for this grammar) Consider the formula. Successive operations on the stack which lead to this formula being accepted are shown below.

9
9 Propositional Logic - operations F F v T TA ~ A ( F ) p FT T A A r p Input RemainingStack ~ p (p r) p (p r) (p r) p r) r) ) F F T T ~A T A T p T T A ( F ) F) F T) T T) A T) p T) T) A) r) )

10
10 Algebraic expressions example P = {E E + T | E – T | T (q 0,x,E ) (q 0,x,E+T ) etc. T T * F | T / F | F (q 0,x,T ) (q 0,x,T*F ) etc. F ( E ) | a | b |... } (q 0,x,F ) (q 0,x,(E) ) etc. Also, (q 0,ax,a ) (q 0,x, ) for all a. Start symbol is E. Now lets consider the expression: a + b * (c + d).

11
11 Algebraic expressions example E E+T T F a T*F F bE E() +T T F c F d Input remainingStack a + b * (c + d) + b * (c + d) b * (c + d) * (c + d) (c + d) c + d) + d) d) ) E E + T T + T F + T a + T + T T T * F F * F b * F * F F ( E ) E) E + T) T + T) F + T) c + T + T) T) F) d) )

12
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.

13
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.

14
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 theLR parsing algorithm which is very efficient and widely used.

15
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 with a 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.

16
16

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google