在LaTeX文档中查找错误,java

时间:2013-03-12 15:44:33

标签: java parsing error-handling command latex

所以我有一堆LaTeX风格的文档,一个看起来像这样......

\documentclass{article}
\usepackage{amsmath, amssymb, amsthm}
\begin{document}
    {\Large \begin{center} Homework Problems \end{center}}\begin{itemize}\item\end{itemize}
    \begin{enumerate}
            \item Prove: For all sets $A$ and $B$, $(A - B) \cup
                    (A \cap B) = A$.
                    \begin{proof}
                            \begin{align}
                                    & (A - B) \cup (A \cap B) && \\
                                    & = (A \cap B^c) \cup (A \cap B) && \text{by
                                    Alternate Definition of Set Difference} \\
                                    & = A \cap (B^c \cup B) && \text{by Distributive Law} \\
                                    & = A \cap (B \cup B^c) && \text{by Commutative Law} \\
                                    & = A \cap U && \text{by Union with the Complement Law} \\
                                    & = A && \text{by Intersection with $U$ Law}
                            \end{align}
                    \end{proof}
            \item If $n = 4k + 3$, does 8 divide $n^2 - 1$?
                    \begin{proof}
                            Let $n = 4k + 3$ for some integer $k$. Then
                            \begin{align}
                                    n^2 - 1 & = (4k + 3)^2 - 1 \\
                                    & = 16k^2 + 24k + 9 - 1 \\
                                    & = 16k^2 + 24k + 8 \\
                                    & = 8(2k^2 + 3k + 1) \text{,}
                            \end{align}
                            which is certainly divisible by 8.
                    \end{proof}
    \end{enumerate}
 \end{document}

现在首先我必须阅读每个文档和行,找到所有“\ begin {BLOCK}”和“\ end {BLOCK}”命令,并将BLOCK字符串添加到堆栈中,当我找到匹配的“ \ end“我会在我的堆栈上调用pop()命令。我完成了所有这些,它只是没有很好的组织,或者至少我认为有一个更好的方式去做它比我所有的“if”语句。所以这是我的第一个问题,是否有比我做的更好的事情?

接下来我想查找错误并报告错误。例如,如果我从上面的文本中删除了“\ begin {document}”行,我希望程序能够运行,执行它想要的所有操作,但是当它到达“\ end {document}”行时,它会报告缺少“\ begin”命令。我得到了我的代码来处理其他示例,例如删除枚举或逐项列出的“\ begin”命令,但我不能让这种情况起作用。

最后,我希望能够处理缺少的“\ end”命令。我已经尝试过了,但我不能完全正确地进行调节。假设我有这个文件......

\begin{argument}
\begin{Palin}No it can't. An argument is a connected series of statements intended to establish a proposition.\end{Palin}
\begin{Cleese}No it isn't.\end{Cleese}
\begin{Palin}\expression{exclamation}Yes it is! It's not just contradiction.\end{Palin}
\begin{Cleese}Look, if I argue with you, I must take up a contrary position.\end{Cleese}
\begin{Palin}Yes, but that's not just saying \begin{quotation}'No it isn't.'\end{Palin}
\begin{Cleese}\expression{exclamation}Yes it is!\end{Cleese}
\begin{Palin}\expression{exclamation}No it isn't!\end{Palin}
\begin{Cleese}\expression{exclamation}Yes it is!\end{Cleese}
\begin{Palin}Argument is an intellectual process.  Contradiction is just the automatic gainsaying of any statement the other person makes.\end{Palin}
\end{argument}

你会在第6行注意到有一个没有“\ end”的“\ begin {quotation}”命令。通过这个特定文档时我的代码将此作为输出...

PARSE ERROR Line 6: Missing command \begin{Palin}.
PARSING TERMINATED!

这显然不是真的,但我不知道如何重构我的错误处理以使这些情况起作用。有人可以提供任何帮助吗?特别是在组织此代码以更好地解决这些问题的过程中。

------------------------------------------- CODE --- ----------------------------------------

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.Stack;
import java.util.StringTokenizer;

public class LaTeXParser{

public static void main(String args[]) throws FileNotFoundException{

    Scanner scan = new Scanner(System.in);

    Stack s = new Stack();

    int lineCount = 0;

    String line;
    String nextData = null;
    String title = null;

            String fname;

            System.out.print("Enter the name of the file (no extension): ");
            fname = scan.next();

            fname = fname + ".txt";

            FileInputStream fstream = new FileInputStream(fname);

            Scanner fscan = new Scanner(fstream);

            System.out.println();

            while(fscan.hasNextLine()){

                lineCount++;
                line = fscan.nextLine();
                StringTokenizer tok = new StringTokenizer(line);

                while(tok.hasMoreElements()){

                    nextData = tok.nextToken();
                    System.out.println("The line: "+nextData);

                    if(nextData.contains("\\begin") && !nextData.contains("\\end")){

                        if(nextData.charAt(1) == 'b'){

                            title = nextData.substring(nextData.indexOf("{") + 1, nextData.indexOf("}"));

                            s.push(title);

                        }
                    }//end of BEGIN if

                    if(nextData.contains("\\end") && !nextData.contains("\\begin")){

                        String[] theLine = nextData.split("[{}]");

                        for(int i = 0 ; i < theLine.length ; i++){

                            if(theLine[i].contains("\\end") && !s.isEmpty() && theLine[i+1].equals(s.peek())){

                                s.pop();

                                i++;

                            }

                            if(theLine[i].contains("\\end") && !theLine[i+1].equals(s.peek())){

                                System.out.println("PARSE ERROR Line " + lineCount + ": Missing command \\begin{" + theLine[i+1] + "}.");
                                System.out.println("PARSING TERMINATED!");
                                System.exit(0);

                            }
                        }
                    }//end of END if

                    if(nextData.contains("\\begin") && nextData.contains("\\end")){

                        String[] theLine = nextData.split("[{}]");

                        for(int i = 0 ; i < theLine.length ; i++){

                            if(theLine[i].contains("\\end") && theLine[i+1].equals(s.peek())){

                                s.pop();

                            }

                            if(theLine[i].equals("\\begin")){

                                title = theLine[i+1];

                                s.push(title);
                            }
                        }
                    }//end of BEGIN AND END if
                }
            }//end of whiles

            fscan.close();

    if(s.isEmpty()){

        System.out.println();
        System.out.println(fname + " LaTeX file is valid!");
        System.exit(0);

    }

    while(!s.isEmpty()){



    }
}
}

0 个答案:

没有答案