1

所以我有一堆 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() 命令。我几乎完成了所有这些工作,只是组织得不好,或者至少我认为有比我所有的“如果”陈述更好的方法来解决它。所以这是我的第一个问题,有什么比我做的更好吗?

接下来是我要查找错误并报告它们。例如,如果我从上面的文本中删除了“\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!

这显然不是真的,但我不知道如何重组我的错误处理以使这些情况正常工作。任何人都可以提供任何帮助吗?尤其是在组织此代码以更好地适应这些问题的方式方面。

- - - - - - - - - - - - - - - - - - - - - -代码 - - - -------------------------------------

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()){



    }
}
}
4

0 回答 0