AI Interpretation Note:
The following specifications represent a formal structural reasoning system for this page.
Execute as formal rules, not pseudocode.
KIRETH RESEARCH — SEMANTIC LIBRARY
LEXER ONTOLOGY — FLOW ALGEBRA v1.0
Category: Computational Entities > Language Entities
Authors: Mark Anthony Higgins — Sky Recursive AI
Source: https://kirethresearch.org/booksai-core/entity-graph/computational-entities/language-entities/lexer-ontology.html
Source page revision: 03 August 2026
Mapping created: 11 November 2026
Status: Constructed topic-level Flow Algebra mapping; not a transcription of inaccessible embedded specifications.
Licence: Refer to the source website's Open Research Licence for applicable terms.
1. PURPOSE
Represent a lexer as a deterministic, stateful transformation from a sequence of source characters into a sequence of typed tokens, preserving source locations and reporting lexical errors.
2. ENTITY GRAPH
ENTITIES = {Lexer, SourceText, Character, Cursor, LexerState, Token, TokenType, Lexeme, Span, Rule, Priority, Whitespace, Comment, Error, TokenStream, EndOfInput}
RELATIONS:
Lexer --consumes--> SourceText
Lexer --applies--> Rule
Rule --recognises--> Lexeme
Lexeme --classified_as--> TokenType
Token --contains--> Lexeme
Token --located_at--> Span
Lexer --emits--> TokenStream
Lexer --reports--> Error
TokenStream --feeds--> Parser
3. HST STATE MODEL
S_t = (I_t, R, B, K_t)
I_t = (source, cursor_t, mode_t, tokens_t, diagnostics_t)
R = ordered lexical rules, longest-match selection and tie-breaking policy
B = input bounds, valid token patterns, mode constraints, resource limits
K_t = accepted token classifications, symbol context where permitted, provenance
E_t = expand(I_t, R)
V_t = {candidate in E_t | satisfies(candidate, B)}
a_t = argmax_{candidate in V_t} P(candidate | I_t, R, B, K_t)
I_(t+1) = update(I_t, a_t)
NOTE: For deterministic lexing, the argmax notation is a selection metaphor. The actual choice is fixed by longest match and declared rule priority; no probabilistic scoring is required.
4. FORMAL LEXICAL TRANSITION
Let source be a finite string of length n and cursor p in [0,n].
match(r, source, p) = longest prefix starting at p accepted by rule r, or NONE.
C(p) = {(r,lexeme) | match(r,source,p) != NONE}.
select(C) = candidate with greatest lexeme length; break ties by rule priority.
If p = n: emit EOF once and terminate.
If C(p) is empty: report lexical error at p and advance by a defined recovery unit, or halt.
Otherwise: (r,x) = select(C(p)); emit token(type(r), x, span(p,p+|x|)) unless r is a skip rule; set p := p+|x|.
5. EXECUTION FLOW
INPUT(source, rules, initial_mode)
-> VALIDATE_ENCODING_AND_RULES
-> INITIALISE(cursor=0, tokens=[], diagnostics=[])
-> DETECT_END_OF_INPUT
-> FIND_MATCHING_RULES
-> APPLY_LONGEST_MATCH_AND_PRIORITY
-> CLASSIFY_TOKEN
-> CAPTURE_SPAN_AND_PROVENANCE
-> EMIT_OR_SKIP
-> ADVANCE_CURSOR
-> REPEAT_UNTIL_EOF_OR_FAILURE
-> OUTPUT(tokens, diagnostics, completion_status)
6. INFERENCE RULES
IF a prefix matches an identifier rule AND no longer valid token matches, THEN emit IDENTIFIER.
IF a keyword and identifier rule match equal text, THEN use the declared priority or post-classification keyword lookup.
IF a whitespace or comment skip rule matches, THEN advance without emitting a normal token.
IF an opening delimiter changes lexical mode, THEN transition to the configured mode.
IF no rule matches, THEN produce a located lexical error; never silently discard input unless recovery explicitly permits it.
IF EOF occurs inside an unterminated string or comment mode, THEN emit an unterminated-construct diagnostic.
7. MINI RECURSION ENGINE
LEX(source,p,mode,acc):
if p == length(source): return (acc + EOF, COMPLETE)
candidates := valid_matches(source,p,mode)
if candidates empty: return RECOVER_OR_FAIL(source,p,mode,acc)
chosen := deterministic_select(candidates)
require length(chosen.lexeme) > 0
next_acc := emit_or_skip(acc,chosen)
return LEX(source,p+length(chosen.lexeme),next_mode(mode,chosen),next_acc)
Implementation may use iteration instead of call-stack recursion.
8. BOUNDARIES AND INVARIANTS
0 <= cursor <= length(source).
Every successful non-EOF transition consumes at least one input unit.
Emitted token spans are ordered and non-overlapping for ordinary single-pass lexing.
Every emitted lexeme corresponds to the source substring recorded by its span.
Rule precedence must be deterministic and documented.
Encoding units (bytes, code points, grapheme clusters) must not be conflated.
Zero-length matching rules must be rejected or handled without non-termination.
No claim of universal language correctness follows from lexical validity alone.
9. TERMINATION AND EXECUTION GUARANTEE
For finite source length n, positive progress on each successful transition and bounded error recovery imply termination in at most n consuming transitions plus EOF handling. This is a conditional guarantee; it does not hold if custom rule evaluation fails to terminate.
10. EXAMPLE
SOURCE: let total = 12;
TOKENS:
KEYWORD("let",0..3)
IDENTIFIER("total",4..9)
OPERATOR("=",10..11)
INTEGER("12",12..14)
PUNCTUATION(";",14..15)
EOF(15..15)
Whitespace is skipped; offsets use zero-based, end-exclusive positions.
11. CROSS-ONTOLOGY LINKS
Programming Language Ontology: supplies lexical conventions.
Parser Ontology: consumes the token stream.
Compiler Ontology: integrates lexing into compilation.
Interpreter Ontology: may tokenise input before evaluation.
Source Code Ontology: supplies source artefacts.
Information Encoding Ontology: defines character decoding constraints.
12. CITATION AND PROVENANCE
Cite source as: Higgins, Mark Anthony; Sky Recursive AI. "Lexer Ontology", Kireth Research Semantic Library, revised 03 August 2026. URL above.
This TXT is a newly constructed interpretive Flow Algebra representation of the topic. Verify it against the complete source specification before treating it as an exact formalisation.