Backus–Naur form (BNF) of Excel formulae

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 16947
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Backus–Naur form (BNF) of Excel formulae

Post by ChrisGreaves »

See also variables: Global with Public, or as parameters
I found some excellent web pages for anyone (like me) interested in parsing formulae in Excel cells,
Here's the best so far; BMF descriptions are scarces.
https://onlinelibrary.wiley.com/doi/10.1002/smr.1895 "Parsing Excel formulas: A grammar and its application on 4 large datasets"
Cheers, Chris
26 March 2025: I found these links in a text file on my laptop, hidden within a ZIP file from around 2020.
Links
http://vangelder.orconhosting.net.nz/excel/audxl.html Missing
http://ewbi.blogs.com/develops/2004/12/ ... mula_p.htm Missing
http://ewbi.blogs.com/develops/2007/03/ ... ula_p.html Missing
http://ewbi.blogs.com/develops/2007/04/ ... ula_p.html Missing
http://homepages.ecs.vuw.ac.nz/~elvis/db/Excel.shtml (Invesitgation into Excel Syntax and a Formula Grammar)
http://en.wikipedia.org/wiki/Syntax_diagram (Syntax diagrams (or railroad diagrams))
http://groups.google.com/group/comp.com ... 36f2b511a4 (MS Excel grammar, BNF?)
http://wiki.oasis-open.org/office/Expression_Syntax Expression_Syntax: Any implementation MUST support the syntax as described below, except for …
http://poi.apache.org/spreadsheet/formula.html the current state of formula support in Apache POI.
http://www.garshol.priv.no/download/text/bnf.html BNF and EBNF: What are they and how do they work?
http://unicode.org/Public/UNIDATA/extra ... gClass.txt DerivedCombiningClass-16.0.0.txt

If anyone can track down the pages marked "Missing" I would be grateful
Last edited by ChrisGreaves on 26 Mar 2025, 18:45, edited 4 times in total.
Never panic in a room that holds a computer.

User avatar
HansV
Administrator
Posts: 80248
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Backus–Naur Form (BNF) of Excel formulae

Post by HansV »

Have you tried applying Edsger Dijkstra's shunting-yard algorithm to Excel formulas? :evilgrin:

(See Shunting-yard algorithm on Wikipedia)
Best wishes,
Hans

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 16947
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Re: Backus–Naur Form (BNF) of Excel formulae

Post by ChrisGreaves »

HansV wrote:Have you tried applying Edsger Dijkstra's shunting-yard algorithm to Excel formulas?
Yup. Years ago when I was engaged in the compiler/interpreter writing business. We got to the point where we could be handed a language description in BN form (e.g. A language to support a new application for the Offset printer crowd) at 9am, and had a working compiler, compiled to run on an ICL1903A, ready to ship out the door at 5pm.
It was a chain of state-tables, transition matrices etc. The first version ran on what was basically a punched-card implementation of APL - which we had written. Given the speed of those days machines, and the unpredictable job queue, it was in all a minor miracle.

I took a closer look at one of the links above. Rob Van Gelder's tokeniser. If I've read it correctly it's a super-fast way to play around converting cell references to range names and vice-versa.
Never panic in a room that holds a computer.

User avatar
Jan Karel Pieterse
Microsoft MVP
Posts: 658
Joined: 24 Jan 2010, 17:51
Status: Microsoft MVP
Location: Weert, The Netherlands

Re: Backus–Naur Form (BNF) of Excel formulae

Post by Jan Karel Pieterse »

My reftreeanalyser (free demo version) tries to parse cell formulas too:
http://www.jkp-ads.com/reftreeanalyser.asp
Regards,

Jan Karel Pieterse
Excel MVP jkp-ads.com

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 16947
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Re: Backus–Naur Form (BNF) of Excel formulae

Post by ChrisGreaves »

Jan Karel Pieterse wrote:My reftreeanalyser (free demo version)
Hi Jan Karel.
I have broken your analyzer.
I got the same result (image below) with two separate workbooks.
Please let me know here, or via PM or email, how I can best help you to resolve the issue.
I am (on this machine) WinXP-SP3 Office 2000. :innocent:

Code: Select all

05/18/2010 10:38:50 AM      RefTreeAnalyserDemo         Error 13: Type mismatch in modMenu.CreateMenu
05/18/2010 10:44:30 AM      RefTreeAnalyserDemo         Error 13: Type mismatch in modMenu.CreateMenu
05/18/2010 10:46:28 AM      RefTreeAnalyserDemo         Error 13: Type mismatch in modMenu.CreateMenu
05/18/2010 10:46:30 AM      RefTreeAnalyserDemo         Error 13: Type mismatch in modMenu.CreateMenu
05/18/2010 10:46:31 AM      RefTreeAnalyserDemo         Error 13: Type mismatch in modMenu.CreateMenu
I have RevoUninstalled the package because I couldn't load Excel without these messages, followed by the familiar "Microsoft Excel has encountered a problem ..."
1.JPG
You do not have the required permissions to view the files attached to this post.
Never panic in a room that holds a computer.

