2

For those that haven't seen it, ZPL is a text-based markup language used for printing labels from special-purpose printers. A UPS shipping label would be a good example.

The markup language is quite primitive. There are modal commands to set a few state values, but the bulk of the language is one-liners that take the form <command><parameters><data>. There is no flow-control or nesting.

I want to write a rendering engine that interprets ZPL and renders to an image. There are a couple projects that have started down this road, but those projects are not complete enough for commercial use.

My question is: What do I need to learn to write a great <something> that will parse a ZPL document and convert that into commands I can execute? (By 'execute', I mean draw.)

Is this a parser? a lexer? tokenizer? interpreter? Is there some subset of compiler design theory that would be the most efficient path to success for this kind of project? ... and what is that called?

user3384842
  • 436
  • 5
  • 14
  • Just curious, but can you share the reason you need this? I work with ZPL a lot and although this would be an interesting tool to have, I don't know if I would ever need it. It would be an interesting project! Thanks! – Elton Saunders Jan 07 '15 at 17:37
  • We want to be able to write the markup once, in ZPL, and use that markup to print to any media: ZPL printer, laser printer, screen image. I have seen one product that promises write-once (in C#), export anything, label code. However the ZPL output from that library is extremely verbose. That makes the ZPL printing too slow to be practical. – user3384842 Jan 07 '15 at 22:13
  • I guess I am late and I am also just saying an opinion, however in my opinion there is no need for any fancy tools, given that ZPL is just just a sequance of linear commands with no nesting and that all commands start with the "^" or "~" character, and as such just basic regex or even string.split("") would suffice... – yoel halb Apr 05 '16 at 17:04
  • 1
    Hello I would recommend you to have a look at this project I think it has a very advanced rendering. https://github.com/BinaryKits/BinaryKits.Zpl – Tino Hager Sep 06 '21 at 19:08

2 Answers2

2

In your case, it is probably an interpreter.

If there are no conditionals, then presumably the markup is produced by reading the ZPL document from top to bottom.

What you need is a parser to tear apart the ZPL commands. If you are lucky after you tear one apart, everything you need to execute is available, either from the parameters of the command, or from previous ZPL modal commands.

So:

loop
    parse ZPL fragment
    execute ZPL fragment
end loop

If you want to read about building simple parsers, you can do it here: Is there an alternative for flex/bison that is usable on 8-bit embedded systems?

It should be obvious that at each point where the parse has recognized a construct, it has all the information from the content of the construct, thus you are prepared to execute it.

Of course, you have to know what the ZPL commands mean, and have the machinery available to execute them. Not everbody knows how to convert markup requests into updates on an image. If you know how to do that, you just a lot of sweat in front of you (why don't you just go get a working version of ZPL?). Best of luck to you.

If you don't know how to do that, you'll need to ask another question. And if you have to ask about all of parsing/interpreting/executing you're not likely to finish this project.

Community
  • 1
  • 1
Ira Baxter
  • 93,541
  • 22
  • 172
  • 341
2

You need to understand what regular expressions are, the single rule that governs the lexer (longest match wins), and how the parser works.

There is good intro to this subject (and fun) by Scott Stanchfield ANTLR 3.x Tutorial -- worth watching even if you won't use ANTLR.

And then you would need to know ZPL itself.

For C# there are already some lexer and parser generators usually with some examples to make you comfortable -- just take your pick from Irony, Coco/R, GOLD, ANTLR, LLLPG, Sprache, or my NLT.

greenoldman
  • 16,895
  • 26
  • 119
  • 185