1 Announcements HW2 due Thursday, 2/18 in class and in Homework Server Note on Problem 3: Show means...

44
1 Announcements HW2 due Thursday, 2/18 in class and in Homework Server Note on Problem 3: Show means prove Note on 4&7: Will cover material today Download SWI Prolog Prolog Project will be next Thursday, 2/18 Spring 16 CSCI 4430, A Milanova

description

3 Today’s Lecture Outline Bottom-up (LR) parsing Characteristic Finite State Machine (CFSM) From CFSM to SLR(1) parsing table Conflicts in SLR(1) LR parsing variants Logic Programming and Prolog Logic programming Logic programming concepts Prolog constructs: facts, rules, queries

Transcript of 1 Announcements HW2 due Thursday, 2/18 in class and in Homework Server Note on Problem 3: Show means...

Page 1: 1 Announcements HW2 due Thursday, 2/18 in class and in Homework Server Note on Problem 3: Show means prove Note on 47: Will cover material today Download.

1

Announcements HW2 due Thursday, 2/18 in class and in

Homework Server Note on Problem 3: Show means prove Note on 4&7: Will cover material today

Download SWI Prolog

Prolog Project will be next Thursday, 2/18

Spring 16 CSCI 4430, A Milanova

Page 2: 1 Announcements HW2 due Thursday, 2/18 in class and in Homework Server Note on Problem 3: Show means prove Note on 47: Will cover material today Download.

Spring 16 CSCI 4430, A Milanova 2

Last Class

Bottom-up (LR) parsing Model of the LR parser LR Items Characteristic Finite State Machine (CFSM)

Page 3: 1 Announcements HW2 due Thursday, 2/18 in class and in Homework Server Note on Problem 3: Show means prove Note on 47: Will cover material today Download.

3

Today’s Lecture Outline

Bottom-up (LR) parsing Characteristic Finite State Machine (CFSM) From CFSM to SLR(1) parsing table Conflicts in SLR(1) LR parsing variants

Logic Programming and Prolog Logic programming Logic programming concepts Prolog constructs: facts, rules, queries

Page 4: 1 Announcements HW2 due Thursday, 2/18 in class and in Homework Server Note on Problem 3: Show means prove Note on 47: Will cover material today Download.

4

Programming Language SyntaxBottom-up Parsing

Logic Programming and Prolog

Read: finish Scott, Chapter 2.3.3, start Scott, Chapter 11.1, 11.2 (beginning)

Spring 16 CSCI 4430, A Milanova

Page 5: 1 Announcements HW2 due Thursday, 2/18 in class and in Homework Server Note on Problem 3: Show means prove Note on 47: Will cover material today Download.

5

Model of the LR parser

sm

Xm

sm-1

Xm-1

…s0

LR Parser

a1 … ai … an $$Input:

Stack:

action goto

State

GrammarSymbol

Parsing table:

action[s,a]: Do we shift or reduce?goto[s,A]: After reduction tononterminal A, what state is pushed on top of the stack? Spring 16 CSCI 4430, A Milanova

Page 6: 1 Announcements HW2 due Thursday, 2/18 in class and in Homework Server Note on Problem 3: Show means prove Note on 47: Will cover material today Download.

start •exprexpr •expr+termexpr •termterm •term*idterm •id

0

start expr• expr expr•+term

1expr

expr term• term term•*id

2term

term id•

3id

expr expr+•termterm •term *idterm • id

4

+

term term*• id

6

*

termexpr expr+term• term term•*id

5*

term term *id•

7

Characteristic Finite State Machine (CFSM)

idid

The collection of sets of items with transitions is a DFA. This DFA is one part of the CFSM (we will see the other part). CFSM states are parsing states. Transitions on terminals represent shifts. Transitions on nonterminals represent gotos. 6

Page 7: 1 Announcements HW2 due Thursday, 2/18 in class and in Homework Server Note on Problem 3: Show means prove Note on 47: Will cover material today Download.

start •exprexpr •expr+termexpr •termterm •term*idterm •id

0

start expr• expr expr•+term

1expr

expr term• term term•*id

2term

term id•

3id

expr expr+•termterm •term *idterm • id

4

+

term term*• id

6

*

termexpr expr+term• term term•*id

5*

term term *id•

7

Question

idid

7

• After parser pops right-hand side term*id off the stack (as it reduces in state 7), what state(s) could end up on top of the stack?

Page 8: 1 Announcements HW2 due Thursday, 2/18 in class and in Homework Server Note on Problem 3: Show means prove Note on 47: Will cover material today Download.

start •exprexpr •expr+termexpr •termterm •term*idterm •id

0

start expr• expr expr•+term

1expr

expr term• term term•*id

2term

term id•

3id

expr expr+•termterm •term *idterm • id

4

+

term term*• id

6

*

termexpr expr+term• term term•*id

5*

term term* id•

7

CFSM

idid

• 3,7 contain reduce items (e.g., A α•). Reduce states.• 0,4,5 contain shift items (A α•aβ). Shift states.• 1,2,6 contains both reduce and shift items. Shift-reduce states.8

Page 9: 1 Announcements HW2 due Thursday, 2/18 in class and in Homework Server Note on Problem 3: Show means prove Note on 47: Will cover material today Download.

“Reduce by” Labels

For every state that contains a reduce item A α•, add label “reduce by A α on FOLLOW(A)”

For example, in state 2 we have:

reduce by expr term on $$,+.

Spring 16 CSCI 4430, A Milanova 9

expr term• term term•*id

2

Page 10: 1 Announcements HW2 due Thursday, 2/18 in class and in Homework Server Note on Problem 3: Show means prove Note on 47: Will cover material today Download.

start •exprexpr •expr+termexpr •termterm •term*idterm •id

0

start expr• expr expr•+term

1expr

expr term• term term•*id

2

term

term id•

3id

expr expr+•termterm •term *idterm • id

4

+

term term*• id

6

*

termexpr expr+term• term term•*id

5*

term term* id• 7

CFSM

id

id

reduce by term id on$$,+,*

accept on $$

reduce by expr term on$$,+

reduce by expr expr+term on $$,+

reduce by term term*id on $$,+,*

10

Page 11: 1 Announcements HW2 due Thursday, 2/18 in class and in Homework Server Note on Problem 3: Show means prove Note on 47: Will cover material today Download.

11

From CFSM to SLR(1) Parsing Table1. expr expr + term 3. term term * id2. expr term 4. term id

id + * $$ expr term0 shift 3 1 21 shift 4 accept2 reduce 2 shift 5 reduce 2

3 reduce 4 reduce 4 reduce 4

4 shift 3 65 shift 76 reduce 1 shift 5 reduce 1

7 reduce 3 reduce 3 reduce 3

state

White – action tableBlue – goto table

Page 12: 1 Announcements HW2 due Thursday, 2/18 in class and in Homework Server Note on Problem 3: Show means prove Note on 47: Will cover material today Download.

Spring 16 CSCI 4430, A Milanova 12

SLR(1) Parsing TableInput: An augmented grammar G’ (G with starting production start …)Output: Functions action and goto for G’

Construct C = {I0,I1,…In} the collection of sets of LR items with transitions

State i is constructed from Ii . The parsing actions for state i area) If item A α•aβ is in Ii and there is a transition from Ii to Ij on a, then set action[i,a] to “shift j”b) If item A α• is in Ii then set action[i,a] to “reduce by A α” for all terminals a in FOLLOW(A)c) If start …• is in Ii then set action[i,$$] to “accept”

The goto transition for state i are constructed for all nonterminals A using the rule: If there is transition from Ii to Ij on A, set goto[i,A]=j

If the table contains no multiply-defined entries, the grammar is said to be SLR(1)

Page 13: 1 Announcements HW2 due Thursday, 2/18 in class and in Homework Server Note on Problem 3: Show means prove Note on 47: Will cover material today Download.

Spring 16 CSCI 4430, A Milanova 13

Conflicts in SLR(1): Shift-reduce Shift-reduce conflict in state k on a:

State k contains item A β• and a is in FOLLOW(A)

and State k contains item A’ α•aβ’

