Участник:Lit-uriy/Песочница4

Материал из Wiki.crossplatform.ru

Перейти к: навигация, поиск

As Andreas already pointed out, we got lots of great feedback and suggestions from customers at Developers Days; thanks to everyone who participated and chatted with us. One question I got was “So, how do I use this QLALR thing?” As you might know, QLALR is a parser generator hosted on Trolltech Labs; just download it from there and (in theory you could then) start using it. In Qt, we use QLALR to generate the parser for QtScript and QXmlStream. Even though I’m still in a bit of daze due to jet lag, I’ll now attempt to explain how to get started using QLALR to create a parser for your own super-duper language (or existing language XYZ), and embed it into your Qt application, QtScript-style.

For those of you who haven’t read a few books on compilers and aren’t familiar with similar tools like bison/yacc (and the related flex/lex), the QLALR documentation might feel a bit lackluster. For the rest of you, it probably feels the same. I’m not saying the QLALR docs are bad; I’m just saying they don’t exist. One way to get your feet wet is to have a look at the QLALR grammar (.g) files in the Qt sources; src/script/qscript.g for QtScript, and src/xml/stream/qxmlstream.g for QXmlStream. However, both of these are non-trivial and part of a greater whole, and thus difficult to rip out and use as the basis for your own parser. So I’ve made a simple example that shows the basic setup.

The example is a parser for a small toy language I call Qbicle. You can grab the source code here. To build Qbicle, do the usual qmake and make (you don’t need QLALR installed to try it). Run the example to see the result of evaluating some Qbicle statements. To embed Qbicle in your own application (it’s super-useful, I promise!), include qbicle.pri in your QMake project (.pro) file.

The main Qbicle class is QbicleEngine, whose declaration looks like this: