JFLEX AND CUP, cannot make it work correctly

大兔子大兔子 提交于 2021-01-29 01:42:48

问题


I'm working with jflex and cup, trying to make a html parser, but can't make it work correctly,

Netbeans, the compile process dont stop, always continues, Can't add more "tokens" correctly in the parse tree, Also can't add space in "TEXTO" , this break the entire tree

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

                    }

thanks enter image description here


回答1:


I think that you should do this:

for {texto}, since it is a string composed by text and number, you should redefine it in this way:

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

Then, if the program doesn't stop, there could be some problems during the read of the source file.



来源:https://stackoverflow.com/questions/23832272/jflex-and-cup-cannot-make-it-work-correctly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!