Let’s see today how a parser is built with GrammGen using code. The idea is to define using GrammGen how to build a tree with expressions detected in the text under process. The text is written in the programming language that we want to implement. The expression tree is created using the GrammGen definition, given by code.
I presented in the previous post for an example of a console program, which employs GrammGen. A parser is defined using rules. The rules indicate:
– How to process text characters
– How then form text elements, such as numbers, names, operators
– How to go from putting together such elements, expressions, commands that define our language to implement.
In the sample of calculator
https://github.com/ajlopez/GrammGen/tree/master/Samples/Calculator
the rules are defined by the parser:
https://github.com/ajlopez/GrammGen/blob/master/Samples/Calculator/Calculator/ExpressionParser.cs
The first rules:
private static Rule[] rules = new Rule[]{Rule.Or(' ', '\t', '\r', '\n').Skip(),Rule.Or('+', '-').Generate("Oper0"),Rule.Or('*', '/').Generate("Oper1"),// ...
The idea is to get an Expression. The first rule defines that anyone (Rule.Or) of space, tab, carriage return, new line items are ignored (. Skip ()). You can put more than one rule of this type, for example, to set comments to be ignored from the input text.
The second rule defines both + (the plus sign) and – (minus sign) produce a called terminal Oper0. They differ from * (the token) and / (divide sign) because we want to take precedence among operators.
There is a rule to construct integers:
Rule.Get("0-9").OneOrMore().Generate("Integer", MakeIntegerConstantExpression),
The Rule.Get allows you to specify a range of characters, and then, with the fluent interface defined, you can attach the .OneOrMore () method, which specifies that the rule can be applied once or several times. When you can not apply more, it ends generating a node called "Integer". But in GrammGen a node, in addition to a name, may have an associated object. Al. A second parameter can be passed to Generate method, with a function that creates the associated object.
MakeIntegerConstantExpression job is:
– Get the object that was formed with the rule, in this case, a concatenated string with all digits were found.
– Transform it to integer
– Create a ConstantExpression with the intever as constant value.
The class ConstantExpression is not from GrammGen. It is defined by us. We have the freedom to define the objects we want to attach to each node of the GrammGen generated tree.
The code of MakeIntegerConstantExpression is:
private static object MakeIntegerConstantExpression(object obj){int value = int.Parse((string)obj);return new ConstantExpression(value);}
In the next posts we will see how the nodes of binary operations are formed, how operator precedence is implemented, how to manage left recursion, and evaluation of the generated expressions.
Stay tuned!
Angel “Java” Lopez
http://www.ajlopez.com