/** * Fach: ALG * Logo2Java-Parser * by Gilles Zimmermann, Stefan Urech * (c) by zugut.ch * 30.01.2006 */ options { LOOKAHEAD = 1; DEBUG_PARSER = true; } PARSER_BEGIN(Logo) import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class Logo { public static FileWriter fileWriter = null; public static String outputFileName = ".." + File.separatorChar + "build" + File.separatorChar; public static int repcount = 0; public static void main(String args[]) throws ParseException, TokenMgrError { if( args == null || args.length < 1 ) { System.out.println("Wrong parameter!"); System.exit(0); } FileReader reader = null; try { reader = new FileReader( new File(args[0] )); } catch (FileNotFoundException e) { System.out.println("File Not Found: " + args[0]); System.exit(1); } Logo parser = new Logo(reader); try { parser.program(); System.out.println("\nFinished!"); } catch (ParseException x) { System.out.println("Syntaxtic Error\n" + x.toString() ); throw x; } catch (TokenMgrError x) { System.out.println("Lexical Error\n" + x.toString() ); throw x; } } /** * Schreibt einen Text in die Output-Datei */ public static void Write( String text ) { try { fileWriter.write(text); } catch( IOException e ) { System.out.println( e.toString() ); System.exit(0); } } /** * Schreibt einen Text in die Output-Datei */ public static void WriteLine( String text ) { try { fileWriter.write(text + "\n" ); } catch( IOException e ) { System.out.println( e.toString() + "\n\r"); System.exit(0); } } /** * Erstellt den Filewriter für den gegebenen Dateiname */ public static void CreateFileWriter( String filename ) { try { FileWriter htmlWriter = new FileWriter( outputFileName + filename + ".html", false ); htmlWriter.write( "\n" ); htmlWriter.flush(); htmlWriter.close(); } catch( IOException e ) { System.out.println( e.toString() ); System.exit(0); } try { fileWriter = new FileWriter( outputFileName + filename + ".java", false ); } catch( IOException e ) { System.out.println( e.toString() ); System.exit(0); } } public static void CloseFileWriter() { try { fileWriter.flush(); fileWriter.close(); } catch( IOException e ) { System.out.println( e.toString() ); System.exit(0); } } } PARSER_END(Logo) SKIP: { "#" : WithinComment } SKIP: { "\n" : DEFAULT } SKIP: { "\r" : DEFAULT } SKIP: { "\r\n" : DEFAULT } MORE: { <~[]> } SKIP: { } SKIP: { } TOKEN : { | | | | | | | | | | | | | | | | | | | | | <#DIGIT: ["0"-"9"]> | <#DIGITS: ()+>| ("." )?>| <#LCHAR: ["a"-"z"]> | <#UCHAR: ["A"-"Z"]> | <#CHAR: | > | | ( | )*> } void program() : { Token t; } { t = { CreateFileWriter( t.image.toLowerCase() ); WriteLine("import java.awt.Graphics;"); WriteLine(""); WriteLine("public class " + t.image.toLowerCase() + " extends java.applet.Applet {"); WriteLine(""); WriteLine("private LogoPrimitives logo;"); } (subroutine())* { WriteLine("public void paint(Graphics g) {"); WriteLine("logo = new LogoPrimitives(this);"); WriteLine(""); } (statement())* { WriteLine("}"); WriteLine("}"); CloseFileWriter(); } } void subroutine() : { String komma = ""; Token t = null; } { t = { Write( "public void " + t.image + "( " ); } ( ":" t = { Write( komma + "double " + t.image ); komma = ", "; } )* { Write( ") {" ); } (statement())* { WriteLine( "}" ); } } void statement() : { Token t = null; String komma = ""; } { { WriteLine("logo.cs();"); } | { WriteLine("logo.pd();"); }| { WriteLine("logo.pu();"); }| { WriteLine("logo.ht();"); }| { WriteLine("logo.st();"); }| { Write("logo.fd("); } nExpr() { WriteLine(");"); }| { Write("logo.bk("); } nExpr() { WriteLine(");"); }| { Write("logo.lt("); } nExpr() { WriteLine(");"); }| { Write("logo.rt("); } nExpr() { WriteLine(");"); }| { Write("logo.wait("); } nExpr() { WriteLine(");"); }| ( { repcount++; Write("for( int repcount_" + repcount + "=0 ; repcount_" + repcount + " < "); } nExpr() { WriteLine(" ; repcount_" + repcount + "++ ){"); } "[" (statement())* "]" { WriteLine("}"); repcount--; } ) | ( { Write("if( "); } bExpr() { Write(" ){"); } "[" (statement())* "]" { WriteLine("}"); } ) | ( { Write("if( "); } bExpr() { Write(" ){"); } "[" (statement())* "]" { Write("}else{"); } "[" (statement())* "]" { WriteLine("}"); } ) | ( t = { Write(t.image + "(" ); } ( { Write(komma); } nExpr() { komma = ", "; } )* { Write( ");" ); } ) } void nExpr() : { Token t = null; } { nTerm() ( (t = "+"|t = "-") { Write( t.image ); } nTerm() )* } void nTerm() : { Token t = null; } { nFactor() ( (t = "*"|t = "/") { Write( t.image ); } nFactor() )* } void nFactor() : { Token t = null; } { ("-"{ Write( "-" ); })? ( t = { Write( t.image );} | { Write( "repcount_" + repcount );} | ":" t = { Write( t.image ); } | ( "(" { Write( "(" ); } nExpr() ")" { Write( ")" ); } ) ) } void bExpr() : {} { bTerm() ( { Write( "||" ); } bTerm() )* } void bTerm() : {} { bFactor() ( { Write( "&&" ); } bFactor() )* } void bFactor() : { Token t = null; } { { Write( "true" ); } | { Write( "false" ); } | ( "(" { Write( "!(" ); } bExpr() { Write( ")" ); } ")" ) | ( nExpr() ( t = "==" | t = "!=" | t = "<" | t = ">" | t = "<=" | t = ">=" ) { Write( t.image ); } nExpr() ) }