COMP Logic for Computer Scientists. Lecture 23

Similar documents
Recursive definition of sets and structural induction

Syntax Analysis Top Down Parsing

COMP Logic for Computer Scientists. Lecture 25

BRAND STANDARD GUIDELINES 2014

Visual Identity Guidelines. Abbreviated for Constituent Leagues

2.2 Syntax Definition

recruitment Logo Typography Colourways Mechanism Usage Pip Recruitment Brand Toolkit

Brand Guidelines October, 2014

Wisconsin Retirement Testing Preparation

Lamé s Theorem. Strings. Recursively Defined Sets and Structures. Recursively Defined Sets and Structures

TABLE OF CONTENTS. 3 Intro. 4 Foursquare Logo. 6 Foursquare Icon. 9 Colors. 10 Copy & Tone Of Voice. 11 Typography. 13 Crown Usage.

Chapter 3: CONTEXT-FREE GRAMMARS AND PARSING Part 1

announcements CSE 311: Foundations of Computing review: regular expressions review: languages---sets of strings

Parsing. Zhenjiang Hu. May 31, June 7, June 14, All Right Reserved. National Institute of Informatics

Recursively Defined Functions

Media Kit & Brand Guidelines

The ABC s of Web Site Evaluation

For fine ti.ito vvt* have hcen in IS PUBLISHED EVERY. OF LOWELL. n t m r n T i l t " ^ n a i I Howk & White, CAPITAL, ,000.

Multicast Tree Aggregation in Large Domains

Growing Our Own Through Collaboration

Lecture 8: Context Free Grammars

Macro O Compensate a single cartridge ActiveEdge tool

COMP Intro to Logic for Computer Scientists. Lecture 2

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

This book is licensed under a Creative Commons Attribution 3.0 License

Tranont Mission Statement. Tranont Vision Statement. Change the world s economy, one household at a time.

Section 8: Monomials and Radicals

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

CS321 Languages and Compiler Design I. Winter 2012 Lecture 4

Lecture Chapter 6 Recursion as a Problem Solving Technique

EECS 700 Functional Programming

Palatino. Palatino. Linotype. Palatino. Linotype. Linotype. Palatino. Linotype. Palatino. Linotype. Palatino. Linotype

PracticeAdmin Identity Guide. Last Updated 4/27/2015 Created by Vanessa Street

Eureka Math. Grade 7, Module 6. Student File_A. Contains copy-ready classwork and homework

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

Lexical Analysis - 1. A. Overview A.a) Role of Lexical Analyzer

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

How to Register for Summer Camp. A Tutorial

CMPSCI 250: Introduction to Computation. Lecture #7: Quantifiers and Languages 6 February 2012

CMSC 330: Organization of Programming Languages

Parsing II Top-down parsing. Comp 412

BRANDING AND STYLE GUIDELINES

Handwriting Standards

CIS552: Advanced Programming

CSCE 314 Programming Languages. Functional Parsers

BRAND BOOK. Copyright 2016 WashU Student Union Student Union Brand Guidebook 1

Announcements. Written Assignment 1 out, due Friday, July 6th at 5PM.

Brand Standards. V1 For Internal Use. Newcastle Systems Brand Standards 1

Similar Polygons Date: Per:

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages

CS153: Compilers Lecture 4: Recursive Parsing

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

Working with Z3 (10 points)

I.D. GUIDE Kentucky Campus Version 1.0

Computers and FORTRAN Language Fortran 95/2003. Dr. Isaac Gang Tuesday March 1, 2011 Lecture 3 notes. Topics:

Concept of Curve Fitting Difference with Interpolation

HEL HEL HEL HEL VETIC HEL VETIC HEL HEL VETICA HEL HEL ETICA ETIC VETIC HEL VETIC HEL HEL C VETICA ETI- HEL HEL VETI HEL VETICA VETIC HEL HEL VETICA

A Simple Syntax-Directed Translator

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

CPS 506 Comparative Programming Languages. Syntax Specification

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

CS3600 SYSTEMS AND NETWORKS

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer. Compiler Design

Principles of Programming Languages COMP251: Syntax and Grammars

ECS 120 Lesson 7 Regular Expressions, Pt. 1

ITEC2620 Introduction to Data Structures

CS 314 Principles of Programming Languages

Midterm I - Solution CS164, Spring 2014

Lecture 5 - Axiomatic semantics

Programming Languages Third Edition

