r/apljk • u/[deleted] • Apr 12 '20
Array thinking for lexing/parsing
(NB. I am aware that array languages themselves may be implemented by comparing a stack of characters taken from the rightmost end of the input string against a table of options. I'm not asking how to parse an array language using a language like C; I'm asking how you'd parse another language using an array language in an idiomatic way.)
There are many approaches to lexing/parsing but the ones I know don't involve thinking in terms of operations on whole arrays. How does one use array thinking to deal with this kind of problem?
(If it was just a matter of turning:
123 $ abcinto:
123
$
abcI could use character classes to mask the original input (e.g.assuming class 0 is white space, 1 is digits, 2 is lowercase letters, 53 happens to be $,...):
1 1 1 0 0 53 0 2 2 2and then I can tokenise based on when the numbers change.)
But how does one interpret input that depends on previous input in complex ways?
e.g.1 a number may have a decimal point, but not initially and no more more than one, such that 1.23 is well-formed but .123 and 1.2.3 are malformed.
e.g.2 You may have heredocs where it has an arbitrary input from earlier in the array of characters:
a=<<ANYTHING
...text...
ANYTHINGThe concepts I'm missing are how to use whole-array operations to handle this sort of intricate dependence on what has already occurred in the same array. Is anyone willing to explain?
4
u/jaccarmac Apr 12 '20
Are you familiar with Aaron Hsu and his compiler project? https://github.com/Co-dfns/Co-dfns It's an APL compiler, so between that and it being a couple years since I watched his walkthrough of the code it may not be useful in terms of lexing and parsing. However, I do seem to recall his discussion of how he managed to create a compiler that can perform options on the entire AST at once.
2
Apr 12 '20
Yeah, I have been watching some of his conference talks about it. I haven't seen an explanation that was accessible to my level of inexperience with APL.
3
u/jaccarmac Apr 12 '20
I'm also inexperienced, wish I could help more. He did an overview of an older version for HN, based on the assertion that he could explain the compiler to a non-APL programmer in just a couple hours. Might be useful if you've only seen conf talks. https://youtu.be/gcUWTa16Jc0
2
Apr 12 '20
If he has succeeded in making out that accessible, it should help me a lot. I'll give it a watch, thanks. 🙂
2
u/jaccarmac Apr 12 '20
I not-so-distinctly remember learning about his method for dealing with trees from that video (though as you can tell it didn't exactly stick). Good luck and Godspeed.
3
u/chrispsn_ok Apr 12 '20 edited Apr 12 '20
For k at least, there's lots of parser examples, and they're all short enough to work through in an afternoon. I can't speak for whether they embody 'array thinking' but if they don't, that may itself be insightful. Check out:
effbiae's parser club
various parsers on nsl.com
parser-combinators in oK (John Earnest)
1
2
u/icendoan Apr 12 '20
Scan using a parsing mode; this let's you output different classes for the same character.
1
Apr 12 '20
By scan I assume you mean
\i.e performing a reduction with each step retained for later reference. However I'm not familiar with the idea of a parsing mode. Would you mind elaborating on what this is? Do you know of any resources that describe it in more detail?3
u/icendoan Apr 12 '20
I doubt it's standard literature. But the idea is that you wrap up both the result and the required next parser state in the output of your scan (and yes, I mean
\).This lets you write something like a tokeniser that produces an index vector you can cut into your tree whilst also injecting context-dependence where you need it.
1
0
Apr 12 '20
[deleted]
2
Apr 12 '20
All my education and experience is with non-array languages. I'm trying to get to a point where I can do common undergraduate-level tasks in array languages. Parsing is one such example.
8
u/tangentstorm Apr 12 '20
Let's focus on lexing, since that tends to involve "regular" language.
A "regular" language is one that can be parsed with just a state machine -- that is, without the use of a stack.
A state machine is just a graph of state nodes connected by edges, with one edge per character class leading out of each node.
So, for your hypothetical language, when you're in the start state, and you encounter a digit, that puts you in the "initial number parsing" state.
If, in the initial number parsing state, you encounter another digit, you're still in the initial number parsing state. If you encounter a decimal point, now you're in the "after the decimal point" state.
So, let's say there are three kinds of characters: whitespace, digit, and decimal point.
We can model the state transition graph as a table (array):
We assign whitespace, digit, and decimal point the character clasess 0 1 2 to correspond with the order of the columns in our table, and then we can simplify the table to just this:
Now we want to scan through the input string, processing each character in turn.
At each step in the scan, we have a current state (initially 0), and the numeric class of the next character. These correspond to coordinates in our table.
We can use the new
F.:primitive in j901 to perform the transitions:s =: '12.3 .45' NB. input stringc =: 1 1 2 1 0 2 1 1 NB. character classes for sNB. 0 : whitespaceNB. 1 : digitNB. 2 : decimal point[ m =: 0 1 3,0 1 2, 0 2 3 ,:3 NB. state transition table0 1 3 NB. row 0: initial state0 1 2 NB. row 1: in number, before decimal point0 2 3 NB. row 2: in number, after decimal point3 3 3 NB. row 3: error stateNB. each row says which state to go to given an input character class[ r =: 0 (m {~ <@,~) F:.] c NB. result: state at each character
[ r =: 0 (m {~ <@,~) F:.] c NB. result: state at each character1 1 2 2 0 3 3 3NB. pretty printer. each column is input, class, new state(<@,.)"1|:s,,/"1 ":"0 c,:r┌─┬─┬─┬─┬─┬─┬─┬─┐│1│2│.│3│ │.│4│5││1│1│2│1│0│2│1│1││1│1│2│2│0│3│3│3│└─┴─┴─┴─┴─┴─┴─┴─┘It's even easier in K, because you can use the matrix directly as an operator with
0 m\c...Of course, you probably want to also do something at each state transition - or at least at some of them. For example, every time you transition out of whitespace, you want to mark the start of a new token, and every time you transition back to whitespace, you want to mark the end.
For that, J combines all of this into what's probably the most complicated J primitive: sequential machine. https://code.jsoftware.com/wiki/Vocabulary/semico#dyadic
Basically, this dyad (
;:) takes a whole bunch of stuff crammed into the x argument (transition table, the input mapping, and the initial state and input pointer, etc) and then the input string as the y argument.Instead of plain ints, though, the transitions are given as two numbers: the next state and a code indicating what the machine should do with the current token.
One of the action codes is "stop", which lets you freeze the state of the machine. You could use this to start and stop the machine and do your own stack management, if you wanted to make a recursive descent parser, for example.
Despite all this, I think if I were going to write a parser from J in scratch, I would probably just use regexes to tokenize, and then write my own logic in explicit code.
(In fact, that's exactly what I did for the J syntax highlighter I use for my presentation tool: https://github.com/tangentstorm/j-talks/blob/master/preztool/run.ijs#L34 )