Grammar Rules in Prolog!!

Similar documents
Grammar Rules in Prolog

Programming Language Definition. Regular Expressions

CMPS Programming Languages. Dr. Chengwei Lei CEECS California State University, Bakersfield

Syntax. A. Bellaachia Page: 1

COP 3402 Systems Software Top Down Parsing (Recursive Descent)

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

Programming Lecture 3

CSCE 314 Programming Languages

Theoretical Part. Chapter one:- - What are the Phases of compiler? Answer:

This book is licensed under a Creative Commons Attribution 3.0 License

Formal Languages. Formal Languages

Dr. D.M. Akbar Hussain

CMSC 330: Organization of Programming Languages. Context Free Grammars

Lexical and Syntax Analysis. Top-Down Parsing

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

Non-deterministic Finite Automata (NFA)

COP 3402 Systems Software Syntax Analysis (Parser)

Describing Syntax and Semantics

Defining syntax using CFGs

Lexical and Syntax Analysis

Chapter 3: CONTEXT-FREE GRAMMARS AND PARSING Part 1

Specifying Syntax. An English Grammar. Components of a Grammar. Language Specification. Types of Grammars. 1. Terminal symbols or terminals, Σ

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.

Chapter 3. Describing Syntax and Semantics

a name refers to an object side effect of assigning composite objects

Chapter 3. Describing Syntax and Semantics

3. Context-free grammars & parsing

CSCI312 Principles of Programming Languages!

Chapter 3. Describing Syntax and Semantics ISBN

Chapter 3. Describing Syntax and Semantics

Principles of Programming Languages COMP251: Syntax and Grammars

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

Syntax. In Text: Chapter 3

Chapter 3. Describing Syntax and Semantics ISBN

Case by Case. Chapter 3

Programming Languages Third Edition

Syntax Intro and Overview. Syntax

programming languages need to be precise a regular expression is one of the following: tokens are the building blocks of programs

Introduction to Syntax Analysis

Introduction to Lexing and Parsing

Formal Languages and Grammars. Chapter 2: Sections 2.1 and 2.2

Context-Free Languages and Parse Trees

Introduction to Parsing. Lecture 8

UNIT I Programming Language Syntax and semantics. Kainjan Sanghavi

CPS 506 Comparative Programming Languages. Syntax Specification

COSC252: Programming Languages: Semantic Specification. Jeremy Bolton, PhD Adjunct Professor

Habanero Extreme Scale Software Research Project

CMSC 330: Organization of Programming Languages

Building Compilers with Phoenix

Principles of Programming Languages COMP251: Syntax and Grammars

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

Languages and Compilers

CSE 12 Abstract Syntax Trees

Defining Program Syntax. Chapter Two Modern Programming Languages, 2nd ed. 1

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

CMSC 330: Organization of Programming Languages. Context Free Grammars

Context-Free Grammars

3. DESCRIBING SYNTAX AND SEMANTICS

CMSC 330: Organization of Programming Languages

Chapter 4. Syntax - the form or structure of the expressions, statements, and program units

Syntax and Semantics

flex is not a bad tool to use for doing modest text transformations and for programs that collect statistics on input.

CMSC 330: Organization of Programming Languages

Programming Language Syntax and Analysis

CSE 3302 Programming Languages Lecture 2: Syntax

Part 5 Program Analysis Principles and Techniques

Context-Free Languages & Grammars (CFLs & CFGs) Reading: Chapter 5

Lecture 4: Syntax Specification

CMSC 330: Organization of Programming Languages. Context Free Grammars

Informatica 3 Syntax and Semantics

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

Topic of the day: Ch. 4: Top-down parsing

Outline. Limitations of regular languages. Introduction to Parsing. Parser overview. Context-free grammars (CFG s)

Context Free Grammars. CS154 Chris Pollett Mar 1, 2006.

Homework. Lecture 7: Parsers & Lambda Calculus. Rewrite Grammar. Problems

CSE302: Compiler Design

Parsing. source code. while (k<=n) {sum = sum+k; k=k+1;}

Regular Expressions. Agenda for Today. Grammar for a Tiny Language. Programming Language Specifications

Syntax Analysis/Parsing. Context-free grammars (CFG s) Context-free grammars vs. Regular Expressions. BNF description of PL/0 syntax

CS415 Compilers. Syntax Analysis. These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University

High Level Languages. Java (Object Oriented) This Course. Jython in Java. Relation. ASP RDF (Horn Clause Deduction, Semantic Web) Dr.

British Informatics Olympiad Final 30 March 1 April, 2007 Sponsored by Lionhead Studios. Parsing

A programming language requires two major definitions A simple one pass compiler

Functional Programming in Haskell Prof. Madhavan Mukund and S. P. Suresh Chennai Mathematical Institute

Processadors de Llenguatge II. Rafael Ramirez

