CO4301 – Advanced Games Development Week 2 Introduction to Parsing

Slides:



Advertisements
Similar presentations
INFIX, PREFIX, & POSTFIX EXPRESSIONS. Infix Notation We usually write algebraic expressions like this: a + b This is called infix notation, because the.
Advertisements

COMPILER CONSTRUCTION WEEK-2: LANGUAGE DESCRIPTION- SYNTACTIC STRUCTURE:
Regular Expressions, Backus-Naur Form and Reverse Polish Notation.
Expression Trees What is an Expression tree? Expression tree implementation Why expression trees? Evaluating an expression tree (pseudo code) Prefix, Infix,
Stacks - 3 Nour El-Kadri CSI Evaluating arithmetic expressions Stack-based algorithms are used for syntactical analysis (parsing). For example.
Prefix, Postfix, Infix Notation
Arithmetic Expressions Infix form –operand operator operand 2+3 or a+b –Need precedence rules –May use parentheses 4*(3+5) or a*(b+c)
COSC 2006 Chapter 7 Stacks III
Lecture 12 – ADTs and Stacks.  Modularity  Divide the program into smaller parts  Advantages  Keeps the complexity managable  Isolates errors (parts.
Arithmetic Expressions
Topic 15 Implementing and Using Stacks
Department of Technical Education Andhra Pradesh
Reverse Polish Notation (RPN) & Stacks CSC 1401: Introduction to Programming with Java Week 14 – Lecture 2 Wanda M. Kunkle.
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.
CS 206 Introduction to Computer Science II 03 / 16 / 2009 Instructor: Michael Eckmann.
Topic 15 Implementing and Using Stacks
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Data Structures Stacks.
CS 206 Introduction to Computer Science II 10 / 28 / 2009 Instructor: Michael Eckmann.
The Stack and Queue Types Lecture 10 Hartmut Kaiser
Binary Trees. Node structure Data A data field and two pointers, left and right.
Data Structures Lecture : Stacks (Infix, Postfix and Prefix Expressions) Azhar Maqsood NUST Institute of Information Technology (NIIT)
Stack Applications.
CSC 205 Programming II Postfix Expressions. Recap: Stack Stack features Orderly linear structure Access from one side only – top item Stack operations.
(c) University of Washington20d-1 CSC 143 Java Applications of Trees.
© University of Auckland Trees CS 220 Data Structures & Algorithms Dr. Ian Watson.
Computer Science Department Data Structure & Algorithms Problem Solving with Stack.
DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 19: Stacks and Queues (part 2)
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 2)
Basic Data Structures Stacks. A collection of objects Objects can be inserted into or removed from the collection at one end (top) First-in-last-out.
Wednesday, March 2 Homework #2 is posted Homework #2 is posted Due Friday, March 4 (BOC) Due Friday, March 4 (BOC) Program #5 is posted Program #5 is posted.
CHP-3 STACKS.
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.
Stacks A stack is a linear data structure that can be accessed only at one of its ends for storing and retrieving data LIFO (Last In First Out) structure.
ADTS, GRAMMARS, PARSING, TREE TRAVERSALS Lecture 13 CS2110 – Spring
1 Data Structures and Algorithms Stack. 2 The Stack ADT Introduction to the Stack data structure Designing a Stack class using dynamic arrays Linked Stacks.
CC 215 DATA STRUCTURES MORE ABOUT STACK APPLICATIONS Dr. Manal Helal - Fall 2014 Lecture 6 AASTMT Engineering and Technology College 1.
Applications of Stack Maitrayee Mukerji. Stacks Last In First Out (LIFO List) ◦ FILO? Insertions and Deletions from the same end called the Top Push(),
Review Use of Stack Introduction Stack in our life Stack Operations
Regular Expressions, Backus-Naur Form and Reverse Polish Notation
Stacks Access is allowed only at one point of the structure, normally termed the top of the stack access to the most recently added item only Operations.
Programming Abstractions
Infix to postfix conversion
Introduction to Parsing (adapted from CS 164 at Berkeley)
Syntax Analysis Chapter 4.
Even-Even Devise a grammar that generates strings with even number of a’s and even number of b’s.
ASTs, Grammars, Parsing, Tree traversals
Stack application: postponing data usage
Stacks – Calculator Application
PART II STACK APPLICATIONS
ENERGY 211 / CME 211 Lecture 15 October 22, 2008.
Stacks, Queues, and Deques
Infix to Postfix Conversion
ADTs, Grammars, Parsing, Tree traversals
R.Rajkumar Asst.Professor CSE
ASTs, Grammars, Parsing, Tree traversals
Queue Applications Lecture 31 Mon, Apr 9, 2007.
Infix to Postfix Conversion
Computer Science 2 5/17/2016 Finish the Queue Program
Topic 15 Implementing and Using Stacks
CSC 143 Java Applications of Trees.
(Part 2) Infix, Prefix & Postfix
Queue Applications Lecture 31 Tue, Apr 11, 2006.
CO4301 – Advanced Games Development Week 3 Parsing Continued
Data Structures and Algorithms 2/2561
Context Free Grammars-II
Stacks A stack is an ordered set of elements, for which only the last element placed into the stack is accessible. The stack data type is also known as.
Presentation transcript:

CO4301 – Advanced Games Development Week 2 Introduction to Parsing Gareth Bellaby

Regular expressions

Regular expression A regular expression is a sequence of symbols and characters which define a search pattern. Typically their main use is pattern matching with strings, e.g. search for string within a text. Powerful: search and replace, variants upon a pattern, etc.

Regular expression The characters are literals. The symbols are meta-characters which describe what constitutes a match. For example, wildcard for text search: *.txt

