Automata Theory - PowerPoint PPT Presentation

1 / 16
About This Presentation
Title:

Automata Theory

Description:

A deterministic push-down automaton that evaluates postfix expressions (like an ... The automaton is assumed to make the correct choice of rule at each point ... – PowerPoint PPT presentation

Number of Views:566
Avg rating:3.0/5.0
Slides: 17
Provided by: mt76
Category:

less

Transcript and Presenter's Notes

Title: Automata Theory


1
Automata Theory
NPDA
Part 3
December 2001
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
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).

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.

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 - 25)
    becomes 8 3 4 negate / 6 2 5
    and evaluates to 29.

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

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.

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.
9
Propositional Logic - operations
10
Algebraic expressions example
  • P E ? E T E T T (q0,x,E?) ?
    (q0,x,ET?) etc.
  • T ? T F T / F F (q0,x,T?) ?
    (q0,x,TF?) 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 lets consider the expression a b (c
    d).

11
Algebraic expressions example
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
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
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.

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
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com