This shows you the differences between two versions of the page.
alf:laboratoare:08_fr_java [2023/05/02 01:37] alexandra.negoita02 [Exercises] |
alf:laboratoare:08_fr_java [2023/05/02 10:24] (current) alexandra.negoita02 [Exercises] |
||
---|---|---|---|
Line 1: | Line 1: | ||
====== TP 8 - Génération de code ====== | ====== TP 8 - Génération de code ====== | ||
+ | <note warning> | ||
+ | Vous devez **accepter** l'assignment d'ici est travailler avec ce **repository**: [[https://classroom.github.com/a/77p5JNmk|Lab 8]] | ||
+ | </note> | ||
===== Three Address Code ===== | ===== Three Address Code ===== | ||
Line 117: | Line 120: | ||
; | ; | ||
</code> | </code> | ||
- | |||
- | <code javascript index.js> | ||
- | import { ASTNode } from "./index"; | ||
- | import symbol_tree from './index'; | ||
- | import { Expression, ValueNode, AttributionNode, FunctionCallNode } from './index'; | ||
- | |||
- | var variable_id = 0; | ||
- | |||
- | let results: string[] = []; | ||
- | function nextVar () | ||
- | { | ||
- | return 'var' + variable_id++; | ||
- | } | ||
- | |||
- | |||
- | function writeThreeAddressCode (node) | ||
- | { | ||
- | if (node.id === 'StatementsNode') | ||
- | { | ||
- | for (var statement of node.statements) | ||
- | { | ||
- | writeThreeAddressCode(statement); | ||
- | } | ||
- | } | ||
- | else | ||
- | if (node instanceof FunctionCallNode) | ||
- | { | ||
- | /**TODO: | ||
- | * generate the three address code for each parameter of the node.parameters array | ||
- | * write on the screen the three address code for each parameter | ||
- | * node.result will be nextVar() | ||
- | * write on the screen the three address code for function call | ||
- | */ | ||
- | } | ||
- | else | ||
- | if (node instanceof ValueNode) | ||
- | { | ||
- | // the result for a number is the number itself | ||
- | node.result = node.value; | ||
- | } | ||
- | else | ||
- | if (node instanceof AttributionNode) | ||
- | { | ||
- | /** TODO: | ||
- | * generate the three address code for node.value | ||
- | * write on the screen the three address code for an attribution*/ | ||
- | } | ||
- | else | ||
- | if (node instanceof Expression) | ||
- | { | ||
- | if (node.left !== undefined && node.right !== undefined) | ||
- | { | ||
- | writeThreeAddressCode (node.left); | ||
- | writeThreeAddressCode (node.right); | ||
- | |||
- | // node.left.result is the result of node.left | ||
- | // node.right.result is the result of node.right | ||
- | | ||
- | /** TODO: | ||
- | * node.result will be nextVar() | ||
- | * write on the screen the three address code based on result, left result, right result and operator*/ | ||
- | } | ||
- | } | ||
- | } | ||
- | |||
- | var ast = parser.parse (str); | ||
- | console.log (JSON.stringify(ast, null, 4)); | ||
- | |||
- | writeThreeAddressCode(ast); | ||
- | |||
- | |||
- | </code> | ||
- | |||
===== Exercises ===== | ===== Exercises ===== | ||
- Dans le fichier **ex1.txt** écrivez le //three address code// pour les expressions suivantes (**1p**) | - Dans le fichier **ex1.txt** écrivez le //three address code// pour les expressions suivantes (**1p**) | ||
- | * (4+2)/5-3 | ||
* (4+2)/5-3 | * (4+2)/5-3 | ||
* e = (a-2)+(a-5) | * e = (a-2)+(a-5) |