up: Table of contents | prev: Lexical Analyzer | next: Testing the Lexer

2.2 Building the Lexer

These instructions are for the Microsoft Visual Studio Community Edition, and for the x64 configuration.

Generating the Lexer

The example code is in the examples/minilang directory. We have created a C++ console application project with name minilang to the examples/minilang directory and put the Minilang.lexer that contains the lexer defined in the previous stage to the examples/minilang directory and included it in the project.

Now open a command prompt in the examples/minilang directory and execute the following command:

        slg -v Minilang.lexer
    

The SoulNG lexer generator slg reads a Minilang.lexer file. If the generator detects no errors, it generates a lexical analyzer for the definitions in the .lexer file as C++ code:

        C:\soulng-1.0.0\examples\minilang>slg -v Minilang.lexer
        > C:/soulng-1.0.0/examples/minilang/Minilang.lexer
        ==> C:/soulng-1.0.0/examples/minilang/MinilangTokens.hpp
        ==> C:/soulng-1.0.0/examples/minilang/MinilangTokens.cpp
        ==> C:/soulng-1.0.0/examples/minilang/MinilangKeywords.hpp
        ==> C:/soulng-1.0.0/examples/minilang/MinilangKeywords.cpp
        ==> C:/soulng-1.0.0/examples/minilang/ClassMap.hpp
        ==> C:/soulng-1.0.0/examples/minilang/ClassMap.cpp
        ==> C:/soulng-1.0.0/examples/minilang/MinilangLexer.hpp
        ==> C:/soulng-1.0.0/examples/minilang/MinilangLexer.cpp
    

By giving the -v option, you can instruct the generator to print the paths of the input and output files as it reads and generates them. If the -v option is not given, the generator prints nothing if the .lexer definition contains no errors.

The slg tool creates C++ header and source files with the file names corresponding to the identifiers defined for the tokens, keywords and lexer declarations in the .lexer file.

The tool also generates files ClassMap.cpp and ClassMap.hpp that will contain a Unicode character class map for the lexer.

If a project contains many lexers, each must have a unique class map. A unique class map can be created with a class map declaration in a .lexer file.

Add the generated source and header files to the minilang project.

Compiler Settings for the Lexer Project

The generated lexer uses the soulng/lexer and soulng/util libraries that in turn depend on the Boost C++ libraries, so we must set the include and linker directories of the minilang project to have it compile.

Setting the INCLUDE path of the minilang project

Add a path to the soulng-1.0.0 root directory to the C/C++ / General / Additional Include Directories. Also add a path to the Boost directory to Additional Include Directories. We have boost version 1.68 installed under the C:\Program Files\boost directory so we add C:\Program Files\boost\include\boost-1_68 to the include path. Finally add the ".." path to the include path, so we can include headers under the minilang directory in the project.

Linker Settings

Add a path to the soulng-1.0.0/lib directory to the Linker / General / Additional Library Directories. Also add a path to the Boost library directory. We have installed the boost libraries to the C:\Program Files\boost\lib directory. Finally add "$OutDir" to the library path, so we link all libraries defined in the current solution. We have currently no other libraries in this solution.

The SoulNG libraries have #pragma comment(lib, "<library>.lib") declarations in the header files, so they are automatically added to the linked libraries.

Main

Because we are building a program it must have a main function defined somewhere. Add a source file Main.cpp with the following content to the project:

        #include <iostream>

        int main(int argc, const char** argv)
        {
            return 0;
        }
    

Compiling the Lexer Project

Now the minilang project should compile without errors. Some warnings are generated that can be turned off by setting the C/C++ Advanced / Disable Specific Warnings value to 4251;4244.

up: Table of contents | prev: Lexical Analyzer | next: Testing the Lexer