Lexer in D

Dmitry Olshansky dmitry.olsh at gmail.com
Sat Mar 2 10:15:25 PST 2013


02-Mar-2013 22:09, Namespace пишет:
>> No-no and no.
>>
>> Just translate list of keywords/types to a bunch of switch-cases that
>> will give you near optimal performance. Use CTFE to construct that
>> string of D code.
>> In fact I proposed to do just 2 levels of switches: first switch by
>> length then by first char.
>
> I don't understand.
> You don't mean something like:
>
> switch (value) {
> case "if": isKeyword = true; break;
> ...
>
> right?

No.
switch(value.length)
case 2:
	switch(value[0])
	{
	case 'i':
		switch(value[1]){
		case 'f':
			return value.length == 2;
		case ...
	...
	}
...
case 3:
...
}

>
>> Even simple if you don't want to go for CTFE code-generation is to
>> make plain array of array. The length of array is exactly 256 i.e.
>>
>> Even this should have decent speed:
>>
>> string[][256] keywords = [
>> null,
>> null,
>> ...
>> //at index 'i'
>> ['int', 'if', 'invariant', 'idouble', 'ifloat' ...]
>> //array of strings starting with char for each
>> ...
>> ];
>>
>> bool isKeyword(string value)
>> {
>>     string[] toSearch = keywords[value[0]];
>>     return toSearch.canFind(value);
>> }
>
> Ok I will try it. Thank you.


-- 
Dmitry Olshansky


More information about the Digitalmars-d-learn mailing list