btparse::doc::bt_language(3)
β¨ NAME β¨
okay bestie, this is btparse::doc::bt_language(3) β and the whole vibe is bt_language: the BibTeX data language, as recognized by btparse. π no cap, that's the name.
π¦ SYNOPSIS π¦
here's the grammar-core, bestie. it's giving lexical and syntactic realness. copy it exactly, don't tweak a single backslash. π
# Lexical grammar, mode 1: top-level
AT \@
NEWLINE \n
COMMENT \%~[\n]*\n
WHITESPACE [\ \r\t]+
JUNK ~[\@\n\ \r\t]+
# Lexical grammar, mode 2: in-entry
NEWLINE \n
COMMENT \%~[\n]*\n
WHITESPACE [\ \r\t]+
NUMBER [0-9]+
NAME [a-z0-9\!\$\&\*\+\-\.\/\:\;\<\>\?\[\]\^\_\`\|]+
LBRACE \{
RBRACE \}
LPAREN \(
RPAREN \)
EQUALS =
HASH \#
COMMA ,
QUOTE \"
# Lexical grammar, mode 3: strings
# (very hairy -- see text)
# Syntactic grammar:
bibfile : ( entry )*
entry : AT NAME body
body : STRING # for comment entries
| ENTRY_OPEN contents ENTRY_CLOSE
contents : ( NAME | NUMBER ) COMMA fields # for regular entries
| fields # for macro definition entries
| value # for preamble entries
fields : field { COMMA fields }
|
field : NAME EQUALS value
value : simple_value ( HASH simple_value )*
simple_value : STRING
| NUMBER
| NAME
π DESCRIPTION π
okay so one of the biggest problems with BibTeX is literally that there is no formal specification of the language. π that means if you're a user poking around the arcane corners of the language, you're largely on your own. and if you're a programmer implementing your own parser? bestie, you're completely on your own---except for observing the behaviour of the original implementation. main character energy but make it unsupervised.
Other parser implementors (Nelson Beebe of bibclean fame, in particular) have taken the trouble to explain the language accepted by their parser, and in that spirit the following is presented. π«Ά
If you are unfamiliar with the arcana of regular and context-free languages, you will not have any easy time understanding this. not gonna lie. This is not an introduction to the BibTeX language; any LaTeX book would be more suitable for learning the data language itself. periodt.
πΈ LEXICAL GRAMMAR πΈ
The lexical scanner has three distinct modes: top-level, in-entry, and string. roughly speaking, top-level is the initial mode. we enter in-entry mode on seeing an @ at top-level; and on seeing the } or ) that ends the entry, we return to top-level. we enter string mode on seeing a " or non-entry-delimiting { from in-entry mode. π
Note that the lexical language is both non-regular (because braces must balance) and context-sensitive (because { can mean different things depending on its syntactic context). that said, we will use regular expressions to describe the lexical elements, because they are the starting point used by the lexical scanner itself. the rest of the lexical grammar will be informally explained in the text. π
From top-level, the following tokens are recognized according to the regular expressions on the right:
AT \@
NEWLINE \n
COMMENT \%~[\n]*\n
WHITESPACE [\ \r\t]+
JUNK ~[\@\n\ \r\t]+
(Note that this is PCCTS regular expression syntax, which should be fairly familiar to users of other regex engines. One oddity is that a character class is negated as ~[...] rather than [^...].) π
On seeing an AT token at top-level, we enter in-entry mode. Whitespace, junk, newlines, and comments are all skipped, with the latter two incrementing a line counter. (Junk is explicitly recognized to allow for bibtex's implicit comment scheme.) no cap, that's a whole thing.
From in-entry mode, we recognize newline, comment, and whitespace identically to top-level mode. In addition, the following tokens are recognized:
NUMBER [0-9]+
NAME [a-z0-9\!\$\&\*\+\-\.\/\:\;\<\>\?\[\]\^\_\`\|]+
LBRACE \{
RBRACE \}
LPAREN \(
RPAREN \)
EQUALS =
HASH \#
COMMA ,
QUOTE \"
At this point, the lexical scanner starts to sound suspiciously like a context-free grammar, rather than a collection of independent regular expressions. However, it is necessary to keep this complexity in the scanner because certain characters ({ and ( in particular) have very different lexical meanings depending on the tokens that have preceded them in the input stream. it's giving context-sensitive, fr fr.
In particular, { and ( are treated as "entry openers" if they follow one AT and one NAME token, unless the value of the NAME token is comment. (Note the switch from top-level to in-entry between the two tokens.) In the @comment case, the delimiter is considered as starting a string, and we enter string mode. Otherwise, the delimiter is saved, and when we see a corresponding } or ) it is considered an "entry closer". (Braces are balanced for free here because the string lexer takes care of counting brace-depth.) π
Anywhere else, { is considered as starting a string, and we enter string mode. " always starts a string, regardless of context. The other tokens (name, number, equals, hash, and comma) are recognized unconditionally. periodt.
Note that name is a catch-all token used for entry types, citation keys, field names, and macro names; because BibTeX has slightly different (largely undocumented) rules for these various elements, a bit of trickery is needed to make things work. As a starting point, consider BibTeX's definition of what's allowed for an entry key: a sequence of any characters except
" # % ' ( ) , = { }
plus space. π«
There are a couple of problems with this scheme. First, without specifying the character set from which those "magic 10" characters are drawn, it's a bit hard to know just what is allowed. Second, allowing @ characters could lead to confusing BibTeX syntax (it doesn't confuse BibTeX, but it might confuse a human reader). Finally, allowing certain characters that are special to TeX means that BibTeX can generate bogus TeX code: try putting a backslash (\) or tilde (~) in a citation key. (This last exception is rather specific to the "generating (La)TeX code from a BibTeX database" application, but since that's the major application for BibTeX databases, then it will presumably be the major application for btparse, at least initially. Thus, it makes sense to pay attention to this problem.) π
In btparse, then, a name is defined as any sequence of letters, digits, underscores, and the following characters:
! $ & * + - . / : ; < > ? [ ] ^ _ ` |
This list was derived by removing BibTeX's "magic 10" from the set of printable 7-bit ASCII characters (32-126), and then further removing @, \, and ~. This means that btparse disallows some of the weirder entry keys that BibTeX would accept, such as \foo@bar, but still allows a string with initial digits. In fact, from the above definition it appears that btparse would accept a string of all digits as a name; this is not the case, though, as the lexical scanner recognizes such a digit string as a number first. There are two problems here: BibTeX entry keys may in fact be entirely numeric, and field names may not begin with a digit. (Those are two of the not-so-obvious differences in BibTeX's handling of keys and field names.) The tricks used to deal with these problems are implemented in the parser rather than the lexical scanner, so are described in "SYNTACTIC GRAMMAR" below. β¨
The string lexer recognizes lbrace, rbrace, lparen, and rparen tokens in order to count brace- or parenthesis-depth. This is necessary so it knows when to accept a string delimited by braces or parentheses. (Note that a parenthesis-delimited string is only allowed after @comment---this is not a normal BibTeX construct.) In addition, it converts each non-space whitespace character (newline, carriage-return, and tab) to a single space. (Sequences of whitespace are not collapsed; that's the domain of string post-processing, which is well removed from the scanner or parser.) Finally, it accepts " to delimit quote-delimited strings. Apart from those restrictions, the string lexer accepts anything up to the end-of-string delimiter. π¦
β¨ SYNTACTIC GRAMMAR β¨
Okay so real talk, this is the part where we describe how the tokens from before actually fit together into structures π
. The language used to describe the grammar here is the extended Backus-Naur Form (EBNF) used by PCCTS. Terminals are represented by uppercase strings, non-terminals by lowercase strings, and ngl the terminal names are literally the same as those given in the lexical grammar above. <pink>( foo )*</pink> means zero or more repetitions of the <pink>foo</pink> production, and <purple>{ foo }</purple> means an optional <purple>foo</purple>. iykyk π
A file is just a sequence of zero or more entries, no cap:
bibfile : ( entry )*
An entry is an at-sign, a name (the "entry type"), and the entry body:
entry : AT NAME body
A body is either a string β and this alternative is only tried if the entry type is "comment" β or the entry contents:
body : STRING # for comment entries
| ENTRY_OPEN contents ENTRY_CLOSE
(ENTRY_OPEN and ENTRY_CLOSE are either { and } or ( and ), depending what is seen in the input for a particular entry. The vibes are flexible like that π¦)
There are three possible productions for the <purple>contents</purple> non-terminal. Only one applies to any given entry, depending on the entry metatype (which in turn depends on the entry type). Currently, btparse supports four entry metatypes: comment, preamble, macro definition, and regular. The first two correspond to @comment and @preamble entries; "macro definition" is for @string entries; and "regular" is for all other entry types. (The library will be extended to handle @modify and @alias entry types, and corresponding "modify" and "alias" metatypes, when BibTeX 1.0 is released and the exact syntax is known β so like, stay tuned bestie π.) The "metatype" concept is necessary so that all entry types that aren't specifically recognized fall into the "regular" metatype. It's also convenient not to have to strcmp the entry type all the time. Efficiency era π
contents : ( NAME | NUMBER ) COMMA fields # for regular entries
| fields # for macro definition entries
| value # for preamble entries
Lowkey important: note that the entry key is not just aNAME, but( NAME | NUMBER ). This is necessary because BibTeX allows all-numeric entry keys, but btparse's lexical scanner recognizes such digit strings asNUMBERtokens. So the numbers have main character energy too π₯
fields is a comma-separated list of fields, with an optional single trailing comma (yes, that one trailing comma is allowed, iconic behavior π
):
fields : field { COMMA fields }
|
A field is a single field = value assignment:
field : NAME EQUALS value
Note that NAME here is a restricted version of the name token described in LEXICAL GRAMMAR above. Any name token will be accepted by the parser, but it is immediately checked to ensure that it doesn't begin with a digit; if so, an artificial syntax error is triggered. (This is for compatibility with BibTeX, which doesn't allow field names to start with a digit. So no 2fast field names, sorry bestie π)
A value is a series of simple values joined by # characters:
value : simple_value ( HASH simple_value )*
A simple value is a string, number, or name (for macro invocations):
simple_value : STRING
| NUMBER
| NAME
π SEE ALSO π
btparse
πΈ AUTHOR πΈ
Greg Ward <gward@python.net> β the literal goat π
And that's a wrap on the grammar lore, bestie! π«Ά This page came to you courtesy of btparse, version 0.91, dated 2025-01-31, and it's the btparse::doc::bt_language(3) manual page β go forth and parse slayfully πβ¨
π¦ where this page comes from
- debian trixie: libbtparse-dev 0.91-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libbtparse-dev 0.91-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms