Traduction et Sémantique Traduction dirigée par la syntaxe

Documents pareils
Le prototype de la fonction main()

Programmation système I Les entrées/sorties

Introduction au langage C

Le système de gestion des fichiers, les entrées/sorties.

1/24. I passer d un problème exprimé en français à la réalisation d un. I expressions arithmétiques. I structures de contrôle (tests, boucles)

Suivant les langages de programmation, modules plus avancés : modules imbriqués modules paramétrés par des modules (foncteurs)

Programmation système en C/C++

Chap 4: Analyse syntaxique. Prof. M.D. RAHMANI Compilation SMI- S5 2013/14 1

Cours Programmation Système

TP n 2 Concepts de la programmation Objets Master 1 mention IL, semestre 2 Le type Abstrait Pile

Algorithmique et Programmation, IMA

1. Structure d un programme C. 2. Commentaire: /*..texte */ On utilise aussi le commentaire du C++ qui est valable pour C: 3.

Génie Logiciel avec Ada. 4 février 2013

Introduction à Java. Matthieu Herrb CNRS-LAAS. Mars

Cours 14 Les fichiers

Les fichiers. Chapitre 4

Machines virtuelles fonctionnelles (suite) Compilation ML Java

Cours de C++ François Laroussinie. 2 novembre Dept. d Informatique, ENS de Cachan

Cours 6 : Tubes anonymes et nommés

OCL - Object Constraint Language

3IS - Système d'exploitation linux - Programmation système

Java Licence Professionnelle CISII,

Introduction à la programmation orientée objet, illustrée par le langage C++ Patrick Cégielski

Rappels Entrées -Sorties

IN Cours 1. 1 Informatique, calculateurs. 2 Un premier programme en C

Cours de Programmation 2

Les structures. Chapitre 3

Rappel. Analyse de Données Structurées - Cours 12. Un langage avec des déclaration locales. Exemple d'un programme

COMPARAISONDESLANGAGESC, C++, JAVA ET

Cours de C. Petits secrets du C & programmation avancée. Sébastien Paumier

Cours No 3 : Identificateurs, Fonctions, Premières Structures de contrôle.

Centre CPGE TSI - Safi 2010/2011. Algorithmique et programmation :

OS Réseaux et Programmation Système - C5

Classes et Objets en Ocaml.

Analyse de sécurité de logiciels système par typage statique

Cours d Algorithmique-Programmation 2 e partie (IAP2): programmation 24 octobre 2007impérative 1 / 44 et. structures de données simples

Éléments d informatique Cours 3 La programmation structurée en langage C L instruction de contrôle if

Dans le chapitre 1, nous associions aux fichiers ouverts des descripteurs de fichiers par lesquels nous accédions aux fichiers.

Théorie des Langages

Programmer en JAVA. par Tama

Programmation impérative

Plan. Exemple: Application bancaire. Introduction. OCL Object Constraint Language Le langage de contraintes d'uml

INTRODUCTION A JAVA. Fichier en langage machine Exécutable

Les chaînes de caractères

Expression des contraintes. OCL : Object C o n t r a i n t L a n g u a g e

Premiers Pas en Programmation Objet : les Classes et les Objets

SUPPORT DE COURS. Langage C

Brefs rappels sur la pile et le tas (Stack. / Heap) et les pointeurs

Bases de programmation. Cours 5. Structurer les données

Paris Airports - Web API Airports Path finding

MISE A NIVEAU INFORMATIQUE LANGAGE C - EXEMPLES DE PROGRAMMES. Université Paris Dauphine IUP Génie Mathématique et Informatique 2 ème année

Une introduction à Java

Programmation avec des objets : Cours 7. Menu du jour

Introduction à la programmation Travaux pratiques: séance d introduction INFO0201-1

Traduction des Langages : Le Compilateur Micro Java

Programmation système de commandes en C

ACTIVITÉ DE PROGRAMMATION

INFO-F-105 Language de programmation I Séance VI

Exceptions. 1 Entrées/sorties. Objectif. Manipuler les exceptions ;

LMI 2. Programmation Orientée Objet POO - Cours 9. Said Jabbour. jabbour@cril.univ-artois.fr

Cours 1 : La compilation

Logiciel Libre Cours 2 Fondements: Programmation

Interpréteur d algèbre relationnelle

INITIATION AU LANGAGE C SUR PIC DE MICROSHIP

INITIATION AU LANGAGE JAVA

EPREUVE OPTIONNELLE d INFORMATIQUE CORRIGE

Stockage du fichier dans une table mysql:

Prénom : Matricule : Sigle et titre du cours Groupe Trimestre INF1101 Algorithmes et structures de données Tous H2004. Loc Jeudi 29/4/2004

Recherche dans un tableau

Cours de Système : Gestion de Fichiers

Les structures de données. Rajae El Ouazzani

