EBNF - how do I specify no whitespace allowed?

272 views
Skip to first unread message

Matthew Lock

unread,
Feb 5, 2014, 8:47:32 PM2/5/14
to eto-...@googlegroups.com
Hello,
I'm trying to find my feet using EBNF, but I'm not sure how to say "no whitespace allowed between these elements":

I'm have a simple grammar to match a street address, with an optional letter after the house number:

            ws := ? Terminals.WhiteSpace ?;

            letter := ? Terminals.Letter ?;
            digit := ? Terminals.Digit ?;          

            simple value := letter, {letter};

            street_name := simple value;

            number := digit, {digit};

            house_number :=  (number, letter) | number;

            grammar := house_number, street_name;

When I give it the string "134 smith" the element "house_number" contains "134 s" and "street_name" contains "mith".

I need to define the house_number as digits followed immediately by a letter with no whitespace allowed between the 2. How do I do that?

Thanks!

Matthew Lock

unread,
Feb 5, 2014, 9:47:32 PM2/5/14
to eto-...@googlegroups.com
... as a hacky workaround I replaced all the spaces in my input string with pipes, and I've just included pipes in my grammar to simulate explicit whitespace elements. A bit crazy but it works.

Curtis Wensley

unread,
Feb 5, 2014, 10:46:19 PM2/5/14
to eto-...@googlegroups.com

It seems there might be a bug in eto.parse when defining terminals here, where it is checking for "==", not ":=".  This should set it so that there is no whitespace defined between its children, as you have defined in your grammar.

for ebnf grammars in eto.parse, a rule defined with '=' will have its children separated by whitespace.

A rule defined with ':=' should have no whitespace.

Hope this helps.  If you end up fixing the bug, can you send a pull request?  otherwise I'll fix it up.

Thanks!
Curtis.

Matthew Lock

unread,
Feb 5, 2014, 11:19:54 PM2/5/14
to eto-...@googlegroups.com
Thanks for the reply.

Where should "==" go in my grammar?

I tried putting it like so, as I don't want to allow a space between the number and the optional letter:

street_number == number, [letter];

but I get the following runtime error when building the grammar:

Error parsing ebnf:
Index=504, Context="t_number =>>>= number, "
Expected:
integer: Number
optional sequence: Sequence
repeated sequence: Sequence

Curtis Wensley

unread,
Feb 5, 2014, 11:40:31 PM2/5/14
to eto-...@googlegroups.com

The bug is that the grammar specifies that it should be := or =, but is testing for == after it is parsed, which is incorrect.

So, you cannot currently specify a rule with no whitespace.

Fortunately, I just pushed a quick fix for this one (not to nuget, just to github) that will make ":=" treat the rule as a terminal correctly.

Hope this helps!
Curtis.

Matthew Lock

unread,
Feb 6, 2014, 7:36:57 PM2/6/14
to eto-...@googlegroups.com
Thanks I really appreciate that!

Looking forward to seeing it in Nuget soon as I have to deploy the project with references to Nuget packages for other developers.

Curtis Wensley

unread,
Feb 9, 2014, 4:12:08 AM2/9/14
to eto-...@googlegroups.com
The fix should now be on nuget with version 1.3.1.

Cheers!
Curtis.

Matthew Lock

unread,
Feb 9, 2014, 6:54:14 PM2/9/14
to eto-...@googlegroups.com
Terrific! That's working nicely now. Thanks for all your help.
Reply all
Reply to author
Forward
0 new messages