+ All Categories
Home > Documents > CS3230R. What is a parser? What is an LR parser? A bottom-up parser that efficiently handles...

CS3230R. What is a parser? What is an LR parser? A bottom-up parser that efficiently handles...

Date post: 19-Jan-2016
Category:
Upload: corey-berry
View: 216 times
Download: 2 times
Share this document with a friend
25
LR PARSERS CS3230R
Transcript
Page 1: CS3230R. What is a parser? What is an LR parser? A bottom-up parser that efficiently handles deterministic context-free languages in guaranteed linear.

LR PARSERSCS3230R

Page 2: CS3230R. What is a parser? What is an LR parser? A bottom-up parser that efficiently handles deterministic context-free languages in guaranteed linear.

What is a parser?

Page 3: CS3230R. What is a parser? What is an LR parser? A bottom-up parser that efficiently handles deterministic context-free languages in guaranteed linear.

What is an LR parser?

A bottom-up parser that efficiently handles deterministic context-free languages in

guaranteed linear time

Page 4: CS3230R. What is a parser? What is an LR parser? A bottom-up parser that efficiently handles deterministic context-free languages in guaranteed linear.

Deterministic context-free what?

Class of context-free languages that can be accepted by a deterministic pushdown

automaton

Page 5: CS3230R. What is a parser? What is an LR parser? A bottom-up parser that efficiently handles deterministic context-free languages in guaranteed linear.

Deterministic context-free what? Unambiguous Of great practical use

Page 6: CS3230R. What is a parser? What is an LR parser? A bottom-up parser that efficiently handles deterministic context-free languages in guaranteed linear.

Context-sensitive grammars

int x;typedef int x;

Page 7: CS3230R. What is a parser? What is an LR parser? A bottom-up parser that efficiently handles deterministic context-free languages in guaranteed linear.

Context-sensitive grammars

MyObject object;MyObject object(parameters);MyObject object(); // ?

Page 8: CS3230R. What is a parser? What is an LR parser? A bottom-up parser that efficiently handles deterministic context-free languages in guaranteed linear.

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: CS3230R. What is a parser? What is an LR parser? A bottom-up parser that efficiently handles deterministic context-free languages in guaranteed linear.

What is an LR parser?

Bottom-up construction of syntax tree

Page 10: CS3230R. What is a parser? What is an LR parser? A bottom-up parser that efficiently handles deterministic context-free languages in guaranteed linear.

What is an LR parser?

Unsuited for human languages, which require more flexible but slower methods

Shift-reduce parser

Page 11: CS3230R. What is a parser? What is an LR parser? A bottom-up parser that efficiently handles deterministic context-free languages in guaranteed linear.

Shift-reduce

ShiftAdvances 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: CS3230R. What is a parser? What is an LR parser? A bottom-up parser that efficiently handles deterministic context-free languages in guaranteed linear.

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: CS3230R. What is a parser? What is an LR parser? A bottom-up parser that efficiently handles deterministic context-free languages in guaranteed linear.

Decisions, decisions…

When to shift, when to reduce?

Page 14: CS3230R. What is a parser? What is an LR parser? A bottom-up parser that efficiently handles deterministic context-free languages in guaranteed linear.

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: CS3230R. What is a parser? What is an LR parser? A bottom-up parser that efficiently handles deterministic context-free languages in guaranteed linear.

Decisions, decisions…

Observation:CFGs are unambiguousFinite number of states

Page 16: CS3230R. What is a parser? What is an LR parser? A bottom-up parser that efficiently handles deterministic context-free languages in guaranteed linear.

Decisions, decisions… Preprocessing

Encode each state the parser may be in with a number

Goal → Sums EOF

Sums → Sums + Products

Sums → Products

Products → Products * Value

Products → Value

Value → int

Value → id

Page 17: CS3230R. What is a parser? What is an LR parser? A bottom-up parser that efficiently handles deterministic context-free languages in guaranteed linear.

Decisions, decisions… Preprocessing

Encode each state the parser may be in with a number

Sums → ●Sums + Products

Sums → Sums ● + Products

Sums → Sums + ● Products

Sums → Sums + Products ●

Page 18: CS3230R. What is a parser? What is an LR parser? A bottom-up parser that efficiently handles deterministic context-free languages in guaranteed linear.

Decisions, decisions…

PreprocessingDecide in advance whether to shift or

reduceNote what state the parser will be in

afterwardsBuild a static parse table with this

information

Page 19: CS3230R. What is a parser? What is an LR parser? A bottom-up parser that efficiently handles deterministic context-free languages in guaranteed linear.

Decisions, decisions…

Parse table

Page 20: CS3230R. What is a parser? What is an LR parser? A bottom-up parser that efficiently handles deterministic context-free languages in guaranteed linear.

Decisions, decisions…

LR parsers are table-driven FSMs

Page 21: CS3230R. What is a parser? What is an LR parser? A bottom-up parser that efficiently handles deterministic context-free languages in guaranteed linear.

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: CS3230R. What is a parser? What is an LR parser? A bottom-up parser that efficiently handles deterministic context-free languages in guaranteed linear.

Example<expression> ::= <number> | <string> | <boolean> | <pair> | fun (<id>, ...) -> <expression> | <id> | (<expression>) | <unary-op> <expression> | <expression> <binary-op> <expression>

Page 23: CS3230R. What is a parser? What is an LR parser? A bottom-up parser that efficiently handles deterministic context-free languages in guaranteed linear.

Example<expression> ::= | if <expression> then <expression> else <expression> | let <id> = <expression> in <expression> | <id>(<expression>, ...)

Page 24: CS3230R. What is a parser? What is an LR parser? A bottom-up parser that efficiently handles deterministic context-free languages in guaranteed linear.

Example

Page 25: CS3230R. What is a parser? What is an LR parser? A bottom-up parser that efficiently handles deterministic context-free languages in guaranteed linear.

Thanks!


Recommended