Re: [xsl] How to dynamically evaluate an equation in the input XML document?

Subject: Re: [xsl] How to dynamically evaluate an equation in the input XML document?
From: "Dimitre Novatchev dnovatchev@xxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Thu, 15 Jul 2021 15:43:21 -0000
> You can short-circuit the process by generating the lexical analyser and
syntax analyser directly from a BNF specification
> using a parser generator; a popular choice in this community is Rex from
Gunther Rademacher
> (though this does an excellent job, it suffers from (a) not being
published as open source, and (b) being poorly documented).
> A benefit of Rex is that you can generate the parser components as XSLT
or XQuery code.


And a general LR-1 parser written entirely in XSLT has been implemented
long ago (around 2007 if I remember well) and is part of the FXSL 2.0
library:

https://github.com/dnovatchev/FXSL-XSLT2/blob/master/f/func-lrParse.xsl

I have used it to parse XPath 2.0 and a subset of JSON:


https://github.com/dnovatchev/FXSL-XSLT2/blob/master/newWork/XPathParse1.xsl


https://github.com/dnovatchev/FXSL-XSLT2/blob/master/newWork/xpath-parse-test.xsl


https://github.com/dnovatchev/FXSL-XSLT2/blob/master/newWork/testFunc-jason-document.xsl

Cheers,
Dimitre

On Thu, Jul 15, 2021 at 8:28 AM Michael Kay mike@xxxxxxxxxxxx <
xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> wrote:

>
> >
> > I have XML documents like this:
> >
> > <Convert-to-Celsius>
> >    <equation>(Fahrenheit - 32) * (5/9)</equation>
> >    <variable>
> >        <name>Fahrenheit</name>
> >        <value>32</value>
> >    </variable>
> > </Convert-to-Celsius>
> >
> > The document contains an equation which might contain variables. If it
> does contain variables, then I need to fetch their values and replace the
> variables in the equation with their values and then compute the value of
> the equation.
> >
>
> As Dimitre points out, this is an expression (or formula), not an equation.
>
> Writing a simple expression interpreter is a common exercise on
> undergraduate computer science courses, and it's not clear from your
> question whether you are familiar with the basic principles. The typical
> solution would be to write a lexical analyser that splits the expression
> into tokens, then add a syntax analyser to build a syntax tree that
> represents the grammatical structure of the expression, and then (using the
> interpreter design pattern) write a depth-first recursive tree walker that
> evaluates the expression nodes in this tree; the interpreter would have
> access to a context object that contains the bindings of variables to
> values, typically as an XDM map.
>
> If this description is too terse, then there are plenty of textbooks that
> explain it in more detail.
>
> You can short-circuit the process by generating the lexical analyser and
> syntax analyser directly from a BNF specification using a parser generator;
> a popular choice in this community is Rex from Gunther Rademacher (though
> this does an excellent job, it suffers from (a) not being published as open
> source, and (b) being poorly documented). A benefit of Rex is that you can
> generate the parser components as XSLT or XQuery code.
>
> Many people in this community would choose to use XPath as the expression
> language rather than inventing your own. That has the benefit that you
> don't need to specify and implement the language yourself, it's already
> been done; and you can then use xsl:evaluate directly for the evaluation.
>
> Michael Kay
> Saxonica
> 
>
>

-- 
Cheers,
Dimitre Novatchev
---------------------------------------
Truly great madness cannot be achieved without significant intelligence.
---------------------------------------
To invent, you need a good imagination and a pile of junk
-------------------------------------
Never fight an inanimate object
-------------------------------------
To avoid situations in which you might make mistakes may be the
biggest mistake of all
------------------------------------
Quality means doing it right when no one is looking.
-------------------------------------
You've achieved success in your field when you don't know whether what
you're doing is work or play
-------------------------------------
To achieve the impossible dream, try going to sleep.
-------------------------------------
Facts do not cease to exist because they are ignored.
-------------------------------------
Typing monkeys will write all Shakespeare's works in 200yrs.Will they write
all patents, too? :)
-------------------------------------
Sanity is madness put to good use.
-------------------------------------
I finally figured out the only reason to be alive is to enjoy it.

Current Thread