Chapter Summary. Recursively defined Functions. Recursively defined sets and Structures Structural Induction

Brand Standards September 2016 CREATED BY M3 GROUP

15 212: Principles of Programming. Some Notes on Grammars and Parsing

CMSC 330: Organization of Programming Languages. Context Free Grammars

Introduction to Automata Theory. BİL405 - Automata Theory and Formal Languages 1

Victorian Cursive Handwriting On Dotted Thirds

Visit MathNation.com or search "Math Nation" in your phone or tablet's app store to watch the videos that go along with this workbook!

Lexical Analysis. Sukree Sinthupinyo July Chulalongkorn University

Quadratic Functions Date: Per:

CMSC 330: Organization of Programming Languages. Formal Semantics of a Prog. Lang. Specifying Syntax, Semantics

The Vertex Cover Problem. Shangqi Wu Presentation of CS 525 March 11 th, 2016

ITEC2620 Introduction to Data Structures

1.2 Round-off Errors and Computer Arithmetic

How to Register for Summer Camp. A Tutorial

Syntax Analysis Check syntax and construct abstract syntax tree

Formal Languages. Formal Languages

Compilers Course Lecture 4: Context Free Grammars

Recursive Definitions Structural Induction Recursive Algorithms

Introduction to Logic Programming

Lecture 5: Predicate Calculus. ffl Predicate Logic ffl The Language ffl Semantics: Structures

Warmup Problem. Translate the following sentence from English into Propositional Logic. I want to eat ice cream even though I am on a diet.

What is a Logic Program. Introduction to Logic Programming. Introductory Examples. Introductory Examples. Foundations, First-Order Language

Imperative Paradigm. Syntax. Role of Programming Languages. Expressions. Expressions. Role of Programming Languages. Symbols.

CMSC 330 Practice Problem 4 Solutions

FINAL EXAM REVIEW CH Determine the most precise name for each quadrilateral.

Haskell Review COMP360

Recursion defining an object (or function, algorithm, etc.) in terms of itself. Recursion can be used to define sequences

Chapter 18: Decidability

CMSC 330: Organization of Programming Languages. Context Free Grammars

Transcription:

COMP 1002 Logic for Computer cientists Lecture 23 B 5 2 J

Admin stuff Assignment 3 extension Because of the power outage, assignment 3 now due on Tuesday, March 14 (also 7pm) Assignment 4 to be posted by tomorrow. Due March 21 st.

Recursive definitions of sets o far, we talked about recursive definitions of sequences. We can also give recursive definitions of sets. E.g: recursive definition of a set = 0,1 Basis: empty string is in. Recursive step: if ww, then www and ww1 Here, ww0 means string w with 0 appended at the end; same for w1 Alternatively: Basis: empty string, 0 and 1 are in. Recursive step: if s and t are in, then st here, st is concatenation: symbols of s followed by symbols of t If s = 101 and t= 0011, then st = 1010011 Additionally, need a restriction condition: the set contains only elements produced from basis using recursive step rule.

Trees In computer science, a tree is an undirected graph without cycles Undirected: all edges go both ways, no arrows. Cycle: sequence of edges going back to the same point. Recursive definition of trees: Base: A single vertex vv is a tree. Recursion: Let TT be a tree, and vv a new vertex. Then a new tree consist of TT, vv, and an edge (connection) between some vertex of TT and vv. Restriction: Anything that cannot be constructed with this rule from this base is not a tree. 2 1 3 Undirected cycle (not a tree) vv 1 vv 2 vv 3 vv 5 vv 4

Arithmetic expressions uppose you are writing a piece of code that takes an arithmetic expression and, say evaluates it. 5*3-1, 40-(x+1)*7, etc How to describe a valid arithmetic expression? Define a set of all valid arithmetic expressions recursively. Base: A number or a variable is a valid arithmetic expression. 5, 100, x, a, Recursion: If A and B are valid arithmetic expressions, then so are (A), AA + BB, AA BB, AA BB, AA / B. Constructing 40-(x+1)*7: first construct 40, x, 1, 7. Then x+1. Then (x+1). Then (x+1)*7, finally 40-(x+1)*7 Caveat: how do we know the order of evaluation? On that later. Restriction: nothing else is a valid arithmetic expression.

Formulas What is a well-formed propositional logic formula? pp qq rr pp rr Base: a propositional variable pp, qq, rr Or a constant TTTTTTTT, FFFFFFFFFF Recursion: If F and G are propositional formulas, so are FF, FF, FF GG, FF GG, FF GG, FF GG. And nothing else.

