GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — SI3-Compiler/analyse_syntaxique.py at main · Other-Project/SI3-Compiler
This repository was archived by the owner on Jun 15, 2024. It is now read-only.
Expand file tree/ Copy pathanalyse_syntaxique.pyMore file actions 244 lines (189 loc) · 6.23 KB / Copy pathanalyse_syntaxique.pyFile metadata and controls244 lines (189 loc) · 6.23 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 import sys from sly import Parser from analyse_lexicale import FloLexer import arbre_abstrait class FloParser(Parser): if __name__ == "__main__": debugfile = "parser.out" # On récupère la liste des lexèmes de l'analyse lexicale tokens = FloLexer.tokens # Règles gramaticales et actions associées @_("instructions") def prog(self, p): return arbre_abstrait.Program(p.instructions) @_("functions instructions") def prog(self, p): return arbre_abstrait.Program(p.instructions, p.functions) @_('IF "(" expr ")" "{" instructions "}" condition_else') def condition(self, p): return arbre_abstrait.If(p.expr, p.instructions, p.condition_else) @_("") def condition_else(self, p): pass @_("ELSE condition") def condition_else(self, p): return arbre_abstrait.Else(arbre_abstrait.Instructions([p.condition])) @_('ELSE "{" instructions "}"') def condition_else(self, p): return arbre_abstrait.Else(p.instructions) @_('WHILE "(" expr ")" "{" instructions "}"') def while_loop(self, p): return arbre_abstrait.While(p.expr, p.instructions) @_("condition", "while_loop") def block_operator(self, p): return p[0] @_('instruction ";" instructions') def instructions(self, p): p.instructions.instructions.insert(0, p.instruction) return p.instructions @_("block_operator instructions") def instructions(self, p): p.instructions.instructions.insert(0, p.block_operator) return p.instructions @_("") def instructions(self, p): return arbre_abstrait.Instructions() @_("function") def variable(self, p): return p.function @_("function") def instruction(self, p): return p.function @_("declarationFunction") def functions(self, p): l = arbre_abstrait.Functions() l.functions.insert(0, p.declarationFunction) return l @_("functions declarationFunction") def functions(self, p): p.functions.append(p.declarationFunction) return p.functions @_('IDENTIFIANT "(" args ")"') def function(self, p): return arbre_abstrait.Function(p.IDENTIFIANT, p.args) @_('TYPE IDENTIFIANT "(" declarationArgs ")" "{" instructions "}"') def declarationFunction(self, p): return arbre_abstrait.DeclarationFunction(p.TYPE, p.IDENTIFIANT, p.instructions, p.declarationArgs) @_("") def args(self, p): pass @_("expr") def args(self, p): a = arbre_abstrait.Args() a.listArgs.append(p.expr) return a @_('expr "," args') def args(self, p): p.args.listArgs.insert(0, p.expr) return p.args @_('TYPE IDENTIFIANT "," declarationArgs') def declarationArgs(self, p): declaration = arbre_abstrait.Declaration(p.TYPE, p.IDENTIFIANT) p.declarationArgs.declarations.insert(0, declaration) return p.declarationArgs @_("TYPE IDENTIFIANT") def declarationArgs(self, p): a = arbre_abstrait.ArgsDeclaration() declaration = arbre_abstrait.Declaration(p.TYPE, p.IDENTIFIANT) a.declarations.append(declaration) return a @_("") def declarationArgs(self, p): pass @_('TYPE IDENTIFIANT "=" expr') def instruction(self, p): return arbre_abstrait.Declaration(p.TYPE, p.IDENTIFIANT, p.expr) @_("TYPE IDENTIFIANT") def instruction(self, p): return arbre_abstrait.Declaration(p.TYPE, p.IDENTIFIANT) @_('IDENTIFIANT "=" expr') def instruction(self, p): return arbre_abstrait.Assignment(p.IDENTIFIANT, p.expr) @_("RETURN expr") def instruction(self, p): return arbre_abstrait.Return(p.expr) @_("somme") def boolean(self, p): return p.somme @_('somme "+" produit', 'somme "-" produit') def somme(self, p): return arbre_abstrait.Operation(p[1], p[0], p[2]) @_("produit") def somme(self, p): return p.produit @_("factor") def produit(self, p): return p.factor @_('"-" factor') def produit(self, p): return arbre_abstrait.Operation("*", arbre_abstrait.Integer(-1), p.factor) @_('produit "*" factor', 'produit "/" factor', 'produit "%" factor') def produit(self, p): return arbre_abstrait.Operation(p[1], p[0], p[2]) @_("variable") def factor(self, p): return p.variable @_("IDENTIFIANT") def variable(self, p): return arbre_abstrait.Variable(p.IDENTIFIANT) @_('"(" expr ")"') def factor(self, p): return p.expr @_("INTEGER") def factor(self, p): return arbre_abstrait.Integer(p.INTEGER) @_("b_or") def expr(self, p): return p.b_or @_( 'somme "<" somme', 'somme ">" somme', "somme EQ somme", "somme LE somme", "somme GE somme", "somme NE somme", "boolean_value EQ boolean_value", "boolean_value NE boolean_value", ) def boolean(self, p): return arbre_abstrait.Operation(p[1], p[0], p[2]) @_("boolean_value") def boolean(self, p): return p.boolean_value @_("BOOLEAN") def boolean_value(self, p): return arbre_abstrait.Boolean(p.BOOLEAN) @_("b_and OR b_or") def b_or(self, p): return arbre_abstrait.Operation(p[1], p[0], p[2]) @_("b_and") def b_or(self, p): return p.b_and @_("b_not AND b_and") def b_and(self, p): return arbre_abstrait.Operation(p[1], p[0], p[2]) @_("b_not") def b_and(self, p): return p.b_not @_("NOT boolean") def b_not(self, p): return arbre_abstrait.Operation(p.NOT, p.boolean) @_("boolean") def b_not(self, p): return p.boolean def error(self, p): print("Erreur de syntaxe", p, file=sys.stderr) exit(1) if __name__ == "__main__": lexer = FloLexer() parser = FloParser() if len(sys.argv) < 2: print("usage: python3 analyse_syntaxique.py NOM_FICHIER_SOURCE.flo") else: with open(sys.argv[1], "r") as f: data = f.read() try: arbre = parser.parse(lexer.tokenize(data)) arbre.afficher() except EOFError: exit(1) |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |