ANTLR4 and expression parsing (Part 3)

For Part 2 of this exercise, click here Introduction We ended part 2 with a working calculator, although it was basic. Given a simple mathematical expression we can get a result back. The goal of this post is to get the UomConvert expression working. We will also cover FxSpot calculations in a future blog post. Implementing UomConvert Let’s remind ourselves what UomConvert looks like (refer back to the grammar file). We want to parse something like the following example: 2 * UomConvert(MT,Kg) The result of the above expression should be 2 multiplied by the conversion factor of kilograms from metric tons. This one is simple enough: 1MT = 1000 Kg. So the conversion factor from kilograms to metric tons is 1000 and our final result should be 2000.   We can use this for our tests after the implementation is complete. Let’s begin. Returning a decimal is not enough The first step is simple and it is the same as in part 2: we have to override the VisitUomConvertFunc method from our base class. We can extract the from and to units of measurement from the context, but then what? How do we get a decimal value? Returning a decimal is no… continue reading

Continue reading ANTLR4 and expression parsing (Part 3). This article appeared first on CTRM Center.

Source: CTRM Center

Related Posts

Leave a reply