CSCI 3130: Formal languages and automata theory Tutorial 6

Slides:



Advertisements
Similar presentations
Parsing V: Bottom-up Parsing
Advertisements

CSCI 3130: Formal languages and automata theory Tutorial 9
A question from last class: construct the predictive parsing table for this grammar: S->i E t S e S | i E t S | a E -> B.
CSCI 3130: Formal languages and automata theory Tutorial 5
Compiler Designs and Constructions
Transparency No. 2-1 Formal Language and Automata Theory Homework 3.
Bottom-up Parsing A general style of bottom-up syntax analysis, known as shift-reduce parsing. Two types of bottom-up parsing: Operator-Precedence parsing.
Pushdown Automata Consists of –Pushdown stack (can have terminals and nonterminals) –Finite state automaton control Can do one of three actions (based.
LR-Grammars LR(0), LR(1), and LR(K).
CS 310 – Fall 2006 Pacific University CS310 Pushdown Automata Sections: 2.2 page 109 October 9, 2006.
1 Normal Forms for Context-free Grammars. 2 Chomsky Normal Form All productions have form: variable and terminal.
CS 536 Spring Bottom-Up Parsing: Algorithms, part 1 LR(0), SLR Lecture 12.
Prof. Fateman CS 164 Lecture 91 Bottom-Up Parsing Lecture 9.
SLR PARSING TECHNIQUES Submitted By: Abhijeet Mohapatra 04CS1019.
LR(k) Parsing CPSC 388 Ellen Walker Hiram College.
CSCI 3130: Formal languages and automata theory Tutorial 2 Chin.
10/10/2002© 2002 Hal Perkins & UW CSED-1 CSE 582 – Compilers LR Parsing Hal Perkins Autumn 2002.
CSCI 3130: Formal languages and automata theory Tutorial 3 Chin.
CSCI 3130: Automata theory and formal languages Andrej Bogdanov The Chinese University of Hong Kong Pushdown.
CSCI 3130: Formal languages and automata theory Tutorial 1 Lee Chin Ho.
CSCI 3130: Automata theory and formal languages Andrej Bogdanov The Chinese University of Hong Kong LR(0) grammars.
LR Parser: LR parsing is a bottom up syntax analysis technique that can be applied to a large class of context free grammars. L is for left –to –right.
Bernd Fischer RW713: Compiler and Software Language Engineering.
Bottom-Up Parsing Algorithms LR(k) parsing L: scan input Left to right R: produce Rightmost derivation k tokens of lookahead LR(0) zero tokens of look-ahead.
Syntax and Semantics Structure of programming languages.
Recap: Nondeterministic Finite Automaton (NFA) A deterministic finite automaton (NFA) is a 5-tuple (Q, , ,s,F) where: Q is a finite set of elements called.
CS 404 Introduction to Compiler Design
Parsing Bottom Up CMPS 450 J. Moloney CMPS 450.
Programming Languages Translator
Bottom-up parsing Goal of parser : build a derivation
LR(k) grammars The Chinese University of Hong Kong Fall 2009
Bottom-Up Parsing.
Parsing IV Bottom-up Parsing
Table-driven parsing Parsing performed by a finite state machine.
CSE 105 theory of computation
COP4620 – Programming Language Translators Dr. Manuel E. Bermudez
PUSHDOWN AUTOMATA. PUSHDOWN AUTOMATA Hierarchy of languages Regular Languages  Finite State Machines, Regular Expression Context Free Languages 
Bottom-Up Syntax Analysis
Pushdown Automata Reading: Chapter 6.
Context-Free Languages
Regular Grammar - Finite Automaton
LR(0) grammars The Chinese University of Hong Kong Fall 2010
LR(1) grammars The Chinese University of Hong Kong Fall 2010
BOTTOM UP PARSING Lecture 16.
Bottom Up Parsing.
Context-Free Languages
NFAs, DFAs, and regular expressions
CSCI 2670 Introduction to Theory of Computing
CSCI 2670 Introduction to Theory of Computing
Parsing IV Bottom-up Parsing
LR Parsing. Parser Generators.
Parsers for programming languages
Parsers for programming languages
Chapter 2 Context-Free Language - 01
LR(1) grammars The Chinese University of Hong Kong Fall 2011
Compiler Construction
Compiler Construction
Bottom-up parsing is also known as shift-reduce parsing
CSE 105 theory of computation
FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY
Pushdown automata The Chinese University of Hong Kong Fall 2011
LR(k) grammars The Chinese University of Hong Kong Fall 2008
Context Free Grammars-II
Normal Forms for Context-free Grammars
CSCI 2670 Introduction to Theory of Computing
COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 7, 10/09/2003 Prof. Roy Levow.
CSCI 2670 Introduction to Theory of Computing
CSE 105 theory of computation
Parsing CSCI 432 Computer Science Theory
Presentation transcript:

CSCI 3130: Formal languages and automata theory Tutorial 6 Chin

Reminder Homework 4 is due next thursday.

LR(0) parsing A  a•Ab After reading an ‘a’, the next possible symbol is A  aA•b Or expanding the ‘A’ (think of aaAbb) A  •aAb

LR(0) parsing E  E+T | a T  b | c

LR(0) parsing E  E+T | a T  b | c E  •E+T E  E•+T E  E+•T Put a dot • in any possible positions These are valid items. The bottom ones are complete items. When you see complete items, reduce. E  •E+T E  E•+T E  E+•T E  E+T• E  •a E  a• T  •c T  c• T  •b T  b•

LR(0) parsing E  •E+T E  E•+T E  E+•T E  E+T• E  •a E  a• T  •c T  •b T  b• Suppose we are given a+b+c Before reading anything, the valid items are: E  •E+T, E  •a, T  •b, T  •c No complete items, shift. state stack valid items reading action 1 e E  •E+T, E  •a, T  •b, T  •c •a+b+c Shift

LR(0) parsing E  •E+T E  E•+T E  E+•T E  E+T• E  •a E  a• T  •c T  •b T  b• Read an input and push it in the stack. a•+b+c E  •E+T < fail E  •a T  •b < fail T  •c < fail E  •E+T E  a• T  •b T  •c a state stack valid items reading action 1 e E  •E+T, E  •a, T  •b, T  •c •a+b+c Shift 2 a

LR(0) parsing E  •E+T E  E•+T E  E+•T E  E+T• E  •a E  a• T  •c T  •b T  b• The valid item is E  a• When you see exactly one item and it is complete, reduce. * If there are more than 1 complete items, R/R conflict. * If there is another item, but it is not complete, S/R conflict. state stack valid items reading action 1 e E  •E+T, E  •a, T  •b, T  •c •a+b+c Shift 2 a E  a• a•+b+c Reduce

LR(0) parsing E  •E+T E  E•+T E  E+•T E  E+T• E  •a E  a• T  •c T  •b T  b• How to reduce: Look at the corresponding complete item (i.e. E  a•) R.H.S. has k symbols, pop k symbols out of the stack (i.e. pop ‘a’) Consider the valid items in corresponding state (i.e. items in state 1) Then push the symbol on the L.H.S. to the stack (i.e. push ‘E’) state stack valid items reading action 1 e E  •E+T, E  •a, T  •b, T  •c •a+b+c Shift 2 a E  a• a•+b+c Reduce E

LR(0) parsing E  •E+T E  E•+T E  E+•T E  E+T• E  •a E  a• T  •c T  •b T  b• E  •E+T E  •a < fail T  •b < fail T  •c < fail E  E•+T E  a• T  •b T  •c E state stack valid items reading action 2 a E  a• a•+b+c Reduce e E  •E+T, E  •a, T  •b, T  •c E E  E•+T

LR(0) parsing E  •E+T E  E•+T E  E+•T E  E+T• E  •a E  a• T  •c T  •b T  b• The valid item is: E  E•+T No complete items, shift. state stack valid items reading action 2 a E  a• a•+b+c Reduce e E  •E+T, E  •a, T  •b, T  •c E E  E•+T Shift

LR(0) parsing E  •E+T E  E•+T E  E+•T E  E+T• E  •a E  a• T  •c T  •b T  b• Read an input and push it in the stack. a+•b+c E  E•+T E  E+•T + state stack valid items reading action 2 a E  a• a•+b+c Reduce e E  •E+T, E  •a, T  •b, T  •c E E  E•+T Shift 3 E+ a+•b+c

LR(0) parsing E  •E+T E  E•+T E  E+•T E  E+T• E  •a E  a• T  •c T  •b T  b• The valid items are E  E+•T T  •b <- This comes from expanding T  •b T  •c <- This comes from expanding T  •c No complete items, shift. state stack valid items reading action 2 a E  a• a•+b+c Reduce e E  •E+T, E  •a, T  •b, T  •c E E  E•+T Shift 3 E+ E  E+•T, T  •b, T  •c a+•b+c

LR(0) parsing E  •E+T E  E•+T E  E+•T E  E+T• E  •a E  a• T  •c T  •b T  b• Read an input and push it in the stack. a+b•+c E  E+•T < fail T  •b T  •c < fail T  b• b state stack valid items reading action 3 E+ E  E+•T, T  •b, T  •c a+•b+c Shift 4 E+b

LR(0) parsing E  •E+T E  E•+T E  E+•T E  E+T• E  •a E  a• T  •c T  •b T  b• The valid item is T  b• When you see exactly one item and it is complete, reduce. state stack valid items reading action 3 E+ E  E+•T, T  •b, T  •c a+•b+c Shift 4 E+b T  b• a+b•+c Reduce

LR(0) parsing E  •E+T E  E•+T E  E+•T E  E+T• E  •a E  a• T  •c T  •b T  b• How to reduce: Look at the corresponding complete item (i.e. T  b•) R.H.S. has k symbols, pop k symbols out of the stack (i.e. pop ‘b’) Consider the valid items in corresponding state (i.e. items in state 3) Then push the symbol on the L.H.S. to the stack (i.e. push ‘T’) state stack valid items reading action 3 E+ E  E+•T, T  •b, T  •c a+•b+c Shift 4 E+b T  b• a+b•+c Reduce E+T

LR(0) parsing E  •E+T E  E•+T E  E+•T E  E+T• E  •a E  a• T  •c T  •b T  b• E  E+•T T  •b < fail T  •c < fail E  E+T• T  •b < fail T  •c < fail T state stack valid items reading action 4 E+b T  b• a+b•+c Reduce E+ E  E+•T, T  •b, T  •c E+T E  E+T•

LR(0) parsing E  •E+T E  E•+T E  E+•T E  E+T• E  •a E  a• T  •c T  •b T  b• The valid item is E  E+T• When you see exactly one item and it is complete, reduce. state stack valid items reading action 4 E+b T  b• a+b•+c Reduce E+ E  E+•T, T  •b, T  •c E+T E  E+T•

LR(0) parsing E  •E+T E  E•+T E  E+•T E  E+T• E  •a E  a• T  •c T  •b T  b• How to reduce: Look at the corresponding complete item (i.e. E  E+T•) R.H.S. has k symbols, pop k symbols out of the stack (i.e. pop ‘E+T’) Consider the valid items in corresponding state (i.e. items in state 1) Then push the symbol on the L.H.S. to the stack (i.e. push ‘E’) state stack valid items reading action 1 e E  •E+T, E  •a, T  •b, T  •c •a+b+c Shift … 4 E+T E  E+T• Reduce E

LR(0) parsing E  •E+T E  E•+T E  E+•T E  E+T• E  •a E  a• T  •c T  •b T  b• E  •E+T E  •a < fail T  •b < fail T  •c < fail E  E•+T E  a• T  •b T  •c E state stack valid items reading action 4 E+T E  E+T• a+b•+c Reduce e E  •E+T, E  •a, T  •b, T  •c E E  E•+T Shift

LR(0) parsing E  •E+T E  E•+T E  E+•T E  E+T• E  •a E  a• T  •c T  •b T  b• The valid item is: E  E•+T No complete items, shift. state stack valid items reading action 4 E+T E  E+T• a+b•+c Reduce e E  •E+T, E  •a, T  •b, T  •c E E  E•+T Shift

LR(0) parsing E  •E+T E  E•+T E  E+•T E  E+T• E  •a E  a• T  •c T  •b T  b• Read an input and push it in the stack. a+b+•c E  E•+T T  E+•T + state stack valid items reading action E E  E•+T a+b•+c Shift 5 E+ a+b+•c

LR(0) parsing E  •E+T E  E•+T E  E+•T E  E+T• E  •a E  a• T  •c T  •b T  b• The valid items are E  E+•T T  •b <- This comes from expanding T  •b T  •c <- This comes from expanding T  •c No complete items, shift. state stack valid items reading action E E  E•+T a+b•+c Shift 5 E+ E  E+•T, T  •b, T  •c a+b+•c

LR(0) parsing E  •E+T E  E•+T E  E+•T E  E+T• E  •a E  a• T  •c T  •b T  b• Read an input and push it in the stack. a+b+c• E  E+•T < fail T  •b < fail T  •c T  c• c state stack valid items reading action 4 E E  E•+T a+b•+c Shift 5 E+ E  E+•T, T  •b, T  •c a+b+•c

LR(0) parsing E  •E+T E  E•+T E  E+•T E  E+T• E  •a E  a• T  •c T  •b T  b• The valid item is T  c• When you see exactly one item and it is complete, reduce. state stack valid items reading action 5 E+ E  E+•T, T  •b, T  •c a+b+•c Shift 6 E+c T  •c a+b+c• Reduce

LR(0) parsing E  •E+T E  E•+T E  E+•T E  E+T• E  •a E  a• T  •c T  •b T  b• How to reduce: Look at the corresponding complete item (i.e. T  c•) R.H.S. has k symbols, pop k symbols out of the stack. (i.e. pop ‘c’) Consider the valid items in corresponding state (i.e. items in state 5) Then push the symbol on the L.H.S. to the stack (i.e. push ‘T’) state stack valid items reading action 6 E+c T  •c a+b+c• Reduce E+ E  E+•T, T  •b, T  •c E+T

LR(0) parsing E  •E+T E  E•+T E  E+•T E  E+T• E  •a E  a• T  •c T  •b T  b• E  E+•T T  •b < fail T  •c < fail E  E+T• T  •b < fail T  •c < fail T state stack valid items reading action 6 E+c T  •c a+b+c• Reduce E+ E  E+•T, T  •b, T  •c E+T

LR(0) parsing E  •E+T E  E•+T E  E+•T E  E+T• E  •a E  a• T  •c T  •b T  b• The valid item is E  E+T• When you see exactly one item and it is complete, reduce. state stack valid items reading action 6 E+c T  •c a+b+c• Reduce E+ E  E+•T, T  •b, T  •c E+T E  E+T•

LR(0) parsing E  •E+T E  E•+T E  E+•T E  E+T• E  •a E  a• T  •c T  •b T  b• How to reduce: Look at the corresponding complete item (i.e. E  E+T•) R.H.S. has k symbols, pop k symbols out of the stack. (i.e. pop ‘E+T’) Consider the valid items in corresponding state (i.e. items in state 1) Then push the symbol on the L.H.S. to the stack (i.e. push ‘E’) state stack valid items reading action 1 e E  •E+T, E  •a, T  •b, T  •c •a+b+c Shift … 6 E+ E  E+•T, T  •b, T  •c E+T E  E+T• Reduce

LR(0) parsing T  •c E  •a T  c• E  a• T  •b T  b• state stack valid items reading action 1 e E  •E+T, E  •a, T  •b, T  •c •a+b+c Shift 2 a E  a• a•+b+c Reduce E E  E•+T 3 E+ E  E+•T, T  •b, T  •c a+•b+c 4 E+b T  b• a+b•+c E+T E  E+T• 5 a+b+•c 6 E+c T  •c a+b+c• E  •a E  a• T  •c T  c• T  •b T  b•

LR(0) parsing E  •E+T E  E•+T E  E+•T E  E+T• E  •a E  a• T  •c T  •b T  b• How to reduce: Look at the corresponding complete item (i.e. E  E+T•) R.H.S. has k symbols, pop k symbols out of the stack. (i.e. pop ‘E+T’) Consider the valid items in corresponding state (i.e. items in state 1) Then push the symbol on the L.H.S. to the stack (i.e. push ‘E’) state stack valid items reading action 1 e E  •E+T, E  •a, T  •b, T  •c •a+b+c Shift … 6 E+ E  E+•T, T  •b, T  •c E+T E  E+T• Reduce

LR(0) parsing: NFA representation a, b: terminals A, B, C: variables a, b, d: mixed strings X: terminal or variable e notation q0 S  •a For every item S  •a X A  •X A  X• For every item A  •X e A  •C C  •d For every pair of items A  •C, C  •d

LR(0) parsing: NFA representation E  •E+T E  E•+T E  E+•T E  E+T• E  •a E  a• T  •c T  c• T  •b T  b• Each of these items is a state E  •E+T E  E•+T E  E+•T E  E+T• E  •a E  a• q0 T  •c T  c• T  •b T  b•

LR(0) parsing: NFA representation a, b: terminals A, B, C: variables a, b, d: mixed strings X: terminal or variable e notation q0 S  •a For every item S  •a E  •E+T E  E•+T E  E+•T E  E+T• e E  •a E  a• e q0 e T  •b T  b• e T  •c T  c•

LR(0) parsing: NFA representation a, b: terminals A, B, C: variables a, b, d: mixed strings X: terminal or variable notation X A  •X A  X• For every item A  •X E + T E  •E+T E  E•+T E  E+•T E  E+T• e a E  •a E  a• e q0 e b T  •b T  b• e c T  •c T  c•

LR(0) parsing: NFA representation a, b: terminals A, B, C: variables a, b, d: mixed strings X: terminal or variable notation e A  •C C  •d For every pair of items A  •C, C  •d E  •E+T E  E•+T E  E+•T E  E+T• E  •a E  a• T  •b T  b• T  •c T  c• q0 e E + T a b c

LR(0) parsing: NFA representation Convert the NFA to DFA E  •E+T E  •a T  •b T  •c E  E•+T E  E+•T E  E+T• E  a• T  b• T  c• E + T a b c 1 2 3 4 5 6 7

LR(0) parsing: remember state in stack When we reduce, we have to consider the valid items in the corresponding state in the past. How? Remember the states in the stack instead. E  •E+T E  •a T  •b T  •c E  E•+T E  E+•T E  E+T• E  a• T  b• T  c• E + T a b c 1 2 3 4 5 6 7

LR(0) parsing: remember state in stack E  •E+T E  •a T  •b T  •c E  E•+T E  E+•T E  E+T• E  a• T  b• T  c• E + T a b c 1 2 3 4 5 6 7 Stack State Reading Action  1 •a+b+c S 2 a•+b+c R 3 13 6 a+b•+c 136 4 7 a+b+•c 5 a+b+c•

End Questions?