OMeta an OO Language for Pattern Matching Alessandro Warth and Ian - - PowerPoint PPT Presentation

ometa an oo language for pattern matching
SMART_READER_LITE
LIVE PREVIEW

OMeta an OO Language for Pattern Matching Alessandro Warth and Ian - - PowerPoint PPT Presentation

OMeta an OO Language for Pattern Matching Alessandro Warth and Ian Piumarta UCLA / Viewpoints Research Institute 1 Programming language research idea prototype validation Lexical Analysis Parsing AST Transformations Code Generation 2


slide-1
SLIDE 1

OMeta an OO Language for Pattern Matching

Alessandro Warth and Ian Piumarta UCLA / Viewpoints Research Institute

1

slide-2
SLIDE 2

Lexical Analysis Parsing AST Transformations Code Generation

Programming language research

idea prototype validation

2

slide-3
SLIDE 3

lex, for lexical analysis yacc, for parsing visitors, for AST transformations and code generation

We have special weapons...

3

slide-4
SLIDE 4

... but prototypes are still “expensive”

idea prototype validation

prototype

what about me?

4

slide-5
SLIDE 5

Why do we care?

  • PL researchers have lots of ideas...
  • ... but can only afford to prototype

a few of the “more promising”

  • nes

5

slide-6
SLIDE 6

The ideal prototype

  • ... should be
  • quick to implement
  • easy to change
  • extensible
  • “efficient enough”

6

slide-7
SLIDE 7

OMeta

  • An Object-Oriented language for

Pattern Matching

  • Intended for rapid language prototyping

(but not limited to that domain)

  • OO: extend your prototypes using familiar

mechanisms

  • inheritance, overriding, ...

7

slide-8
SLIDE 8
  • OMeta compiler
  • JavaScript compiler
  • “almost” ECMA-262 compliant
  • Some JS extensions
  • Toylog interface to Prolog for children
  • me

t a

8

slide-9
SLIDE 9

Roadmap

  • OMeta’s pattern matching
  • Object-Oriented features
  • Other interesting features
  • Experience

9

slide-10
SLIDE 10

Why Pattern Matching?

  • lexical analysis: characters → tokens
  • parsing: tokens → parse trees
  • constant folding and other
  • ptimizations: parse trees → parse trees
  • (naive) code generation:

parse trees → code It’s a unifying idea!

10

slide-11
SLIDE 11

11

slide-12
SLIDE 12

Pattern Matching

  • ML-style pattern matching
  • Can you write a lexer / parser with it?
  • Yes, but...
  • “That’s what ML-lex and ML-yacc are for!”
  • OMeta is based on PEGs

12

slide-13
SLIDE 13

Parsing Expression Grammars (PEGs)

  • Recognition-based foundation for describing

syntax

  • Only prioritized choice
  • no ambiguities
  • easy to understand
  • Backtracking, unlimited lookahead
  • Semantic predicates, e.g., ?[x == y]

[Ford, ‘04]

13

slide-14
SLIDE 14

About the examples

  • 2 versions of OMeta:
  • OMeta/Squeak
  • OMeta/COLA
  • Slightly different syntaxes
  • Use different languages for semantic actions

and predicates

14

slide-15
SLIDE 15

