[Cuis] Why or why not OMeta? (was Re: Brainstorming question: what non-trivial uses can you think of for an object-based parser? (strings not invited))

Thierry Goubier thierry.goubier at gmail.com
Sat May 23 15:00:04 CDT 2015


Le 23/05/2015 18:16, Frank Shearar a écrit :
> I finally found it: I once implemented pattern matching with OMeta:
> http://www.lshift.net/blog/2011/05/15/algebraic-data-types-and-ometa2/
>
> Given a standard binary tree implementation with Tree whose subclasses
> are Node, Leaf or Empty, I could write:
>
> depth =
>          {#Empty} -> [0]
>          | {#Leaf anything} -> [1]
>          | {#Node depth:l depth:r} -> [(l max: r) + 1]
>
>      sum =
>          {#Empty} -> [0]
>          | {#Leaf anything:v} -> [v]
>          | {#Node sum:l sum:r} -> [l + r]
>
> which, I think, is pretty neat.

After reading through, the SmaCC syntax would be:

depth:
	<Empty> { 0 }
	| <Leaf> <Any> { 1 }
	| <Node> depth 'l' depth 'r' { (l max: r) + 1 }
	;

sum:
	<Empty> { 0 }
	| <Leaf> <Number> 'v' { v }
	| <Node> sum 'l' sum 'r' { l + r }
	;

Which is a very clever way of using a parser :)

And a nice benefit of this is that it would validate that the entry is a 
binary tree.

Thanks Frank for that example.

Thierry





More information about the Cuis mailing list