regex C11 has introduced a library for regular expressions: <regex> By default uses the ECMAScript syntax. Based on JavaScript JavaScript aims to be compatible with ECMAScript. http://www.ecma-international.org/ecma-262/7.0/index.html http://www.cplusplus.com/reference/regex/ECMAScript/http://www.ecma-international.org/ecma-262/7.0/index.html

regex Can also set flags to follow some other common grammars: awk, grep. Set the flag when you define the expression. For a broad overview see: https://msdn.microsoft.com/en-us/library/bb982382.aspx std::regex ex("\\bword$", ECMAScript | icase);

regex regex word("[[:alpha:]]+"); Define a regular expression using: regex regex word("[[:alpha:]]+");

regex regex expression("dog"); string str = "dog"; if (regex_match(str, expression)) { cout << "match" << endl; } str = "doggy"; expression = "(dog)(.*)";

regex Two other functions commands in the library: regex_search Search for sequence replace_replace Replace matched sequence

Parsing an arithmetic operation

Parsing Pronounced with a ‘z’: “parzing”. You would parse a sentence, or you would parse an arithmetic expression. Parsing is the process of breaking a sentence or expression into its constituent parts. At the same time you determine the relationship between the parts. This is done in order to analyse the sentence or expression. The tool that does this is called a parser. The result of the evaluation is called a parse.

Applications Parsing is an important topic within computing. There are natural language parsers which break a sentence down into its parts of speech, e.g. noun, verb, noun-phrase, etc. Compilers parse program code. eXtensible Markup Language (XML) is a parsed language that defines a set of rules for encoding a text.

Arithmetic expressions The purpose of parsing is to convert input into a form that can be processed or analysed. In some cases it is also possible to automate the analysis. The parsing of arithmetic expressions is straightforward because they following a limited set of clear rules. With an arithmetic expression what is required is that we can enter an expression and then evaluate it.

Expression types Operator. An operator operates on operands, function, e.g. + - / * Operand. An operand is the number, the thing the operator is operating upon. 3 + 4 + is an operator, 3 and 4 are operands Three types of arithmetic expressions: Infix Prefix Postfix

Expression types The root term refers to the position of the operator. Infix is where the operator appears between the operands. 4 + 5 Prefix is where the operator appears before the operands. Known as "Polish notation". Avoids the need for parentheses. Used with a type of LISP expression and in certain calculators. + 4 5

Expression types Postfix is where the operator appears afterthe operands. 4 5 + Known as "Reversed Polish notation". Reverse Polish Notation was used in a number of calculators for a while. The reason for this is that it straightforward to implement the notation using a stack and it was easy to implement a stack on the existing electronic architecture.

Parsing infix notation Standard mathematics is written using infix notation. Use a tree structure in order to parse and evaluate it. Each operand becomes a leaf node. Each operator is a branching node. Lowest priority operators appear higher in the tree. Evaluate by walking from left to right, moving through the tree structure.

Parsing infix notation

Parsing postfix notation I want to start with the parsing and evaluation of postfix notation. Avoids need for parentheses. No ambiguities with precedence. Postfix is more straightforward than infix. Prefix is similar to postfix so a bit redundant to look at both of these.

Examples a b + a + b a b / a / b a b - a - b 4 2 / 3 + The result is 5 because: 4 2 / 3 + 4 / 2 = 2, put result back 2 3 + = 5

Examples 6 3 2 4 + - * 2 + 4 = 6 6 3 6 - * 3 – 6 = -3 6 -3 * 6 * -3 = -18 -18 3 6 + 4 * 2 / 3 + 6 = 9 9 4 * 2 / 9 * 4 = 36 36 2 / 36 / 2 = 18 18

Using a stack The arithmetic expression is read from left to right. Use a stack in order to process each separate operation. Operands (numbers) are pushed onto the stack and popped from the stack. The operators are used to perform an arithmetic operation on the operands popped from the stack. The result of an operation is pushed on to the stack.

Algorithm Parse from left to right. While input: If an operand is read, push it on to the stack. If an operator is read, pop the top two operands from the stack. Perform the given operation on them. Push the result on to the stack. If no more input, push the result from the stack. Note: no error conditions!

Example 1 Infix: 2 * 4 + 1 + 3 * 5 Postfix: 2 4 * 1 + 3 5 * + Answer is 24

Example 1

Examples Infix: ( a * b ) - c Postfix: a b * c - Infix: ( a * b ) / c Infix: a - (b + c) * d Postfix: a b c + d * -

Examples Expression is: 2 3 + 1 4 - * 2 3 + 1 4 - * first operator (2 3 +) 1 4 - * 5 1 4 - * next operator 5 (1 4 -) * 5 -3 * next operator (5 -3 *) -15

Examples 6 3 2 4 + - * 2 + 4 6 3 6 - * 3 - 6 6 -3 * 6 * -3 -18 6 3 2 4 + - * 2 + 4 6 3 6 - * 3 - 6 6 -3 * 6 * -3 -18 3 6 + 4 * 2 / 3 + 6 9 4 * 2 / 9 * 4 36 2 / 36 / 2 18

Examples 3 2 + 6 4 - * 3 + 2 5 6 4 - * 6 - 4 5 2 * 5 * 2 10 6 4 3 3 1 - + * + 3 - 1 6 4 3 2 + * + 3 + 2 6 4 5 * + 4 * 5 6 20 + 6 + 20 26

Examples 6 4 + 3 3 1 - * + 6 + 4 10 3 3 1 - * + 3 - 1 10 3 2 * + 3 * 2 6 4 + 3 3 1 - * + 6 + 4 10 3 3 1 - * + 3 - 1 10 3 2 * + 3 * 2 10 6 + 10 + 6 16