TD3: tableaux avancées, première classe et chaînes


Programmation en langage C

C++ Programmer. en langage. 8 e édition. Avec une intro aux design patterns et une annexe sur la norme C++11. Claude Delannoy

GOL502 Industries de services

Programmation Classique en langage C

Compilation (INF 564)

STAGE IREM 0- Premiers pas en Python

ARBRES BINAIRES DE RECHERCHE

Langage Java. Classe de première SI

3. SPÉCIFICATIONS DU LOGICIEL. de l'expression des besoins à la conception. Spécifications fonctionnelles Analyse fonctionnelle et méthodes

PHP et mysql. Code: php_mysql. Olivier Clavel - Daniel K. Schneider - Patrick Jermann - Vivian Synteta Version: 0.9 (modifié le 13/3/01 par VS)

Programmation C++ (débutant)/instructions for, while et do...while

Théorie de la Programmation

Présentation du langage et premières fonctions

Programmation C. J.-F. Lalande. 15 novembre 2012

Langage C. Patrick Corde. 22 juin Patrick Corde ( Patrick.Corde@idris.fr ) Langage C 22 juin / 289

Langage et Concepts de ProgrammationOrientée-Objet 1 / 40

Arbres binaires de recherche

RAPPELS SUR LES METHODES HERITEES DE LA CLASSE RACINE Object ET LEUR SPECIALISATION (i.e. REDEFINITION)

Arguments d un programme

Utilitaires méconnus de StrataFrame

UML. Diagrammes de classes (suite) Delphine Longuet.

Algorithmique, Structures de données et langage C

Environnements de développement (intégrés)

Info0101 Intro. à l'algorithmique et à la programmation. Cours 3. Le langage Java

Objets et Programmation. origine des langages orientés-objet

Object Constraint Language (OCL)

La programmation orientée objet Gestion de Connexions HTTP Manipulation de fichiers Transmission des données PHP/MySQL. Le langage PHP (2)

Transcription:

Traduction et Sémantique Traduction dirigée par la syntaxe Luigi Santocanale LIF, Université de Provence Marseille, FRANCE 19 mars 2010

Plan Traduction dirigée par la syntaxe Grammaires attribués Attributs synthétisés et hérités Implantation ascendante des grammaires S-attribuées Un exemple : construction de la syntaxe abstraite Grammaires L-attribuées Schémas de traduction Traduction ascendante Chercher un attribut dans la pile : les marqueurs

Plan 3/41 Traduction dirigée par la syntaxe Grammaires attribués Attributs synthétisés et hérités Implantation ascendante des grammaires S-attribuées Un exemple : construction de la syntaxe abstraite Grammaires L-attribuées Schémas de traduction Traduction ascendante Chercher un attribut dans la pile : les marqueurs

4/41 Définitions dirigées par la syntaxe Grammaire attribuée : toute production possède un ensemble de règles sémantiques : Production Règle sémantique A X 1 X n a 1 a 2 a k Chaque règle sémantique (ou action) a i : calcule un attribut d un symbole parmi A,X 1,,X n, ou produit un effet de bord

4/41 Définitions dirigées par la syntaxe Grammaire attribuée : toute production possède un ensemble de règles sémantiques : Production Règle sémantique A X 1 X n a 1 a 2 a k Chaque règle sémantique (ou action) a i : calcule un attribut d un symbole parmi A,X 1,,X n, ou produit un effet de bord

4/41 Définitions dirigées par la syntaxe Grammaire attribuée : toute production possède un ensemble de règles sémantiques : Production Règle sémantique A X 1 X n a 1 a 2 a k Chaque règle sémantique (ou action) a i : calcule un attribut d un symbole parmi A,X 1,,X n, ou produit un effet de bord

Grammaire attribuée pour une calculette 5/41 Production Règle sémantique L E \n E E + T E T T T F T F F (E) F chiffre Imprimer(Eval) Eval := E val + T val Eval := T val T val := T val Fval T val := Fval Fval := Eval Fval := chiffrevallex

Grammaire attribuée pour une calculette 5/41 Production Règle sémantique L E \n E E 1 + T E T T T 1 F T F F (E) F chiffre Imprimer(Eval) Eval := E 1 val + T val Eval := T val T val := T 1 val Fval T val := Fval Fval := Eval Fval := chiffrevallex

Attribution de types dans une déclaration 6/41 Production Règle sémantique D T L T entier T reel L L 1, id L id Ltype := T type T type := entier T type := reel L 1 type := Ltype AjouterType(identree, Ltype) AjouterType(identree, Ltype)

7/41 Formellement Une grammaire attribuée consiste de : un grammaire G = V,Σ,S,P pour tout X V, une collection d attributs de X : {Xi i I X } pour toute production A X 1,,X n, une collection de règles a i de la forme b := f (c 1,c 2,,c k ) où b,c 1,,c k sont des attribuits de A,X 1,,X n

