⍝ Lex of APL src line:
1 disp 1 tokens '1 2 3+4 5 6' ⍝ numeric vectors as single token.
┌→────┬─┬─────┐
│1 2 3│+│4 5 6│
└────→┴→┴────→┘
1 disp 0 tokens '1 2 3+4 5 6' ⍝ numeric vectors as multiple tokens.
┌→┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┐
│1│ │2│ │3│+│4│ │5│ │6│
└→┴→┴→┴→┴→┴→┴→┴→┴→┴→┴→┘
⍕⍪1 disp∘tokens¨⎕NR'tokens'
┌→─────┬─┬─┬───┬─┬─┬───────────────────────────┬──────────────────────┐
│tokens│←│{│⎕ML│←│1│ │⍝ Lex of APL src line.│
└─────→┴→┴→┴──→┴→┴→┴──────────────────────────→┴─────────────────────→┘
┌→───┬─┬─┬─┬─┬─┬─┬──┬─┬─┬──────────────────────────┬──────────────────────────────────┐
│ │⍺│←│0│ │⋄│ │nv│←│⍺│ │⍝ numeric vectors as single token.│
└───→┴→┴→┴→┴→┴→┴→┴─→┴→┴→┴─────────────────────────→┴─────────────────────────────────→┘
┌→───┬────┬─┬─┬─┬─┬─┬───┬─┬─┬─┬─┬─┬─┬───┬─┬─────┬─────────┬─────────────────────┐
│ │alph│←│{│(│0│≤│⎕NC│⍪│⍵│)│/│⍵│}│⎕AV│~│'⍺⍵∇'│ │⍝ alphabet for names.│
└───→┴───→┴→┴→┴→┴→┴→┴──→┴→┴→┴→┴→┴→┴→┴──→┴→┴────→┴────────→┴────────────────────→┘
┌→───┬───┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬───────────────────────┬─────────────────────┐
│ │all│←│{│+│/│∧│\│⍺│∊│⍵│}│ │⍝ No. of leading ⍺∊⍵.│
└───→┴──→┴→┴→┴→┴→┴→┴→┴→┴→┴→┴→┴──────────────────────→┴────────────────────→┘
┌→───┬───┬─┬─┬─┬─┬─┬─┬─┬─┬─┬───┬─┬─┬─┬─┬─┬────────────────┬─────────────────────┐
│ │acc│←│{│(│⍺│,│↑│/│⍵│)│lex│⊃│↓│/│⍵│}│ │⍝ accumulated tokens.│
└───→┴──→┴→┴→┴→┴→┴→┴→┴→┴→┴→┴──→┴→┴→┴→┴→┴→┴───────────────→┴────────────────────→┘
┌→───┬───┬─┬─┐
│ │lex│←│{│
└───→┴──→┴→┴→┘
┌→───────┬─┬─┬─┬─┬─┬─┬─┬─┬─┬──┬─┬─┬─┬──────────────────┬──────────────────────────┐
│ │0│=│⍴│⍵│:│⍺│ │⋄│ │hd│←│⊃│⍵│ │⍝ Next char else finished.│
└───────→┴→┴→┴→┴→┴→┴→┴→┴→┴→┴─→┴→┴→┴→┴─────────────────→┴─────────────────────────→┘
┌→───────┬──┬─┬───┬─┬─┬─┬───────────────────────┬──────────────┐
│ │hd│=│' '│:│⍺│{│ │⍝ White Space.│
└───────→┴─→┴→┴──→┴→┴→┴→┴──────────────────────→┴─────────────→┘
┌→───────────┬────┬─┬─┬─┬───┬───┐
│ │size│←│⍵│ │all│' '│
└───────────→┴───→┴→┴→┴→┴──→┴──→┘
┌→───────────┬─┬─┬───┬─┬────┬─┬─┐
│ │⍺│ │acc│ │size│ │⍵│
└───────────→┴→┴→┴──→┴→┴───→┴→┴→┘
┌→───────┬─┬─┐
│ │}│⍵│
└───────→┴→┴→┘
┌→───────┬──┬─┬────┬─┬─┬─┬──────────────────────┬──────┐
│ │hd│∊│alph│:│⍺│{│ │⍝ Name│
└───────→┴─→┴→┴───→┴→┴→┴→┴─────────────────────→┴─────→┘
┌→───────────┬────┬─┬─┬─┬───┬─┬────┬─┬──┐
│ │size│←│⍵│ │all│ │alph│,│⎕D│
└───────────→┴───→┴→┴→┴→┴──→┴→┴───→┴→┴─→┘
┌→───────────┬─┬─┬───┬─┬────┬─┬─┐
│ │⍺│ │acc│ │size│ │⍵│
└───────────→┴→┴→┴──→┴→┴───→┴→┴→┘
┌→───────┬─┬─┐
│ │}│⍵│
└───────→┴→┴→┘
┌→───────┬──┬─┬───┬─┬─┬─┬───────────────────────┬─────────────┐
│ │hd│∊│'⎕'│:│⍺│{│ │⍝ System Name│
└───────→┴─→┴→┴──→┴→┴→┴→┴──────────────────────→┴────────────→┘
┌→───────────┬────┬─┬─┬─┬─┬─┬─┬─┬─┬───┬─┬────┐
│ │size│←│1│+│(│1│↓│⍵│)│all│ │alph│
└───────────→┴───→┴→┴→┴→┴→┴→┴→┴→┴→┴──→┴→┴───→┘
┌→───────────┬─┬─┬───┬─┬────┬─┬─┐
│ │⍺│ │acc│ │size│ │⍵│
└───────────→┴→┴→┴──→┴→┴───→┴→┴→┘
┌→───────┬─┬─┐
│ │}│⍵│
└───────→┴→┴→┘
┌→───────┬──┬─┬────┬─┬─┬─┬──────────────────────┬──────────────┐
│ │hd│=│''''│:│⍺│{│ │⍝ Char literal│
└───────→┴─→┴→┴───→┴→┴→┴→┴─────────────────────→┴─────────────→┘
┌→───────────┬────┬─┬─┬─┬─┬─┬─┬─┬─┬──┬─┬─┬─┬─┬─┬──┬─┬─┐
│ │size│←│+│/│∧│\│{│⍵│∨│¯1│⌽│⍵│}│≠│\│hd│=│⍵│
└───────────→┴───→┴→┴→┴→┴→┴→┴→┴→┴→┴─→┴→┴→┴→┴→┴→┴─→┴→┴→┘
┌→───────────┬─┬─┬───┬─┬────┬─┬─┐
│ │⍺│ │acc│ │size│ │⍵│
└───────────→┴→┴→┴──→┴→┴───→┴→┴→┘
┌→───────┬─┬─┐
│ │}│⍵│
└───────→┴→┴→┘
┌→───────┬──┬─┬──┬─┬───┬─┬─┬─┬────────────────────┬─────────────────┐
│ │hd│∊│⎕D│,│'¯'│:│⍺│{│ │⍝ Numeric literal│
└───────→┴─→┴→┴─→┴→┴──→┴→┴→┴→┴───────────────────→┴────────────────→┘
┌→───────────┬───┬─┬─┬─┬───┬─┬──┬─┬──────┬─┬──┬─┬───┬──┬───────────────────────────────┐
│ │max│←│⍵│ │all│ │⎕D│,│'.¯EJ'│,│nv│/│' '│ │⍝ numbers with trailing blanks.│
└───────────→┴──→┴→┴→┴→┴──→┴→┴─→┴→┴─────→┴→┴─→┴→┴──→┴─→┴──────────────────────────────→┘
┌→───────────┬────┬─┬───┬─┬─┬─┬─┬─┬───┬─┬─┬───┬─┬─┬─────┬───────────────────────────────┐
│ │size│←│max│-│+│/│∧│\│' '│=│⌽│max│↑│⍵│ │⍝ .. without trailing blanks.│
└───────────→┴───→┴→┴──→┴→┴→┴→┴→┴→┴──→┴→┴→┴──→┴→┴→┴────→┴──────────────────────────────→┘
┌→───────────┬─┬─┬───┬─┬────┬─┬─┐
│ │⍺│ │acc│ │size│ │⍵│
└───────────→┴→┴→┴──→┴→┴───→┴→┴→┘
┌→───────┬─┬─┐
│ │}│⍵│
└───────→┴→┴→┘
┌→───────┬──┬─┬──────┬─┬─┬─┬────────────────────┬──────────────────────┐
│ │hd│∊│'⍺⍵∇:'│:│⍺│{│ │⍝ ⍺⍺ or ⍵⍵ or ∇∇ or ::│
└───────→┴─→┴→┴─────→┴→┴→┴→┴───────────────────→┴─────────────────────→┘
┌→───────────┬────┬─┬─┬─┬───┬─┬──┐
│ │size│←│⍵│ │all│ │hd│
└───────────→┴───→┴→┴→┴→┴──→┴→┴─→┘
┌→───────────┬─┬─┬───┬─┬────┬─┬─┐
│ │⍺│ │acc│ │size│ │⍵│
└───────────→┴→┴→┴──→┴→┴───→┴→┴→┘
┌→───────┬─┬─┐
│ │}│⍵│
└───────→┴→┴→┘
┌→───────┬──┬─┬───┬─┬─┬─┬───┬─┬─┬─┬─┬─┬───────────────┬─────────┐
│ │hd│=│'⍝'│:│⍺│ │acc│(│⍴│⍵│)│⍵│ │⍝ Comment│
└───────→┴─→┴→┴──→┴→┴→┴→┴──→┴→┴→┴→┴→┴→┴──────────────→┴────────→┘
┌→───────┬─┬─┬───┬─┬─┬─┬─┬───────────────────────┬────────────────────┐
│ │⍺│ │acc│ │1│ │⍵│ │⍝ Single char token.│
└───────→┴→┴→┴──→┴→┴→┴→┴→┴──────────────────────→┴───────────────────→┘
┌→───┬─┐
│ │}│
└───→┴→┘
┌→───┬─┬─┬─┬─┬──┬─┬───┬─┬─┐
│ │(│0│⍴│⊂│''│)│lex│,│⍵│
└───→┴→┴→┴→┴→┴─→┴→┴──→┴→┴→┘
┌→┐
│}│
└→┘
⍝ Alternative coding:
tokens2←{⎕ML←0 ⍝ Lex of APL source line.
⍺←0 ⋄ nv←⍺/' ' ⍝ numeric vector as single tokn.
alph←⎕A,⎕Á,'_∆⍙',26↑17↓⎕AV ⍝ Alphabet for names
all←{+/∧\⍺∊⍵} ⍝ ∇ No of leading ⍺∊⍵
acc←{n←⍺⍺ ⍵ ⋄ (⍺,⊂n↑⍵)lex n↓⍵} ⍝ ∇∇ Accumulated tokens
lex←{ ⍝ ∇ Next lexical token
0=⍴⍵:⍺ ⋄ hd←⊃⍵ ⍝ Next char else finished
hd=' ':⍺{⍵ all' '}acc ⍵ ⍝ White Space
hd∊alph:⍺{⍵ all alph,⎕D}acc ⍵ ⍝ Name
hd∊'⎕:':⍺{1+(1↓⍵)all alph,':'}acc ⍵ ⍝ System Name / Keyword / guard
hd='''':⍺{+/∧\{⍵∨¯1⌽⍵}≠\⍵=hd}acc ⍵ ⍝ Char literal
hd∊⎕D,'¯':⍺{⍵ all ⎕D,'.¯E',nv}acc ⍵ ⍝ Numeric scalar literal
hd∊'⍺⍵∇':⍺{⍵ all hd}acc ⍵ ⍝ Meta character.
hd='⍝':⍺⍴acc ⍵ ⍝ Comment
⍺{1}acc ⍵ ⍝ Single char token
}
(0⍴⊂'')lex,⍵
}
1 disp 1 tokens2 '1 2 3+4 5 6' ⍝ numeric vectors as single token.
┌→────┬─┬─────┐
│1 2 3│+│4 5 6│
└────→┴→┴────→┘
1 disp 0 tokens2 '1 2 3+4 5 6' ⍝ numeric vectors as multiple tokens.
┌→┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┐
│1│ │2│ │3│+│4│ │5│ │6│
└→┴→┴→┴→┴→┴→┴→┴→┴→┴→┴→┘
⍕⍪1 disp∘tokens2¨⎕NR'tokens2'
┌→──────┬─┬─┬───┬─┬─┬──────────────────────────────┬─────────────────────────┐
│tokens2│←│{│⎕ML│←│0│ │⍝ Lex of APL source line.│
└──────→┴→┴→┴──→┴→┴→┴─────────────────────────────→┴────────────────────────→┘
┌→───────┬─┬─┬─┬─┬─┬─┬──┬─┬─┬─┬───┬──────────────────────────┬────────────────────────────────┐
│ │⍺│←│0│ │⋄│ │nv│←│⍺│/│' '│ │⍝ numeric vector as single tokn.│
└───────→┴→┴→┴→┴→┴→┴→┴─→┴→┴→┴→┴──→┴─────────────────────────→┴───────────────────────────────→┘
┌→───────┬────┬─┬──┬─┬──┬─┬─────┬─┬──┬─┬──┬─┬───┬──────────────┬────────────────────┐
│ │alph│←│⎕A│,│⎕Á│,│'_∆⍙'│,│26│↑│17│↓│⎕AV│ │⍝ Alphabet for names│
└───────→┴───→┴→┴─→┴→┴─→┴→┴────→┴→┴─→┴→┴─→┴→┴──→┴─────────────→┴───────────────────→┘
┌→───────┬───┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬───────────────────────────┬─────────────────────┐
│ │all│←│{│+│/│∧│\│⍺│∊│⍵│}│ │⍝ ∇ No of leading ⍺∊⍵│
└───────→┴──→┴→┴→┴→┴→┴→┴→┴→┴→┴→┴→┴──────────────────────────→┴────────────────────→┘
┌→───────┬───┬─┬─┬─┬─┬──┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬───┬─┬─┬─┬─┬─┬──────────┬───────────────────────┐
│ │acc│←│{│n│←│⍺⍺│ │⍵│ │⋄│ │(│⍺│,│⊂│n│↑│⍵│)│lex│ │n│↓│⍵│}│ │⍝ ∇∇ Accumulated tokens│
└───────→┴──→┴→┴→┴→┴→┴─→┴→┴→┴→┴→┴→┴→┴→┴→┴→┴→┴→┴→┴→┴──→┴→┴→┴→┴→┴→┴─────────→┴──────────────────────→┘
┌→───────┬───┬─┬─┬───────────────────────────────────┬──────────────────────┐
│ │lex│←│{│ │⍝ ∇ Next lexical token│
└───────→┴──→┴→┴→┴──────────────────────────────────→┴─────────────────────→┘
┌→───────────┬─┬─┬─┬─┬─┬─┬─┬─┬─┬──┬─┬─┬─┬──────────────────────┬─────────────────────────┐
│ │0│=│⍴│⍵│:│⍺│ │⋄│ │hd│←│⊃│⍵│ │⍝ Next char else finished│
└───────────→┴→┴→┴→┴→┴→┴→┴→┴→┴→┴─→┴→┴→┴→┴─────────────────────→┴────────────────────────→┘
┌→───────────┬──┬─┬───┬─┬─┬─┬─┬─┬───┬───┬─┬───┬─┬─┬─────────────┬─────────────┐
│ │hd│=│' '│:│⍺│{│⍵│ │all│' '│}│acc│ │⍵│ │⍝ White Space│
└───────────→┴─→┴→┴──→┴→┴→┴→┴→┴→┴──→┴──→┴→┴──→┴→┴→┴────────────→┴────────────→┘
┌→───────────┬──┬─┬────┬─┬─┬─┬─┬─┬───┬─┬────┬─┬──┬─┬───┬─┬─┬───────┬──────┐
│ │hd│∊│alph│:│⍺│{│⍵│ │all│ │alph│,│⎕D│}│acc│ │⍵│ │⍝ Name│
└───────────→┴─→┴→┴───→┴→┴→┴→┴→┴→┴──→┴→┴───→┴→┴─→┴→┴──→┴→┴→┴──────→┴─────→┘
┌→───────────┬──┬─┬────┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬───┬─┬────┬─┬───┬─┬───┬─┬─┬─┬───────────────────────────────┐
│ │hd│∊│'⎕:'│:│⍺│{│1│+│(│1│↓│⍵│)│all│ │alph│,│':'│}│acc│ │⍵│ │⍝ System Name / Keyword / guard│
└───────────→┴─→┴→┴───→┴→┴→┴→┴→┴→┴→┴→┴→┴→┴→┴──→┴→┴───→┴→┴──→┴→┴──→┴→┴→┴→┴──────────────────────────────→┘
┌→───────────┬──┬─┬────┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬──┬─┬─┬─┬─┬─┬─┬─┬──┬─┬───┬─┬─┬──┬──────────────┐
│ │hd│=│''''│:│⍺│{│+│/│∧│\│{│⍵│∨│¯1│⌽│⍵│}│≠│\│⍵│=│hd│}│acc│ │⍵│ │⍝ Char literal│
└───────────→┴─→┴→┴───→┴→┴→┴→┴→┴→┴→┴→┴→┴→┴→┴─→┴→┴→┴→┴→┴→┴→┴→┴─→┴→┴──→┴→┴→┴─→┴─────────────→┘
┌→───────────┬──┬─┬──┬─┬───┬─┬─┬─┬─┬─┬───┬─┬──┬─┬─────┬─┬──┬─┬───┬─┬─┬─┬────────────────────────┐
│ │hd│∊│⎕D│,│'¯'│:│⍺│{│⍵│ │all│ │⎕D│,│'.¯E'│,│nv│}│acc│ │⍵│ │⍝ Numeric scalar literal│
└───────────→┴─→┴→┴─→┴→┴──→┴→┴→┴→┴→┴→┴──→┴→┴─→┴→┴────→┴→┴─→┴→┴──→┴→┴→┴→┴───────────────────────→┘
┌→───────────┬──┬─┬─────┬─┬─┬─┬─┬─┬───┬─┬──┬─┬───┬─┬─┬───────────┬─────────────────┐
│ │hd│∊│'⍺⍵∇'│:│⍺│{│⍵│ │all│ │hd│}│acc│ │⍵│ │⍝ Meta character.│
└───────────→┴─→┴→┴────→┴→┴→┴→┴→┴→┴──→┴→┴─→┴→┴──→┴→┴→┴──────────→┴────────────────→┘
┌→───────────┬──┬─┬───┬─┬─┬─┬───┬─┬─┬──────────────────────┬─────────┐
│ │hd│=│'⍝'│:│⍺│⍴│acc│ │⍵│ │⍝ Comment│
└───────────→┴─→┴→┴──→┴→┴→┴→┴──→┴→┴→┴─────────────────────→┴────────→┘
┌→───────────┬─┬─┬─┬─┬───┬─┬─┬───────────────────────────┬───────────────────┐
│ │⍺│{│1│}│acc│ │⍵│ │⍝ Single char token│
└───────────→┴→┴→┴→┴→┴──→┴→┴→┴──────────────────────────→┴──────────────────→┘
┌→───────┬─┐
│ │}│
└───────→┴→┘
┌→───────┬─┬─┬─┬─┬──┬─┬───┬─┬─┐
│ │(│0│⍴│⊂│''│)│lex│,│⍵│
└───────→┴→┴→┴→┴→┴─→┴→┴──→┴→┴→┘
┌→───┬─┐
│ │}│
└───→┴→┘
⍝∇ tokens
Back to: code
Back to: Workspaces