[GSoC’11] Lexing and parsing

spir denis.spir at gmail.com
Wed Mar 23 05:15:26 PDT 2011


On 03/22/2011 11:27 PM, Ilya Pupatenko wrote:
> As I understand, the library should allow programmer to write grammar directly
> in D (ideally, the syntax should be somehow similar to EBNF) and the resulting
> parser will be generated by D compiler while compiling the program. This method
> allows integration of parsing in D code; it can make code simpler and even
> sometimes more efficient.

Do you mean the grammer itself to be D source code? (Instead of EBNF-like plain 
text compiled by a parser generator.) If yes, then you may have a look at 
pyparsing for a similar tool: a python parsing lib in which the grammer is 
written in python. The particular point of pyparsing is that it is based on 
PEG, which you may lkike or not.

I have used it for a while, it works very fine, practically. What I don't like 
is its using of syntactic tricks to make pattern expressions supposedly more 
"natural", but in fact creates an obstacle in beeing itself a parallel language 
to be learnt. For this reason and some others, I wrote a variant (2, in fact) 
where patterns really are plain source code without tricks, eg:
	digits = String(Klass("0..9"))
	sign = Choice(Char('+'), Char('-'))
	integer = Compose(Optional(sign), digits)
I have an implementation of such a parsing lib in and for D (mostly working, 
but probably with many points to enhance, in particular for performance). It 
allows associating "match actions" to patterns:
	integer = (new Compose(Optional(sign), digits)) (toInt);
	plus = (new Char('+')) (drop)
	intSum = new Compose(integer, plus, integer) (doSum);

Denis
-- 
_________________
vita es estrany
spir.wikidot.com



More information about the Digitalmars-d mailing list