User avatar
Jan Karel Pieterse
Microsoft MVP
Posts: 658
Joined: 24 Jan 2010, 17:51
Status: Microsoft MVP
Location: Weert, The Netherlands

Re: Backus–Naur Form (BNF) of Excel formulae

Post by Jan Karel Pieterse »

Hi Chris

I have had users report this error before. Never could find out what causes it.
Could you go into the registry and find this key:

HKEY_CURRENT_USER\Software\VB and VBA Program Settings\XLRefTreeAnalyser\Settings

Please tell me what it lists for the toolbar values.
Regards,

Jan Karel Pieterse
Excel MVP jkp-ads.com

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 16947
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Re: Backus–Naur Form (BNF) of Excel formulae

Post by ChrisGreaves »

Jan Karel Pieterse wrote:Please tell me what it lists for the toolbar values.
Jan Karel: It doesn't!
As far as I can see, it doesn't get as far as the registry settings.
Screen Snapshots
"Under" and "Stain" are my two utilities.
Stain is an Execl application; Under is a Word application.

P.S. PM follows ....
Never panic in a room that holds a computer.

User avatar
Jan Karel Pieterse
Microsoft MVP
Posts: 658
Joined: 24 Jan 2010, 17:51
Status: Microsoft MVP
Location: Weert, The Netherlands

Re: Backus–Naur Form (BNF) of Excel formulae

Post by Jan Karel Pieterse »

Hi Chris,

I fixed the issue, I've updated the downloads on my website.
Regards,

Jan Karel Pieterse
Excel MVP jkp-ads.com

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 16947
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Re: Backus–Naur Form (BNF) of Excel formulae

Post by ChrisGreaves »

Jan Karel Pieterse wrote:I fixed the issue,
Very good! Might we learn, in a general way, the nature of the bug? I mean in the sense of "I forgot to .." or similar that might have an impact on others of us distributing applications for a variety of office versions. I'm not asking for any proprietary code or secrets here. Just that you mentioned that this had been a (nagging?) problem with other installations.
1.JPG
:clapping: :clapping: :clapping: :clapping: :clapping: :clapping: :clapping: :clapping: :clapping: :clapping: :clapping: :clapping: :clapping: :clapping: :clapping: :clapping: :clapping: :clapping: :clapping: :clapping: :clapping: :clapping:
You do not have the required permissions to view the files attached to this post.
Never panic in a room that holds a computer.

User avatar
Jan Karel Pieterse
Microsoft MVP
Posts: 658
Joined: 24 Jan 2010, 17:51
Status: Microsoft MVP
Location: Weert, The Netherlands

Re: Backus–Naur Form (BNF) of Excel formulae

Post by Jan Karel Pieterse »

The tool comes with an addin that handles the shortcut keys. In the original version, the COM addin loaded the Excel addin, now I install the excel addin and the com addin does not open the excel addin anymore.
Regards,

Jan Karel Pieterse
Excel MVP jkp-ads.com

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 16947
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Re: Backus–Naur Form (BNF) of Excel formulae

Post by ChrisGreaves »

Jan Karel Pieterse wrote:... and the com addin does not open the excel addin anymore.
Thanks Jan Karel, for the explanation.
Since I'm not creating COM addins, it's not something I need worry about.

Yet.
Never panic in a room that holds a computer.

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 16947
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Re: Backus–Naur Form (BNF) of Excel formulae

Post by ChrisGreaves »

ChrisGreaves wrote:
04 May 2010, 10:14
... Rob Van Gelder's tokeniser ...
In a fit of boredom :grin: I have decided to document Rob Van Gelder's Excel Tokeniser. After close examination I believe that his program code (VBA) is based on (N.Amer) a State Transition Table a.k.a. (UK) a Turing Machine.
My first link "Formula Tokeniser" from 15 years ago leads to dead-ends, both with Rob Van Gelder and with Daily Dose Of Excel.

This is not critical, more a politeness thing, but if any of you have a link (or knowledge) to either Rob or Dose, I'd appreciate it.
Thanks, Chris

(Later) I have captured a page on Excel Syntax by Daniel Barringer at "Invesitgation into Excel Syntax and a Formula Grammar", but the pages were last updated 2006. C
Never panic in a room that holds a computer.

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 16947
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Re: Backus–Naur form (BNF) of Excel formulae

Post by ChrisGreaves »

