Interpreting the D grammar
MakersF via Digitalmars-d
digitalmars-d at puremagic.com
Sun Aug 2 09:37:01 PDT 2015
On Sunday, 2 August 2015 at 14:50:35 UTC, Jacob Carlborg wrote:
> I'm trying to read the D grammar [1] to enhance the D TextMate
> bundle. If we take the add expression as an example. It's
> defined like this in the grammar:
>
> AddExpression:
> MulExpression
> AddExpression + MulExpression
> AddExpression - MulExpression
> CatExpression
>
> And like this in the grammar made by Brian [2]:
>
> addExpression:
> mulExpression
> | addExpression ('+' | '-' | '~') mulExpression
> ;
>
> I'm not so familiar with grammars but this looks like it's
> recursive. Is it possible to translate this piece of grammar to
> a regular expression? TextMate uses regular expressions and a
> couple of enhancements/extensions to define a grammar for a
> language.
>
> [1] http://dlang.org/grammar.html
> [2] https://rawgit.com/Hackerpilot/DGrammar/master/grammar.html
Of course it's recursive! Do you want the grammar to be able to
only define a finite number of programs?
But in this case you could write the original grammar rule as
mul |
cat |
(mul|cat)((+|-) (mul|cat))* (+|-) (mul|cat)
but you lose the precedence of the operation as it is a flat list
and not a tree
More information about the Digitalmars-d
mailing list