From fa92b08399708d7846d61094a9b73983684d2138 Mon Sep 17 00:00:00 2001 From: schodet Date: Thu, 8 May 2003 15:18:58 +0000 Subject: Ajout du module automate --- 2003/i/buzz/src/automate/GNUmakefile | 30 +++++++++++ 2003/i/buzz/src/automate/action.cc | 17 ++++++ 2003/i/buzz/src/automate/action.h | 33 ++++++++++++ 2003/i/buzz/src/automate/etape.h | 19 +++++++ 2003/i/buzz/src/automate/grafcet.cc | 54 +++++++++++++++++++ 2003/i/buzz/src/automate/grafcet.h | 79 +++++++++++++++++++++++++++ 2003/i/buzz/src/automate/grammar.yy | 95 +++++++++++++++++++++++++++++++++ 2003/i/buzz/src/automate/lexer.ll | 73 +++++++++++++++++++++++++ 2003/i/buzz/src/automate/receptivite.cc | 50 +++++++++++++++++ 2003/i/buzz/src/automate/receptivite.h | 47 ++++++++++++++++ 2003/i/buzz/src/automate/transition.h | 19 +++++++ 11 files changed, 516 insertions(+) create mode 100644 2003/i/buzz/src/automate/GNUmakefile create mode 100644 2003/i/buzz/src/automate/action.cc create mode 100644 2003/i/buzz/src/automate/action.h create mode 100644 2003/i/buzz/src/automate/etape.h create mode 100644 2003/i/buzz/src/automate/grafcet.cc create mode 100644 2003/i/buzz/src/automate/grafcet.h create mode 100644 2003/i/buzz/src/automate/grammar.yy create mode 100644 2003/i/buzz/src/automate/lexer.ll create mode 100644 2003/i/buzz/src/automate/receptivite.cc create mode 100644 2003/i/buzz/src/automate/receptivite.h create mode 100644 2003/i/buzz/src/automate/transition.h (limited to '2003/i') diff --git a/2003/i/buzz/src/automate/GNUmakefile b/2003/i/buzz/src/automate/GNUmakefile new file mode 100644 index 0000000..0f0e9b7 --- /dev/null +++ b/2003/i/buzz/src/automate/GNUmakefile @@ -0,0 +1,30 @@ +SRCDIR = .. +CXXFLAGS = -Wall -g +CPPFLAGS = -I$(SRCDIR) + +TARGETS = grafcet.a +grafcet_a_SOURCES = grafcet.cc receptivite.cc action.cc grammar.tab.cc lexer.cc + +all: $(TARGETS) + +grammar.tab.cc grammar.tab.hh: grammar.yy + bison $< + +lexer.cc: lexer.ll grammar.tab.hh + flex -o$@ $< + +grafcet.a: ${grafcet_a_SOURCES:%.cc=grafcet.a(%.o)} + +.dep/%.d: %.cc .dep + @set -e; $(CC) -M $(CPPFLAGS) $< \ + | sed 's/\($*\)\.o[ :]*/\1.o .dep\/$*.d : /g' > $@; \ + [ -s $@ ] || rm -f $@ + +-include $(grafcet_a_SOURCES:%.cc=.dep/%.d) + +.dep: + @mkdir .dep + +clean: + rm -f $(TARGETS) *.o + rm -rf .dep diff --git a/2003/i/buzz/src/automate/action.cc b/2003/i/buzz/src/automate/action.cc new file mode 100644 index 0000000..d5e4ade --- /dev/null +++ b/2003/i/buzz/src/automate/action.cc @@ -0,0 +1,17 @@ +// action.cc +// buzz - Programme du robot Efrei Robotique I1-I2 2003 +// Copyright (C) 2003 Nicolas Schodet +// +#include "action.h" + +#include + +namespace Automate +{ + +// Destructeur. +Action::~Action () +{ +} + +} diff --git a/2003/i/buzz/src/automate/action.h b/2003/i/buzz/src/automate/action.h new file mode 100644 index 0000000..7fb9806 --- /dev/null +++ b/2003/i/buzz/src/automate/action.h @@ -0,0 +1,33 @@ +#ifndef action_h +#define action_h +// action.h +// buzz - Programme du robot Efrei Robotique I1-I2 2003 +// Copyright (C) 2003 Nicolas Schodet + +#include + +namespace Automate +{ + class Action + { + public: + // Destructeur. + virtual ~Action (); + // Execute l'action. Renvoie true si terminée. + virtual bool run (void) = 0; + }; + + class ActionPrint : public Action + { + string m_s; + public: + // Constructeur. + ActionPrint (string &s) : m_s (s) { } + // Destructeur. + ~ActionPrint (); + // Execute l'action. Renvoie true si terminée. + bool run (void); + }; +} + +#endif // action_h diff --git a/2003/i/buzz/src/automate/etape.h b/2003/i/buzz/src/automate/etape.h new file mode 100644 index 0000000..e6d9fe8 --- /dev/null +++ b/2003/i/buzz/src/automate/etape.h @@ -0,0 +1,19 @@ +#ifndef etape_h +#define etape_h +// etape.h +// buzz - Programme du robot Efrei Robotique I1-I2 2003 +// Copyright (C) 2003 Nicolas Schodet + +namespace Automate +{ + class Etape + { + public: + int num; + public: + // Constructeur. + Etape (int _num) { num = _num; } + }; +} + +#endif // etape_h diff --git a/2003/i/buzz/src/automate/grafcet.cc b/2003/i/buzz/src/automate/grafcet.cc new file mode 100644 index 0000000..b2af52e --- /dev/null +++ b/2003/i/buzz/src/automate/grafcet.cc @@ -0,0 +1,54 @@ +// grafcet.cc +// buzz - Programme du robot Efrei Robotique I1-I2 2003 +// Copyright (C) 2003 Nicolas Schodet +// +#include "grafcet.h" +#include "erreur/erreur.h" + +Automate::Grafcet *parse_file (const char *filename); + +namespace Automate +{ + +// Pointeur vers l'instance unique du grafcet. +Grafcet *Grafcet::m_instance = 0; + +// Constructeur. +Grafcet::Grafcet () +{ +} + +Grafcet::Grafcet (const char *filename) +{ + Grafcet *g = parse_file (filename); + if (!g) throw ErreurFatale ("Impossible de lire le grafcet"); + m_grafcet = g->m_grafcet; + delete g; +} + +// Destructeur. +Grafcet::~Grafcet () +{ + for (vector::iterator i = m_grafcet.begin (); + i != m_grafcet.end (); ++i) + { + switch (i->type) + { + case etape: + delete i->elem.etape; + break; + case action: + delete i->elem.action; + break; + case receptivite: + delete i->elem.receptivite; + break; + case transition: + delete i->elem.transition; + break; + } + } +} + + +} // namespace Automate diff --git a/2003/i/buzz/src/automate/grafcet.h b/2003/i/buzz/src/automate/grafcet.h new file mode 100644 index 0000000..09b87a1 --- /dev/null +++ b/2003/i/buzz/src/automate/grafcet.h @@ -0,0 +1,79 @@ +#ifndef grafcet_h +#define grafcet_h +// grafcet.h +// buzz - Programme du robot Efrei Robotique I1-I2 2003 +// Copyright (C) 2003 Nicolas Schodet +#include "etape.h" +#include "action.h" +#include "receptivite.h" +#include "transition.h" + +#include + +namespace Automate +{ + +class Grafcet +{ + public: + // Type d'un élément du grafcet. + enum GrafcetElemType + { + etape, action, receptivite, transition + }; + // Structure des éléments. + struct GrafcetElem + { + GrafcetElemType type; + union + { + Etape *etape; + Action *action; + Receptivite *receptivite; + Transition *transition; + } elem; + // Constructeurs. + GrafcetElem (Etape *_etape) + { + type = etape; + elem.etape = _etape; + } + GrafcetElem (Action *_action) + { + type = action; + elem.action = _action; + } + GrafcetElem (Receptivite *_receptivite) + { + type = receptivite; + elem.receptivite = _receptivite; + } + GrafcetElem (Transition *_transition) + { + type = transition; + elem.transition = _transition; + } + }; + // Contient le grafcet complet. + vector m_grafcet; + // Etapes actives. + enum ActiveStep + { + inactive, start, active, stop + }; + vector m_active_etapes; + // Pointeur vers l'instance unique du grafcet. + static Grafcet *m_instance; + public: + // Constructeurs. + Grafcet (); + Grafcet (const char *filename); + // Destructeur. + ~Grafcet (); + // Retourne une référence vers l'instance. + static Grafcet &getInstance () { return *m_instance; } +}; + +} + +#endif // grafcet_h diff --git a/2003/i/buzz/src/automate/grammar.yy b/2003/i/buzz/src/automate/grammar.yy new file mode 100644 index 0000000..709c8e3 --- /dev/null +++ b/2003/i/buzz/src/automate/grammar.yy @@ -0,0 +1,95 @@ +%{ +#include +using namespace std; + +#include "grafcet.h" +using namespace Automate; + +void +yyerror (const char *s); + +int +yylex (void); + +Grafcet *input_grafcet; + +%} + +%defines + +%union { + int num; + double fl; + bool boolean; + string *s; + Grafcet *grafcet; + Etape *etape; + Action *action; + Receptivite *receptivite; + Transition *transition; +} + +%token NUM +%token FLOAT +%token BOOL +%token STRING +%token ERR GOTO +%token PRINT +%type grafcet +%type etape +%type action +%type receptivite +%type transition +%type flnum + +/* Priorité des opérateurs. */ +%right '+' +%right '.' +%right '!' + +%% + +input: grafcet { input_grafcet = $1; } +; + +grafcet: + /* Rien */ { $$ = new Grafcet (); } + | grafcet etape { $$ = $1; $$->m_grafcet.push_back (Grafcet::GrafcetElem ($2)); } + | grafcet action { $$ = $1; $$->m_grafcet.push_back (Grafcet::GrafcetElem ($2)); } + | grafcet receptivite { $$ = $1; $$->m_grafcet.push_back (Grafcet::GrafcetElem ($2)); } + | grafcet transition { $$ = $1; $$->m_grafcet.push_back (Grafcet::GrafcetElem ($2)); } +; + +etape: '#' NUM { $$ = new Etape ($2); } +; + +action: PRINT STRING { $$ = new ActionPrint (*$2); delete $2; } +; + +receptivite: + BOOL { $$ = new ReceptiviteBool ($1); } + | receptivite '.' receptivite + { $$ = new ReceptiviteBoolOp ($1, '.', $3); } + | receptivite '+' receptivite + { $$ = new ReceptiviteBoolOp ($1, '+', $3); } + | '!' receptivite { $$ = new ReceptiviteBoolOp ('!', $2); } +; + +transition: + GOTO NUM { $$ = new Transition ($2); } +; + +flnum: FLOAT { $$ = $1; } + | NUM { $$ = $1; } + | flnum '/' flnum { $$ = $1 / $3; } +; + +%% + +void +yyerror (const char *s) +{ + cerr << "Parse error: " << s << endl; +} + +// vim: ft=yacc diff --git a/2003/i/buzz/src/automate/lexer.ll b/2003/i/buzz/src/automate/lexer.ll new file mode 100644 index 0000000..6760aee --- /dev/null +++ b/2003/i/buzz/src/automate/lexer.ll @@ -0,0 +1,73 @@ +%{ +#include +using namespace std; + +#include "grafcet.h" +using namespace Automate; + +#include "grammar.tab.hh" + +int yyparse (void); + +Grafcet *input_grafcet; + +%} + +ID [_a-zA-Z][-_a-zA-Z0-9]* + +NUM [-+]?[0-9]+ + +FLOAT [-+]?[0-9]*"."[0-9]* + +STRING \"[^\n"]\" + +%option noyywrap nounput + +%% + +"goto" return GOTO; + +"print" return PRINT; + +[-#+=.<>] return yytext[0]; + +{FLOAT} { + yylval.fl = strtod (yytext, 0); + return FLOAT; +} + +{NUM} { + yylval.num = strtol (yytext, 0, 0); + return NUM; +} + +{STRING} { + yylval.s = new string (yytext + 1, yyleng - 2); + return STRING; +} + +"true" { yylval.boolean = true; return BOOL; } +"false" { yylval.boolean = false; return BOOL; } + +[ \t\n]+ /* Rien à battre. */ + +. return ERR; + +%% + +Grafcet *parse_string (const char *s) +{ + yy_scan_string (s); + return yyparse () ? 0 : input_grafcet; +} + +Grafcet *parse_file (const char *filename) +{ + yyin = fopen (filename, "r"); + if (!yyin) return 0; + int r = yyparse (); + fclose (yyin); + return r ? 0 : input_grafcet; +} + +// vim: ft=lex diff --git a/2003/i/buzz/src/automate/receptivite.cc b/2003/i/buzz/src/automate/receptivite.cc new file mode 100644 index 0000000..44accea --- /dev/null +++ b/2003/i/buzz/src/automate/receptivite.cc @@ -0,0 +1,50 @@ +// receptivite.cc +// buzz - Programme du robot Efrei Robotique I1-I2 2003 +// Copyright (C) 2003 Nicolas Schodet +// +#include "receptivite.h" + +namespace Automate +{ + +// Destructeur. +Receptivite::~Receptivite () +{ +} + +// Destructeur. +ReceptiviteBool::~ReceptiviteBool () +{ +} + +// Retourne la valeur de la receptivité. +bool +ReceptiviteBool::test (void) +{ + return m_bool; +} + +// Destructeur. +ReceptiviteBoolOp::~ReceptiviteBoolOp () +{ + if (m_left) delete m_left; + if (m_right) delete m_right; +} + +// Retourne la valeur de la receptivité. +bool +ReceptiviteBoolOp::test (void) +{ + switch (m_op) + { + case '.': + return m_left->test () && m_right->test (); + case '+': + return m_left->test () || m_right->test (); + case '!': + return ! m_right->test (); + } + return false; +} + +} diff --git a/2003/i/buzz/src/automate/receptivite.h b/2003/i/buzz/src/automate/receptivite.h new file mode 100644 index 0000000..04399b2 --- /dev/null +++ b/2003/i/buzz/src/automate/receptivite.h @@ -0,0 +1,47 @@ +#ifndef receptivite_h +#define receptivite_h +// receptivite.h +// buzz - Programme du robot Efrei Robotique I1-I2 2003 +// Copyright (C) 2003 Nicolas Schodet + +namespace Automate +{ + class Receptivite + { + public: + // Destructeur. + virtual ~Receptivite (); + // Retourne la valeur de la receptivité. + virtual bool test (void) = 0; + }; + + class ReceptiviteBool : public Receptivite + { + bool m_bool; + public: + // Constructeur. + ReceptiviteBool (bool _bool) { m_bool = _bool; } + // Destructeur. + ~ReceptiviteBool (); + // Retourne la valeur de la receptivité. + bool test (void); + }; + + class ReceptiviteBoolOp : public Receptivite + { + char m_op; + Receptivite *m_left, *m_right; + public: + // Constructeurs. + ReceptiviteBoolOp (Receptivite *left, char op, Receptivite *right) + { m_left = left; m_op = op; m_right = right; } + ReceptiviteBoolOp (char op, Receptivite *right) + { m_left = 0; m_op = op; m_right = right; } + // Destructeur. + ~ReceptiviteBoolOp (); + // Retourne la valeur de la receptivité. + bool test (void); + }; +} + +#endif // receptivite_h diff --git a/2003/i/buzz/src/automate/transition.h b/2003/i/buzz/src/automate/transition.h new file mode 100644 index 0000000..5eb7491 --- /dev/null +++ b/2003/i/buzz/src/automate/transition.h @@ -0,0 +1,19 @@ +#ifndef transition_h +#define transition_h +// transition.h +// buzz - Programme du robot Efrei Robotique I1-I2 2003 +// Copyright (C) 2003 Nicolas Schodet + +namespace Automate +{ + class Transition + { + public: + int num; + public: + // Constructeur. + Transition (int _num) { num = _num; } + }; +} + +#endif // transition_h -- cgit v1.2.3