Programmatically generating lexers and parsers

The primary way to generate lexers and parsers is to use the command line tool (See Command line options). However it is also possible to programmatically generate lexers and parsers using the Hime.CentralDogma.dll library. In fact, the command line tool himecc.exe is simply a front end for this library. Directly using the CentralDogma library, it is possible to dynamically generate a parser, compile it and load it in order to use it, all at runtime.
To programmatically generate a lexer and a parser, the CompilationTask class must be used. It offers the following methods for selecting inputs:
Method Description
AddInputFile(string file) Adds a new file as input by giving the file’s name.
AddInputRaw(string data) Adds a new piece of text as input. This methods is useful when the grammar to compile is already in memory.
AddInputRaw(string name, string data) Same as above but also specifies a name for the piece of data so that it appears with a friendly name in the log.
AddInputRaw(Stream stream) Adds a new stream as input.
AddInputRaw(string name, Stream stream) Same as above but also specifies a name for the piece of data so that it appears with a friendly name in the log.
AddInputRaw(TextReader reader) Adds a new text reader as input.
AddInputRaw(string name, TextReader reader) Same as above but also specifies a name for the piece of data so that it appears with a friendly name in the log.


The following properties are used to customize the task:
Property Description
Mode This can have three values: Source so that only the lexer and parser sources are generated, Assembly so that the lexer and parser are compile and only the resulting assembly is produced, SourceAndAssembly to have both.
GrammarName Specifies the name of the grammar to compile when more than one are provided in the inputs.
Method Specifies the parsing method employed by the generated parser. Possible values are LR0 for LR(0), LR1 for LR(1), LALR1 for LALR(1), RNGLR1 for GLR on a LR(1) graph, RNGLALR1 for GLR on a LALR(1) graph and LRStar for LR(*).
OutputPrefix Specifies the folder and/or file names for the generated files.
OutputLog Set to true if the log should be output as a MHTML file.
OutputDocumentation Set to true if the compiled grammar’s documentation should be generated.
Namespace Specifies the namespace for the generated code.
CodeAccess Specifies the access modifier for the generated code. It can be either Public or Internal.

Last edited May 21, 2013 at 5:47 PM by lwouters, version 1

Comments

No comments yet.