Regular Expressions, Backus-Naur Form and Reverse Polish Notation.

Slides:



Advertisements
Similar presentations
Programming Language Concepts
Advertisements

ICE1341 Programming Languages Spring 2005 Lecture #6 Lecture #6 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University.
C O N T E X T - F R E E LANGUAGES ( use a grammar to describe a language) 1.
Stacks & Their Applications COP Stacks  A stack is a data structure that stores information arranged like a stack.  We have seen stacks before.
COMP-421 Compiler Design Presented by Dr Ioanna Dionysiou.
CS252: Systems Programming
Stacks - 3 Nour El-Kadri CSI Evaluating arithmetic expressions Stack-based algorithms are used for syntactical analysis (parsing). For example.
Chapter Chapter Summary Languages and Grammars Finite-State Machines with Output Finite-State Machines with No Output Language Recognition Turing.
CS 330 Programming Languages 09 / 13 / 2007 Instructor: Michael Eckmann.
Infix, Postfix, Prefix.
Reverse Polish Expressions Some general observations about what they are and how they relate to infix expressions. These 9 slides provide details about.
Postfix notation. About postfix notation Postfix, or Reverse Polish Notation (RPN) is an alternative to the way we usually write arithmetic expressions.
Chapter 3 Describing Syntax and Semantics Sections 1-3.
CS 206 Introduction to Computer Science II 03 / 16 / 2009 Instructor: Michael Eckmann.
Chapter 3 Describing Syntax and Semantics Sections 1-3.
Regular Language & Expressions. Regular Language A regular language is one that a finite state machine (fsm) will accept. ‘Alphabet’: {a, b} ‘Rules’:
Chapter 2 Syntax A language that is simple to parse for the compiler is also simple to parse for the human programmer. N. Wirth.
Copyright © Cengage Learning. All rights reserved.
Data Structures Lecture : Stacks (Infix, Postfix and Prefix Expressions) Azhar Maqsood NUST Institute of Information Technology (NIIT)
Comp 245 Data Structures Stacks. What is a Stack? A LIFO (last in, first out) structure Access (storage or retrieval) may only take place at the TOP NO.
Stack Applications.
Computer Science Department Data Structure & Algorithms Problem Solving with Stack.
Winter 2007SEG2101 Chapter 71 Chapter 7 Introduction to Languages and Compiler.
1 INFO 2950 Prof. Carla Gomes Module Modeling Computation: Language Recognition Rosen, Chapter 12.4.
A sentence (S) is composed of a noun phrase (NP) and a verb phrase (VP). A noun phrase may be composed of a determiner (D/DET) and a noun (N). A noun phrase.
Languages, Grammars, and Regular Expressions Chuck Cusack Based partly on Chapter 11 of “Discrete Mathematics and its Applications,” 5 th edition, by Kenneth.
CS 331, Principles of Programming Languages Chapter 2.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
PART I: overview material
LANGUAGE DESCRIPTION: SYNTACTIC STRUCTURE
C H A P T E R TWO Syntax and Semantic.
TextBook Concepts of Programming Languages, Robert W. Sebesta, (10th edition), Addison-Wesley Publishing Company CSCI18 - Concepts of Programming languages.
Copyright © Curt Hill Languages and Grammars This is not English Class. But there is a resemblance.
CMSC 330: Organization of Programming Languages Context-Free Grammars.
Context Free Grammars.
*. zero or more of the preceeding character. A* = blank (empty string), A, AA, AAA,…
The College of Saint Rose CIS 433 – Programming Languages David Goldschmidt, Ph.D. from Concepts of Programming Languages, 9th edition by Robert W. Sebesta,
ISBN Chapter 3 Describing Syntax and Semantics.
CS 331, Principles of Programming Languages Chapter 2.
1Computer Sciences Department. Book: INTRODUCTION TO THE THEORY OF COMPUTATION, SECOND EDITION, by: MICHAEL SIPSER Reference 3Computer Sciences Department.
CHP-3 STACKS.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
Programming Languages and Design Lecture 2 Syntax Specifications of Programming Languages Instructor: Li Ma Department of Computer Science Texas Southern.
Prefix, Postfix, Infix Notation. Infix Notation  To add A, B, we write A+B  To multiply A, B, we write A*B  The operators ('+' and '*') go in between.
1 A well-parenthesized string is a string with the same number of (‘s as )’s which has the property that every prefix of the string has at least as many.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 1 Ahmed Ezzat.
Regular Expressions, Backus-Naur Form and Reverse Polish Notation
Chapter 3 – Describing Syntax
Describing Syntax and Semantics
System Software Unit-1 (Language Processors) A TOY Compiler
Syntax Specification and Analysis
Classification of Languages
Even-Even Devise a grammar that generates strings with even number of a’s and even number of b’s.
CO4301 – Advanced Games Development Week 2 Introduction to Parsing
Week 14 - Friday CS221.
Stacks Chapter 4.
Stack application: postponing data usage
STACK CHAPTER 03 Developed By :- Misha Ann Alexander Data Structures.
Visit for more Learning Resources
PART II STACK APPLICATIONS
3.5 Programming paradigms
Programming Language Syntax 2
ENERGY 211 / CME 211 Lecture 15 October 22, 2008.
Chapter 7 Regular Grammars
R.Rajkumar Asst.Professor CSE
Theory of Computation Languages.
High-Level Programming Language
Context Free Grammars-II
COMPILER CONSTRUCTION
Presentation transcript:

