introduction to programming -...
TRANSCRIPT
![Page 1: Introduction to Programming - OpenClassroomopenclassroom.stanford.edu/MainFolder/courses/Compilers/...Alex Aiken SLR Parsing •LR(0) Parsing: Assume –stack contains a –next input](https://reader033.vdocuments.mx/reader033/viewer/2022052519/5f1a4c1950bdcf6c4f694aac/html5/thumbnails/1.jpg)
Alex Aiken
Compilers
SLR Parsing
![Page 2: Introduction to Programming - OpenClassroomopenclassroom.stanford.edu/MainFolder/courses/Compilers/...Alex Aiken SLR Parsing •LR(0) Parsing: Assume –stack contains a –next input](https://reader033.vdocuments.mx/reader033/viewer/2022052519/5f1a4c1950bdcf6c4f694aac/html5/thumbnails/2.jpg)
Alex Aiken
SLR Parsing
• LR(0) Parsing: Assume – stack contains a – next input is t – DFA on input a terminates in state s
• Reduce by X b if – s contains item X b.
• Shift if – s contains item X b.tw – equivalent to saying s has a transition labeled t
![Page 3: Introduction to Programming - OpenClassroomopenclassroom.stanford.edu/MainFolder/courses/Compilers/...Alex Aiken SLR Parsing •LR(0) Parsing: Assume –stack contains a –next input](https://reader033.vdocuments.mx/reader033/viewer/2022052519/5f1a4c1950bdcf6c4f694aac/html5/thumbnails/3.jpg)
Alex Aiken
SLR Parsing
• LR(0) has a reduce/reduce conflict if:
– Any state has two reduce items:
– X b. and Y w.
• LR(0) has a shift/reduce conflict if:
– Any state has a reduce item and a shift item:
– X b. and Y w.td
![Page 4: Introduction to Programming - OpenClassroomopenclassroom.stanford.edu/MainFolder/courses/Compilers/...Alex Aiken SLR Parsing •LR(0) Parsing: Assume –stack contains a –next input](https://reader033.vdocuments.mx/reader033/viewer/2022052519/5f1a4c1950bdcf6c4f694aac/html5/thumbnails/4.jpg)
Alex Aiken
SLR Parsing
S’ . E
E . T
E .T + E
T .(E)
T .int * T
T .int
S’ E . E T.
E T. + E
T int. * T
T int.
T (. E)
E .T
E .T + E
T .(E)
T .int * T
T .int
E T + E.
E T + . E
E .T
E .T + E
T .(E)
T .int * T
T .int
T int * .T
T .(E)
T .int * T
T .int
T int * T.
T (E.)
T (E).
E T
(
int
int *
)
E
E
T
int
(
(
int
T +
(
T
![Page 5: Introduction to Programming - OpenClassroomopenclassroom.stanford.edu/MainFolder/courses/Compilers/...Alex Aiken SLR Parsing •LR(0) Parsing: Assume –stack contains a –next input](https://reader033.vdocuments.mx/reader033/viewer/2022052519/5f1a4c1950bdcf6c4f694aac/html5/thumbnails/5.jpg)
Alex Aiken
SLR Parsing
• SLR = “Simple LR”
• SLR improves on LR(0) shift/reduce heuristics
– Fewer states have conflicts
![Page 6: Introduction to Programming - OpenClassroomopenclassroom.stanford.edu/MainFolder/courses/Compilers/...Alex Aiken SLR Parsing •LR(0) Parsing: Assume –stack contains a –next input](https://reader033.vdocuments.mx/reader033/viewer/2022052519/5f1a4c1950bdcf6c4f694aac/html5/thumbnails/6.jpg)
Alex Aiken
SLR Parsing
• Idea: Assume – stack contains a – next input is t – DFA on input a terminates in state s
• Reduce by X b if
– s contains item X b. – t Follow(X)
• Shift if
– s contains item X b.tw
![Page 7: Introduction to Programming - OpenClassroomopenclassroom.stanford.edu/MainFolder/courses/Compilers/...Alex Aiken SLR Parsing •LR(0) Parsing: Assume –stack contains a –next input](https://reader033.vdocuments.mx/reader033/viewer/2022052519/5f1a4c1950bdcf6c4f694aac/html5/thumbnails/7.jpg)
Alex Aiken
SLR Parsing
• If there are conflicts under these rules, the grammar is not SLR
• The rules amount to a heuristic for detecting handles
– The SLR grammars are those where the heuristics detect exactly the handles
![Page 8: Introduction to Programming - OpenClassroomopenclassroom.stanford.edu/MainFolder/courses/Compilers/...Alex Aiken SLR Parsing •LR(0) Parsing: Assume –stack contains a –next input](https://reader033.vdocuments.mx/reader033/viewer/2022052519/5f1a4c1950bdcf6c4f694aac/html5/thumbnails/8.jpg)
Alex Aiken
SLR Parsing
S’ . E
E . T
E .T + E
T .(E)
T .int * T
T .int
S’ E . E T.
E T. + E
T int. * T
T int.
T (. E)
E .T
E .T + E
T .(E)
T .int * T
T .int
E T + E.
E T + . E
E .T
E .T + E
T .(E)
T .int * T
T .int
T int * .T
T .(E)
T .int * T
T .int
T int * T.
T (E.)
T (E).
E T
(
int
int *
)
E
E
T
int
(
(
int
T +
(
T
![Page 9: Introduction to Programming - OpenClassroomopenclassroom.stanford.edu/MainFolder/courses/Compilers/...Alex Aiken SLR Parsing •LR(0) Parsing: Assume –stack contains a –next input](https://reader033.vdocuments.mx/reader033/viewer/2022052519/5f1a4c1950bdcf6c4f694aac/html5/thumbnails/9.jpg)
Alex Aiken
SLR Parsing
• Lots of grammars aren’t SLR
– including all ambiguous grammars
• We can parse more grammars by using precedence declarations
– Instructions for resolving conflicts
![Page 10: Introduction to Programming - OpenClassroomopenclassroom.stanford.edu/MainFolder/courses/Compilers/...Alex Aiken SLR Parsing •LR(0) Parsing: Assume –stack contains a –next input](https://reader033.vdocuments.mx/reader033/viewer/2022052519/5f1a4c1950bdcf6c4f694aac/html5/thumbnails/10.jpg)
Alex Aiken
SLR Parsing
• Consider our favorite ambiguous grammar:
– E E + E | E * E | (E) | int
• The DFA for this grammar contains a state with the following items:
– E E * E . E E . + E
– shift/reduce conflict!
• Declaring “* has higher precedence than +” resolves this conflict in favor of reducing
![Page 11: Introduction to Programming - OpenClassroomopenclassroom.stanford.edu/MainFolder/courses/Compilers/...Alex Aiken SLR Parsing •LR(0) Parsing: Assume –stack contains a –next input](https://reader033.vdocuments.mx/reader033/viewer/2022052519/5f1a4c1950bdcf6c4f694aac/html5/thumbnails/11.jpg)
Alex Aiken
SLR Parsing
• The term “precedence declaration” is misleading
• These declarations do not define precedence; they define conflict resolutions
– Not quite the same thing!
![Page 12: Introduction to Programming - OpenClassroomopenclassroom.stanford.edu/MainFolder/courses/Compilers/...Alex Aiken SLR Parsing •LR(0) Parsing: Assume –stack contains a –next input](https://reader033.vdocuments.mx/reader033/viewer/2022052519/5f1a4c1950bdcf6c4f694aac/html5/thumbnails/12.jpg)
Alex Aiken
SLR Parsing
1. Let M be DFA for viable prefixes of G 2. Let |x1…xn$ be initial configuration 3. Repeat until configuration is S|$
• Let a|w be current configuration • Run M on current stack a • If M rejects a, report parsing error
• Stack a is not a viable prefix
• If M accepts a with items I, let a be next input • Shift if X b. a g I • Reduce if X b. I and a Follow(X) • Report parsing error if neither applies
![Page 13: Introduction to Programming - OpenClassroomopenclassroom.stanford.edu/MainFolder/courses/Compilers/...Alex Aiken SLR Parsing •LR(0) Parsing: Assume –stack contains a –next input](https://reader033.vdocuments.mx/reader033/viewer/2022052519/5f1a4c1950bdcf6c4f694aac/html5/thumbnails/13.jpg)
Alex Aiken
SLR Parsing
• If there is a conflict in the last step, grammar is not SLR(k)
• k is the amount of lookahead
– In practice k = 1