Generation

generate functionMon, 20 Mar 2023

public class Semantico { public static TablaSimbolos ts; public static void main(String[] args) throws IOException { StringReader sr = new StringReader("int x, y; x=y+3;"); ts = new TablaSimbolos(); AnalizadorLexico lex = new AnalizadorLexico(sr); TokenPersonalizado tp; Sintactico s = new Sintactico(lex); } } public class Sintactico { private AnalizadorLexico lex; private TokenPersonalizado tp; private StringBuffer mensajes; private Token token; private TokenPersonalizado lexico; public Sintactico(AnalizadorLexico lex) throws IOException { this.lex = lex; mensajes = new StringBuffer(); tp = lex.yylex(); this.lexico = tp; this.simoloI(); mensajes.append("\nFin del análisis...\n"); //Simbolo sim = Semantico.ts.buscar("int x, n1;"); //System.out.println(sim); //System.out.println(tp); } /*private void simoloI() throws IOException { boolean salida = false; while (!salida) { if (tp.getToken() == Token.ID) { tp = lex.yylex(); if (tp == null || !(tp.getLexema().equals("="))) { tp = lex.yylex(); this.simoloE(); if (tp == null || !(tp.getLexema().equals(";"))) { tp = lex.yylex(); } else { salida = true; } } else { mensajes.append("Error: Se esperaba ';'\n"); } } else if (tp.getToken() == Token.INT) { System.out.println(tp); this.simoloD(); } else if (tp == null) { salida = true; } else { salida = true; } } }*/ private void simoloI() throws IOException { boolean salida = false; while (!salida) { if (tp == null) { break; } if (tp.getToken() == Token.ID) { System.out.println(tp); tp = lex.yylex(); if (tp.getLexema().equals("=")) { tp = lex.yylex(); this.simoloE(); if (tp.getLexema().equals(";")) { System.out.println(tp); tp = lex.yylex(); } else { salida = true; } } else { mensajes.append("Error: Se esperaba ';'\n"); } } else if (tp.getToken() == Token.INT) { this.simoloD(); //tp = lex.yylex(); if (tp != null || (tp.getLexema().equals(";"))) { tp = lex.yylex(); } else { salida = true; } } } } private void simoloD() throws IOException { boolean salida = false; System.out.println(tp); if (tp.getToken() == Token.INT) { //System.out.println(tp); while (!salida) { tp = lex.yylex(); if (tp.getToken() == Token.ID) { System.out.println(tp); tp = lex.yylex(); System.out.println(tp); if (tp == null || !(tp.getLexema().equals(","))) { salida = true; } } } } } private void simoloF() throws IOException { if (tp.getToken() == Token.ID) { tp = lex.yylex(); } else if (tp.getToken() == Token.NUMERO) { tp = lex.yylex(); } else if (tp.getLexema().equals("(")) { tp = lex.yylex(); this.simoloE(); if (tp.getLexema().equals(")")) { tp = lex.yylex(); } else { mensajes.append("Error: Se esperaba ')'\n"); } } else { mensajes.append("Error: Se esperaba 'num, variable o ('\n"); } } private void simoloT() throws IOException { this.simoloF(); while (tp.getLexema().equals("*")) { tp = lex.yylex(); this.simoloF(); } } private void simoloE() throws IOException { boolean salida = false; while (!salida) { this.simoloT(); if (lexico.getToken() == Token.OP_ARIT) { if (lexico.getLexema().equals("+") || lexico.getLexema().equals("-")) { token = lexico.getToken(); lexico = lex.yylex(); } } else if (true) { } else { mensajes.append("Error: Se esperaba 'num, variable o ('\n"); } salida = true; } } public String getMensajes() { return this.mensajes.toString(); } } Hacer que imprima lo que lee el StringBuffer

public class Sintactico { private AnalizadorLexico lex; private TokenPersonalizado tp; private StringBuffer mensajes; private Token token; private TokenPersonalizado lexico; public Sintactico(AnalizadorLexico lex) throws IOException { this.lex = lex; mensajes = new StringBuffer(); tp = lex.yylex(); this.lexico = tp; this.simoloI(); mensajes.append("\nFin del análisis...\n"); //Simbolo sim = Semantico.ts.buscar("int x, n1;"); //System.out.println(sim); //System.out.println(tp); } /*private void simoloI() throws IOException { boolean salida = false; while (!salida) { if (tp.getToken() == Token.ID) { tp = lex.yylex();

Want to kickstart your project?Use the new AI Studio to create your code