Project Description
The Hime Parser Generator generates lexers and parsers for .Net 2.0 and above. It supports deterministic (LALR) and generalized (GLR) parsing methods.
Features
- Generate lexers and parsers either through the command line tool or programmatically using the library.
- Generate either the lexers and parsers as C# sources, or the compiled version as a .Net assembly.
- Generate a documentation of the produced parsers for debugging purposes as HTML pages.
- Expressive input language for the specification of lexical and syntactical rules.
Quick How-to
Write your grammar in the input language. For example, a grammar for simple mathematical expressions:
cf grammar MathExp
{
options
{
Axiom = "exp";
Separator = "SEPARATOR";
}
terminals {
INTEGER -> [1-9] [0-9]* | '0' ;
REAL -> INTEGER? '.' INTEGER (('e' | 'E') ('+' | '-')? INTEGER)?
| INTEGER ('e' | 'E') ('+' | '-')? INTEGER ;
NUMBER -> INTEGER | REAL ;
WHITE_SPACE -> 0x0020 | 0x0009 | 0x000B | 0x000C ;
SEPARATOR -> WHITE_SPACE+;
}
rules {
exp_atom-> NUMBER
| '(' exp ')' ;
exp_op0 -> exp_atom
| exp_op0 '*' exp_atom
| exp_op0 '/' exp_atom;
exp_op1 -> exp_op0
| exp_op1 '+' exp_op0
| exp_op1 '-' exp_op0;
exp -> exp_op1 ;
}
} Execute the following command line:
himecc MathExp.gramThe program generates 4 files:
- MathExpLexer.cs, a C# source file for the generated lexer to be included in your project
- MathExpParser.cs, a C# source file for the generated parser to be included in your project
- MathExpLexer.bin, a resource file to be included as an embedded resource in your project
- MathExpParser.bin, a resource file to be included as an embedded resource in your project
To parse mathematical expression, include a reference to Hime.Redist.dll in your project and use the following lines:
MathExpLexer lexer = new MathExpLexer("2 + 3");
MathExpParser parser = new MathExpParser(lexer);
ASTNode root = parser.Parse() ;