ITEC2620 Introduction to Data Structures

Similar documents
Automata Theory TEST 1 Answers Max points: 156 Grade basis: 150 Median grade: 81%

ITEC2620 Introduction to Data Structures

Data Compression Fundamentals

Compiler Construction

JNTUWORLD. Code No: R

Glynda, the good witch of the North

Chapter 4: Regular Expressions

Notes for Comp 454 Week 2

Chapter 18: Decidability

Assignment No.4 solution. Pumping Lemma Version I and II. Where m = n! (n-factorial) and n = 1, 2, 3

Chapter 3: Describing Syntax and Semantics. Introduction Formal methods of describing syntax (BNF)

CMPSCI 250: Introduction to Computation. Lecture #28: Regular Expressions and Languages David Mix Barrington 2 April 2014

CS402 Theory of Automata Solved Subjective From Midterm Papers. MIDTERM SPRING 2012 CS402 Theory of Automata

CMSC 330 Practice Problem 4 Solutions

This book is licensed under a Creative Commons Attribution 3.0 License

CMSC 330, Fall 2009, Practice Problem 3 Solutions

Lecture 8: Context Free Grammars

Plan for Today. Regular Expressions: repetition and choice. Syntax and Semantics. Context Free Grammars

Proof Techniques Alphabets, Strings, and Languages. Foundations of Computer Science Theory

Bayesian Networks and Decision Graphs

1. Which of the following regular expressions over {0, 1} denotes the set of all strings not containing 100 as a sub-string?

CSCE 314 Programming Languages

UNIT I PART A PART B

Context-Free Languages. Wen-Guey Tzeng Department of Computer Science National Chiao Tung University

Context-Free Languages. Wen-Guey Tzeng Department of Computer Science National Chiao Tung University

CHAPTER TWO LANGUAGES. Dr Zalmiyah Zakaria

Introduction to Syntax Analysis

Multiple Choice Questions

Architecture of Compilers, Interpreters. CMSC 330: Organization of Programming Languages. Front End Scanner and Parser. Implementing the Front End

Formal Languages. Grammar. Ryan Stansifer. Department of Computer Sciences Florida Institute of Technology Melbourne, Florida USA 32901

CS 403 Compiler Construction Lecture 3 Lexical Analysis [Based on Chapter 1, 2, 3 of Aho2]

LALR Parsing. What Yacc and most compilers employ.

Introduction to Syntax Analysis. The Second Phase of Front-End

Part 3. Syntax analysis. Syntax analysis 96

CSE 311 Lecture 21: Context-Free Grammars. Emina Torlak and Kevin Zatloukal

COP 3402 Systems Software Syntax Analysis (Parser)

CSE 3302 Programming Languages Lecture 2: Syntax

Where We Are. CMSC 330: Organization of Programming Languages. This Lecture. Programming Languages. Motivation for Grammars

Languages and Strings. Chapter 2

CMSC 330: Organization of Programming Languages. Architecture of Compilers, Interpreters

CMSC 330: Organization of Programming Languages. Context Free Grammars

If you are going to form a group for A2, please do it before tomorrow (Friday) noon GRAMMARS & PARSING. Lecture 8 CS2110 Spring 2014

CPS 506 Comparative Programming Languages. Syntax Specification

Context-Free Languages. Wen-Guey Tzeng Department of Computer Science National Chiao Tung University

CS402 - Theory of Automata FAQs By

Chapter 3: CONTEXT-FREE GRAMMARS AND PARSING Part 1

Dr. D.M. Akbar Hussain

Regular Expressions. Lecture 10 Sections Robb T. Koether. Hampden-Sydney College. Wed, Sep 14, 2016

Introduction to Parsing

CMSC 330: Organization of Programming Languages

Lec-5-HW-1, TM basics

CMSC 330: Organization of Programming Languages. Context Free Grammars

CS 3100 Models of Computation Fall 2011 This assignment is worth 8% of the total points for assignments 100 points total.

KHALID PERVEZ (MBA+MCS) CHICHAWATNI

DVA337 HT17 - LECTURE 4. Languages and regular expressions

CMPT 755 Compilers. Anoop Sarkar.

ADTS, GRAMMARS, PARSING, TREE TRAVERSALS

CMSC 330: Organization of Programming Languages. Context-Free Grammars Ambiguity

Welcome! CSC445 Models Of Computation Dr. Lutz Hamel Tyler 251

Lecture 10 Parsing 10.1

CS 493: Algorithms for Massive Data Sets Dictionary-based compression February 14, 2002 Scribe: Tony Wirth LZ77

CMSC 330: Organization of Programming Languages

ADTS, GRAMMARS, PARSING, TREE TRAVERSALS. Regrades 10/6/15. Prelim 1. Prelim 1. Expression trees. Pointers to material

Skyup's Media. PART-B 2) Construct a Mealy machine which is equivalent to the Moore machine given in table.

Context-Free Grammars

a. Determine the EPS, FIRST and FOLLOWs set for this grammar.

PART 3 - SYNTAX ANALYSIS. F. Wotawa TU Graz) Compiler Construction Summer term / 309

Principles of Programming Languages COMP251: Syntax and Grammars

Parsing. Note by Baris Aktemur: Our slides are adapted from Cooper and Torczon s slides that they prepared for COMP 412 at Rice.

CS308 Compiler Principles Syntax Analyzer Li Jiang

CSCE 314 TAMU Fall CSCE 314: Programming Languages Dr. Flemming Andersen. Functional Parsers

CS375: Logic and Theory of Computing

CSE au Final Exam Sample Solution

LANGUAGE PROCESSORS. Presented By: Prof. S.J. Soni, SPCE Visnagar.

CS412/413. Introduction to Compilers Tim Teitelbaum. Lecture 2: Lexical Analysis 23 Jan 08

Context-Free Grammars and Languages (2015/11)

Principles of Programming Languages COMP251: Syntax and Grammars

CS Lecture 2. The Front End. Lecture 2 Lexical Analysis

2. λ is a regular expression and denotes the set {λ} 4. If r and s are regular expressions denoting the languages R and S, respectively

CMSC 330: Organization of Programming Languages

Week 2: Syntax Specification, Grammars

CS 314 Principles of Programming Languages

ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών

Dr. D.M. Akbar Hussain

Parsing II Top-down parsing. Comp 412

Formal Languages and Compilers Lecture VI: Lexical Analysis

CSCE 314 Programming Languages. Functional Parsers

Key to Homework #8. (a) S aa A bs bbb (b) S AB aaab A aab aaaaaaab B bba bbbb

Grammars & Parsing. Lecture 12 CS 2112 Fall 2018

Context-Free Grammars

Syntax Analysis. Prof. James L. Frankel Harvard University. Version of 6:43 PM 6-Feb-2018 Copyright 2018, 2015 James L. Frankel. All rights reserved.

CS 314 Principles of Programming Languages. Lecture 3

QUESTION BANK. Formal Languages and Automata Theory(10CS56)

Context-Free Languages. Wen-Guey Tzeng Department of Computer Science National Chiao Tung University

Goals: Define the syntax of a simple imperative language Define a semantics using natural deduction 1

CMPSCI 250: Introduction to Computation. Lecture #36: State Elimination David Mix Barrington 23 April 2014

Introduction. Introduction. Introduction. Lexical Analysis. Lexical Analysis 4/2/2019. Chapter 4. Lexical and Syntax Analysis.

1. The output of lexical analyser is a) A set of RE b) Syntax Tree c) Set of Tokens d) String Character

Formal Languages and Compilers Lecture V: Parse Trees and Ambiguous Gr

Fall Compiler Principles Lecture 3: Parsing part 2. Roman Manevich Ben-Gurion University

Transcription:

ITEC2620 Introduction to Data Structures Lecture 9b Grammars I Overview How can a computer do Natural Language Processing? Grammar checking? Artificial Intelligence Represent knowledge so that brute force computation can achieve a result Result appears like intelligence Dictionary Definitions I Language The special terms used by/for a specific application Grammar Rules about using words in a language Words Any set of symbols/characters which represent a unit of meaning Dictionary Definitions II Grammars use words Grammars are the rules which build units of meaning into larger ideas Grammars define languages Rules of a language 1

Example I Mathematical expressions Words numbers and operators Grammar rules for a valid equation Internal operator Post-operator (reverse Polish notation) Language physics is spoken in the language of math Example II How does JAVA know if an equation is valid? Need a systematic method to parse (analyze grammatically) an equation and determine if it is valid i.e. follows the rules of the grammar Definitions Tokens or Terminals T = set of symbols that can appear in the input Sentence A possible sequence (up to infinite length) of the terminals Details T i The set of all sentences with i tokens T 0 = { Φ } The null sentence T j = T x T j-1 Set product T * = j=0u T j All possible sentences 2

