ANTLR 的 BibTex 语法 [英] BibTex grammar for ANTLR

查看:46
本文介绍了ANTLR 的 BibTex 语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 ANTLR 中寻找 bibtex 语法以用于业余爱好项目.我不想把时间花在编写 ANTLR 语法上(这对我来说可能需要一些时间,因为它会涉及学习曲线).所以我很感激任何指点.

I'm looking for a bibtex grammar in ANTLR to use in a hobby project. I don't want to spend my time for writing ANTLR grammar (this may take some time for me because it will involve a learning curve). So I'd appreciate for any pointers.

注意:我找到了 bibtex 语法,用于 bison 和 yacc,但找不到任何用于 antlr 的语法.

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

推荐答案

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

Here's a (very) rudimentary BibTex grammar that emits an AST (contrary to a simple parse tree):

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')+
  ;  

(如果您不想要 AST,请删除所有 -> 及其右侧的所有内容并删除 options{...}tokens{...} 块)

(if you don't want the AST, remove all -> and everything to the right of it and remove both the options{...} and tokens{...} blocks)

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

which can be tested with the following class:

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);
  }
}

以及以下示例 Bib-input(文件:test.bib):

and the following example 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 the demo

If you now generate a parser & lexer from the grammar:

java -cp antlr-3.3.jar org.antlr.Tool BibTex.g

并编译所有.java源文件:

javac -cp antlr-3.3.jar *.java

最后运行Main类:

java -cp .:antlr-3.3.jar Main

视窗

java -cp .;antlr-3.3.jar Main

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

You'll see some output on your console which corresponds to the following AST:

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

强调:我没有正确测试语法!我之前写过它,但从未在任何项目中真正使用过它.

To emphasize: I did not properly test the grammar! I wrote it a while back and never really used it in any project.

这篇关于ANTLR 的 BibTex 语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