Results 1 to 7 of 7
  1. #1
    mustafa is offline Member
    Join Date
    Dec 2013
    Posts
    3
    Rep Power
    0

    Question Javacc code generation

    Hi everyone. I am developing a mini language for my school, but I am stuck. How can I convert my syntax into the syntax of C language and then compile it with gcc and execute it. I am under linux mint. I am using javacc

    Java Code:
    options
    {
    	JDK_VERSION = "1.5";
    	static = true;
    	/*debug_parser = true;*/
    }
    
    PARSER_BEGIN(EG0)
    import java.util.*;
    import java.util.*;
    import java.io.*;
    
    public class EG0
    {
    	private static List<Identificador> identificadores = new ArrayList<Identificador>();
    	private static Token tipo1 = null, tipo2 = null, tipo3 = null, tipo4 = null;
    	
    	public static void main(String args[])throws ParseException
    	{
    		
    		try
    		{
    			EG0 parser = new EG0(new FileInputStream("/home/mustafa/Escritorio/JavaCC/EG0/programa.musta"));
    		
    			parser.tokentizer();
    			
    			for(Identificador i: identificadores)
    			{
    				System.out.println(i.getNombre());
    				System.out.println(i.getTipo());
    				System.out.println(i.getValor());
    			}
    			
    		}
    		
    		catch(Exception e)
    		{
    			System.out.println(e.getMessage());
    		}
    	}
    }
    PARSER_END(EG0)
    
    /*Secuencias de espace*/
    SKIP:
    {
    	" " |
    	"\r" |
    	"\t" |
    	"\n"
    }
    
    /*Operadores aritmeticos*/
    TOKEN:
    {
    	<SUMA: "+"> |
    	<RESTA: "-"> |
    	<MULTIPLICAR: "*"> |
    	<DIVIDIR: "/"> |
    	<RECIDUO: "%"> |
    	<IGUAL: "=">
    }
    
    /*Operadores relacionales*/
    TOKEN:
    {
    	<MAYOR: ">"> |
    	<MENOR: "<"> |
    	<MAYORIGUAL: ">="> |
    	<MENORIGUAL: "<="> |
    	<IGUALIGUAL: "==">
    }
    
    /*Operadores logicos*/
    TOKEN:
    {
    	<AND: "&&"> |
    	<OR: "!!"> |
    	<NOT: "!=">
    }
    
    /*Palabras reservadas del lenguaje*/
    TOKEN:
    {
    	<INICIO: "=)"> |
    	<FIN: "=("> |
    	<ENTERO: "intx"> |
    	<REAL: "floatx"> |
    	<CADENA: "stringx"> |
    	<IF: "ifx"> |
    	<ELSE: "elsex"> |
    	<FOR: "forx"> |
    	<WHILE: "whilex"> |
    	<SWITCH: "switchx"> |
    	<CASE: "casex"> |
    	<BREAK: "breakx"> |
    	<INPUT: "inputx"> |
    	<OUTPUT: "outputx">
    }
    
    /*Valores numericos, caracteres e identificadores*/
    TOKEN:
    {
    	<VALOR_ENTERO: (<digitos>)+> |
    	<VALOR_REAL: (<digitos>)+"."(<digitos>)+> |
    	<VALOR_CADENA: ("\""(~["\""])+"\"")> |
    	<IDENTIFICADOR: (<letras>) (<letras> | <digitos>)*> |
    	<#digitos: ["0"-"9"]> |
    	<#letras: ["a"-"z", "A"-"Z"]>
    }
    
    /*Lexemas simples*/
    TOKEN:
    {
    	 <LPAREN: "(">  | <RPAREN: ")"> | <SEMICOLON: ";"> | <INCREMENTO: "++"> | <PUNTOPUNTO: ":">
    }
    
    /*Analizador lexico*/
    public void tokentizer():
    {}
    {
    	<INICIO>
    	{
    		System.out.println("#include <stdio.h>");
    		System.out.println("#include <stdlib.h>");
    		System.out.println("");
    		System.out.println("int main()");
    		System.out.println("{");
    	} 
    	
    	(intX() | floatX() | stringX() | outputX()) 
    	
    	<FIN>
    	{
    		System.out.println("return 0;");
    		System.out.println("}");
    	}
    }
    
    /*Tipo de dato int*/
    public void intX():
    {
    	Token tipo;
    	Token nombre;
    	Token valores;
    	Identificador id;
    }
    {
    	(tipo = <ENTERO>) (nombre = <IDENTIFICADOR>) <IGUAL> (valores = valores()) <SEMICOLON>
    	
    	{
    		id = new Identificador();
    		id.setTipo(tipo.image);
    		id.setNombre(nombre.image);
    		id.setValor(valores.image);
    	}
    	
    	/*Analisis semantico*/
    	{
    		if(valores == tipo1)
    		{
    			/*Generacion de codigo*/
    			if(id.getTipo().equals("intx"))
    				System.out.print("int " + id.getNombre());
    			
    			if(valores != null)
    				System.out.print(" = " + id.getValor());
    			
    			System.out.println(";");
    			
    		}
    		
    		if(valores == tipo2)
    			System.out.println("El valor " + tipo2 + " es semanticamente incorrecto");
    			
    		if(valores == tipo3)
    			System.out.println("El valor " + tipo3 + " es semanticamente incorrecto");
    			
    		if(valores == tipo4)
    			System.out.println("El identificador " + tipo4 + " es semanticamente incorrecto");
    	}
    }
    
    /*Tipo de dato float*/
    public void floatX():
    {
    	Token tipo;
    	Token nombre;
    	Token valores;
    	Identificador id;
    }
    {
    	(tipo = <REAL>) (nombre = <IDENTIFICADOR>) <IGUAL> (valores = valores()) <SEMICOLON>
    	
    	{
    		id = new Identificador();
    		id.setTipo(tipo.image);
    		id.setNombre(nombre.image);
    		id.setValor(valores.image);
    	}
    	
    	/*Analisis semantico*/
    	{
    		if (valores == tipo1)
    			System.out.println("El valor " + tipo1 + " es semanticamente incorrecto");
    			
    		if(valores == tipo2)
    		{
    			/*Generacion de codigo*/
    			if(id.getTipo().equals("floatx"))
    				System.out.print("double " + id.getNombre());
    			
    			if(valores != null)
    				System.out.print(" = " + id.getValor());
    		
    			System.out.println(";");
    			
    		}
    		
    		if(valores == tipo3)
    			System.out.println("El valor " + tipo3 + " es semanticamente incorrecto");
    			
    		if(valores == tipo4)
    			System.out.println("El identificador " + tipo4 + " es semanticamente incorrecto");
    	}
    }
    
    /*Tipo de dato string*/
    public void stringX():
    {
    	Token tipo;
    	Token nombre;
    	Token valores;
    	Identificador id;
    }
    {
    	(tipo = <CADENA>) (nombre = <IDENTIFICADOR>) <IGUAL> (valores = valores()) <SEMICOLON>
    	{
    		id = new Identificador();
    		id.setTipo(tipo.image);
    		id.setNombre(nombre.image);
    		id.setValor(valores.image);
    		
    		/*Analisis semantico*/
    		if (valores == tipo1)
    			System.out.println("El valor " + tipo1 + " es semanticamente incorrecto");
    			
    		if(valores == tipo2)
    			System.out.println("El valor " + tipo2 + " es semanticamente incorrecto");
    		
    		if(valores == tipo3)
    		{
    			/*Generacion de codigo*/
    			if(id.getTipo().equals("stringx"))
    				System.out.print("char " + id.getNombre() + "[255]");
    		
    			if(valores != null)
    				System.out.print(" = " + id.getValor());
    		
    			System.out.println(";");
    		
    		}
    		
    		if(valores == tipo4)
    			System.out.println("El identificador " + tipo4 + " es semanticamente incorrecto");
    	}
    }
    
    /*Valores, caracteres e identificadores*/
    public Token valores():
    {}
    {
    	(tipo1 = <VALOR_ENTERO> | tipo2 = <VALOR_REAL> | tipo3 = <VALOR_CADENA> | tipo4 = <IDENTIFICADOR>)
    	
    	{
    		if(tipo1 != null)
    			return tipo1;
    			
    		if(tipo2 != null)
    			return tipo2;
    			
    		if(tipo3 != null)
    			return tipo3;
    			
    		if(tipo4 != null)
    			return tipo4;
    	}
    }
    
    /*Funciones de entrada y salida*/
    public void outputX():
    {
    	Token letra;
    }
    {
    	<OUTPUT> (letra = <VALOR_CADENA>) <SEMICOLON>
    	{
    		System.out.println("printf(" + letra + ");");
    	}
    }
    this is my programa.musta
    Java Code:
    =)
    outputx "Hola Mundo!";
    =(

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,926
    Rep Power
    8

    Default Re: Javacc code generation

    This isn't exactly trivial, and you've mentioned several different individual steps. Which part of this is giving you trouble?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Javacc code generation

    According to the docs, the feature to generate C code from JavaCC is in early testing. You'll probably need to go to the JavaCC forum or mailing list to find out how to use it.
    Get in the habit of using standard Java naming conventions!

  4. #4
    mustafa is offline Member
    Join Date
    Dec 2013
    Posts
    3
    Rep Power
    0

    Default Re: Javacc code generation

    How do I convert my file named "programa.musta" into another file programa.c and then compile it with gcc and execute it?

    Java Code:
    import java.util.*;
    import java.util.*;
    import java.io.*;
    
    public class EG0
    {
    
    	public static void main(String args[])throws ParseException
    	{
    		
    		try
    		{
    			EG0 parser = new EG0(new FileInputStream("/home/mustafa/Escritorio/JavaCC/EG0/programa.musta"));
    		
    			parser.tokentizer();
    			
    		}
    		
    		catch(Exception e)
    		{
    			System.out.println(e.getMessage());
    		}
    	}
    }
    Sorry for my ambiguities :(
    Last edited by mustafa; 01-02-2014 at 08:07 AM.

  5. #5
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,926
    Rep Power
    8

    Default Re: Javacc code generation

    Quote Originally Posted by mustafa View Post
    How do I convert my file named "programa.musta" into another file programa.c and then compile it with gcc and execute it?
    What have you tried? You've received two replies that you seem to be ignoring. What are you confused about?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    mustafa is offline Member
    Join Date
    Dec 2013
    Posts
    3
    Rep Power
    0

    Default Re: Javacc code generation

    Quote Originally Posted by KevinWorkman View Post
    What have you tried? You've received two replies that you seem to be ignoring. What are you confused about?
    There is something that I must do in the "try" part of the code. I dont know how to setup in order to convert the file called programa.musta into another one called programa.c and then compile the C file and run it. But everything has to be in the "try".

    I know I have to use the classes FileOutputStream, File.. etc but I dont know how. I would like to see an example.

    Thanks for replaying me!!

  7. #7
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Javacc code generation

    My answer is still the same: find the JavaCC support forum and ask there. If what you want to do is even possible, it is only possible with a brand new feature of JavaCC that is still in beta testing. I have no idea how to use it, and it's unlikely that anyone else here knows, either. Seek ye the JavaCC forum!
    Get in the habit of using standard Java naming conventions!

Similar Threads

  1. How to handle error in parser javacc
    By rrl in forum New To Java
    Replies: 14
    Last Post: 06-12-2013, 09:58 AM
  2. Maven & Axis2 plugin - different stub code generation
    By ClaudioQ in forum Advanced Java
    Replies: 0
    Last Post: 11-13-2012, 05:54 PM
  3. Automatic Stringbuilder code generation
    By JeffB in forum Reviews / Advertising
    Replies: 0
    Last Post: 10-23-2012, 01:53 AM
  4. Code generation library?
    By kjkrum in forum Advanced Java
    Replies: 3
    Last Post: 05-25-2011, 01:48 AM
  5. JavaCC parser generator.
    By Danpanda in forum New To Java
    Replies: 0
    Last Post: 04-19-2010, 12:07 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •