Projet Compilation Analyseur Lexical En Java

Similar documents
DAFTAR LAMPIRAN. Source Code Java Aplikasi Keyword to Image Renamer Split

TP 2 : Application SnapTchat 20 février 2015

Part II : Lexical Analysis

SampleApp.java. Page 1

COMP16121 Sample Code Lecture 1

APPENDIX. public void cekroot() { System.out.println("nilai root : "+root.data); }

Goals. Lecture 7 More GUI programming. The application. The application D&D 12. CompSci 230: Semester JFrame subclass: ListOWords

17 GUI API: Container 18 Hello world with a GUI 19 GUI API: JLabel 20 GUI API: Container: add() 21 Hello world with a GUI 22 GUI API: JFrame: setdefau

Answer on question #61311, Programming & Computer Science / Java

AppBisect > PrBisect > class Functie. AppBisect > PrBisect > class Punct. public class Functie { double x(double t) { return t;

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Multiple Choice Questions: Identify the choice that best completes the statement or answers the question. (15 marks)

PART 23. Java GUI Advanced JList Component. more items.

Example 3-1. Password Validation

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

!"# $ %&# %####' #&() % # # # #&* # ## +, # -

ILLUSTRATION DES EXCEPTIONS

Tutorial: using Java to access the Vault API. Barbara Han Autodesk Developer Technical Services

Classes internes, Classes locales, Classes anonymes

AP CS Unit 11: Graphics and Events

/* Write a Program implementing GUI based Calculator using Swing */

Lampiran A. SOURCE CODE PROGRAM

// autor igre Ivan Programerska sekcija package mine;

Object-Oriented Software Engineering Re-exam, 2012 (Also Object-Oriented Analysis, Design and Programming, Re-exam, 2012)

Java et Mascopt. Jean-François Lalande, Michel Syska, Yann Verhoeven. Projet Mascotte, I3S-INRIA Sophia-Antipolis, France

CSCI 201L Midterm Written Summer % of course grade

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

JAVA NOTES GRAPHICAL USER INTERFACES

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

PART 22. Java GUI Library SWT GUI Library SWING GUI Library Swing First Program

Graphical User Interface

GUI (Graphic User Interface) Programming. Part 2 (Chapter 8) Chapter Goals. Events, Event Sources, and Event Listeners. Listeners

TD : Compilateur ml2java semaine 3

Multimedia Programming

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Systems Programming. Bachelor in Telecommunication Technology Engineering Bachelor in Communication System Engineering Carlos III University of Madrid

Chapter 13 Lab Advanced GUI Applications Lab Objectives. Introduction. Task #1 Creating a Menu with Submenus

AnimatedImage.java. Page 1

Programming Languages and Techniques (CIS120)

H212 Introduction to Software Systems Honors

Compilation TP 0.0 : The target architecture: Digmips

Example: Building a Java GUI

HomeWork 3. In this homework, an ArchJava application that allows one to query one s scores in a networked environment is presented.

INSTITUTO POLITÉCNICO NACIONAL ESCUELA SUPERIOR DE CÓMPUTO

Example: Building a Java GUI

MIT AITI Swing Event Model Lecture 17

CSCI 201L Midterm Written SOLUTION Summer % of course grade

Midterm assessment - MAKEUP Fall 2010

Final Examination Semester 3 / Year 2008

Datenbank-Praktikum. Universität zu Lübeck Sommersemester 2006 Lecture: Swing. Ho Ngoc Duc 1

TP 3 des architectures logicielles Séance 3 : Architecture n-tiers distribuée à base d EJB. 1 Préparation de l environnement Eclipse

JRadioButton account_type_radio_button2 = new JRadioButton("Current"); ButtonGroup account_type_button_group = new ButtonGroup();

UE 2I002 (ex LI230) : éléments de programmation par objets avec Java TD10 - Exceptions

Analyse statique de programmes avioniques

Object-Oriented Software Engineering (Re-exam for Object-Oriented Analysis, Design and Programming)

PlaniSphere. Creating a plug-in for PlaniSphere.

First Name: AITI 2004: Exam 2 July 19, 2004

OOP Assignment V. For example, the scrolling text (moving banner) problem without a thread looks like:

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

GUI Applications. Let s start with a simple Swing application in Java, and then we will look at the same application in Jython. See Listing 16-1.

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

/** Creates new form NewJFrame */ public NewJFrame() { initcomponents(); initblogsearch(); //initializes Index List box }

Lab 4. D0010E Object-Oriented Programming and Design. Today s lecture. GUI programming in

First Name: AITI 2004: Exam 2 July 19, 2004

Apéndice A. Código fuente de aplicación desarrollada para probar. implementación de IPv6

Do not turn to the next page until the start of the exam.

Based on slides by Prof. Burton Ma

Page 1 of 16. Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Calculator Class. /** * Create a new calculator and show it. */ public Calculator() { engine = new CalcEngine(); gui = new UserInterface(engine); }

Java Programming Summer 2008 LAB. Thursday 8/21/2008

TD 2. Correction TP info

package As7BattleShip;

import java.applet.applet; import java.applet.audioclip; import java.net.url; public class Vjesala2 {

Building a GUI in Java with Swing. CITS1001 extension notes Rachel Cardell-Oliver

Java Help Files. by Peter Lavin. May 22, 2004

class BankFilter implements Filter { public boolean accept(object x) { BankAccount ba = (BankAccount) x; return ba.getbalance() > 1000; } }

Lecture 3: Java Graphics & Events

69 Section 7: Example:Single times table with exception catching 70 Aim 74 Single times table with exception catching

About Transferring License Rights for. PL7 V4.5 and Unity Pro V2.3 SP1 Software

1 Looping Constructs (4 minutes, 2 points)

COMP Assignment #10 (Due: Monday, March 11:30pm)

Chapter 13 Lab Advanced GUI Applications

COSC 123 Computer Creativity. Graphics and Events. Dr. Ramon Lawrence University of British Columbia Okanagan

Read me carefully before making your connections!

Exercice G09 (v3) File: Shape.java Project: G09-MiniDraw_v3 1GGE

RAIK 183H Examination 2 Solution. November 10, 2014

CS 209 Programming in Java #10 Exception Handling

SERIE N 1. 4 ème Année informatiques. Exercice 3. Exercice 4 (Manuel Scolaire) Page [1] Les enregistrements & Les fichiers Infoslpm.e-monsite.

Swing Programming Example Number 2

Class 34: Introduction to Threads

Chapter #1. Program to demonstrate applet life cycle

Building Java Programs Bonus Slides

VHDL par Ahcène Bounceur VHDL

Chapter 11: Exceptions Lab Exercises

PART1: Choose the correct answer and write it on the answer sheet:

Chapter 9 Designing Graphical User Interfaces (GUIs)

SunVTS Quick Reference Card

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Java Programming Lecture 6

Transcription:

Projet Compilation Analyseur Lexical En Java Réalisé par : Soufiane ATTIF 2 G I Soufiane ATTIF 2 G I 1

I.Présentation de Projet : Projet Compilation Analyseur Lexical en Java Le but est de réaliser un analyseur syntaxique d un sous ensemble du langage C appelé C --. Le langage est formé principalement par : Des mots clés : if, then, else, extern, for, void, while, return et int. Des Identificateurs : commençant par une lettre et formés de lettres et chiffres. Des opérateurs : <, >, <<,>>,+,-,*, /,&,&&,,,!,!=,==,<=,>=. Des commentaires : /* blabla...*/ II.Diagramme d état : Ce langage est régit par l automate suivant : blanc chiffre chiffre S 1 NUM Lettre, chiffre lettre 2 ID / 3 * 4 * 5 * COMENT * > = 6 > SUP_EGL DEC_DR Soufiane ATTIF 2 G I 2

SUP < 7 = < INF_EGL DEC_GH INF & 8 & ET_LOG 9 ET_BIN OU_LOG! 10 = OU_BIN DIF NON_LOG = = 11 EGL ERROR ELSE ERROR Soufiane ATTIF 2 G I 3

III.Programme : Ce projet réalisé en java, est divisé en 3 classes : Token : classes où sont définis les tokens du langages. Scanner : classes où définit le traitement du flux du fichier de lecture. scannertest : classe permettant de boucler sur les traitement. Les classes graphiques. Ces classes forment un package nommé compilateur, et font appel l une à l autre suivant des principes de traitement du flux. Le programme lit un fichier à partir d un emplacement fournit et détermine les lexèmes et les erreurs commises. Le code : Compilateur.java package compilateur; import java.awt.borderlayout; import java.awt.dimension; import java.io.*; import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.jlabel; import java.awt.*; import javax.swing.jtextfield; import javax.swing.jtogglebutton; import javax.swing.jbutton; import javax.swing.jtextarea; import java.awt.event.actionevent; import java.awt.event.actionlistener; import javax.swing.jscrollpane; public class compilateur extends JFrame { JPanel contentpane; JLabel jlabel1 = new JLabel(); JLabel jlabel2 = new JLabel(); JTextField jtextfield1 = new JTextField(); Soufiane ATTIF 2 G I 4

JButton jbutton1 = new JButton(); JScrollPane jscrollpane1 = new JScrollPane(); JTextArea jtextarea1 = new JTextArea(); JLabel jlabel3 = new JLabel(); JScrollPane jscrollpane2 = new JScrollPane(); JTextArea jtextarea2 = new JTextArea(); public compilateur() { try { setdefaultcloseoperation(exit_on_close); jbinit(); catch (Exception exception) { exception.printstacktrace(); private void jbinit() throws Exception { contentpane = (JPanel) getcontentpane(); contentpane.setlayout(null); setsize(new Dimension(469, 500)); settitle("analyseur lexical"); jlabel1.setfont(new java.awt.font("garamond", Font.BOLD, 20)); jlabel1.settext("analyseur Lexical"); jlabel1.setbounds(new Rectangle(124, 4, 159, 39)); jlabel2.settext("chemin :"); jlabel2.setbounds(new Rectangle(12, 45, 47, 23)); jtextfield1.setbounds(new Rectangle(54, 47, 178, 21)); jbutton1.setbounds(new Rectangle(288, 48, 94, 20)); jbutton1.settext("compiler"); jbutton1.addactionlistener(new compilateur_jbutton1_actionadapter(this)); jtextfield1.addactionlistener(new compilateur_jtextfield1_actionadapter(this)); jscrollpane1.setbounds(new Rectangle(12, 81, 372, 200)); jlabel3.settext("erreurs :"); jlabel3.setbounds(new Rectangle(13, 289, 49, 23)); jscrollpane2.setbounds(new Rectangle(12, 310, 375, 100)); contentpane.add(jlabel1); contentpane.add(jlabel2); contentpane.add(jtextfield1); contentpane.add(jbutton1); contentpane.add(jscrollpane1); contentpane.add(jlabel3); contentpane.add(jscrollpane2); jscrollpane2.getviewport().add(jtextarea2); jscrollpane1.getviewport().add(jtextarea1); Soufiane ATTIF 2 G I 5

public void jbutton1_actionperformed(actionevent e) { jtextarea1.settext("======== Debut [Compilation Test] ========\n"); try { Scanner scanner = new Scanner(new FileInputStream(jTextField1.getText())); scanner.next(); while (scanner.symbol!= ScannerTest.EOF) { jtextarea2.settext(scanner.err); jtextarea1.append("[ligne:" + scanner.ligne + "] "); jtextarea1.append(scannertest.representation(scanner.symbol)); if ((scanner.symbol == ScannerTest.NUM) (scanner.symbol == ScannerTest.ID) (scanner.symbol == ScannerTest.SUP) (scanner.symbol == ScannerTest.INF) (scanner.symbol == ScannerTest.SUP_EGL) (scanner.symbol == ScannerTest.INF_EGL) (scanner.symbol == ScannerTest.EGL) (scanner.symbol == ScannerTest.DIF) (scanner.symbol == ScannerTest.IF) (scanner.symbol == ScannerTest.THEN) (scanner.symbol == ScannerTest.ELSE) (scanner.symbol == ScannerTest.FOR) (scanner.symbol == ScannerTest.WHILE) (scanner.symbol == ScannerTest.EXTERN) (scanner.symbol == ScannerTest.INT) (scanner.symbol == ScannerTest.VOID) (scanner.symbol == ScannerTest.RETURN)) { jtextarea1.append("(" + scanner.chars + ")\n"); else { jtextarea1.append("\n"); scanner.next(); scanner.close(); catch (IOException ex) { //jtextarea1.append(ex); System.exit(-1); jtextarea1.append("======== Fin [compilation Test] ========\n"); Soufiane ATTIF 2 G I 6

public void jtextfield1_actionperformed(actionevent e) { class compilateur_jtextfield1_actionadapter implements ActionListener { private compilateur adaptee; compilateur_jtextfield1_actionadapter(compilateur adaptee) { this.adaptee = adaptee; public void actionperformed(actionevent e) { adaptee.jtextfield1_actionperformed(e); class compilateur_jbutton1_actionadapter implements ActionListener { private compilateur adaptee; compilateur_jbutton1_actionadapter(compilateur adaptee) { this.adaptee = adaptee; public void actionperformed(actionevent e) { adaptee.jbutton1_actionperformed(e); Lexical.java package compilateur; import java.awt.toolkit; import javax.swing.swingutilities; import javax.swing.uimanager; import java.awt.dimension; Soufiane ATTIF 2 G I 7

public class Lexical { boolean packframe = false; public Lexical() { compilateur frame = new compilateur(); if (packframe) { frame.pack(); else { frame.validate(); Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize(); Dimension framesize = frame.getsize(); if (framesize.height > screensize.height) { framesize.height = screensize.height; if (framesize.width > screensize.width) { framesize.width = screensize.width; frame.setlocation((screensize.width - framesize.width) / 2, (screensize.height - framesize.height) / 2); frame.setvisible(true); public static void main(string[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { try { UIManager.setLookAndFeel(UIManager. getsystemlookandfeelclassname()); catch (Exception exception) { exception.printstacktrace(); ); new Lexical(); Soufiane ATTIF 2 G I 8

Scanner.java package compilateur; import java.io.*; public class Scanner implements Token { // dernier symbole (lexeme) qui a ete lu (voir Token) public int symbol; // position public int ligne = 1; public String err =""; // representation en characteres du dernier symbole public String chars; // le flux de characteres private InputStream in; // prochain charactere private char c; // tampon pour assembler des characteres private StringBuffer buf = new StringBuffer(); // constructeur // premier charactere deja lu au bout de la construction Scanner(InputStream in) { this.in = in; nextchar(); // traitement d'erreur public String error(string msg) { return err += (msg+"\n"); //////////////////////////////////////////////////////////////// Soufiane ATTIF 2 G I 9

// lit le prochain charactere private void nextchar() { try { c = (char)in.read(); catch(ioexception ex) { error("erreur de lecture : " + ex.tostring()); //////////////////////////////////////////////////////////////// // lit le prochain lexeme // celui-ci est stocke dans symbol, son contenu dans chars public void next() { // espaces blancs while (c <= ' ') { //ligne if(c =='\n') this.ligne += 1; nextchar(); switch(c) { // nombres case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': buf.setlength(0); while ('0' <= c && c <= '9'){ chars = buf.tostring(); symbol = NUM; // mots cle et identificateurs case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g': case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n': case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u': case 'v': case 'w': case 'x': case 'y': case 'z': case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G': case 'H': case 'I': case 'J': case 'K': case 'L': case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U': Soufiane ATTIF 2 G I 10

case 'V': case 'W': case 'X': case 'Y': case 'Z': buf.setlength(0); while (('0' <= c && c <= '9') ('a' <= c && c <= 'z') ('A' <= c && c <= 'Z')) { chars = buf.tostring(); // mots cle if, then, else,... if (chars.equals("if")) symbol = IF; else if (chars.equals("then")) symbol = THEN; else if (chars.equals("else")) symbol = ELSE; else if (chars.equals("for")) symbol = FOR; else if (chars.equals("while")) symbol = WHILE; else if (chars.equals("extern")) symbol = EXTERN; else if (chars.equals("int")) symbol = INT; else if (chars.equals("void")) symbol = VOID; else if (chars.equals("return")) symbol = RETURN; else // identificateur symbol = ID; // operateurs numeriques case '+': symbol = PLUS; nextchar(); case '-': symbol = MOINS; nextchar(); case '*': symbol = MULT; nextchar(); case '/': buf.setlength(0); if(c == '*') { nextchar(); while ((c!= '*') && (c!= (char) -1) ) { buf.append(c); nextchar(); if(c!= '*'){ Soufiane ATTIF 2 G I 11

nextchar(); error("commentaire non terminé: " + " (ligne :"+ligne+")"); symbol = ERROR; else nextchar(); if(c == '/') {symbol = COMENT; else symbol = DIV; nextchar(); case '<': buf.setlength(0); if(c == '=') {symbol = INF_EGL; buf.append(c); nextchar(); else if(c == '<') {symbol = DEC_GH; buf.append(c); nextchar(); else symbol = INF; chars = buf.tostring(); case '>': buf.setlength(0); if(c == '=') {symbol = SUP_EGL; buf.append(c); nextchar(); else if(c == '>') {symbol = DEC_DR; buf.append(c); nextchar(); else symbol = SUP; chars = buf.tostring(); case '!': buf.setlength(0); if(c == '=') {symbol = DIF; Soufiane ATTIF 2 G I 12

buf.append(c); nextchar(); else symbol = NON_LOG; chars = buf.tostring(); case '&': buf.setlength(0); if(c == '&') {symbol = ET_LOG; buf.append(c); nextchar(); else symbol = ET_BIN; chars = buf.tostring(); case ' ': buf.setlength(0); if(c == ' ') {symbol = OU_LOG; buf.append(c); nextchar(); else symbol = OU_BIN; chars = buf.tostring(); case '=': buf.setlength(0); if(c == '=') {symbol = EGL; buf.append(c); chars = buf.tostring(); nextchar(); else error("charactere incorrect: " +"="+c + " (ligne :"+ligne+")"); // delimiteurs (, ), {,, ;, case '(': symbol = PAR_OUV; nextchar(); case ')': symbol = PAR_FER; nextchar(); case '{': symbol = ACC_OUV; nextchar(); Soufiane ATTIF 2 G I 13

case '': symbol = ACC_FER; nextchar(); case ';': symbol = PT_VRG; nextchar(); case ',': symbol = VRG; nextchar(); case (char) -1: symbol = EOF; default: error("charactere incorrect: " + c + " (ligne :"+ligne+")"); // fermer flux d'entree public void close() throws IOException { in.close(); ScannerTest.java package compilateur; import java.io.*; public class ScannerTest implements Token { // representation public static String representation(int symbol) { switch(symbol) { case ERROR: return "<error>"; case EOF: return "EOF"; Soufiane ATTIF 2 G I 14

// valeurs case NUM: return "NUM"; case ID: return "ID"; // operateurs numeriques case PLUS: return "PLUS"; case MOINS: return "MOINS"; case MULT: return "MULTIPLICATION"; case DIV: return "DIV"; case ET_BIN: return "ET BINAIRE"; case OU_BIN: return "OU BINAIRE"; case DEC_DR: return "DECALAGE A DROITE"; case DEC_GH: return "DECALAGE A GAUCHE"; // operateurs booleen case NON_LOG: return "NON LOGIQUE"; case ET_LOG: return "ET LOGIQUE"; case OU_LOG: return "OU LOGIQUE"; // operateurs de comparaison case INF: return "OPERATEUR INFERIEUR"; case SUP: return "OPERATEUR SUPERIEUR"; case INF_EGL: return "OPERATEUR INFERIEUR OU EGAL"; case SUP_EGL: return "OPERATEUR SUPERIEUR OU EGAL"; case EGL: return "OPERATEUR EGAL"; case DIF: return "OPERATEUR DIFERENT"; // mots cle case EXTERN: return "MOT CLE "; case INT: return "MOT CLE "; case VOID: return "MOT CLE "; case RETURN: return "MOT CLE "; case IF: return "MOT CLE "; case THEN: return "MOT CLE "; case ELSE: return "MOT CLE "; case FOR: return "MOT CLE "; case WHILE: return "MOT CLE "; // parentheses case PAR_OUV: return "PARENTHESE OUVRANTE"; case PAR_FER: return "PARENTHESE FERMANTE"; case ACC_OUV: return "ACCOLADE OUVRANTE"; Soufiane ATTIF 2 G I 15

case ACC_FER: return "ACCOLADE FERMANTE"; case COMENT: return "COMMENTAIRE"; case VRG: return "VIRGULE"; case PT_VRG: return "POINT VIRGULE"; default: return "<INCONNU>"; Token.java package compilateur; interface Token { static final int // symboles speciaux ERROR = 0, EOF = ERROR + 1, // valeurs NUM = EOF + 1, ID = NUM + 1, // operateurs numeriques PLUS = ID + 1, MOINS = PLUS + 1, MULT = MOINS + 1, DIV = MULT + 1, ET_BIN = DIV + 1, OU_BIN = ET_BIN + 1, DEC_DR = OU_BIN + 1, DEC_GH = DEC_DR + 1, // operateurs booleen Soufiane ATTIF 2 G I 16

NON_LOG = DEC_GH + 1, ET_LOG = NON_LOG + 1, OU_LOG = ET_LOG + 1, // operateurs de comparaison INF = OU_LOG + 1, SUP = INF + 1, INF_EGL = SUP + 1, SUP_EGL = INF_EGL + 1, EGL = SUP_EGL + 1, DIF = EGL + 1, // mots cle EXTERN = DIF + 1, INT = EXTERN + 1, VOID = INT + 1, RETURN = VOID + 1, IF = RETURN + 1, THEN = IF + 1, ELSE = THEN + 1, FOR = ELSE + 1, WHILE = FOR + 1, // parentheses PAR_OUV = WHILE + 1, PAR_FER = PAR_OUV + 1, ACC_OUV = PAR_FER + 1, ACC_FER = ACC_OUV + 1, COMENT = ACC_FER +1, VRG = COMENT + 1, PT_VRG = VRG + 1; IV.Jeux d essai : Pour exécuter le programme il suffit de double cliquer sur l archivage compilateur2.jar (ceci suppose le JDK installé sur la machine). Soufiane ATTIF 2 G I 17

Après, il faut spécifier l emplacement du fichier à scanner dans la zone chemin, puis cliquer sur compiler et c est fait! Soufiane ATTIF 2 G I 18