Simple Example I T = { a, b } Imagine a keyboard with two characters a and b All sentences will be a sequence of a s & b s Imagine a monkey using this keyboard What are all the sentences it could type? Simple Example II T 0 = { Φ } T 1 = { a, b } T 2 = { aa, ab, ba, bb } T 3 = { aaa, aab, aba, abb, baa, bab, bba, bbb } T 4 = { } Simple Example III T * = { Φ, a, b, aa, ab, ba, bb, aaa, aab, aba, abb, baa, bab, bba, bbb, } The possible sentences for T = { a, b }: The (infinite) set of strings that consist of a s & b s (or the empty string/null sentence) A Better Example T = { (, ), +, -, *, /, a...z, 0...9 } T * represents all possible sequences Things that a grade 2 student could come up with Not all will be valid arithmetic expressions The grammar of arithmetic expressions defines which sequences are valid sentences in the language of math 3

Formal Definitions I A Grammar G consists of Terminals Non-terminals Start symbol Productions G = ( T, N, S, P ) Formal Definitions II Terminals (T) The (only) symbols that can appear in the sentence T = { a, b } T = { (, ), +, -, *, /, a...z, 0...9 } Formal Definitions III Non-terminals (N) Variables that denote a class of terminals or a set of terminals Not in the sentence internal variables Class operators, digits Set phrases Meaningful sub-groups of terminals Formal Definitions IV Start Symbol (S) Used to define the initial starting point in the grammar Could be S (a non-terminal used only as the start symbol), or another other nonterminal 4

Formal Definitions V Productions (P) The re-writing rules Defines how non-terminals can become terminals and/or other non-terminals Only the sentences which can be produced through these rules are valid Full Example I A grammar that specifies valid arithmetic expressions with variables a-z and single digits 0-9 Java could use such a grammar to check that entered code is syntactically correct Full Example II G = ( T, N, S, P ) T = { (, ), +, -, *, /, a...z, 0...9 } N = { expr, op, id } expression, operator, identifier S = expr P the following (six) productions: Full Example III a) expr expr op expr A valid expression can consist of a valid expression, followed by an operator, followed by a valid expression b) expr ( expr ) c) expr - expr d) expr id Sets of variables sub-phrases 5

Full Example IV e) op + - * / f) id a b c... z 0 1 2... 9 Classes of variables Interchangeable tokens You can switch any + with a - and still have a valid sentence Full Example V Note: the production rules are recursive The same non-terminal can appear on both sides of a production rule Reduces the quantity of rules required expr -id A redundant production Full Example VI Sentence Generation Use production rules to generate an expression means can derive or can re-write Full Example VII expr expr op expr // rule a expr + expr // rule e - expr + expr // rule c - (expr) + expr // rule b - ( expr op expr ) + expr // rule a - ( expr * expr ) + expr // rule e - ( id * id ) + id // rule d - ( a * b ) + c // rule f 6

Full Example VIII We have derived the sentence - ( a * b ) + c from expr derivation is the sequence of steps No longer just a monkey at a keyboard Note: derivation is usually less interesting than parsing (the reverse process) Another Example I G = ( T, N, S, P ) T = { a, b } N = { S, A, B } (S = S) // redundant from above Another Example II P: S AB A aa B bb is the null symbol It allows a non-terminal to become nothing Another Example III Can we derive aab? S AB aab aaab aa B aab aabb aab aab 7

Another Example IV Can we derive b? S AB B B bb b b Another Example V Can we derive bba? S AB B B bb bbb bb bb??? Note: A can become, cannot become A Another Example VI G = ( T, N, S, P ) T = { a, b } N = { S, A, B } P: S AB A aa B bb Valid sentences in defined language: 0 or more a s followed by 0 or more b s Formal Definitions A string s of terminals is a valid sentence in the language defined by a grammar G = ( T, N, S, P ) if and only if S s Subsequently, the language defined by G is the set of all sentences s such that s is a sentence in T * and S derives s. L(G) = { s s T* and S s } 8

Overview of Grammars Three main problems Derive a sentence from a grammar Develop a grammar for a language Given a grammar, determine if an input is valid We have done derivation The next two parts are more interesting Readings and Assignments Suggested Readings from Shaffer (third edition) 9