4

I have a limited amount of input types:

34:56 = sensorA#, sensorA#, sensorB#
2:5 = { led# }
66 = otherSensor
2,3,4,5 = greenRelay#, redRelay#, relayA#, relayA#
  • a:b implies range.
  • {name} implies a global name for the dataset.
  • # represents automatic enumeration in the name (not relevant for the question)
  • Single values or coma separated values means what you'd expect for it.
  • Less names than values implies automatic name assignation (not relevant for the question)

I need to extract the numerical values from the left side of the expression and the names from the right side so I can iterate to assign the names to the values. I don't know hot to handle this task, I've been reading and I have sought a solution but I'd like to reach a good methodology for this case.

Should I replace all the spaces and tabs before processing?

Should I use regex just to verify the correctness of the input or for something more?

Should I use just plain string manipulation? I'm using Golang and strings are immutable, string manipulation implies allocations and a lot of code (speed is not REALLY important here but I'd like to find the correct way to solve this).

Should I write a lexer and parser for this?

2

Should I replace all the spaces and tabs before processing?

You can do this if you want whitespace to be as meaningless as it is in c, c++, java, c#.

This means doing a double pass over the file. For very large files this can be prohibitive because it forces you to hold the whole thing in memory or create a temp file. There are techniques to consume whitespace on the fly. Consider them before you resort to this.

Should I use regex just to verify the correctness of the input or for something more?

Not every language can be validated with regex. Be sure of which category you're in before you commit to it.

Should I use just plain string manipulation? I'm using Golang and strings are immutable, string manipulation implies allocations and a lot of code (speed is not REALLY important here but I'd like to find the correct way to solve this).

"A lot of code" is not a good way to define a language. Here's a good way:

http://www.bottlecaps.de/rr/ui

Should I write a lexer and parser for this?

This offers the most power of anything you've mentioned. There are likely simpler alternatives that center around reusing parsers written for things like json or xml but then you're just shoving your input types into a different data format.

  • Fair enough. I think I'll go with the parser solution because I already have a lexer (the configuration files of the project are in YAML format with some fields in a custom syntax which are composed by expressions, formulas and what I have put in the question). Thanks for the clarification about the whitespaces, seems reasonable. I wanted to handwrite my solution for this specific parsing task in order to learn more, that's why I didn't consider using a parser generator or other sophisticated tools. – Lupo Dharkael Jan 28 '17 at 17:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.