The parser does not know whether it is at the end of production A β and thus should reduce by A β, or it is in the middle of production A’ αaβ’ and thus should shift a and continue looking for β’

Page 14: 1 Announcements HW2 due Thursday, 2/18 in class and in Homework Server Note on Problem 3: Show means prove Note on 47: Will cover material today Download.

Spring 16 CSCI 4430, A Milanova 14

Conflicts in SLR(1): Reduce-reduce

Reduce-reduce conflict in state k on a: State k contains item A β• and a is in FOLLOW(A)and State k contains item A’ β’• and a is in FOLLOW(A’)

The parser does not know whether it is at the end of production A β and thus should reduce by A β, or whether it is at the end of production A’ β’ and thus should reduce by A’ β’

Usually, a reduce-reduce conflict indicates a serious problem with the grammar

Page 15: 1 Announcements HW2 due Thursday, 2/18 in class and in Homework Server Note on Problem 3: Show means prove Note on 47: Will cover material today Download.

Resolving Conflicts in SLR(1)

In some cases, it makes sense to use a non-SLR(1) grammar (e.g., an ambiguous grammar) Interestingly, we can still ensure desired behavior

by choosing one of the conflicting actions E.g., we can resolve a shift-reduce conflict by

deterministically choosing the shift or the reduce

Spring 16 CSCI 4430, A Milanova 15

Page 16: 1 Announcements HW2 due Thursday, 2/18 in class and in Homework Server Note on Problem 3: Show means prove Note on 47: Will cover material today Download.

Exercise

Construct the CFSM and SLR(1) parsing table for above grammar This grammar is ambiguous and as expected we

have shift-reduce conflict(s) Resolve the conflict(s) so that + is left-

associative

Spring 16 CSCI 4430, A Milanova 16

start expr expr expr + expr | id

Page 17: 1 Announcements HW2 due Thursday, 2/18 in class and in Homework Server Note on Problem 3: Show means prove Note on 47: Will cover material today Download.

Question Recall the id-list grammars we saw earlier “Top-down” grammar:

“Bottom-up” grammar:

We saw that the top-down grammar is LL(1), but the bottom-up one is not LL(1)

How about SLR(1)?

17

list id list_tail list_tail , id list_tail | ;

list list_prefix ;list_prefix list_prefix , idlist_prefix id

Page 18: 1 Announcements HW2 due Thursday, 2/18 in class and in Homework Server Note on Problem 3: Show means prove Note on 47: Will cover material today Download.

18

Lecture Outline

Bottom-up (LR) parsing Characteristic Finite State Machine (CFSM) SLR(1) parsing table Conflicts in SLR(1) LR parsing variants

Logic Programming and Prolog Logic programming Logic programming concepts Prolog constructs: facts, rules, queries

Page 19: 1 Announcements HW2 due Thursday, 2/18 in class and in Homework Server Note on Problem 3: Show means prove Note on 47: Will cover material today Download.

19

LR Parsing Variants

LR(0), SLR(1), LALR(1), LR(1), … LR(k) LR(0), SLR(1), LALR(1) are most practical

Use the same set of parsing states Differ in the handling of ”shift-reduce” states (states

with both a shift item and a reduce item) LR(0) uses 0 tokens of lookahead

Disallows shift-reduce states Our running example is not LR(0)

E.g., state 2: expr term• term term•*id

2

Page 20: 1 Announcements HW2 due Thursday, 2/18 in class and in Homework Server Note on Problem 3: Show means prove Note on 47: Will cover material today Download.

Spring 16 CSCI 4430, A Milanova 20

LR Parsing Variants SLR(1) – uses 1 token of lookahead

Resolves (some) shift-reduce states by peeking at one token ahead

Adds labels “reduce by A β on FOLLOW(A)” to states containing items A β•. The FOLLOW sets serve as filters

If after filtering by FOLLOW, there are no shift-reduce and no reduce-reduce conflicts, then grammar is SLR(1)

Is our running example SLR(1)? Yes. Filtering by FOLLOW resolves the shift-reduce issue

in state 2: expr term• term term•*id

2

reduce by expr term on $$,+

Page 21: 1 Announcements HW2 due Thursday, 2/18 in class and in Homework Server Note on Problem 3: Show means prove Note on 47: Will cover material today Download.

Spring 16 CSCI 4430, A Milanova 21

Parsing Variants

LALR(1) Uses the same set of states as SLR(1) Constructs local, context-sensitive FOLLOW sets

and is able to avoid more conflicts An efficiency hack Most common parsers in practice

LR(1) Uses a different set of states More states, in order to keep paths disjoint

Page 22: 1 Announcements HW2 due Thursday, 2/18 in class and in Homework Server Note on Problem 3: Show means prove Note on 47: Will cover material today Download.

22

Hierarchy of Grammar Classes

LL(0) < LL(1) < LL(k) LR(0) < SLR(1) < LALR(1) < LR(1) < LR(k) Also, LL(k) < LR(k)

Question: SLR(1) parsers are more powerful than LL(1) ones. Why, what is the intuition?

Page 23: 1 Announcements HW2 due Thursday, 2/18 in class and in Homework Server Note on Problem 3: Show means prove Note on 47: Will cover material today Download.

23

Exercise Consider the grammar:

start expr expr expr + expr | expr * expr | id

Construct the CFSM for this grammar First, construct the sets of LR items with transitions Second, add “reduce by” labels

Resolve the conflicts in such a way that the operators will behave “normally”: + and * are left-associative * has higher precedence than +

Spring 16 CSCI 4430, A Milanova

Page 24: 1 Announcements HW2 due Thursday, 2/18 in class and in Homework Server Note on Problem 3: Show means prove Note on 47: Will cover material today Download.

24

Lecture Outline

Bottom-up (LR) parsing Characteristic Finite State Machine (CFSM) SLR(1) parsing table Conflicts in SLR(1) LR parsing variants

Logic Programming and Prolog Logic programming Logic programming concepts Prolog language constructs: facts, rules, queries

Page 25: 1 Announcements HW2 due Thursday, 2/18 in class and in Homework Server Note on Problem 3: Show means prove Note on 47: Will cover material today Download.

Spring 16 CSCI 4430, A Milanova 25

Moving on: Logic Programming

Download and install SWI Prolog on laptop!

J.R.Fisher’s Prolog Tutorial:http://www.csupomona.edu/~jrfisher/www/prolog_tutorial/

contents.html

Page 26: 1 Announcements HW2 due Thursday, 2/18 in class and in Homework Server Note on Problem 3: Show means prove Note on 47: Will cover material today Download.

Spring 16 CSCI 4430, A Milanova 26

Logic Programming Logic programming

Logic programming is declarative programming Logic program states what (logic), not how (control) Programmer declares axioms

In Prolog, facts and rules Programmer states a theorem, or a goal

In Prolog, a query

Language implementation determines how to use the axioms to prove the goal

Page 27: 1 Announcements HW2 due Thursday, 2/18 in class and in Homework Server Note on Problem 3: Show means prove Note on 47: Will cover material today Download.

Spring 16 CSCI 4430, A Milanova 27

Logic ProgrammingProlog Program: Java Program:rainy(seattle).rainy(rochester).cold(rochester).snowy(X):-rainy(X),cold(X).

? - snowy(C).C = rochester

HashSet rainy = new HashSet();HashSet cold = new HashSet();HashSet snowy = new HashSet();

rainy.add(“seattle”);rainy.add(“rochester”);cold.add(“rochester”);

for (Iterator it=rainy.iterator(); it.hasNext();) { String city = (String) it.next(); if (cold.contains(city)) snowy.add(city);}

for (Iterator it=snowy.iterator(); it.hasNext();) { System.out.println((String) it.next());}

Page 28: 1 Announcements HW2 due Thursday, 2/18 in class and in Homework Server Note on Problem 3: Show means prove Note on 47: Will cover material today Download.

28

Logic Programming

Language interpreter is an inference engine Resolution Unification, backward chaining, backtracking

Logic programming style is characterized by Database of facts and rules that represent logical

relations. Computation is modeled as search (queries) over this database

Manipulation of lists and use of recursion, in which it is very similar to the functional programming style

