Constructing a new-line sensitive grammar.

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Constructing a new-line sensitive grammar.

[ICR]
I'm fairly new to writing grammar files. I have succeeded in writing a
grammar file that seems to parse correctly, however I want to adapt it
to be sensitive to newlines. Below is a simplified version of my grammar
file:

%header%
GRAMMARTYPE    = "LL"
%tokens%
ENTRY           = "$entry"
IDENT            = <<[a-zA-Z0-9_-]+>>
WHITESPACE     = <<[ \t]+>> %ignore%
%productions%
File                  = EntryStatement Pairing ;
EntryStatement = ENTRY IDENT ;
Pairing              = IDENT IDENT [Pairing] ;

I have tried the following and variations, but they do not seem to work:

%header%
GRAMMARTYPE    = "LL"
%tokens%
NEWLINE    = <<\n\r|[\n\r]>>
ENTRY         = "$entry"
IDENT          = <<[a-zA-Z0-9_-]+>>
WHITESPACE     = <<[ \t]+>> %ignore%
%productions%
File                  = EntryStatement NEWLINE Pairing ;
EntryStatement = ENTRY IDENT ;
Pairing              = IDENT IDENT [NEWLINE Pairing] ;

But I get the error:

unexpected token "\r" <NEWLINE>, expected < IDENT>, on line: 1 column: 13
unexpected token "(...)" <NEWLINE>, expected < IDENT>, on line: 2 column: 13
unexpected token "(...)" <NEWLINE>, expected < IDENT>, on line: 4 column: 13
etc.

on the input:
$entry TESTA
TESTA TESTB
TESTB TESTC
TESTC TESTD
etc.


_______________________________________________
Grammatica-users mailing list
[hidden email]
http://lists.nongnu.org/mailman/listinfo/grammatica-users
Reply | Threaded
Open this post in threaded view
|

Re: Constructing a new-line sensitive grammar.

Per Cederberg
I think the problem is that the newline sequence in
Windows is \r\n and not the other way around. But
you could support both:

NEWLINE    = <<\n\r|\r\n|[\n\r]>>

Or if you don't care about the number of newlines
at all:

NEWLINE    = <<[\n\r]+>>

Hope this helps!

/Per

[ICR] wrote:

> I'm fairly new to writing grammar files. I have succeeded in writing a
> grammar file that seems to parse correctly, however I want to adapt it
> to be sensitive to newlines. Below is a simplified version of my grammar
> file:
>
> %header%
> GRAMMARTYPE    = "LL"
> %tokens%
> ENTRY           = "$entry"
> IDENT            = <<[a-zA-Z0-9_-]+>>
> WHITESPACE     = <<[ \t]+>> %ignore%
> %productions%
> File                  = EntryStatement Pairing ;
> EntryStatement = ENTRY IDENT ;
> Pairing              = IDENT IDENT [Pairing] ;
>
> I have tried the following and variations, but they do not seem to work:
>
> %header%
> GRAMMARTYPE    = "LL"
> %tokens%
> NEWLINE    = <<\n\r|[\n\r]>>
> ENTRY         = "$entry"
> IDENT          = <<[a-zA-Z0-9_-]+>>
> WHITESPACE     = <<[ \t]+>> %ignore%
> %productions%
> File                  = EntryStatement NEWLINE Pairing ;
> EntryStatement = ENTRY IDENT ;
> Pairing              = IDENT IDENT [NEWLINE Pairing] ;
>
> But I get the error:
>
> unexpected token "\r" <NEWLINE>, expected < IDENT>, on line: 1 column: 13
> unexpected token "(...)" <NEWLINE>, expected < IDENT>, on line: 2
> column: 13
> unexpected token "(...)" <NEWLINE>, expected < IDENT>, on line: 4
> column: 13
> etc.
>
> on the input:
> $entry TESTA
> TESTA TESTB
> TESTB TESTC
> TESTC TESTD
> etc.
>
>
> _______________________________________________
> Grammatica-users mailing list
> [hidden email]
> http://lists.nongnu.org/mailman/listinfo/grammatica-users
>
>


_______________________________________________
Grammatica-users mailing list
[hidden email]
http://lists.nongnu.org/mailman/listinfo/grammatica-users