0

So I have a .txt file (Excellon) which I want to interpret.

Example file:

M48
FMAT,2
ICI,OFF
METRIC,TZ,000.000
T1C1.016
%
G90
M71
T1
X36551Y-569519
X17780Y-589280

When I scan the file I seperate the statement (e.g. METRIC) and save this in a string. After this I want to execute code based on the value of this string.

What would be the best practice to execute commands on statement detection.

if(String == "METRIC")
{
  execute code;
}
else if (String == "M48")
{
  execute code;
}
etc.

Or something like this:

switch(String)
{
  case: "M48"
    execute code;
    break;
  case: "METRIC"
    execute code;
    break;
  etc.
}

Or are both of these methods wrong and should I use a different method?

I found this: Switch or if statements in writing an interpreter in java they are talking about using a map should I also try this? If so could you provide a simple example because I don't really understand this method.

user6480540
  • 74
  • 1
  • 10
  • 2
    Well, only one of these alternatives is valid C++, so there's clearly a preferable one between them. Beyond that, it's really dependant on many external factors, and there is not really a true one-size-fits-all optimal solution. –  Oct 04 '18 at 15:52
  • how are the other not valid? – user6480540 Oct 04 '18 at 15:59
  • Your compiler can easily tell you that, just give it a try. –  Oct 04 '18 at 16:00
  • okay, probably fixed now. Wrote this as a quick example. – user6480540 Oct 04 '18 at 16:07
  • You could convert the strings to integer tokens when you read in the file. – Galik Oct 04 '18 at 16:08
  • 1) The reason for them not being valid C++ (as Frank commented) is that you cannot `switch` by strings (https://stackoverflow.com/questions/650162/why-the-switch-statement-cannot-be-applied-on-strings). 2) The `case` label in C++ looks like `case "M48":` instead of `case: "M48"`. – Algirdas Preidžius Oct 04 '18 at 16:10
  • If you have some complex grammar - you can try [Antrl](https://tomassetti.me/antlr-mega-tutorial/) i.e. compiler or compilers. For something more simpler you can try [boost spirit](https://theboostcpplibraries.com/boost.spirit) – Victor Gubin Oct 04 '18 at 16:32
  • Also there is a classic Dragon book [Compilers: Principles, Techniques, and Tools](https://en.wikipedia.org/wiki/Compilers:_Principles,_Techniques,_and_Tools) you'd better read before write your interpreter manually. – Victor Gubin Oct 04 '18 at 16:36

2 Answers2

0

The proper answer will depend on many factors, but reading between the lines of your post, I am 98% sure that what you want is a simple tokenizer to enum:

enum class Token {
  AAA,
  BBB,
  CCC
};

// Trivially implementable as a if() {} else if {} sequence,
// or as a trie search if you want to get fancy.
Token token_from_string(const std::string& str);

// and in the code.
Token tok = token_from_string(String);
switch(tok) {
  case Token::AAA:
  break;
  case Token::BBB:
  break;
  case Token::CCC:
  break;
}

And then, a good practice is to tokenize the string as soon as it comes out of the stream, and then operate on the token itself.

  • I'm not so experienced in programming, so could you please explain how tokenizing is a better practice than just use the string in a switch case. Because I have difficulty understanding how I am supposed to implement your code. – user6480540 Oct 04 '18 at 18:05
  • @user6480540, well, you can't use strings in switch statements, so that's kind of a non starter. –  Oct 04 '18 at 18:06
0

Q: What would be the best practice to execute commands on statement detection.

You want to change control flow, when a certain string is found. A switch is saying "pick one of commands based on this variables value". You could also use if/else.

Q: If so could you provide a simple example because I don't really understand this method.

The Excellon file format isn't far away from CNC g-code.

This is an example for a switch from an EXCELLON to GCODE converter.

The trick would be to modify the output method generateFile, to not generate the G-code file using fprint's, but call your commands instead (probably move, lift, wait, etc.).

You could also start with a g-code parser and modify it to handle the excellon format.

Jens A. Koch
  • 39,862
  • 13
  • 113
  • 141