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.

Slides:



Advertisements
Similar presentations
Compiler construction in4020 – lecture 4 Koen Langendoen Delft University of Technology The Netherlands.
Advertisements

Bottom up Parsing Bottom up parsing trys to transform the input string into the start symbol. Moves through a sequence of sentential forms (sequence of.
Review: LR(k) parsers a1 … a2 … an $ LR parsing program Action goto Sm xm … s1 x1 s0 output input stack Parsing table.
CSE 5317/4305 L4: Parsing #21 Parsing #2 Leonidas Fegaras.
Mooly Sagiv and Roman Manevich School of Computer Science
COP4020 Programming Languages Logic programming and Prolog Prof. Xin Yuan.
1 May 22, May 22, 2015May 22, 2015May 22, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa Pacific University,
6/12/2015Prof. Hilfinger CS164 Lecture 111 Bottom-Up Parsing Lecture (From slides by G. Necula & R. Bodik)
1 Chapter 5: Bottom-Up Parsing (Shift-Reduce). 2 - attempts to construct a parse tree for an input string beginning at the leaves (the bottom) and working.
1 Bottom Up Parsing. 2 Bottom-Up Parsing l Bottom-up parsing is more general than top-down parsing »And just as efficient »Builds on ideas in top-down.
By Neng-Fa Zhou Syntax Analysis lexical analyzer syntax analyzer semantic analyzer source program tokens parse tree parser tree.
Lecture #8, Feb. 7, 2007 Shift-reduce parsing,
Bottom Up Parsing.
Chapter 4-2 Chang Chi-Chung Bottom-Up Parsing LR methods (Left-to-right, Rightmost derivation)  LR(0), SLR, Canonical LR = LR(1), LALR Other.
Prof. Fateman CS 164 Lecture 91 Bottom-Up Parsing Lecture 9.
Table-driven parsing Parsing performed by a finite state machine. Parsing algorithm is language-independent. FSM driven by table (s) generated automatically.
1 Bottom-up parsing Goal of parser : build a derivation –top-down parser : build a derivation by working from the start symbol towards the input. builds.
Bottom-up parsing Goal of parser : build a derivation
LALR Parsing Canonical sets of LR(1) items
Syntax and Semantics Structure of programming languages.
410/510 1 of 21 Week 2 – Lecture 1 Bottom Up (Shift reduce, LR parsing) SLR, LR(0) parsing SLR parsing table Compiler Construction.
Chap. 6, Bottom-Up Parsing J. H. Wang May 17, 2011.
1 Lecture 15: Introduction to Logic Programming with Prolog (Section 11.3) A modification of slides developed by Felix Hernandez-Campos at UNC Chapel Hill.
1 Compiler Construction Syntax Analysis Top-down parsing.
Syntactic Analysis Natawut Nupairoj, Ph.D. Department of Computer Engineering Chulalongkorn University.
1 LR Parsers  The most powerful shift-reduce parsing (yet efficient) is: LR(k) parsing. LR(k) parsing. left to right right-most k lookhead scanning derivation.
Chapter 3-3 Chang Chi-Chung Bottom-Up Parsing LR methods (Left-to-right, Rightmost derivation)  LR(0), SLR, Canonical LR = LR(1), LALR 
Syntax and Semantics Structure of programming languages.
Chapter 5: Bottom-Up Parsing (Shift-Reduce)
Prof. Necula CS 164 Lecture 8-91 Bottom-Up Parsing LR Parsing. Parser Generators. Lecture 6.
111 Chapter 6 LR Parsing Techniques Prof Chung. 1.
1 Syntax Analysis Part II Chapter 4 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
Top-Down Parsing CS 671 January 29, CS 671 – Spring Where Are We? Source code: if (b==0) a = “Hi”; Token Stream: if (b == 0) a = “Hi”; Abstract.
C. Varela1 Logic Programming (PLP 11, CTM 9.1) Terms, Resolution, Unification, Search, Backtracking (Prolog) Relational Computation Model (Oz) Carlos Varela.
Knowledge Based Information System
CS 330 Programming Languages 09 / 25 / 2007 Instructor: Michael Eckmann.
Bernd Fischer RW713: Compiler and Software Language Engineering.
Bottom Up Parsing CS 671 January 31, CS 671 – Spring Where Are We? Finished Top-Down Parsing Starting Bottom-Up Parsing Lexical Analysis.
COMP 3438 – Part II-Lecture 6 Syntax Analysis III Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
1 Announcements We are almost done grading HW1 HW2 due today Download SWI Prolog! HW3 (Prolog) will be posted today, due on February 29 th Spring 16 CSCI.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 6: LR grammars and automatic parser generators.
1 Syntax Analysis Part II Chapter 4 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007.
Lecture 5: LR Parsing CS 540 George Mason University.
Compilers: Bottom-up/6 1 Compiler Structures Objective – –describe bottom-up (LR) parsing using shift- reduce and parse tables – –explain how LR.
Bottom-up parsing. Bottom-up parsing builds a parse tree from the leaves (terminals) to the start symbol int E T * TE+ T (4) (2) (3) (5) (1) int*+ E 
1 Chapter 6 Bottom-Up Parsing. 2 Bottom-up Parsing A bottom-up parsing corresponds to the construction of a parse tree for an input tokens beginning at.
Announcements Rainbow grades up HW1, Quiz 1-2 in HW Server Deadline for HW3 (Prolog) is extended to Monday, March 7 th. Submit in HW Server A few notes.
Spring 16 CSCI 4430, A Milanova 1 Announcements HW1 due on Monday February 8 th Name and date your submission Submit electronically in Homework Server.
Chapter 8. LR Syntactic Analysis Sung-Dong Kim, Dept. of Computer Engineering, Hansung University.
Eliminating Left-Recursion Where some of a nonterminal’s productions are left-recursive, top-down parsing is not possible “Immediate” left-recursion can.
COMPILER CONSTRUCTION
Syntax and Semantics Structure of programming languages.
Announcements/Reading
Programming Languages Translator
UNIT - 3 SYNTAX ANALYSIS - II
Chapter 4 Syntax Analysis.
Compiler Construction
Fall Compiler Principles Lecture 4: Parsing part 3
LALR Parsing Canonical sets of LR(1) items
Bottom-Up Syntax Analysis
Syntax Analysis Part II
Subject Name:COMPILER DESIGN Subject Code:10CS63
LR Parsing – The Tables Lecture 11 Wed, Feb 16, 2005.
Parsing #2 Leonidas Fegaras.
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
LALR Parsing Adapted from Notes by Profs Aiken and Necula (UCB) and
Parsing #2 Leonidas Fegaras.
Kanat Bolazar February 16, 2010
Announcements HW2 due on Tuesday Fall 18 CSCI 4430, A Milanova.
Chap. 3 BOTTOM-UP PARSING
Presentation transcript:

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

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

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

4 Programming Language Syntax Bottom-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

5 Model of the LR parser smsm XmXm s m-1 X m-1 … s0s0 LR Parser a1a1 … aiai … anan $$ Input: Stack: action goto State Grammar Symbol Parsing table: action[s, a ]: Do we shift or reduce? goto[s,A]: After reduction to nonterminal A, what state is pushed on top of the stack? Spring 16 CSCI 4430, A Milanova

start  expr expr  expr + term expr  term term  term *id term id 0 start  expr expr  expr + term 1 expr expr  term term  term *id 2 term term  id 3 id expr  expr + term term  term *id term  id 4 + term  term * id 6 * term expr  expr+term term  term *id 5 * term  term *id 7 Characteristic Finite State Machine (CFSM) id 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

start  expr expr  expr + term expr  term term  term *id term id 0 start  expr expr  expr + term 1 expr expr  term term  term *id 2 term term  id 3 id expr  expr + term term  term *id term  id 4 + term  term * id 6 * term expr  expr+term term  term *id 5 * term  term *id 7 Question id 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?

start  expr expr  expr+term expr  term term  term *id term id 0 start  expr expr  expr + term 1 expr expr  term term  term *id 2 term term  id 3 id expr  expr+term term  term *id term  id 4 + term  term * id 6 * term expr  expr+term term  term *id 5 * term  term * id 7 CFSM id 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

“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

start  expr expr  expr+term expr  term term  term *id term id 0 start  expr expr  expr + term 1 expr expr  term term  term *id 2 term term  id 3 id expr  expr+term term  term *id term  id 4 + term  term * id 6 * term expr  expr+term term  term *id 5 * term  term * id 7 CFSM 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

11 From CFSM to SLR(1) Parsing Table 1.expr  expr + term3. term  term * id 2.expr  term 4. term  id id+*$$ exprterm 0shift 312 1shift 4accept 2reduce 2shift 5reduce 2 3reduce 4 4shift 36 5shift 7 6reduce 1shift 5reduce 1 7reduce 3 state White – action table Blue – goto table

Spring 16 CSCI 4430, A Milanova 12 SLR(1) Parsing Table Input: An augmented grammar G’ (G with starting production start  …) Output: Functions action and goto for G’ Construct C = {I 0,I 1,…I n } the collection of sets of LR items with transitions State i is constructed from I i. The parsing actions for state i are a) If item A  α a β is in I i and there is a transition from I i to I j on a, then set action[i, a ] to “shift j” b) If item A  α is in I i then set action[i, a ] to “reduce by A  α” for all terminals a in FOLLOW(A) c) If start  … is in I i 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 I i to I j on A, set goto[i,A]=j If the table contains no multiply-defined entries, the grammar is said to be SLR(1)

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 β’

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

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

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

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, id list_prefix  id

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

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

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 $$,+

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

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?

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

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

