1 / 7

Syntax Directed Translation

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

carys
Download Presentation

Syntax Directed Translation

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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

More Related