SlideServe
Menu
Préférences
© SlideServe · v2026

Syntax Directed Translation

carys avatar
7 diapositives · 312 Views · Jul 28, 2014

Syntax Directed Translation. Syntax directed translation. Yacc can do a simple kind of syntax directed translation from an input sentence to C code We can also think of it as compilation Each node in a parse tree produces a value That value depends on the type of the node

Syntax Directed Translation - slide 1
01 / 7Syntax Directed Translation
1 / 17

Télécharger la présentation

Syntax Directed Translation
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  1. Syntax Directed Translation

  2. Syntax directed translation • Yacc can do a simple kind of syntax directed translation from an input sentence to C code • We can also think of it as compilation • Each node in a parse tree produces a value • That value depends on the type of the node • And on the values produced by its children • The value is usually produced by a rule associated with the node • Just the rules in Yacc, e.g.: { $$ = $1 + $3; }

  3. Why is a 200 after this? pcalc> calc 331 Calculator (type ? for help and . to exit) >> = a 1 1 >> if 1 (= a 100) (= a 200) 100 >> a 200 >>

  4. Example grammar + input string => parse tree e e: e '+' e {$$ = $1+$3;} |e '*' e {$$ = $1*$3;} | NAME {$$ = $1->value;} | NUMBER {$$ = $1;} e + e 1 * 2 + a a 1 * 2

  5. Example grammar + input string => parse tree + annotations e $$ = $1+$3 e: e '+' e {$$ = $1+$3;} |e '*' e {$$ = $1*$3;} | NAME {$$ = $1->value;} | NUMBER {$$ = $1;} $$ = $1*$3 $$ = $1->value e + e 1 * 2 + a a 1 * 2 a’s symbol table entry 1 2

  6. Example Do a post order traversal of the annotated parse tree to determine the execution order of the nodes. e $$ = $1+$3 e: e '+' e {$$ = $1+$3;} |e '*' e {$$ = $1*$3;} | NAME {$$ = $1->value;} | NUMBER {$$ = $1;} $$ = $1*$3 $$ = $1->value e + e 1 * 2 + a a 1 * 2 a’s symbol table entry 1 2

  7. Conditionals if ($2) $$=$3; else $$=$4; e • If evaluates all three args and selects the value to return • Evaluation is bottom up, left to right • Watch out for side effects! $1->value = $3; $$=$3; e e if 1 $1->value = $3; $$=$3; a a = = 1 2