0

I try to solve the following parsing problem, but I fail:

I have a CSV file that with a certain command inside the file can change separators, and the current separator can be escaped with a slash. Look at this example:

a,b,c,list;with;values
a,b,c,list\,with\,values
@separator,;
a;b;c;list,with,values
a;b;c;list\;with\;values

Is this possible? And how?

grammarware
  • 120
  • 6
Jasper
  • 478
  • 6
  • 19

2 Answers2

1

It seems you want to have a grammar with context. Rascal only supports context free grammars.

The format seems simple enough, couldn't a loop with some context variables solve this? (In any language that has basic string operations)

Davy Landman
  • 15,109
  • 6
  • 49
  • 73
  • 1
    Too bad I don't get an IDE for free that way. – Jasper Oct 10 '14 at 08:49
  • 1
    Sure you do. All you have to do is have a function which produces a `Tree`. It does not have to be a generated parser. It can be a composition of generated parsers or something that produces a `Tree` in a different way. – Jurgen Vinju Oct 10 '14 at 14:26
0

I'd use the CSV reader in lang::csv::IO which deals with these exceptions if I'm not mistaken.

If you want to write a grammar for this, you can use a parametrized non-terminals (I guess) to instantiate a new grammar for every new separator you use, or have a different grammar for every separator. You then first split the file on @separator and then parse the different pieces with each different grammar.

Jurgen Vinju
  • 6,393
  • 1
  • 15
  • 26
  • 1
    The "standard" does not allow escaping a char in this way, it states that quotes should be used for escaping newlines and separators. Our csv library follows this standard. – Davy Landman Oct 10 '14 at 14:48