LR PARSERS
description
Transcript of LR PARSERS
![Page 1: LR PARSERS](https://reader035.fdocuments.us/reader035/viewer/2022081505/56816946550346895de0d3c7/html5/thumbnails/1.jpg)
LR PARSERSCS3230R
![Page 2: LR PARSERS](https://reader035.fdocuments.us/reader035/viewer/2022081505/56816946550346895de0d3c7/html5/thumbnails/2.jpg)
What is a parser?
![Page 3: LR PARSERS](https://reader035.fdocuments.us/reader035/viewer/2022081505/56816946550346895de0d3c7/html5/thumbnails/3.jpg)
What is an LR parser?
A bottom-up parser that efficiently handles deterministic context-free languages in
guaranteed linear time
![Page 4: LR PARSERS](https://reader035.fdocuments.us/reader035/viewer/2022081505/56816946550346895de0d3c7/html5/thumbnails/4.jpg)
Deterministic context-free what?
Class of context-free languages that can be accepted by a deterministic pushdown
automaton
![Page 5: LR PARSERS](https://reader035.fdocuments.us/reader035/viewer/2022081505/56816946550346895de0d3c7/html5/thumbnails/5.jpg)
Deterministic context-free what? Unambiguous Of great practical use
![Page 6: LR PARSERS](https://reader035.fdocuments.us/reader035/viewer/2022081505/56816946550346895de0d3c7/html5/thumbnails/6.jpg)
Context-sensitive grammars
int x;typedef int x;
![Page 7: LR PARSERS](https://reader035.fdocuments.us/reader035/viewer/2022081505/56816946550346895de0d3c7/html5/thumbnails/7.jpg)
Context-sensitive grammars
MyObject object;MyObject object(parameters);MyObject object(); // ?
![Page 8: LR PARSERS](https://reader035.fdocuments.us/reader035/viewer/2022081505/56816946550346895de0d3c7/html5/thumbnails/8.jpg)
What is an LR parser? Left-to-right, Rightmost derivation Deterministic – single correct parse
without guesswork or backtracking Lookahead to avoid guessing or
backtracking
![Page 9: LR PARSERS](https://reader035.fdocuments.us/reader035/viewer/2022081505/56816946550346895de0d3c7/html5/thumbnails/9.jpg)
What is an LR parser? Bottom-up construction of syntax tree
![Page 10: LR PARSERS](https://reader035.fdocuments.us/reader035/viewer/2022081505/56816946550346895de0d3c7/html5/thumbnails/10.jpg)
What is an LR parser? Unsuited for human languages, which
require more flexible but slower methods Shift-reduce parser
![Page 11: LR PARSERS](https://reader035.fdocuments.us/reader035/viewer/2022081505/56816946550346895de0d3c7/html5/thumbnails/11.jpg)
Shift-reduce Shift
Advances the input stream by one symbolThat symbol becomes a new single-node
parse tree Reduce
Applies a grammar rule to some of the recent parse trees
Joins them into one with a new root symbol
![Page 12: LR PARSERS](https://reader035.fdocuments.us/reader035/viewer/2022081505/56816946550346895de0d3c7/html5/thumbnails/12.jpg)
Shift-reduce Shifts and reduces until all input has
been consumed, or until a syntax error is encountered
Differs from other parsers in when to reduce and breaking ties between rules
![Page 13: LR PARSERS](https://reader035.fdocuments.us/reader035/viewer/2022081505/56816946550346895de0d3c7/html5/thumbnails/13.jpg)
Decisions, decisions… When to shift, when to reduce?
![Page 14: LR PARSERS](https://reader035.fdocuments.us/reader035/viewer/2022081505/56816946550346895de0d3c7/html5/thumbnails/14.jpg)
Decisions, decisions… Naïve approach:
Compare current parse trees against rulesReduce if any matchesShift if no matches
Check all rules every time Parser gets slower and slower for large
input programs
![Page 15: LR PARSERS](https://reader035.fdocuments.us/reader035/viewer/2022081505/56816946550346895de0d3c7/html5/thumbnails/15.jpg)
Decisions, decisions… Observation:
CFGs are unambiguousFinite number of states
![Page 16: LR PARSERS](https://reader035.fdocuments.us/reader035/viewer/2022081505/56816946550346895de0d3c7/html5/thumbnails/16.jpg)
Decisions, decisions… Preprocessing
Encode each state the parser may be in with a number
Goal → Sums EOFSums → Sums + ProductsSums → ProductsProducts → Products * ValueProducts → ValueValue → intValue → id
![Page 17: LR PARSERS](https://reader035.fdocuments.us/reader035/viewer/2022081505/56816946550346895de0d3c7/html5/thumbnails/17.jpg)
Decisions, decisions… Preprocessing
Encode each state the parser may be in with a number
Sums → ●Sums + ProductsSums → Sums ● + ProductsSums → Sums + ● ProductsSums → Sums + Products ●
![Page 18: LR PARSERS](https://reader035.fdocuments.us/reader035/viewer/2022081505/56816946550346895de0d3c7/html5/thumbnails/18.jpg)
Decisions, decisions… Preprocessing
Decide in advance whether to shift or reduce
Note what state the parser will be in afterwards
Build a static parse table with this information
![Page 19: LR PARSERS](https://reader035.fdocuments.us/reader035/viewer/2022081505/56816946550346895de0d3c7/html5/thumbnails/19.jpg)
Decisions, decisions… Parse table
![Page 20: LR PARSERS](https://reader035.fdocuments.us/reader035/viewer/2022081505/56816946550346895de0d3c7/html5/thumbnails/20.jpg)
Decisions, decisions… LR parsers are table-driven FSMs
![Page 21: LR PARSERS](https://reader035.fdocuments.us/reader035/viewer/2022081505/56816946550346895de0d3c7/html5/thumbnails/21.jpg)
Decisions, decisions… LR parsers are table-driven FSMs Tables are large and difficult to compute
by hand LR parsers are generated from
grammars written in DSLs
![Page 22: LR PARSERS](https://reader035.fdocuments.us/reader035/viewer/2022081505/56816946550346895de0d3c7/html5/thumbnails/22.jpg)
Example<expression> ::= <number> | <string> | <boolean> | <pair> | fun (<id>, ...) -> <expression> | <id> | (<expression>) | <unary-op> <expression> | <expression> <binary-op> <expression>
![Page 23: LR PARSERS](https://reader035.fdocuments.us/reader035/viewer/2022081505/56816946550346895de0d3c7/html5/thumbnails/23.jpg)
Example<expression> ::= | if <expression> then <expression> else <expression> | let <id> = <expression> in <expression> | <id>(<expression>, ...)
![Page 24: LR PARSERS](https://reader035.fdocuments.us/reader035/viewer/2022081505/56816946550346895de0d3c7/html5/thumbnails/24.jpg)
Example
![Page 25: LR PARSERS](https://reader035.fdocuments.us/reader035/viewer/2022081505/56816946550346895de0d3c7/html5/thumbnails/25.jpg)
Thanks!