Is there a BNF description of J?

No, nor can there be. BNFs describe context-free grammars, and J's grammar is not context free. A name's class (the type of that token) is determined at run time, and can change within a single executable statement. For example:

   x =. (x=.+/ . *)~ x =. i. 4 4

However, one can understand J's lexing and parsing without a BNF. The formal description of its lexing rules (rhematics/word formation) can be found in Part I of the Dictionary, and its parsing rules (syntax/grammar) in Part II ยง E.

An easier and more interactive introduction to J's grammar can be obtained from the trace script shipped with J. Simply load 'trace' and then trace 'J sentence' to see how the J sentence is parsed:

   load'trace'
   trace'(+/ % #) i. 5'
 --------------- 3 Adverb -----
 +
 /
 +/
 --------------- 5 Trident ----
 +/
 %
 #
 +/ % #
 --------------- 8 Paren ------
 (
 +/ % #
 )
 +/ % #
 --------------- 1 Monad ------
 i.
 5
 0 1 2 3 4
 --------------- 0 Monad ------
 +/ % #
 0 1 2 3 4
 2
 ==============================
2

Note that the trace facility only covers parsing, not lexing. For a clearer understanding of J's rhematics, one may study the definition of word formation (i.e. monad ;: in terms of dyad ;:).

   mjx=: ' ';(a.{~,65 97+/i.26);'0123456789_';'.';':';''''
   t=. 0 7 2$0 
   NB.        S    A    9    D    C    Q    X
   t=.t,_2]\ 0 0  2 1  3 1  1 1  1 1  4 1  1 1  NB. 0 space
   t=.t,_2]\ 0 3  2 2  3 2  1 0  1 0  4 2  1 2  NB. 1 other
   t=.t,_2]\ 0 3  2 0  2 0  1 0  1 0  4 2  1 2  NB. 2 alphanumeric
   t=.t,_2]\ 0 5  3 0  3 0  3 0  1 0  4 4  1 4  NB. 3 numeric
   t=.t,_2]\ 4 0  4 0  4 0  4 0  4 0  5 0  4 0  NB. 4 quote
   t=.t,_2]\ 0 3  2 2  3 2  1 2  1 2  4 0  1 2  NB. 5 even quotes
   sjx=: t
   
   f=: (0;sjx;<mjx)&;:
   f y=: '(2*a) %~ (-b) (+,-) %: (*:b)-4*a*c'
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+--+-+--+-+-+-+-+-+-+-+-+
|(|2|*|a|)|%|~|(|-|b|)|(|+|,|-|)|%:|(|*:|b|)|-|4|*|a|*|c|
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+--+-+--+-+-+-+-+-+-+-+-+
   (f -: ;:) y
1
   (f -: ;:) '1 2 3 +/ . * 4 5 6'
1
   (f -: ;:) 'gm=: */ %:~ #'
1

If you're unfamiliar with finite state machines in J (i.e. dyad ;:) the Lab "Sequential Machines" provides brief introduction.

See Also
  • chat/2007-November/000678 J syntax easy to parse? I don't think so


  • Contributed by Your Name

    Guides/Language FAQ/J BNF (last edited 2008-12-08 10:45:35 by )