( ):d :n :d :e :n => [d digitValue] => [n * 10 + d] => [{#plus. e. n}] dig ::= “0” | ... | “9” num ::= <num> <dig> | <dig> expr ::= <expr> “+” <num> | <num>

PEGs, OMeta style

15

slide-16
SLIDE 16

Increasing Generality

  • PEGs operate on streams of characters
  • OMeta operates on streams of objects
  • <anything> matches any one object
  • characters, e.g,. $x
  • strings, e.g., ‘hello’
  • numbers, e.g., 42
  • symbols, e.g, #answer
  • lists, e.g., {‘hello’ 42 #answer {}}

16

slide-17
SLIDE 17

Example: evaluating parse trees

num ::= <anything>:n ?[n isNumber] => [n] eval ::= {#plus <eval>:x <eval>:y} => [x + y] | <num>

{#plus. {#plus. 1. 2}. 3} → 6

17

slide-18
SLIDE 18

anything ::= ... ...

OMeta Base

OMeta is Object-Oriented

dig ::= (“0” | ... | “9”):d => [d digitValue] num ::= <num>:n <dig>:d => [n * 10 + d] | <dig> expr ::= <expr>:e “+” <num>:n => [{#plus. e. n}] | <num>

MyLang

expr ::= <expr>:e “-” <num>:n => [{#minus. e. n}] | <super #expr>

MyLang++

dig ::= (“0” | ... | “9”):d => [d digitValue] num ::= <num>:n <dig>:d => [n * 10 + d] | <dig> expr ::= <expr>:e “+” <num>:n => [{#plus. e. n}] | <num>

18

slide-19
SLIDE 19

Extensible pattern matching

meta NullOptimization {

  • pt ::= (OR <opt>*:xs) => `(OR ,@xs)

| (NOT <opt>:x) => `(NOT ,x) | (MANY <opt>:x) => `(MANY ,x) | (MANY1 <opt>:x) => `(MANY1 ,x) | (define <_>:n <opt>:v) => `(define ,n ,v) | (AND <opt>*:xs) => `(AND ,@xs) | (FORM <opt>*:xs) => `(FORM ,@xs) | <_>; } meta OROptimization <: NullOptimization {

  • pt ::= (OR <opt>:x) => x

| (OR <inside>:xs) => `(OR ,@xs) | <super opt>; inside ::= (OR <inside>:xs) <inside>:ys => (append xs ys) | <super opt>:x <inside>:xs => (cons x xs) | <empty> => nil; }

19

slide-20
SLIDE 20

Parameterized productions

digit ::= “0” | “1” | “2” | “3” | “4” | “5” | “6” | “7” | “8” | “9” digit ::= <range $0 $9> range :a :b ::= <anything>:x ?[x >= a] ?[x <= b] => [x]

20

slide-21
SLIDE 21

More about parameterized productions

  • The syntax

range :a :b ::= ... is really shorthand for

range ::= <anything>:a <anything>:b (...)

  • Arguments prepended to input stream
  • Enables pattern matching on arguments

fac 0 => [1] fac :n ::= <fac (n - 1)>:m => [n * m]

21

slide-22
SLIDE 22

Higher-order productions

formals ::= <name> (“,” <name>)* args ::= <expr> (“,” <expr>)* formals ::= <listOf #name> args ::= <listOf #expr> listOf :p ::= <apply p> (“,” <apply p>)*

22

slide-23
SLIDE 23

MetaCOLA = OMeta + COLA

OMeta Parser COLA Parser MetaCOLA Parser

  • duplicated effort
  • versioning problem

23

slide-24
SLIDE 24

Foreign production invocation

  • Lend input stream to another gramamar
  • Compose multiple grammars w/o worrying

about name clashes

meta MetaCOLA { mcola ::= <foreign OMeta ‘ometa> | <foreign COLA ‘cola>; }

24

slide-25
SLIDE 25

(define puts (lambda (s) (let ((idx 0)) (while (!= (char@ s idx) 0) (putchar (char@ s idx)) (set idx (+ idx 1))) (putchar 10))))

Lexically-scoped syntax extensions

25

slide-26
SLIDE 26

(define puts (lambda (s) (let ((idx 0)) (while (!= s[idx] 0) (putchar s[idx]) (set idx (+ idx 1))) (putchar 10))))

Lexically-scoped syntax extensions

26

slide-27
SLIDE 27

Lexically-scoped syntax extensions

(define puts (lambda (s) (let ((idx 0)) { cola ::= <cola>:a ’[’ <cola>:i ’]’ => `(char@ ,a ,i) | <super cola>; } (while (!= s[idx] 0) (putchar s[idx]) (set idx (+ idx 1))) (putchar 10)))) (puts "this is a test") ;; works (printf "%d\n" "abcd"[0]) ;; parse error!

27

slide-28
SLIDE 28

Experience

  • The OMeta compiler
  • parser, optimizer passes, codegen
  • JS compiler (OMeta/Squeak)
  • ~350 LOC (OMeta) for

parser, “declaration visitor”, codegen

  • ~1000 lines of JS for libraries
  • “almost” ECMA-262 compliant
  • me

t a

28

slide-29
SLIDE 29

Experience

  • MJavaScript = Javascript + Macro support
  • ~40 LOC, including additional syntax and macro

expansion pass

macro @repeat(numTimes, body) { var n = numTimes while (n-- > 0) body } @repeat(10 + 5, alert(“hello”))

(cont’d)

  • me

t a

29

slide-30
SLIDE 30

Experience

  • Toylog = Prolog front-end for children
  • ~70 LOC

Homer is Bart’s father. Marge is Bart’s mother. x is y’s parent if x is y’s father or

  • r x is y’s mother.

x is Bart’s parent?

  • me

t a

(cont’d)

30

slide-31
SLIDE 31

Selected Related Work

  • Parsing Expression Grammars [Ford, ‘04]
  • LISP70 Pattern Matcher [T

esler et al., ‘73]

  • Parser combinator libraries [Hutton, ‘92]
  • “Modular Syntax” [Grimm, ’06]

31

slide-32
SLIDE 32

<questions> http://www.cs.ucla.edu/~awarth/ometa

32