Spring 16 CSCI 4430, A Milanova 25 Moving on: Logic Programming Download and install SWI Prolog on laptop! J.R.Fisher’s Prolog Tutorial: nts.html

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

Spring 16 CSCI 4430, A Milanova 27 Logic Programming Prolog 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()); }

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

29 Logic Programming Concepts A Horn Clause is: H  B 1,B 2,…,B n 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 B 1, B 2,..., and B n are all true When the antecedents B i 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).

Spring 16 CSCI 4430, A Milanova 30 Resolution Principle If two Horn clauses are such that A matches D 1, then we can replace D 1 with B 1,B 2,B 3,…, B m For example: A  B 1,B 2,B 3,…, B m C  D 1,D 2,D 3,…, D n C  B 1,B 2,B 3,…, B m,D 2,D 3,…, D n C  A,B D  C

Spring 16 CSCI 4430, A Milanova 31 Horn Clauses in Prolog In Prolog, a Horn clause is written h :- b 1,...,b n. 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).

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_var In 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) 32 Spring 16 CSCI 4430, A Milanova

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

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

35 Queries likes(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 with variable binding force search for more answers Spring 16 CSCI 4430, A Milanova

36 Harder Queries likes(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

37 Harder Queries likes(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

38 Rules likes(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

Spring 16 CSCI 4430, A Milanova 39 Rules likes(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).

Spring 16 CSCI 4430, A Milanova 40 Queen Victoria Example male(albert). male(edward). Put all clauses in file female(alice). family.pl female(victoria). parents(edward,victoria,albert). parents(alice,victoria,albert). ?- [family]. Loads file family.pl true. ?- male(albert). a query true. ?- male(alice). false. ?- parents(edward,victoria,albert). true. ?- parents(bullwinkle,victoria,albert). false. cf Clocksin and Mellish

Spring 16 CSCI 4430, A Milanova 41 Queen Victoria Example ?-female(X). a query X = alice ; ; asks for more answers X = 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!

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 : not asking for more answers ?- sister_of(alice, victoria). false.

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

Next Class Read Chapter Bring your laptops. We will be doing in-class exercises in Prolog. Spring 16 CSCI 4430, A Milanova 44