Parser Combinators in Smalltalk 1 Tims Trick Whats so cool about - - PowerPoint PPT Presentation

parser combinators
SMART_READER_LITE
LIVE PREVIEW

Parser Combinators in Smalltalk 1 Tims Trick Whats so cool about - - PowerPoint PPT Presentation

Parser Combinators in Smalltalk 1 Tims Trick Whats so cool about functional programming? referential transparency if x = foo stuff , then x + x = foo stuff + foo stuff = is mathematical equality, and you can replace equal


slide-1
SLIDE 1

Parser Combinators

in Smalltalk

1

slide-2
SLIDE 2

Tim’s Trick

  • What’s so cool about functional

programming?

  • referential transparency
  • if x = foo stuff, then

x + x = foo stuff + foo stuff

  • = is mathematical equality, and you can replace

equal by equal

2

slide-3
SLIDE 3

What about effects?

  • Suppose that x is not just a value but

has an effect:

if x = nextInt input, then x + x ≠ nextInt input + nextInt input

  • In the presence of effects, you can’t

replace equals by equals

  • Say “bye bye” to equational reasoning

3

slide-4
SLIDE 4

How does Haskell combine equational reasoning and effects?

4

slide-5
SLIDE 5

How does Haskell combine equational reasoning and effects?

4

slide-6
SLIDE 6

How does Haskell combine equational reasoning and effects?

  • Monads separate the definition of

commands from the execution of commands

4

slide-7
SLIDE 7

How does Haskell combine equational reasoning and effects?

  • Monads separate the definition of

commands from the execution of commands

  • Command definition is still referentially

transparent

4

slide-8
SLIDE 8

How does Haskell combine equational reasoning and effects?

  • Monads separate the definition of

commands from the execution of commands

  • Command definition is still referentially

transparent

  • Command execution is “special”
  • do syntax

4

slide-9
SLIDE 9

What about other languages?

  • Can we separate command definition

and command execution in Java, or in Smalltalk?

  • Yes!
  • but the defaults are backwards compared to

Haskell

  • methods can be executed (= do),
  • but that’s the default !
  • How can they be manipulated?

5

slide-10
SLIDE 10

We have sequential composition

  • BNF:

ifStmnt ::= if boolean then stmts* else stmts* fi

  • code:

parseIfStmnt parseKeyword: #if. parseBoolean. parseKeyword: #then. parseStatements. parseKeyword: #else. parseStatements. parse: #fi

6

slide-11
SLIDE 11

but that’s not enough

  • BNF:

stmts* ::= ε | stmt stmt*

  • code:

parseStatements ??

7

slide-12
SLIDE 12

How can we define Squeak combinators for Parsers?

  • make the parsers objects, not methods
  • the parser objects can be
  • 1. run, to perform the parse, or
  • 2. combined, to make more complex parsers

8

slide-13
SLIDE 13

First attempt

9

slide-14
SLIDE 14

First attempt

  • What kind of Smalltalk object will

perform an action when run?

9

slide-15
SLIDE 15

First attempt

  • What kind of Smalltalk object will

perform an action when run?

  • blocks

9

slide-16
SLIDE 16

What about the input ?

  • We need a way of representing a

sequence of objects along with the current position in that sequence

  • Streams provide exactly that

functionality

  • Compare Streams with Iterators

10

slide-17
SLIDE 17

Iterating with do: and with a Stream

  • initialize the collection

c := 'abcde'.

  • with an internal iterator:

c do: [ : each | Transcript show: each ]. Transcript cr.

  • with a Stream (external iterator)

s := ReadStream on: c. [ s atEnd ] whileFalse: [ Transcript show: s next ]. Transcript cr.

11

slide-18
SLIDE 18

Implementing Parsers

  • A parser is implemented as a block.
  • The result of evaluating the block is:
  • if the parse succeeds: a sequence of correctly

parsed items

  • if the parse fails: nil.
  • The effect of evaluating the block is to:
  • advance the underlying stream if the parse succeeds,

and

  • not to advance it if the parse fails.
  • This invariant must be maintained across

compound parsers.

12