Attributs synthétisés : intuition 8/41 Le flot de l information procède des fils vers les parents, et de droite à gauche du symbole de la production : Exemple, la ligne 3 + 4\n

Attributs synthétisés 9/41 Un attribut b est synthétisé si chaque fois que Production Règle sémantique A X 1 X n b := f (c 1,,c k ) b est un attribut de A, c est-à-dire il est de la forme Ai

Attributs synthétisés 9/41 Un attribut b est synthétisé si chaque fois que Production Règle sémantique A X 1 X n b := f (c 1,,c k ) b est un attribut de A, c est-à-dire il est de la forme Ai

L attribut Eval de la calculette est synthétisé 10/41 Production Règle sémantique L E \n E E 1 + T E T T T 1 F T F F (E) F chiffre Imprimer(Eval) Eval := E 1 val + T val Eval := T val T val := T 1 val Fval T val := Fval Fval := Eval Fval := chiffrevallex Tous les attributs de la calculette sont synthétisés

L attribut Eval de la calculette est synthétisé 10/41 Production Règle sémantique L E \n E E 1 + T E T T T 1 F T F F (E) F chiffre Imprimer(Eval) Eval := E 1 val + T val Eval := T val T val := T 1 val Fval T val := Fval Fval := Eval Fval := chiffrevallex Tous les attributs de la calculette sont synthétisés

Attributs hérités 11/41 Le flot d information procède des parents vers les fils, entre frères, (et de gauche à droite d une production) Exemple, la déclaration entier i, j

Attributs hérités 12/41 Un attribut b est héríté si chaque fois que Production Règle sémantique A X 1 X n b := f (c 1,,c k ) b est un attribut de X j, pour quelque j = 1,,n, c est-à-dire il est de la forme X j i

Attributs hérités 12/41 Un attribut b est héríté si chaque fois que Production Règle sémantique A X 1 X n b := f (c 1,,c k ) b est un attribut de X j, pour quelque j = 1,,n, c est-à-dire il est de la forme X j i

L attribut Ltype est hérité 13/41 Production Règle sémantique D T L T entier T reel L L 1, id L id Ltype := T type T type := entier T type := reel L 1 type := Ltype AjouterType(identree, Ltype) AjouterType(identree, Ltype) Ltype est hérité, T type est synthétisé

L attribut Ltype est hérité 13/41 Production Règle sémantique D T L T entier T reel L L 1, id L id Ltype := T type T type := entier T type := reel L 1 type := Ltype AjouterType(identree, Ltype) AjouterType(identree, Ltype) Ltype est hérité, T type est synthétisé

14/41 Implantation ascendante des définitions S-attribués Grammaire S-attribués : tous les attributs sont synthétisés Implantation On étend un analyseur ascendante On place les attributs sur la pile On calcul les attributs du père, avant réduction

14/41 Implantation ascendante des définitions S-attribués Grammaire S-attribués : tous les attributs sont synthétisés Implantation On étend un analyseur ascendante On place les attributs sur la pile On calcul les attributs du père, avant réduction

14/41 Implantation ascendante des définitions S-attribués Grammaire S-attribués : tous les attributs sont synthétisés Implantation On étend un analyseur ascendante On place les attributs sur la pile On calcul les attributs du père, avant réduction

14/41 Implantation ascendante des définitions S-attribués Grammaire S-attribués : tous les attributs sont synthétisés Implantation On étend un analyseur ascendante On place les attributs sur la pile On calcul les attributs du père, avant réduction

Exemple 15/41 Production E E 1 + T Règle sémantique Eval := E 1 val + T val top q n 4 q n 1 q n 2 3 q n 3 etat val

Exemple 15/41 Production E E 1 + T Règle sémantique Eval := E 1 val + T val top q n T 4 q n 1 + q n 2 E 3 q n 3 X etat val

Exemple 15/41 Production E E 1 + T Règle sémantique Eval := E 1 val + T val top q n T 4 q n 1 + q n 2 E 3 q n 3 X ntop q E 7 q n 3 X etat val etat val

Exemple 15/41 Production E E 1 + T Règle sémantique Eval := E 1 val + T val top q n 4 q n 1 q n 2 3 q n 3 ntop q 7 q n 3 etat val etat val

Exemple (cont) 16/41 Production E E 1 + T Règle sémantique Eval := E 1 val + T val q n 4 q n 1 q n 2 3 q n 3 etat val Production E E 1 + T Règle sémantique val[ntop] := val[top 2] + val[top]

Exemple (cont) 16/41 Production E E 1 + T Règle sémantique Eval := E 1 val + T val q n 4 q n 1 q n 2 3 q n 3 etat val Production E E 1 + T Règle sémantique val[ntop] := val[top 2] + val[top]

