JA

Was reading kanbun a parser running in my head? Return marks through the lens of NLP and compilers

Here is what this post covers:

Introduction

Kanbun is classical Chinese text read as Japanese. Japanese students learn it in high school, together with kobun (classical Japanese). “Classical Japanese and kanbun are useless, so drop them and teach programming and math instead” is an argument that has come up again and again in Japan since the Meiji era.

In my previous post, What did I get, as a software engineer, out of choosing a general high school?, I listed kanbun as one of the subjects that turned out useful. Reading it means reordering the words, and that feels similar to tracing string processing in my head. But I also wrote that this was just my own impression and might be bias.

Honestly, kanbun has rarely helped me directly at work. Still, as an engineer, reading kanbun looks a lot like syntactic parsing. In this post I try to unpack that impression using NLP and compiler terms.

The short answer

Reading kanbun (kundoku) can be seen as a syntactic operation that converts an SVO string into SOV Japanese word order by fixed rules.

Kanbun has SVO word order, like English, while Japanese is SOV. It helps to think of the return marks (kaeriten) and the okurigana, the kana added to show inflections and particles, as a notation system that lets a human carry out that conversion.

Mapping to NLP and CS terms

Reading kanbunCorresponding NLP / CS concept
Reading the unmarked text from the startScanning a token sequence (like lexical analysis)
Finding the subject, predicate, and objectPart-of-speech tagging, dependency parsing
Adding return marksReordering words (SVO → SOV)
“Going back” with re-ten or ichi-ni marksPushing to and popping from a stack
Adding okurigana and particlesA generation phase that fills in inflections and function words

What happens in your head

There are two basic rules for reading kanbun with return marks. Read from the top, and when a character has a return mark, skip it for now and come back to it when a mark tells you to.

Let’s look at a concrete example. 我欲レ読レ書 is read in Japanese as 我書を読まんと欲す, which means “I want to read books.” The characters are 我 (I), 欲 (want), 読 (read), and 書 (books), in the same order as the English sentence. Japanese needs the order “books, read, want,” and the re-ten marks (レ) are what tell the reader to reorder.

The reading order of 我欲レ読レ書. The characters 欲 and 読, which carry re-ten marks, are skipped; after reading 書 you go back to 読 and then 欲. Read #1 我 Read #4 欲 レ Read #3 読 レ Read #2 書 go back go back Japanese reading: 我書を読まんと欲す (I want to read books)

欲 and 読 carry re-ten marks, so they are read later. Once you reach 書, you go back to 読 just before it, and then back again to 欲.

Remembering the skipped characters and later going back to them, newest first, is exactly what a stack does in programming. A stack is a container where the last thing you put in is the first thing you take out, like a pile of books you take from the top. Processing the same sentence with a stack looks like this.

Processing 我欲レ読レ書 with a stack. Characters with a re-ten mark are pushed, characters without a mark are output, and then the stack is popped from the top. Step Stack (rightmost is the top) Output (reading order) 1. Read 我 (I) No mark → output it (empty) 我 2. Read 欲 (want) Re-ten mark → push it 欲 我 3. Read 読 (read) Re-ten mark → push it 欲 読 我 4. Read 書 (books) No mark → output it 欲 読 我 書 5. Go back Pop 読 from the top and output it 欲 我 書 読 6. Go back Pop 欲 from the top and output it (empty) 我 書 読 欲 7. Add okurigana Add を, まんと, and す to make a Japanese sentence 我書を読まんと欲す

In step 3, 読 is pushed on top of 欲, and in steps 5 and 6, 読 comes off first because it is on top. The reading rule “go back to the most recently skipped character first” is precisely how a stack behaves.

Written as code, the flow looks like this. It is pseudocode meant only to show the idea: has_return_mark() means “does this character carry a return mark?” and can_resolve() means “has the signal to go back arrived?”

def kundoku(tokens):
    stack = []
    output = []
    for tok in tokens:
        if tok.has_return_mark():   # has a return mark -> hold it
            stack.append(tok)
        else:
            output.append(tok)
            while stack and stack[-1].can_resolve(output):
                output.append(stack.pop())  # pop it back in at the return point
    return add_okurigana(output)    # generate a Japanese sentence with okurigana

Real return marks nest, with ichi-ni marks, jō-ge marks and so on, so the actual process is more complicated.

My own view is that what goes on in your head has four steps.

  1. Telling characters from marks (is it a kanji or a control mark?)
  2. Holding skipped characters in working memory (push)
  3. Taking them back out at the return point and merging them into SOV order (pop)
  4. Interpreting the meaning

In the previous post I called it “tracing,” but to be more precise, you are running a parser and a transpiler in your own head.

Research treats it as a reordering problem too

This view is not just my own idea. One study formulated adding return marks to unmarked text as a rank-based sorting problem solved with a BERT-style model (Association for Natural Language Processing, 2023).

Another attempt adds brackets to return marks to give the syntax a structure, extending the notation from “reading order” to “syntactic structure.” In effect, that is building a parse tree. There is also research on parsing kanbun with Lisp and generating return-mark information.

Unmarked-text exam questions are parser drills

The exercise of adding return marks yourself to unmarked text (hakubun) goes like this.

  1. Look for kanji and set phrases that act as landmarks
  2. Work out where the subject, predicate, and object are
  3. Decide the reading order

What you are doing here is analyzing the structure of a given string in your own head. Hakubun questions are practice in running a parser by hand.

My take on abolishing it

I agree that it has too high a status as a required subject. On the other hand, if “you never use it in real life, so it’s pointless” is the only test, the same logic would cut subjects we want to keep, such as calculus.

Realistically, I think the middle ground is not abolition but making it an elective instead of a requirement, and expanding computer science classes.

Wrapping up

Seen as head training for syntactic parsing, kanbun has value in the age of AI as groundwork for paying attention to the structure of language. In the previous post I wrote that now that AI writes code, the ability to decide things in words and the ability to judge what comes out have both become more important. I think the kanbun habit of following sentence structure mechanically connects, a little, to both.

Rather than a yes-or-no question of “useful or not,” I think the education debate needs people to put into words what a subject actually trains.

References