Compilers Course Lecture 4: Context Free Grammars

LL(k) Parsing. Predictive Parsers. LL(k) Parser Structure. Sample Parse Table. LL(1) Parsing Algorithm. Push RHS in Reverse Order 10/17/2012

Outline. Limitations of regular languages Parser overview Context-free grammars (CFG s) Derivations Syntax-Directed Translation

PL Revision overview

Chapter 3. Syntax - the form or structure of the expressions, statements, and program units

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

Parsing Combinators: Introduction & Tutorial

CSEP 501 Compilers. Languages, Automata, Regular Expressions & Scanners Hal Perkins Winter /8/ Hal Perkins & UW CSE B-1

CSE 413 Programming Languages & Implementation. Hal Perkins Autumn 2012 Grammars, Scanners & Regular Expressions

Syntax Analysis. Amitabha Sanyal. ( as) Department of Computer Science and Engineering, Indian Institute of Technology, Bombay

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

More Assigned Reading and Exercises on Syntax (for Exam 2)

Theory and Compiling COMP360

Syntax. Syntax. We will study three levels of syntax Lexical Defines the rules for tokens: literals, identifiers, etc.

Transcription:

Grammar Rules in Prolog GR-1

Backus-Naur Form (BNF) BNF is a common grammar used to define programming languages» Developed in the late 1950 s Because grammars are used to describe a language they are said to produce sentences GR-2

Grammars and Design Grammars can be used to describe the structure of objects and computations.» Can be used to describe the structure of input > Parse» Can be used to generate output > Compute» Can be used to describe the structure of algorithms > Design GR-3

Grammar Definition A grammar, G, is a 4-tuple G = <T, N, S, P>, where» T a set of terminal symbols > They represent themselves A, begin, 123» N a set of non-terminal symbols > They are enclosed between < and > <program> <while> <letter> <digit>» S N the starting symbol GR-4

Grammar Definition 2» P is a finite set of production or rewrite rules of the form α > and are sequences, strings, of terminal and non-terminal symbols α > 1 α β α ::= β > contains at least one non-terminal symbol GR-5

Types of Grammars Type 0 Unrestricted or General grammars» Correspond to Turing machines» Can compute anything Type 1 Context sensitive grammars» In general not used, as they are too complex Type 2 Context free grammars» Often used to describe the structure of programming languages GR-6

Types of Grammars 2 Type 3 Regular grammars» Correspond > Regular expressions > Finite state machines» Most business problems can be described with regular grammars > Although context free grammars are used, due to their ease of use GR-7

Unrestricted Grammar No restrictions on the definition» In particular permits < α β > Permits erasure of terminal symbols GR-8

Context Sensitive Grammar Restrict productions such that there is no erasure» α β > One exception is that the starting symbol may be in the production <Start> ::= produce the empty sentence The following defines the language A n B n C n for n 1 to be able to (1) <S> ::= <A> <B> C (2) <S> ::= <A> <B> <S> C (3) <B> <A> ::= <A> <B> (4) <B> C ::= B C (5) <B> B ::= B B (6) <A> B ::= A B (7) <A> A ::= A A ε GR-9

α Context Free Grammar Restrict to be a single non-terminal» α = 1 > This permits non-terminals to be removed Note there is no erasure as terminals cannot be removed The following defines the language A n B n for n 0 ε (1) <S> ::= (2) <S> ::= A <S> B GR-10

Restrict Regular Grammar to be a single non-terminal Restrict to have at most one non-terminal, with the nonterminal, if it occurs, being at either end of β» 1 β α β > One exception is that the starting symbol may be in the production <Start> ::= produce the empty sentence to be able to Can restrict, without loss of generality to productions of the following structure giving a Right Regular Grammar (1) <non terminal> ::= terminal (2) <non terminal> ::= terminal <non terminal> ε GR-11

Sentence Generation for A n B n <S> ε Rule 1 <S> A <S> B Rule 2 A B Rule 1 <S> A <S> B Rule 2 A A <S> B B Rule 2 A A B B Rule 1 <S> A <S> B Rule 2 A A <S> B B Rule 2 A A A <S> B B BRule 2 A A A B B B Rule 1 GR-12

Parsing & Prolog Parsing is the opposite of sentence generation» Task is to find a sequence of rules that produce a given sentence Prolog has a built-in notation for representing grammar rules called Definitive Context Grammar (DCG) GR-13

Parsing & Prolog 2 In a DCG the grammar for A n B n is represented as follows (1) S --> [ A ], [ B ]. (2) S --> [ A ], S, [ B ]. Upper case is used in the slide for easier reading, in Prolog lower case (constants) would be used for A and B and not upper case (variables). GR-14