Exemple (cont) 16/41 Production E E 1 + T Règle sémantique Eval := E 1 val + T val q n 4 q n 1 q n 2 3 q n 3 q 7 q n 3 etat val etat val Production Règle sémantique E E 1 + T val[ntop] := val[top 2] + val[top]

Exemple (cont) 16/41 Production E E 1 + T Règle sémantique Eval := E 1 val + T val q n 4 q n 1 q n 2 3 q n 3 Eval = val[top 2] +val[top]; ntop = top 3 + 1; etat[ntop] = Next(etat[top], E); val[ntop] = Eval; q 7 q n 3 etat val etat val Production Règle sémantique E E 1 + T val[ntop] := val[top 2] + val[top]

Exemple (cont) 16/41 Production E E 1 + T Règle sémantique Eval := E 1 val + T val q n 4 q n 1 q n 2 3 q n 3 Eval = val[top 2] +val[top]; ntop = top 3 + 1; etat[ntop] = Next(etat[top], E); val[ntop] = Eval; q 7 q n 3 etat val etat val Production Règle sémantique E E 1 + T val[ntop] := val[top 2] + val[top]

17/41 Construction de la syntaxe abstraite pour le langage lse La syntaxe abstraite en tic (fichier lse_syntabstrtic) : type op = Plus ; type e_ expr = Id of string Const of int Op of op * e_ expr * e_ expr ; type b_ expr = Expr of e_ expr Ite of e_ expr * b_ expr * b_ expr Let of string * e_ expr * b_ expr ;

18/41 Les prototypes engendrés (fichier lse_syntabstrtich) : extern Let * make_let ( char *, e_expr *, b_expr *); extern Ite * make_ite ( e_expr *, b_expr *, b_expr *); extern Expr * make_ Expr ( e_ expr *); extern b_expr * m ake_b_expr_of_expr ( Expr *); extern b_expr * make_b_expr_of_ite ( Ite *); extern b_expr * make_b_expr_of_let ( Let *); extern Op * make_op ( op *, e_expr *, e_expr *); extern Const * make_ Const ( int ); extern Id * make_id ( char *); extern e_expr * make_e_expr_of_id ( Id *); extern e_expr * m a k e _ e _ e x p r _ o f _ C o n s t ( Const *); extern e_expr * make_e_expr_of_op ( Op *); extern Plus * make_ Plus (); extern op * make_ op_ of_ Plus ( Plus *);

19/41 Des définitions supplementaires : /* Fait à la main */ # define makelet (s,e, b ) \ ( m ake_b_expr_of_let ( make_let (s,e, b ))) # define makeite (e, b1, b2 ) \ ( m ake_b_expr_of_ite ( make_ite (e, b1, b2 ))) # define makeexpr ( e ) \ ( m a k e_b_expr_of_expr ( make_expr ( e ))) # define makeplus ( e1, e2 ) \ ( make_e_expr_of_op ( make_op ( make_op_of_plus ( make_plus # define makeid ( s ) \ ( make_e_expr_of_id ( make_id ( s ))) # define makeconst ( n ) \ ( m a k e _ e _ e x p r _ o f _ C o n s t ( make_const ( n )))