Wednesday, March 12, 2025
Yesterday saw real progress. To recapitulate:-
(a) I defined (brain and hand) a simple BNF grammar for a simple spreadsheet processor. Do not be dismayed by the BNF grammar’s simplicity. We can always sit down and nut out a fully-fledged BNF grammar at a later date. This is the only brain/hand/time intensive part of our life here. I contend that all the steps that follow can be automated. I did it in 1973; I can do it again in 2025.
(b) I manually transcribed the BNF Grammar to a State Transition Table; I still have to automate this process.
(c) I manually transcribed the State Transition Table to VBA code; I still have to automate this process.
(d) I then manually tested my VBA code; I still have to automate this process.
With so little automated, why do I see this as Real Progress? Because we can now see our way to defining our BNF grammar as best we can, and then automatically generate a “Tokeniser”. And if our specification of what constitutes a “formula” changes, we just adjust our grammar and press a button. And if a latecomer to the party wants a slightly different definition of a formula (for example, a Real Number can include an Exponent) then they just adjust our grammar and press a button!
Cheers, Chris
You do not have the required permissions to view the files attached to this post.
Never panic in a room that holds a computer.

snb
5StarLounger
Posts: 711
Joined: 14 Nov 2012, 16:06

Re: Backus–Naur form (BNF) of Excel formulae

Post by snb »

I very much doubt whether I understand what you are up to (since a lot of procedures seem te be lacking (e.g. PushRealString & InitialiseIntegerString), this might help:

Code: Select all

Sub M_snb()
    c00 = "=230*310+56^2-12.35"

    For j = 1 To 5
      c00 = Replace(c00, Mid("+-*^/", j, 1), Mid("+-*^/", j, 1) & "_")
    Next
    sn = Split(c00, "_")
    
    For j = 0 To UBound(sn)
      MsgBox sn(j)
    Next
End Sub

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 16947
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Re: Backus–Naur form (BNF) of Excel formulae

Post by ChrisGreaves »

snb wrote:
14 Mar 2025, 17:01
I very much doubt whether I understand what you are up to (since a lot of procedures seem to be lacking ...
Your client wants to invent a language and specifies its BNF. (Or asks you to design a language ...)(or asks you to document a parser/tokeniser for which the original BNF has been mislaid ...)
In all events you want to start from a BNF description of the grammar. Commonly this involves specifying lexical units and the syntax of the language.

The definition of floating-point literals in Python may be an exemplar of mixing several notations

Code: Select all

floatnumber   ::=  pointfloat | exponentfloat
pointfloat    ::=  [intpart] fraction | intpart "."
exponentfloat ::=  (intpart | pointfloat) exponent
intpart       ::=  digit+
fraction      ::=  "." digit+
exponent      ::=  ("e" | "E") ["+" | "-"] digit+
Suppose this to be the entire BNF grammar of a language (it's nowhere near it!)

If we can translate this BNF to a State Transition Table (UK: Turing Machine) then we can have a definition of the language in the form of a State Transition Table. You have a copy of Rob Gelder's VBA code for his tokeniser.

I stared at his code and saw that it had been generated methodically from a State Transition Table. (I suspect methodically by hand owing to anomalies in the coding, rather than methodically by a computer program)

It is dead easy to translate from a State Transition Table to (almost) any language methodically, but especially wth a program.

Whew! If the client will write out (or get someone to write out) a BNF grammar for his purposes in analysing Excel, and if I can translate that BNF to a State transition table, and if I can translate that State transition Table to VBA code, THEN I can take in the BNF and automatically emit (TaDa!) a tokeniser/parser written in VBA.
Which is what the client wants.

I did all this in a crude version of APl 50+ years ago, but have forgotten the process from BNF to State Transition table; it will come back to me.

Over the past two days I wrote the code to translate from the State Transition Table to VBA code and am now putting it through its tests.

The point of this exercise is to show that IF I am given a BNF grammar of a language THEN I can automatically spew out a tokeniser/parser.

My premise is that the original BNF can be very simple. I am using a floating point number as the entire grammar. Nowhere near Excel, yet, but if the client beefs up the BNF grammar to HIS specification of Excel formulas THEN we can generate the appropriate VBA code in under two minutes. And when (not if) the client adjusts the BNF, two minutes gives us an updated tokeniser, instead of man-months puzzling over that 500+ line procedure.

At this stage you just have to guess what procedures such as " PushRealString" and "InitialiseIntegerString" might do. I don't really know that myself; just that at some stage I will need to set up a string accumulator, and at some other time I will need to be able to Push (and Pop) something with a stack.

Did you need the full definition of those Class modules to comprehend what "objTokens.Add" did? I hadn't realized that they were there, I just had faith that something called "objTokens.Add" would add something to something called objTokens. (I further assumed that it might be a collection of Token objects ...)

Cheers, Chris
Never panic in a room that holds a computer.