Exploring how to define a Grammar in APL using BNF-like syntax
Last active
July 18, 2023 14:43
-
-
Save Bruno-366/4ba65477bd31cccb5f28148d1a76e4a5 to your computer and use it in GitHub Desktop.
Exploring how to define a Grammar in APL using BNF-like syntax
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
⍝ Example Grammar: | |
HELLO ← 'hello world' | |
BYE ← 'bye world' | |
CONVERSATION ← HELLO , ' and then ' , BYE | |
⍝ CAPITALIZED words are the non-terminals | |
⍝ 'Strings' are the terminals | |
⍝ Concatenation is done with , |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
⍝ Output: | |
HELLO | |
hello world | |
BYE | |
bye world | |
CONVERSATION | |
hello world and then bye world |
Redefining non-terminals, makes everything a bit easier, since there a more correct mental mapping.
abstracting away for now the fact that terminals are strings with varying length:
- terminals are matrixes with the dimensions 1 ×
length
, ie as many elements as the string's length. - non-terminals are matrixes with the dimensions
a
×length
, where:a
is the amount of "alternations" it has.
By alternations we mean the amount if alternative terminals it can derive to.- and
length
is the length of its longest string
if we concatenate:
- two terminals we get a matrix with the dimensions 1 × (
length
of terminal 1 +length
of terminal 2) - a terminal and a non-terminal we get a matrix with the dimensions
a
× (length
of non-terminal +length
of terminal) - two non-terminals we get a matrix with the dimensions
a'
×length'
, where:length'
=length
of non-terminal 1 +length
of non-terminal 2a'
= alternations (a
) of non-terminal 1 × alternations (a
) of non-terminal 2
if you think about it, this is a much better distinction between non-terminals and terminals compared to;
'hello world' ⍝ this is a terminal
HELLO ← 'hello world' ⍝ this is a non-terminal
⍝ yes I know I'm being facetious,
⍝ here's perhaps a more fair representation of the standard definition, but its still pretty bad:
'this is a terminal' ⍝ this is a terminal
non-terminal ← 'this is a terminal' ⍝ this is a terminal being assign to a non-terminal
HELLO ← ⍪ 'hello world' 'hey world'
HELLO
┌───────────┐
│hello world│
├───────────┤
│hey world │
└───────────┘
BYE ← ⍪ 'bye world' 'cya world'
BYE
┌─────────┐
│bye world│
├─────────┤
│cya world│
└─────────┘
]display ⍪, (HELLO ∘., BYE)
┌→───────────────────────┐
↓ ┌→───────────────────┐ │
│ │hello worldbye world│ │
│ └────────────────────┘ │
│ ┌→───────────────────┐ │
│ │hello worldcya world│ │
│ └────────────────────┘ │
│ ┌→─────────────────┐ │
│ │hey worldbye world│ │
│ └──────────────────┘ │
│ ┌→─────────────────┐ │
│ │hey worldcya world│ │
│ └──────────────────┘ │
└∊───────────────────────┘
(⊂ 'hey world ') ∘., (⊂ 'cya world')
┌───────────────────┐
│hey world cya world│
└───────────────────┘
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
could perhaps use mix
↑
for alternation