繁体   English   中英

JFLEX AND CUP,无法使其正常工作

[英]JFLEX AND CUP, cannot make it work correctly

我正在使用jflex和cup,尝试制作html解析器,但无法使其正常工作,

Netbeans,编译过程不会停止,始终会继续,无法在解析树中正确添加更多“令牌”,也无法在“ TEXTO”中添加空间,这会破坏整个树

lexicoh.jlex

      package compiladorhtml;
      import java_cup.runtime.*;

      %%

      %class Lexer 
      %line
      %column
      %cup

      %{   
          private Symbol symbol(int type) {
              return new Symbol(type, yyline, yycolumn);
          }
          private Symbol symbol(int type, Object value) {
              return new Symbol(type, yyline, yycolumn, value);
          }
      %}

      LineTerminator = \r|\n|\r\n
      WhiteSpace     = {LineTerminator} | [ \t\f]
      texto = [a-zA-Z0-9_]*

      %%

      <YYINITIAL> {
         "::"                { System.out.print("<"); return symbol(sym.INI);}
         "ENCA"              { System.out.print("HEAD>"); return symbol(sym.HEAD);}
         "/"                 { System.out.print("</");  return symbol(sym.FIN);}
         {texto}             { System.out.print(yytext()); return symbol(sym.TEXTO);}
         {WhiteSpace}        { /* just skip what was found, do nothing */ }   
         "&&"                  { System.out.print(""); return symbol(sym.FINAL); }   
      }
      [^]                    { throw new Error("Illegal character <"+yytext()+">"); }

sintaticoh.cup

                package compiladorhtml;
                import java_cup.runtime.*;

                parser code {:

                    public void report_error(String message, Object info) {

                        StringBuilder m = new StringBuilder("Error");

                        if (info instanceof java_cup.runtime.Symbol) {

                            java_cup.runtime.Symbol s = ((java_cup.runtime.Symbol) info);

                            if (s.left >= 0) {                

                                m.append(" in line "+(s.left+1));   

                                if (s.right >= 0)                    
                                    m.append(", column "+(s.right+1));
                            }
                        }

                        m.append(" : "+message);

                        System.err.println(m);
                    }

                    public void report_fatal_error(String message, Object info) {
                        report_error(message, info);
                        System.exit(1);
                    }
                :};



                terminal          INI, HEAD, TEXTO, FIN, FINAL;
                non terminal Object expr_list, expr_part;
                non terminal String expr;

                expr_list ::= expr_list expr_part | expr_part;

                expr_part ::= expr:e;

                expr ::=  INI HEAD 
                        | TEXTO 
                        | FIN HEAD
                        | FINAL;

Java Main

                 public static void main(String[] args) throws IOException, Exception {

                        //CreateFiles();

                        //EJECUTAR PARA VER SI FUNCIONA, YA LO VI Y FUNCIONA
                        File fichero = new File("fichero.txt");
                        PrintWriter writer;
                        try {
                            writer = new PrintWriter(fichero);
                            writer.print("::ENCA NOMBRE ENCABEZADO /ENCA &&");
                            writer.close();
                        } catch (FileNotFoundException ex) {
                            System.out.println(ex);
                        }
                        Lexer thisscanner = new Lexer(new FileReader("fichero.txt"));
                        parser thisparser = new parser(thisscanner);
                        thisparser.parse();

                    }

                    public static void CreateFiles() {
                        String filelex = "path\\lexicoh.jlex";

                        File file = new File(filelex);
                        jflex.Main.generate(file);

                        String opciones[] = new String[5];
                        opciones[0] = "-destdir";
                        opciones[1] = "path";
                        opciones[2] = "-parser";
                        opciones[3] = "parser";
                        opciones[4] = "path\\sintacticoh.cup";
                        try {
                            java_cup.Main.main(opciones);
                        } catch (Exception ex) {
                            Logger.getLogger(CompiladorHTML.class.getName()).log(Level.SEVERE, null, ex);
                        }

                    }

谢谢 在此处输入图片说明

我认为您应该这样做:

对于{texto},由于它是由文本和数字组成的字符串,因此您应按以下方式重新定义它:

{texto} { System.out.println (yytext()); return new symbol(sym.TEXTO, new String (yytext())); }

然后,如果程序没有停止,则在读取源文件的过程中可能会出现一些问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM