ANTLR的BibTex语法

sys*_*ult 6 grammar parsing antlr bibtex

我正在寻找ANTLR中的bibtex语法用于业余爱好项目.我不想花时间编写ANTLR语法(这可能需要一些时间,因为它将涉及学习曲线).任何指针我都很感激.

注意: 我发现bibtex语法用于野牛和yacc但是找不到任何用于antlr的语法.

编辑: 正如Bart指出的那样,我不需要在引用的字符串中解析前导码和tex.

Bar*_*ers 9

这是一个(非常)基本的BibTex语法,它发出一个AST(与一个简单的解析树相反):

grammar BibTex;

options {
  output=AST;
  ASTLabelType=CommonTree;
}

tokens {
  BIBTEXFILE;
  TYPE;
  STRING;
  PREAMBLE;
  COMMENT;
  TAG;
  CONCAT;
}

//////////////////////////////// Parser rules ////////////////////////////////
parse
  :  (entry (Comma? entry)* Comma?)? EOF             -> ^(BIBTEXFILE entry*)
  ;

entry
  :  Type Name Comma tags CloseBrace                 -> ^(TYPE Name tags)
  |  StringType Name Assign QuotedContent CloseBrace -> ^(STRING Name QuotedContent)
  |  PreambleType content CloseBrace                 -> ^(PREAMBLE content)
  |  CommentType                                     -> ^(COMMENT CommentType)
  ;

tags
  :  (tag (Comma tag)* Comma?)?                      -> tag*
  ;

tag
  :  Name Assign content                             -> ^(TAG Name content)
  ;

content
  :  concatable (Concat concatable)*                 -> ^(CONCAT concatable+)
  |  Number
  |  BracedContent
  ;

concatable
  :  QuotedContent
  |  Name
  ;

//////////////////////////////// Lexer rules ////////////////////////////////
Assign
  :  '='
  ;

Concat
  :  '#'
  ;

Comma
  :  ','
  ;

CloseBrace
  :  '}'
  ;

QuotedContent
  :  '"' (~('\\' | '{' | '}' | '"') | '\\' . | BracedContent)* '"'
  ;

BracedContent
  :  '{' (~('\\' | '{' | '}') | '\\' . | BracedContent)* '}'
  ;

StringType
  :  '@' ('s'|'S') ('t'|'T') ('r'|'R') ('i'|'I') ('n'|'N') ('g'|'G') SP? '{'
  ;

PreambleType
  :  '@' ('p'|'P') ('r'|'R') ('e'|'E') ('a'|'A') ('m'|'M') ('b'|'B') ('l'|'L') ('e'|'E') SP? '{'
  ;

CommentType
  :  '@' ('c'|'C') ('o'|'O') ('m'|'M') ('m'|'M') ('e'|'E') ('n'|'N') ('t'|'T') SP? BracedContent
  |  '%' ~('\r' | '\n')*
  ;

Type
  :  '@' Letter+ SP? '{'
  ;

Number
  :  Digit+
  ;

Name
  :  Letter (Letter | Digit | ':' | '-')*
  ;

Spaces
  :  SP {skip();}
  ;

//////////////////////////////// Lexer fragments ////////////////////////////////
fragment Letter
  :  'a'..'z'
  |  'A'..'Z'
  ;

fragment Digit
  :  '0'..'9'
  ;

fragment SP
  :  (' ' | '\t' | '\r' | '\n' | '\f')+
  ;  
Run Code Online (Sandbox Code Playgroud)

(如果你不想要AST,删除->它右边的所有内容和删除它们options{...}tokens{...}块)

可以使用以下类进行测试:

import org.antlr.runtime.*;
import org.antlr.runtime.tree.*;
import org.antlr.stringtemplate.*;

public class Main {
  public static void main(String[] args) throws Exception {

    // parse the file 'test.bib'
    BibTexLexer lexer = new BibTexLexer(new ANTLRFileStream("test.bib"));
    BibTexParser parser = new BibTexParser(new CommonTokenStream(lexer));

    // you can use the following tree in your code
    // see: http://www.antlr.org/api/Java/classorg_1_1antlr_1_1runtime_1_1tree_1_1_common_tree.html
    CommonTree tree = (CommonTree)parser.parse().getTree();

    // print a DOT tree of our AST
    DOTTreeGenerator gen = new DOTTreeGenerator();
    StringTemplate st = gen.toDOT(tree);
    System.out.println(st);
  }
}
Run Code Online (Sandbox Code Playgroud)

和以下示例Bib-input(file:)test.bib:

@PREAMBLE{
  "\newcommand{\noopsort}[1]{} "
  # "\newcommand{\singleletter}[1]{#1} " 
}

@string { 
  me = "Bart Kiers" 
}

@ComMENt{some comments here}

% or some comments here

@article{mrx05,
  auTHor = me # "Mr. X",
  Title = {Something Great}, 
  publisher = "nob" # "ody",
  YEAR = 2005,
  x = {{Bib}\TeX},
  y = "{Bib}\TeX",
  z = "{Bib}" # "\TeX",
},

@misc{ patashnik-bibtexing,
       author = "Oren Patashnik",
       title = "BIBTEXing",
       year = "1988"
} % no comma here

@techreport{presstudy2002,
    author      = "Dr. Diessen, van R. J. and Drs. Steenbergen, J. F.",
    title       = "Long {T}erm {P}reservation {S}tudy of the {DNEP} {P}roject",
    institution = "IBM, National Library of the Netherlands",
    year        = "2002",
    month       = "December",
}
Run Code Online (Sandbox Code Playgroud)

运行演示

如果您现在从语法生成解析器和词法分析器:

java -cp antlr-3.3.jar org.antlr.Tool BibTex.g
Run Code Online (Sandbox Code Playgroud)

并编译所有.java源文件:

javac -cp antlr-3.3.jar *.java
Run Code Online (Sandbox Code Playgroud)

最后运行这个Main类:

*nix中/ MacOS的

java -cp .:antlr-3.3.jar Main
Run Code Online (Sandbox Code Playgroud)

视窗

java -cp .;antlr-3.3.jar Main
Run Code Online (Sandbox Code Playgroud)

您将在控制台上看到与以下AST对应的输出:

在此输入图像描述

(点击图片放大图片,使用graphviz-dev.appspot.com生成)

强调:我没有正确测试语法!我写了一会儿,从来没有在任何项目中使用它.

  • @汤姆,没问题.让我们假装你只是通过电子邮件发送给我,然后我改变了一些语法并通过电子邮件将这个语法邮寄回来.您拥有(书面)许可,可以添加任何类似开源的许可证.如果你想得到我更正式的书面同意,请给我发一条线(我的电子邮件在我的个人资料中),我会在允许的情况下通过电子邮件向你发送语法,以最适合你的方式授权. (2认同)