DCG Translation DCG statements are translated into Prolog The following are examples. n --> n1, n2,, nn. n (S, Rest) :- n1(s, R2), n2(r2, R3),, nn(rn, Rest). n --> [ T1 ], [ T2 ], [ Tn ]. n([t1, T2,, Tn Rest], Rest). n --> n1, [ T2 ], n3, [ T4]. n(s, Rest) :- n1(s, [T2 R3]), n3(r3, [T4 Rest]). n --> [ T1 ], n2, [ T3 ], n4. n([t1 R2], Rest) :- n2(r2, [ T3 R4]), n4(r4, Rest). GR-15

Translation of A n B n S --> [ A ], [ B ]. S --> [ A ], S, [ B ]. ==> s ( [ a, b Rest ], Rest ). s ( [ a R1 ], Rest) :- s ( R1, [ b Rest ] ). Every sentence is represented by 2 lists» Difference lists of symbols > The first list is the sentence you are parsing > The second list is the part of the sentence that is left-over when parsing is done Sample queries s ( [a, b], [ ] ). s ( [a, a, b, b], [ ] ). s ( [a, a, b, b, c], [c] ). GR-16

Movement example move --> step. move --> step, move. step --> [up]. step --> [down]. Example queries move ( [up, up, down], [ ] ). move ( [up, up, left], [ ] ). move ( [up, M, up], [ ] ). Translation move ( List, Rest ) :- step ( List, Rest ). move ( List1, Rest) :- step ( List1, List2 ), move ( List2, Rest ). step ( [up Rest], Rest ). step ( [down Rest], Rest ). GR-17

P is a T example using determinants parse --> [ P ], [ is, a ], [ T ]. Example query parse ( [ John, is, a, person,. ], [ ] ). Translation parse ( S, Sr) :- det1 ( S, S0 ), det2 ( S0, S1 ), det3 ( S1, S2 ), det4 ( S2, Sr ). det1 ( [ P St ], St ). det2 ( [ is, a St ], St ). det3 ( [ T St ], St ). det4 ( [ '. St ], St ). GR-18

Grammars & Algorithms Unrestricted grammars have been used to write programs» Snobol language was used to develop a system called MUMPS that was used in hospital applications circa 1960 s 1970 s GR-19

SNOBOL In Snobol a grammar is defined to translate (rewrite) an input string of symbols to an output string of symbols» The production rules are applied using the Markov algorithm > Developed during the 1940's as yet another description of what it means to compute» Works in a similar way to Prolog > Pattern matching takes place on strings, instead of compound terms GR-20

Markov Algorithm Input» A numbered set of productions α β > Numbering is from 1 up» An input string mastr over the alphabet > No distinction needed for terminals and nonterminals Computation» The productions are applied to the sequence of strings beginning with the input string Output» The resulting string when no production is applicable GR-21

Markov Algorithm PROCEDURE VAR j : integer { An index to a production.} ; k : integer { An index to the occurrence" # # # of an alpha [ j ] in mastr.} ; notatend : boolean { Goes FALSE when algorithm is done.}# ; BEGIN j := 1 { Start at production 1.} ; notatend := true ; WHILE notatend DO BEGIN DO loop body see next slide END END GR-22

Markov Algorithm Body of Loop { Find left most occurrence of alpha.}# k := index ( mastr, 1, alpha [ j ] )# ; IF k = 0 THEN {No alpha, try the next production.} BEGIN j := j+1 {No alpha, try the next production.}# # ; IF j > prodcount {Do we have a production to try?} END END THEN notatend := false {No production, stop.}# ELSE BEGIN {Found alpha, apply production.} replace (mastr, beta [ j ], k, alpha [ j ]. length ) j := 1 {Start with first production again.} END END GR-23

MA Add two binary numbers Alphabet» 0 1 <- The binary digits.» a <- Remember a 1.» b <- Remember a 0.» c <- Remember a carry.» N <- A 1 in the sum.» Z <- A 0 in the sum.» X <- Separator for the two input numbers. GR-24

MA Add two binary numbers 2 Productions» a1 -> 1a ; a0 -> 0a ; <- Travel right with a one» b1 -> 1b ; b0 -> 0b ; <- Travel to right with a zero» 1c -> c0 ; 0c -> 1 ; c -> 1 ; <- Propagate a carry» 1a -> cz ; 0a -> N ; Xa -> N ; <- Add one to least sig digit of n2» 1b -> N ; 0b -> Z ; Xb -> Z ; <- Add zero to least sig digit of n2» 1X -> Xa ; 0X -> Xb ; <- Move least sig digit of n1 to add position» N -> 1 ; Z -> 0 ; <- Recover all zeros and ones An input string» 101X1101 GR-25

SNOBOL Syntactic Sugar Some productions terminate with a period» If such a production is applied, the computation terminates Some productions are labeled Some productions have success and failure tags» If such a production is applied, the Markov algorithm resumes from the production labeled by the success tag» If such a production is not applied, then the Markov algorithm resumes from the production labeled by the failure tag GR-26