From b087f2ddbe1c58d8d8a90f7013b3be8ee67e4988 Mon Sep 17 00:00:00 2001 From: tuvu Date: Mon, 10 Feb 2025 13:27:09 +0100 Subject: [PATCH] tested preety printer --- .vscode/settings.json | 2 +- src/main/antlr/TurtleLexer.g | 31 ----------- src/main/java/TP1/ASD_Turtle.java | 21 ++++---- src/main/java/TP1/Main.java | 79 ++++++++++++++++++++++++++--- src/main/java/TP1/TurtleLexer.java | 23 --------- src/main/java/TP1/TurtleParser.java | 14 ----- 6 files changed, 85 insertions(+), 85 deletions(-) delete mode 100644 src/main/java/TP1/TurtleLexer.java delete mode 100644 src/main/java/TP1/TurtleParser.java diff --git a/.vscode/settings.json b/.vscode/settings.json index 22cfa18..4b6afb0 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,7 +1,7 @@ { "java.server.launchMode": "Standard", "java.gradle.buildServer.enabled": "on", - "java.jdt.ls.java.home": "C:\\Program Files\\Java\\jdk-21", + "java.jdt.ls.java.home": "/usr/lib/jvm/jdk-21.0.6-oracle-x64", "antlr4.generation": { "mode": "none" // Don't generate source file with the antlr4-vscode extension }, diff --git a/src/main/antlr/TurtleLexer.g b/src/main/antlr/TurtleLexer.g index 9b499f7..2365876 100644 --- a/src/main/antlr/TurtleLexer.g +++ b/src/main/antlr/TurtleLexer.g @@ -12,37 +12,6 @@ options { } -LC : '<' - ; -RC : '>' - ; -P: '.' - ; -V: ',' - ; -SEMI: ';' - ; -G: '"' - ; -prog : bloc EOF - ; -bloc : sujet listvc P - ; -sujet : LC entity RC - ; -listvc : LC vc RC more - ; -more : SEMI listvc - | - ; -vc : LC verbe RC listc - ; -listc : LC comp RC morec - ; -morec : V comp - | - ; - // Whitespaces are ignored. diff --git a/src/main/java/TP1/ASD_Turtle.java b/src/main/java/TP1/ASD_Turtle.java index b5537c8..81d56a2 100644 --- a/src/main/java/TP1/ASD_Turtle.java +++ b/src/main/java/TP1/ASD_Turtle.java @@ -20,34 +20,37 @@ public class ASD_Turtle implements TurtleASD { } record PhraseImp(EntityImp entity, List affs) implements Phrase{ public String toString(){ - String str = "< " + this.entity.toString() + " >\n"; - for (AffectationImp aff : this.affs) { - str += aff.toString() + "\n"; + String str = "<" + this.entity.toString() + ">\n"; + for (int i =0; i < this.affs.size(); i++ ){ + str += affs.get(i).toString(); + if (i < this.affs.size() - 1) { + str += ";\n"; + } } - str += "."; + str += ".\n"; return str; } } record AffectationImp(EntityImp entity, List complements) implements Affectation{ public String toString(){ - String str= "< " + this.entity.toString() + " >"; + String str= "<" + this.entity.toString() + ">"; for (int i =0; i < this.complements.size(); i++) { str += (this.complements.get(i)); if (i < this.complements.size() - 1) { - str += ", "; + str += ","; } } - return str + ";"; + return str; } } record Complement_EntityImp(EntityImp entity) implements Complement{ public String toString(){ - return " < " + this.entity.toString() + " >"; + return " <" + this.entity.toString() + ">"; } } record Complement_TextImp(String string) implements Complement{ public String toString(){ - return " \" " + this.string + " \""; + return " \"" + this.string + "\""; } } record EntityImp(String string) implements Entity{ diff --git a/src/main/java/TP1/Main.java b/src/main/java/TP1/Main.java index af3c8ce..4f739c7 100644 --- a/src/main/java/TP1/Main.java +++ b/src/main/java/TP1/Main.java @@ -1,8 +1,7 @@ package TP1; import java.io.IOException; - -import javax.swing.text.html.parser.Entity; +import java.util.List; import org.antlr.runtime.ANTLRFileStream; import org.antlr.runtime.ANTLRInputStream; @@ -10,13 +9,80 @@ import org.antlr.runtime.CharStream; import org.antlr.runtime.CommonTokenStream; import org.antlr.runtime.RecognitionException; -public class Main { +import TP1.ASD_Turtle.*; + +public class Main { /** * Buils example AST and illustrates the translator. + * + LC : '<' + ; + RC : '>' + ; + P: '.' + ; + V: ',' + ; + SEMI: ';' + ; + G: '"' + ; + prog : bloc EOF + ; + bloc : sujet listvc P + ; + sujet : LC entity RC + ; + listvc : LC vc RC more + ; + more : SEMI listvc + | + ; + vc : LC verbe RC listc + ; + listc : LC comp RC morec + ; + morec : V comp + | + ; + */ public static void manualAST() { - Entity entity1 = new EntityImp("A"); + //Ridoux , ; + //System.out.println("Ridoux , ;"); + EntityImp Ridoux = new EntityImp("Ridoux"); + EntityImp type = new EntityImp("type"); + EntityImp personne = new EntityImp("personne"); + EntityImp professeur = new EntityImp("professeur"); + Complement_EntityImp comple_prof = new Complement_EntityImp(professeur); + Complement_EntityImp comple_pers = new Complement_EntityImp(personne); + + AffectationImp aff1 = new AffectationImp(type, List.of(comple_pers, comple_prof)); + PhraseImp ph2 = new PhraseImp(Ridoux, List.of(aff1)); + + //titre + EntityImp titre = new EntityImp("titre"); + Complement_TextImp compilation = new Complement_TextImp("Compilation"); + AffectationImp aff2 = new AffectationImp(titre, List.of(compilation)); + + //auteur + EntityImp auteur = new EntityImp("auteur"); + EntityImp Ferre = new EntityImp("Ferre"); + Complement_EntityImp comple_Ferre = new Complement_EntityImp(Ferre); + Complement_EntityImp comple_Ridoux = new Complement_EntityImp(Ridoux); + AffectationImp aff3 = new AffectationImp(auteur, List.of(comple_Ferre,comple_Ridoux)); + + //type + EntityImp poly = new EntityImp("poly"); + Complement_EntityImp comple_poly = new Complement_EntityImp(poly); + AffectationImp aff4 = new AffectationImp(type, List.of(comple_poly)); + + //poly117 + EntityImp poly117 = new EntityImp("poly117"); + PhraseImp ph1 = new PhraseImp(poly117, List.of(aff4, aff3, aff2)); + TurtleImp t = new TurtleImp(List.of(ph1, ph2)); + System.out.print(t.toString()); } /** @@ -24,8 +90,8 @@ public class Main { * translator. * * @param args standard arguments passed on the commandline - * @throws RecognitionException - */ + * @throws //RecognitionException +**/ public static void antlrAST(String[] args) throws RecognitionException { try { @@ -57,7 +123,6 @@ public class Main { throw new RuntimeException("Unable to proceed"); } } - public static void main(String[] args) throws RecognitionException { // Switch between the two fonctions as you make progress manualAST(); diff --git a/src/main/java/TP1/TurtleLexer.java b/src/main/java/TP1/TurtleLexer.java deleted file mode 100644 index 396fa41..0000000 --- a/src/main/java/TP1/TurtleLexer.java +++ /dev/null @@ -1,23 +0,0 @@ -package TP1; -import org.antlr.runtime.CharStream; -import org.antlr.runtime.Token; -import org.antlr.runtime.TokenSource; - - -public class TurtleLexer implements TokenSource { - public TurtleLexer(CharStream input){ - - } - - @Override - public Token nextToken() { - // TODO Auto-generated method stub - throw new UnsupportedOperationException("Unimplemented method 'nextToken'"); - } - - @Override - public String getSourceName() { - // TODO Auto-generated method stub - throw new UnsupportedOperationException("Unimplemented method 'getSourceName'"); - } -} diff --git a/src/main/java/TP1/TurtleParser.java b/src/main/java/TP1/TurtleParser.java deleted file mode 100644 index 064c471..0000000 --- a/src/main/java/TP1/TurtleParser.java +++ /dev/null @@ -1,14 +0,0 @@ -package TP1; - -import org.antlr.runtime.CommonTokenStream; - -public class TurtleParser { - public TurtleParser(CommonTokenStream token){ - - } - - public TurtleAST turtle(){ - return null; - } - -}