Page 29: 1 Announcements HW2 due Thursday, 2/18 in class and in Homework Server Note on Problem 3: Show means prove Note on 47: Will cover material today Download.

29

Logic Programming Concepts A Horn Clause is: H B1,B2,…,Bn

Antecedents (B’s): conjunction of zero or more terms in predicate calculus; this is the body of the horn clause

Consequent (H): a term in predicate calculus Meaning of a Horn clause:

H is true if B1, B2 , . . . , and Bn are all true When the antecedents Bi are all true, we deduce that consequent

H is true as well For example: tiger(hobbes).child(calvin).likes(calvin,hobbes) tiger(hobbes),child(calvin).

Page 30: 1 Announcements HW2 due Thursday, 2/18 in class and in Homework Server Note on Problem 3: Show means prove Note on 47: Will cover material today Download.

Spring 16 CSCI 4430, A Milanova 30

Resolution Principle

If two Horn clauses

are such that A matches D1,

then we can replace D1 with B1,B2,B3 ,…, Bm

For example:

A B1,B2,B3 ,…, Bm

C D1,D2,D3 ,…, Dn

C B1,B2,B3 ,…, Bm,D2,D3 ,…, Dn

C A,BD C

Page 31: 1 Announcements HW2 due Thursday, 2/18 in class and in Homework Server Note on Problem 3: Show means prove Note on 47: Will cover material today Download.

Spring 16 CSCI 4430, A Milanova 31

Horn Clauses in Prolog In Prolog, a Horn clause is written h :- b1,...,bn.

Horn Clause is called clause Consequent is called goal or head Antecedents are called subgoals or tail Horn Clause with no tail is a fact

E.g., tiger(hobbes). Depends on no other conditions Horn Clause with a tail is a rule

likes(calvin,X) :- tiger(calvin),child(X).

Page 32: 1 Announcements HW2 due Thursday, 2/18 in class and in Homework Server Note on Problem 3: Show means prove Note on 47: Will cover material today Download.

Horn Clauses in Prolog

Clause is composed of terms Constants

Number, e.g., 123, etc. Atoms e.g., calvin, hobbes, foo In Prolog, atoms begin with a lower-case letter!

Variables X, Foo, My_varIn Prolog, variables must begin with upper-case letter!

Structures Consist of an atom, called a functor and a list of arguments:

e.g., tiger(hobbes),likes(calvin,X)

32Spring 16 CSCI 4430, A Milanova

Page 33: 1 Announcements HW2 due Thursday, 2/18 in class and in Homework Server Note on Problem 3: Show means prove Note on 47: Will cover material today Download.

33

Prolog

Program is a database of clauses i.e., facts and rules; the rules help derive more facts

We add simple queries with constants and variables (“binding”), conjunctions and disjunctions

rainy(seattle).rainy(rochester).cold(rochester).snowy(X) :- rainy(X),cold(X).

? - rainy(C).? – snowy(C).

Spring 16 CSCI 4430, A Milanova

Page 34: 1 Announcements HW2 due Thursday, 2/18 in class and in Homework Server Note on Problem 3: Show means prove Note on 47: Will cover material today Download.

Spring 16 CSCI 4430, A Milanova 34

Facts

likes(eve, pie). food(pie).likes(al, eve). food(apple).likes(eve, tom). person(tom).likes(eve, eve).

The combination of the functor and its arity (i.e., its number of arguments) is called a predicate.

functors constants

Page 35: 1 Announcements HW2 due Thursday, 2/18 in class and in Homework Server Note on Problem 3: Show means prove Note on 47: Will cover material today Download.

35

Querieslikes(eve, pie). food(pie).likes(al, eve). food(apple).likes(eve, tom). person(tom).likes(eve, eve).

?-likes(al,eve). ?-likes(al,Who).true. Who=eve.

?-likes(al,pie). ?-likes(eve,W).false. W=pie ;?-likes(eve,al). W=tom ;false. W=eve .

query

answer

variable

answer withvariable binding

force search formore answersSpring 16 CSCI 4430, A Milanova

Page 36: 1 Announcements HW2 due Thursday, 2/18 in class and in Homework Server Note on Problem 3: Show means prove Note on 47: Will cover material today Download.