Formulas What is a well-formed predicate logic formula? xx DD yy Z PP xx, yy QQ xx, zz xx = yy Base: a predicate with free variables P(x), x=y, Recursion: If F and G are predicate logic formulas, so are FF, FF, FF GG, FF GG, FF GG, FF GG. If FF is a predicate logic formula with a free variable x, then xx DD FF and xx DD FF are predicate logic formulas. And nothing else. o xx PPPPPPPPPPPP LLLLLLLLLL xx, yy xx, LLLLLLLLLL yy xx is not a well-formed predicate logic formula!

Grammars A general recursive definition for these is called a grammar. In particular, here we have context-free grammars, where symbols have the same meaning wherever they are. A context-free grammar consists of A set V of variables (using capital letters) Including a start variable. A set Σ of terminals (disjoint from V; alphabet) A set R of rules, where each rule consists of a variable from V and a string of variables and terminals. If AA ww is a rule, we say variable AA yields string w. This is not the same " as implication, a different use of the same symbol. We use shortcut when the same variable might yield several possible strings: AA ww 1 ww 2 ww kk Can use A again within the rule: Recursion! Different occurrences of the same variable can be interpreted as different strings. When left with just terminals, a string is derived. A language generated by a grammar consists of all strings of terminals that can be derived from the start variable by applying the rules. All strings are derived by repeatedly applying the grammar rules to each variable until there are no variables left (just the terminals).

Examples of grammars Example: language {1, 00} consisting of two strings 1 and 00 1 00 Variables:. Terminals: 1 and 00. Example: strings over {0,1} with all 0s before all 1s. 0 _ Variables:. Terminals: 0 and 1. Example: propositional formulas. 1. FF FF FF 2. F FF FF 3. FF FF 4. FF FF 5. FF pp qq rr TTTTTTTT FFFFFFFFFF Here, the only variable is F (it is a start variable), and terminals are,,,,, pp, qq, rr, TTTTTTTT, FFFFFFFFFF To obtain pp qq rr, first apply rule 2, then rule 1, then rule 5 to get p, then rule 3, then rule 5 to get q, then rule 5 to get r. Example: arithmetic expressions. EEEEEEEE EEEEEEEE + EEEEEEEE NUMBER EEEEEEEE EEEEEEEE EEEEEEEE EEEEEEEE EEEEEEEE / EEEEEEEE EEEEEEEE NNNNNNNNNNNN - NUMBER 0DDDDDDDDDDDD 9DDDDDDDDDDDD DDDDDDDDDDDD _ NNNNNNNNNNNN Here, _ stands for empty string. Variables: EXPR, NUMBER, DIGIT ( is starting). Terminals: +,-,*, /, 0,,9. We used separate NUMBER to avoid multiple -. And separate DIGIT to have an empty string to finish writing a number, but to avoid an empty number.

Encoding order of precedence Easier to specify in which order to process parts of the formula. Better grammar for arithmetic expressions (for simplicity, with x,y,z instead of numbers): 1. EEEEEEEE EEEEEEEE + TTTTTTTT EEEEEEEE TTTTTTTT TTTTTTTT 2. TTTTTTTT TTTTTTTT FFFFFFFFFFFF TTTTTTTT / FFFFFFFFFFFF FFFFFFFFFFFF 3. FFFFFFFFFFFF EEEEEEEE x y z Here, variables are EXPR, TERM and FACTOR (with EXPR a starting variable). Now can encode precedence. And put parentheses more sensibly.

Parse trees. Visualization of derivations: parse trees. 0 1. EEEEEEEE EEEEEEEE + TTTTTTTT EEEEEEEE TTTTTTTT TTTTTTTT 2. TTTTTTTT TTTTTTTT FFFFFFFFFFFF TTTTTTTT / FFFFFFFFFFFF FFFFFFFFFFFF 3. FFFFFFFFFFFF EEEEEEEE x y z tring (x+y)*z impler example: 0 1 _ tring 001 1 TERM FACTOR ( EXPR ) EXPR + TERM EXPR TERM * FACTOR z 0 0 TERM FACTOR 1 _ 0 0 1 0 _ 0 0 1 FACTOR y x ( x + y ) * z

Puzzle Do the following two English sentences have the same parse trees? Time flies like an arrow. Fruit flies like an apple.