20/41 Le traducteur (en bison) %{ # include < stdio h > # include < stdlib h > # include " lse_syntabstr tic h " extern void yyerror ( char const *); extern int yylineno ; extern FILE * yyin ; %} % union { char * str ; int num ; union e_expr * e ; union b_expr * b ; }

21/41 Le traducteur (en bison) : les types % union { char * str ; int num ; union e_expr * e ; union b_expr * b ; } % left PLUS % token LPAR RPAR % token IF ELSE % token THEN % token LET EQ IN % token <str > ID % token <num > CONST % type <e > e_ expression % type <b > b_ expression % start S

%% 22/41 Le traducteur (en bison) : la grammaire %% S : b_expression ; b_expression : e_expression { $$ = makeexpr ( $1 );} LPAR b_ expression RPAR { $$ = $2 ;} LET ID EQ e_ expression IN b_ expression { $$ = makelet ( $2, $4, $6 );} IF e_ expression THEN b_ expression ELSE b_ expression { $$ = makeite ( $2, $4, $6 );} ; e_expression : ID { $$ = makeid ( $1 );} CONST { $$ = makeconst ( $1 );} e_ expression PLUS e_ expression { $$ = makeplus ( $1, $3 );} LPAR e_ expression RPAR { $$ = $2 ;} ;

23/41 Le traducteur (en bison) : le main void yyerror ( char const * str ) { fprintf ( stderr," Ligne % d : % s \ n ", yylineno, str ); exit ( EXIT_FAILURE ); } int main ( int argc, char * argv []) { if ( argc > 1 ) { if (( yyin = fopen ( argv [1], " r " ))== NULL ) exit ( EXIT_FAILURE ); yyparse (); fclose ( yyin ); } else exit ( EXIT_ FAILURE ); exit ( EXIT_SUCCESS ); }

Plan 24/41 Traduction dirigée par la syntaxe Grammaires attribués Attributs synthétisés et hérités Implantation ascendante des grammaires S-attribuées Un exemple : construction de la syntaxe abstraite Grammaires L-attribuées Schémas de traduction Traduction ascendante Chercher un attribut dans la pile : les marqueurs

25/41 Grammaires L-attribuées Grammaire L-attribuée L évaluation des attributs peut se faire par un parcours en profondeur gauche (Left=gauche) de l arbre dérivation Tout attribut est synthétisé ou hérité Si Production Règle sémantique A X 1 X i 1 X i X n X i b := f (c 1,,c k ) alors c 1,,c k sont des attributs de X 1,,X i 1, ou des attributs hérités de A

25/41 Grammaires L-attribuées Grammaire L-attribuée L évaluation des attributs peut se faire par un parcours en profondeur gauche (Left=gauche) de l arbre dérivation Tout attribut est synthétisé ou hérité Si Production Règle sémantique A X 1 X i 1 X i X n X i b := f (c 1,,c k ) alors c 1,,c k sont des attributs de X 1,,X i 1, ou des attributs hérités de A

25/41 Grammaires L-attribuées Grammaire L-attribuée L évaluation des attributs peut se faire par un parcours en profondeur gauche (Left=gauche) de l arbre dérivation Tout attribut est synthétisé ou hérité Si Production Règle sémantique A X 1 X i 1 X i X n X i b := f (c 1,,c k ) alors c 1,,c k sont des attributs de X 1,,X i 1, ou des attributs hérités de A

25/41 Grammaires L-attribuées Grammaire L-attribuée L évaluation des attributs peut se faire par un parcours en profondeur gauche (Left=gauche) de l arbre dérivation Tout attribut est synthétisé ou hérité Si Production Règle sémantique A X 1 X i 1 X i X n X i b := f (c 1,,c k ) alors c 1,,c k sont des attributs de X 1,,X i 1, ou des attributs hérités de A

Attribution de types dans une déclaration 26/41 Production Règle sémantique D T L T entier T reel L L 1, id L id Ltype := T type T type := entier T type := reel L 1 type := Ltype AjouterType(identree, Ltype) AjouterType(identree, Ltype) Cette grammaire est L-attribuée

Attribution de types dans une déclaration 26/41 Production Règle sémantique D T L T entier T reel L L 1, id L id Ltype := T type T type := entier T type := reel L 1 type := Ltype AjouterType(identree, Ltype) AjouterType(identree, Ltype) Cette grammaire est L-attribuée

Attribution de types dans une déclaration 26/41 Production Règle sémantique D T L T entier T reel L L 1, id L id Ltype := T type T type := entier T type := reel L 1 type := Ltype AjouterType(identree, Ltype) AjouterType(identree, Ltype) Cette grammaire est L-attribuée

Attribution de types dans une déclaration 26/41 Production Règle sémantique D T L T entier T reel L L 1, id L id Ltype := T type T type := entier T type := reel L 1 type := Ltype AjouterType(identree, Ltype) AjouterType(identree, Ltype) Cette grammaire est L-attribuée

Attribution de types dans une déclaration 26/41 Production Règle sémantique D T L T entier T reel L L 1, id L id Ltype := T type T type := entier T type := reel L 1 type := Ltype AjouterType(identree, Ltype) AjouterType(identree, Ltype) Cette grammaire est L-attribuée

Attribution de types dans une déclaration 26/41 Production Règle sémantique D T L T entier T reel L L 1, id L id Ltype := T type T type := entier T type := reel L 1 type := Ltype AjouterType(identree, Ltype) AjouterType(identree, Ltype) Cette grammaire est L-attribuée

27/41 Schémas de traduction comme une grammaire attribuée, les actions sont insérés à la droite de, n importe où Précise l ordre des actions par rapport à un parcours en profondeur gauche de l arbre Exemple : traduction postfixe des expressions additives E T R R addop T {print(addoplexeme)} R ε T chiffre {print(chiffreval)}

28/41 En bison nous écrivons des schémas de traduction : % union { int num ; char * string ; %} % token < string > addop <num > chiffre %% E : T R ; R : addop T { printf ("% s ", $1 );} R /* vide */ ; T : chiffre { printf ("% d ", $1 );} ; %%

Contraintes 29/41 si une action calcule un attribut de X i alors se trouve à la gauche de X i : A { X i b := f () }X i si une action utilise un attribut de X i alors elle se trouve à la droite de X i : A X i { b := f (X i c ) }

Contraintes 29/41 si une action calcule un attribut de X i alors se trouve à la gauche de X i : A { X i b := f () }X i si une action utilise un attribut de X i alors elle se trouve à la droite de X i : A X i { b := f (X i c ) }

Élimination des reglès intermediaires 30/41 On peut toujours se ramener à une grammaire L-attribués :

Élimination des reglès intermediaires 30/41 On peut toujours se ramener à une grammaire L-attribués : Les schémas E T R R addop T {print(addoplexeme)} R ε T chiffre {print(chiffreval)} sont équivalents E T R R addop T M R ε M ε {print(addoplexeme)} T chiffre {print(chiffreval)}

Élimination des reglès intermediaires 30/41 On peut toujours se ramener à une grammaire L-attribués : Les grammaires E T R R addop T R ε T chiffre E T R R addop T M R ε M ε T chiffre sont équivalentes

31/41 Schémas en Bison Fichier postfixey : % union { int num ; char * string ; %} % token < string > addop <num > chiffre %% E : T R ; R : addop T { printf ("% s ", $1 );} R /* vide */ ; T : chiffre { printf ("% d ", $1 );} ; %% Fichier postfixeoutput : 0 $accept : E $end 1 E : T R 2 $@1 : /* vide */ 3 R : addop T $@1 R 4 /* vide */ 5 T : chiffre

31/41 Schémas en Bison Fichier postfixey : % union { int num ; char * string ; %} % token < string > addop <num > chiffre %% E : T R ; R : addop T { printf ("% s ", $1 );} R /* vide */ ; T : chiffre { printf ("% d ", $1 );} ; %% Fichier postfixeoutput : 0 $accept : E $end 1 E : T R 2 $@1 : /* vide */ 3 R : addop T $@1 R 4 /* vide */ 5 T : chiffre

32/41 Exercice Considérez le fichier interreglesy % union { int num ; char * string ; } % token < string > A < string > B %% start : A { puts ( $1 );} B { puts ( $2 );} %% Expliquez pourquoi nous obtenons : $ bison interregles y interregles y :730-31: $2 de start n ' a pas de type déclaré

Traduction ascendante 33/41 Nous savons comment calculer les attributs synthétisés Pour calculer les attributs hérités : on effectue les actions au moment des réductions, Quand dans A X 1,X i 1X i X n on passe par X i X 1,,X i 1 sont dans la pile, A n est pas dans la pile, on cherchera un attribut herité de A dans la pile

La declaration int p,q,r 34/41 Pile Entrée Action int id,id,id$ int id, id, id$ T int type[ntop] := entier T id,id,id$ T id, id, id$ L id ajoutertype(entree[top], type[ntop]) T L,id,id$ T L, id,id$ T L, id, id$ L L, id ajoutertype(entree[top], type[ntop]) type[top 2] := type[ntop] T L,id$ T L, id$ T L, id $ L L, id ajoutertype(val[top], type[ntop]) type[top 2] := type[ntop] T L $ D T L type[top] := type[top 1] D $

La declaration int p,q,r 34/41 Pile Entrée Action int id,id,id$ int id, id, id$ T int type[ntop] := entier T id,id,id$ T id, id, id$ L id ajoutertype(entree[top], type[ntop]) T L,id,id$ T L, id,id$ T L, id, id$ L L, id ajoutertype(entree[top], type[ntop]) type[top 2] := type[ntop] T L,id$ T L, id$ T L, id $ L L, id ajoutertype(val[top], type[ntop]) type[top 2] := type[ntop] (type[ntop] = type[top 3]) T L $ D T L type[top] := type[top 1] D $

La declaration int p,q,r 34/41 Pile Entrée Action int id,id,id$ int id, id, id$ T int type[ntop] := entier T id,id,id$ T id, id, id$ L id ajoutertype(entree[top], type[ntop]) T L,id,id$ T L, id,id$ T L, id, id$ L L, id ajoutertype(entree[top], type[ntop]) type[top 2] := type[ntop] T L,id$ T L, id$ T L, id $ L L, id ajoutertype(val[top], type[ntop]) type[top 2] := type[top 3] (type[ntop] = type[top 3]) T L $ D T L type[top] := type[top 1] D $

La declaration int p,q,r 34/41 Pile Entrée Action int id,id,id$ int id, id, id$ T int type[ntop] := entier T id,id,id$ T id, id, id$ L id ajoutertype(entree[top], type[ntop]) T L,id,id$ T L, id,id$ T L, id, id$ L L, id ajoutertype(entree[top], type[ntop]) type[top 2] := type[ntop] T L,id$ T L, id$ T L, id $ L L, id ajoutertype(val[top], type[top 3]) type[top 2] := type[top 3] (type[ntop] = type[top 3]) T L $ D T L type[top] := type[top 1] D $

La declaration int p,q,r 34/41 Pile Entrée Action int id,id,id$ int id, id, id$ T int type[ntop] := entier T id,id,id$ T id, id, id$ L id ajoutertype(entree[top], type[ntop]) T L,id,id$ T L, id,id$ T L, id, id$ L L, id ajoutertype(entree[top], type[ntop]) type[top 2] := type[ntop] (type[ntop] = type[top 3]) T L,id$ T L, id$ T L, id $ L L, id ajoutertype(val[top], type[top 3]) type[top 2] := type[top 3] (type[ntop] = type[top 3]) T L $ D T L type[top] := type[top 1] D $

La declaration int p,q,r 34/41 Pile Entrée Action int id,id,id$ int id, id, id$ T int type[ntop] := entier T id,id,id$ T id, id, id$ L id ajoutertype(entree[top], type[ntop]) T L,id,id$ T L, id,id$ T L, id, id$ L L, id ajoutertype(entree[top], type[ntop]) type[top 2] := type[top 3] (type[ntop] = type[top 3]) T L,id$ T L, id$ T L, id $ L L, id ajoutertype(val[top], type[top 3]) type[top 2] := type[top 3] (type[ntop] = type[top 3]) T L $ D T L type[top] := type[top 1] D $

La declaration int p,q,r 34/41 Pile Entrée Action int id,id,id$ int id, id, id$ T int type[ntop] := entier T id,id,id$ T id, id, id$ L id ajoutertype(entree[top], type[ntop]) T L,id,id$ T L, id,id$ T L, id, id$ L L, id ajoutertype(entree[top], type[top 3]) type[top 2] := type[top 3] (type[ntop] = type[top 3]) T L,id$ T L, id$ T L, id $ L L, id ajoutertype(val[top], type[top 3]) type[top 2] := type[top 3] (type[ntop] = type[top 3]) T L $ D T L type[top] := type[top 1] D $

La declaration int p,q,r 34/41 Pile Entrée Action int id,id,id$ int id, id, id$ T int type[ntop] := entier T id,id,id$ T id, id, id$ L id ajoutertype(entree[top], type[ntop]) (type[ntop] = type[top 1]) T L,id,id$ T L, id,id$ T L, id, id$ L L, id ajoutertype(entree[top], type[top 3]) type[top 2] := type[top 3] (type[ntop] = type[top 3]) T L,id$ T L, id$ T L, id $ L L, id ajoutertype(val[top], type[top 3]) type[top 2] := type[top 3] (type[ntop] = type[top 3]) T L $ D T L type[top] := type[top 1] D $

La declaration int p,q,r 34/41 Pile Entrée Action int id,id,id$ int id, id, id$ T int type[ntop] := entier T id,id,id$ T id, id, id$ L id ajoutertype(entree[top], type[top 1]) (type[ntop] = type[top 1]) T L,id,id$ T L, id,id$ T L, id, id$ L L, id ajoutertype(entree[top], type[top 3]) type[top 2] := type[top 3] (type[ntop] = type[top 3]) T L,id$ T L, id$ T L, id $ L L, id ajoutertype(val[top], type[top 3]) type[top 2] := type[top 3] (type[ntop] = type[top 3]) T L $ D T L type[top] := type[top 1] D $

La declaration int p,q,r 34/41 Pile Entrée Action int id,id,id$ int id, id, id$ T int type[ntop] := entier T id,id,id$ T id, id, id$ L id ajoutertype(entree[top], type[top 1]) T L,id,id$ T L, id,id$ T L, id, id$ L L, id ajoutertype(entree[top], type[top 3]) type[top 2] := type[top 3] T L,id$ T L, id$ T L, id $ L L, id ajoutertype(val[top], type[top 3]) type[top 2] := type[top 3] T L $ D T L type[top] := type[top 1] D $

La declaration int p,q,r 34/41 Pile Entrée Action int id,id,id$ int id, id, id$ T int type[ntop] := entier T id,id,id$ T id, id, id$ L id ajoutertype(entree[top], type[top 1]) T L,id,id$ T L, id,id$ T L, id, id$ L L, id ajoutertype(entree[top], type[top 3]) T L,id$ T L, id$ T L, id $ L L, id ajoutertype(val[top], type[top 3]) T L $ D T L type[top] := type[top 1] D $

Implantation de la grammaire attribuée pour les déclarations 35/41 Production Règle sémantique D T L val[top] := val[top 1] T entier T reel val[ntop] := entier val[ntop] := reel L L 1, id AjouterType(val[ntop],val[top 3]) / val[top 2] := val[top 3] / (inutile) L id AjouterType(val[ntop], val[top 1])

Chercher un attribut hérité dans la pile 36/41 Considérez Production S a A C S b A B C C c Règle sémantique Che := Asy Che := Asy Csy := f (Che) Quand on réduit C c, Che peut se trouver à distance 1 ou 2 du sommet la pile

37/41 Pile Entrée Action aac C c valsy[ntop] := f (valhe[ntop]) aac aac S aac valhe[top] := valsy[top 1] S Pile Entrée Action babc C c valsy[ntop] := f (valhe[ntop]) babc babc S babc valhe[top] := valsy[top 2] S

37/41 Pile Entrée Action aac C c valsy[ntop] := f (valhe[ntop]) (valhe[ntop] = valsy[top 1]) aac aac S aac valhe[top] := valsy[top 1] S Pile Entrée Action babc C c valsy[ntop] := f (valhe[ntop]) babc babc S babc valhe[top] := valsy[top 2] S

37/41 Pile Entrée Action aac C c valsy[ntop] := f (valsy[top 1]) (valhe[ntop] = valsy[top 1]) aac aac S aac valhe[top] := valsy[top 1] S Pile Entrée Action babc C c valsy[ntop] := f (valhe[ntop]) babc babc S babc valhe[top] := valsy[top 2] S

37/41 Pile Entrée Action aac C c valsy[ntop] := f (valsy[top 1]) aac aac S aac valhe[top] := valsy[top 1] S Pile Entrée Action babc C c valsy[ntop] := f (valhe[ntop]) babc babc S babc valhe[top] := valsy[top 2] S

37/41 Pile Entrée Action aac C c valsy[ntop] := f (valsy[top 1]) aac aac S aac valhe[top] := valsy[top 1] S Pile Entrée Action babc C c valsy[ntop] := f (valhe[ntop]) babc babc S babc valhe[top] := valsy[top 2] S

37/41 Pile Entrée Action aac C c valsy[ntop] := f (valsy[top 1]) aac aac S aac valhe[top] := valsy[top 1] S Pile Entrée Action babc C c valsy[ntop] := f (valhe[ntop]) (valhe[ntop] = valsy[top 2]) babc babc S babc valhe[top] := valsy[top 2] S

37/41 Pile Entrée Action aac C c valsy[ntop] := f (valsy[top 1]) aac aac S aac valhe[top] := valsy[top 1] S Pile Entrée Action babc C c valsy[ntop] := f (valsy[top 2]) (valhe[ntop] = valsy[top 2]) babc babc S babc valhe[top] := valsy[top 2] S

37/41 Pile Entrée Action aac C c valsy[ntop] := f (valsy[top 1]) aac aac S aac valhe[top] := valsy[top 1] S Pile Entrée Action babc C c valsy[ntop] := f (valsy[top 2]) babc babc S babc valhe[top] := valsy[top 2] S

38/41 Autre exemple Production B α 0 Aα 1 C β 0 Bβ 1 E D C Règle sémantique Ahe := Bhe Bhe := Che Che := Di Pile E A Dβ 0 α 0 Aα 1 valhe[top α 1 ] := val[top β 0 α 0 Aα 1 ] Dβ 0 B Dβ 0 Bβ 1 valhe[top β 1 ] := val[top β 0 Bβ 1 ] DC valhe[top] := val[top 1] E

Marqueurs et copies 39/41 On peut s organiser pour que la valeur d un attribut herité se trouve en val[ntop 1] Production S a A C S b A B C C c Règle sémantique Che := Asy Che := Asy Csy := f (Che) Production S a A N C N ε S b A B M C M ε C c Règle sémantique Che := Nsy, Nhe := Asy Nsy := Nhe Che := Msy, Mhe := Asy Msy := Mhe Csy := f (Che)

Marqueurs et copies Production S a A N C N ε S b A B M C M ε C c Règle sémantique Nhe := Asy, Che := Nsy Nsy := Nhe Mhe := Asy, Che := Msy Msy := Mhe Csy := f (Che) devient Production Règle sémantique S a A N C valhe[top 1] := valsy[top 2], valhe[top] := valsy[top 1] N ε valsy[top] := valhe[top 1] S b A B M C valhe[top 1] := valsy[top 2], valhe[top] := valsy[top 1] M ε valsy[ntop] := Mhe[top 1] C c valsy[top] := valhe[top 1] 40/41

Remarques 41/41 Le non-terminal N apparaît seulement dans les productions S a A N C N ε Il connaît son contexte De même pour M Cette transformations préserve les grammaires LL(1) Elle ne preserve pas les grammaires LR(1) Rappel : LL(1) LR(1)

Remarques 41/41 Le non-terminal N apparaît seulement dans les productions S a A N C N ε Il connaît son contexte De même pour M Cette transformations préserve les grammaires LL(1) Elle ne preserve pas les grammaires LR(1) Rappel : LL(1) LR(1)

Remarques 41/41 Le non-terminal N apparaît seulement dans les productions S a A N C N ε Il connaît son contexte De même pour M Cette transformations préserve les grammaires LL(1) Elle ne preserve pas les grammaires LR(1) Rappel : LL(1) LR(1)

Remarques 41/41 Le non-terminal N apparaît seulement dans les productions S a A N C N ε Il connaît son contexte De même pour M Cette transformations préserve les grammaires LL(1) Elle ne preserve pas les grammaires LR(1) Rappel : LL(1) LR(1)