36

Harder Querieslikes(eve, pie). food(pie).likes(al, eve). food(apple).likes(eve, tom). person(tom).likes(eve, eve).

?-likes(al,V) , likes(eve,V).

?-likes(eve,W), person(W).

?-likes(A,B).

?-likes(D,D).

and

Page 37: 1 Announcements HW2 due Thursday, 2/18 in class and in Homework Server Note on Problem 3: Show means prove Note on 47: Will cover material today Download.

37

Harder Querieslikes(eve, pie). food(pie).likes(al, eve). food(apple).likes(eve, tom). person(tom).likes(eve, eve).

?-likes(eve,W),likes(W,V).

?-likes(eve,W),person(W),food(V).

?-likes(eve,V),(person(V);food(V)).or

same binding

Spring 16 CSCI 4430, A Milanova

Page 38: 1 Announcements HW2 due Thursday, 2/18 in class and in Homework Server Note on Problem 3: Show means prove Note on 47: Will cover material today Download.

Spring 16 CSCI 4430, A Milanova 38

Ruleslikes(eve, pie). food(pie).likes(al, eve). food(apple).likes(eve, tom). person(tom).likes(eve, eve).

Add a rule to the database:

rule1:-likes(eve,V),person(V).

?-rule1.true

Page 39: 1 Announcements HW2 due Thursday, 2/18 in class and in Homework Server Note on Problem 3: Show means prove Note on 47: Will cover material today Download.

Spring 16 CSCI 4430, A Milanova 39

Ruleslikes(eve, pie). food(pie).likes(al, eve). food(apple).likes(eve, tom). person(tom).likes(eve, eve).

rule2(V) :- likes(eve,V),person(V).

?-rule2(H).

?-rule2(pie).

rule1 and rule2 are just like any other predicate!

rule1 :- likes(eve,V),person(V).

Page 40: 1 Announcements HW2 due Thursday, 2/18 in class and in Homework Server Note on Problem 3: Show means prove Note on 47: Will cover material today Download.

Spring 16 CSCI 4430, A Milanova 40

Queen Victoria Examplemale(albert). male(edward). Put all clauses in file female(alice). family.plfemale(victoria).parents(edward,victoria,albert). parents(alice,victoria,albert).

?- [family]. Loads file family.pltrue.?- male(albert). a querytrue.?- male(alice).false.?- parents(edward,victoria,albert).true.?- parents(bullwinkle,victoria,albert).false.

cf Clocksinand Mellish

Page 41: 1 Announcements HW2 due Thursday, 2/18 in class and in Homework Server Note on Problem 3: Show means prove Note on 47: Will cover material today Download.

Spring 16 CSCI 4430, A Milanova 41

Queen Victoria Example

?-female(X). a query X = alice ; ; asks for more answersX = victoria. Variable X has been unified to all possible values

that make female(X) true. Variables are upper-case, functors (predicates and

constants) are lower-case!

Page 42: 1 Announcements HW2 due Thursday, 2/18 in class and in Homework Server Note on Problem 3: Show means prove Note on 47: Will cover material today Download.

Spring 16 CSCI 4430, A Milanova 42

Queen Victoria Example Facts alone do not make interesting programs. We

need variables and deductive rules.

sister_of(X,Y) :- female(X),parents(X,M,F), parents(Y,M,F).

?- sister_of(alice,Y).Y = edward <enter>: not asking for more answers?- sister_of(alice, victoria).false.

Page 43: 1 Announcements HW2 due Thursday, 2/18 in class and in Homework Server Note on Problem 3: Show means prove Note on 47: Will cover material today Download.

Spring 16 CSCI 4430, A Milanova 43

Exercises

Download and install SWI Prolog!

Execute “Queen Victoria Example” Load all facts and rules in a file family.pl Execute a few queries

Page 44: 1 Announcements HW2 due Thursday, 2/18 in class and in Homework Server Note on Problem 3: Show means prove Note on 47: Will cover material today Download.

Next Class

Read Chapter 11.2.1-4

Bring your laptops. We will be doing in-class exercises in Prolog.

Spring 16 CSCI 4430, A Milanova 44