Regular Expressions, Backus-Naur Form and Reverse Polish Notation

Starter What does the expression 3+4*5 evaluate to? The answer is 23 Why is the answer not 35?

Objectives To form simple regular expressions for string manipulation and matching To be able to check language syntax by referring to BNF or syntax diagrams To be able to convert simple infix notation to reverse Polish notation and vice versa

Natural Language A natural language comprises a set of words written and spoken by humans Governed by syntax rules that define the order in which words may be put together – “Work students young hard clever”  is an invalid construct – “Clever young students work hard” Governed by semantic rules which define the actual meaning when applied to real world concepts – “The peanut ate the monkey” is a valid construct but lacks meaning – “The monkey ate the peanut” Constructing rules for a natural language is difficult and this is why we use formal languages to communicate with computer systems

Formal Language Used for the precise definition of syntactically correct programs for a programming language Defined using an alphabet and syntax rules The alphabet is defined as a finite set of symbols The rules of syntax define how to construct strings of the language out of symbols It is not feasible to list all the strings. Instead Regular expressions are used.

Regular Expressions Is a way of expressing the language that a machine can use in a formal manner They provide a means of identifying if a string of characters is allowed in a particular language They can be represented using a finite state machine Used extensively in operating systems for pattern matching in commands, searching for information using Google and search and replace in word processors

Regular Expressions If the alphabet of some formal language is {a, b} then here are some regular expressions: a is a regular expression that matches a string consisting of the symbol a ab matches a string consisting of the symbol a followed by b a* matches a string consisting of zero or more a’s

Regular Expression Example Define the syntax of a formal language with alphabet {a, b, c} in which valid strings consist of at least one a or at least one b, followed by two c’s.

Q1 A language L is defined by the alphabet {a, b, c} together with the regular expression (a|c)+bb. (a) Explain what represents a valid string in L. A non-empty string consisting of any combination of as and cs, terminated by two bs. (b) Give two examples of valid strings in L. aaccbb, cacccbb

Metacharacters | separates alternatives e.g. a |b can match a or b ? indicates there is zero or one of the preceding element * indicates there is zero or more of the preceding element + indicates there is one or more of the preceding element [ab] means a or b [a-z] matches all 26 lower case letters n[^t] means an n followed by a character that is not a t

Worked Examples 1.Write down the strings defined by the regular expression b[ea]d? be, ba, bed, bad 2.Write down the strings defined by the regular expression 10*1 11, 101, 1001, 10001,... 3.Write down the strings defined by the regular expression , 1001, 10001,...

Backus-Naur Form (BNF) Some languages cannot be defined by regular expressions BNF allows representations of a wider range of languages Expresses the rules for constructing valid strings in a regular language Defines the terms of the language, characters, words and symbols

BNF Notation ::= means ‘is defined as’ | separates alternatives on the right-hand side of the rule ::= 0|1|2|3|4|5|6|7|8|9 Recursive definition ::= | Syntax of a programming language ::= ::= | | ::= |etc ::= + | -

Syntax Diagrams Alternative way of defining the syntax rules of a language See examples in book pg 65-66

Syntax Diagrams BNF of Select command Syntax diagram showing Select command

Reverse Polish Notation (RPN) We evaluate arithmetic expressions using infix notation e.g An alternative is prefix notation where the operator occurs before the operands e.g Postfix is where the operator occurs after the operands e.g Postfix notation is also called RPN RPN is used in scientific calculators

Examples of Infix and Postfix Expressions InfixPostfix x – yxy- (a+b)/(a-b)ab+ab-/ x+(y^2)xy2^+ 5+((1+2)*4) *+3- Infix means normal mathematical expressions and Postfix means RPN expressions Stacks are used in the evaluation of RPN expressions

Advantages of RPN RPN expressions do not need brackets to show the order of evaluation. This makes them simpler to evaluate by a machine If you try to use an infix (normal) calculator, there will be a limit on the length of expression that can be entered Postfix expressions can be evaluated as they are entered, they are not limited to a certain number of operators in an expression

Evaluating a Postfix Expression The infix expression 5+((1+2)*4)-3 can be written like this in RPN: * InputOperationStackComment 5Push operand5 1 5, 1 2Push operand5, 1, 2 +Add5, 3Pop two values (1, 2) and push result (3) 4Push operand5, 3, 4 *Multiply5, 12Pop two values (3, 4) and push result (12) +Add17Pop two values (5, 12) and push result (17) 3Push operand17, 3 -Subtract14Pop two values (17, 3) and push result (14)

BNF is used by compiler writers to express the syntax of a programming language. The syntax for part of one such language is written in BNF as follows: ::= | ::= 0|1|2|3|4|5|6|7|8|9 ::= +|-|*|/ (a) Do the following expressions conform to this grammar? (b) (i) Express the infix expression 5+6*2 in RPN. (ii) Give one advantage of RPN ExpressionYes/No 14*9 28+6/2 3-6*2 4(4+5)*5

Regular Expressions Class work 1.Complete Q3 Regular expressions in the worksheet. 2.Download and install the regular expression tool Regex Coach and try the regular expressions: a+ a* (ac)* a(a|b)* 1(1│0)*0(1│0)*1

Class work and Homework 1.June 2010 COMP3 Q4 2.June 2011 COMP3 Q9 3.Watch this You Tube video which demonstrates how to convert the infix expression to postfix expression using a Stackvideo 4.June 2011 COMP3 Q5 Hand-in Monday 23 rd April 2012