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

Slides:



Advertisements
Similar presentations
Automata Theory Part 1: Introduction & NFA November 2002.
Advertisements

Programming Language Concepts
CSCI 3130: Formal Languages and Automata Theory Tutorial 5
How to convert a left linear grammar to a right linear grammar
Theory of Computation CS3102 – Spring 2014 A tale of computers, math, problem solving, life, love and tragic death Nathan Brunelle Department of Computer.
C O N T E X T - F R E E LANGUAGES ( use a grammar to describe a language) 1.
Mooly Sagiv and Roman Manevich School of Computer Science
Introduction to Computability Theory
1 Introduction to Computability Theory Lecture7: PushDown Automata (Part 1) Prof. Amos Israeli.
1 Introduction to Computability Theory Lecture5: Context Free Languages Prof. Amos Israeli.
1 … NPDAs continued. 2 Pushing Strings Input symbol Pop symbol Push string.
Costas Busch - RPI1 NPDAs Accept Context-Free Languages.
Courtesy Costas Busch - RPI1 NPDAs Accept Context-Free Languages.
1 Converting NPDAs to Context-Free Grammars. 2 For any NPDA we will construct a context-free grammar with.
Fall 2004COMP 3351 NPDA’s Accept Context-Free Languages.
Postfix notation. About postfix notation Postfix, or Reverse Polish Notation (RPN) is an alternative to the way we usually write arithmetic expressions.
January 14, 2015CS21 Lecture 51 CS21 Decidability and Tractability Lecture 5 January 14, 2015.
1 Pushdown Automata PDAs. 2 Pushdown Automaton -- PDA Input String Stack States.
Class 4: Stacks. cis 335 Fall 2001 Barry Cohen What is a stack? n A stack is an ordered sequence of items, of which only the last (‘top’) item can be.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 7 Mälardalen University 2010.
Pushdown Automata (PDAs)
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 11 Midterm Exam 2 -Context-Free Languages Mälardalen University 2005.
Pushdown Automata Chapters Generators vs. Recognizers For Regular Languages: –regular expressions are generators –FAs are recognizers For Context-free.
1Computer Sciences Department. Book: INTRODUCTION TO THE THEORY OF COMPUTATION, SECOND EDITION, by: MICHAEL SIPSER Reference 3Computer Sciences Department.
PZ03A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ03A - Pushdown automata Programming Language Design.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Foundations of (Theoretical) Computer Science Chapter 2 Lecture Notes (Section 2.2: Pushdown Automata) Prof. Karen Daniels, Fall 2010 with acknowledgement.
CSC312 Automata Theory Lecture # 26 Chapter # 12 by Cohen Context Free Grammars.
Formal Languages, Automata and Models of Computation
Overview of Previous Lesson(s) Over View  A token is a pair consisting of a token name and an optional attribute value.  A pattern is a description.
1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 8 Mälardalen University 2011.
1 Chapter Pushdown Automata. 2 Section 12.2 Pushdown Automata A pushdown automaton (PDA) is a finite automaton with a stack that has stack operations.
CS 154 Formal Languages and Computability March 15 Class Meeting Department of Computer Science San Jose State University Spring 2016 Instructor: Ron Mak.
Theory of Computation Automata Theory Dr. Ayman Srour.
Lecture 11  2004 SDU Lecture7 Pushdown Automaton.
5. Context-Free Grammars and Languages
Formal Languages, Automata and Models of Computation
Pushdown Automata.
PDAs Accept Context-Free Languages
Non Deterministic Automata
CSE 105 theory of computation
Deterministic FA/ PDA Sequential Machine Theory Prof. K. J. Hintz
Linear Bounded Automata LBAs
Pushdown automata Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Even-Even Devise a grammar that generates strings with even number of a’s and even number of b’s.
NPDAs Accept Context-Free Languages
CSE 105 theory of computation
Pushdown Automata PDAs
PDAs Accept Context-Free Languages
Chapter 7 PUSHDOWN AUTOMATA.
Bottom-Up Syntax Analysis
NPDAs Accept Context-Free Languages
فصل دوم Context-Free Languages
CSE 105 theory of computation
4b Lexical analysis Finite Automata
Theory of Computation Lecture #
Pushdown automata Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Compiler Construction
… NPDAs continued.
CSE 105 theory of computation
Pushdown automata Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Pushdown automata The Chinese University of Hong Kong Fall 2011
Pushdown automata Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Context Free Grammars-II
CSE 105 theory of computation
Pushdown automata Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Normal Forms for Context-free Grammars
Pushdown automata Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
CSE 105 theory of computation
CSE 105 theory of computation
Presentation transcript:

Automata Theory December 2001 NPDAPart 3:

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: ( 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 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 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 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 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 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 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 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 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 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 theLR 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