1 / 11

Languages and Compilers (SProg og Oversættere)

Languages and Compilers (SProg og Oversættere). Interpretation. Interpretation. Iterative and Recursive interpreters Interpretive compilers Abstract machines. The usefulness of Interpreters. Quick implementation of new language Remember bootstrapping Testing and debugging

meris
Télécharger la présentation

Languages and Compilers (SProg og Oversættere)

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. Languages and Compilers(SProg og Oversættere) Interpretation

  2. Interpretation • Iterative and Recursive interpreters • Interpretive compilers • Abstract machines

  3. The usefulness of Interpreters • Quick implementation of new language • Remember bootstrapping • Testing and debugging • Portability via Abstract Machine • Hardware emulation

  4. Iterative interpretation • Follows a very simple scheme: • Typical source language will have several instructions • Execution then is just a big case statement • one for each instruction Initialize Do { fetch next instruction analyze instruction execute instruction } while (still running)

  5. Mini-shell Script ::= Command* Command ::= Command-Name Argument* end-of-line Argument ::= Filename | Literal Command-Name ::= create | delete | edit | list | print | quit | Filename

  6. Mini-Shell Interpreter Public void interpret () { //Initialize status = RUNNING; do { //Fetch and analyse the next instruction MiniShellCommand com = readAnalyze(); // Execute this instruction if (com.name.equals(“create”)) create(com.args[0]); else if (com.name.equals(“delete”)) delete(com.args) else if … else if (com.name.equals(“quit”)) status = HALTED; else status = FAILED; } while (status == RUNNING); }

  7. JVM Interpreter The core of a JVM interpreter is basically this: do { byte opcode = fetch an opcode; switch (opcode) { case opCode1 : fetch operands for opCode1; execute action for opCode1; break; case opCode2: fetch operands for opCode2; execute action for opCode2; break; case ... } while (more to do)

  8. Recursive interpretation • Two phased strategy • Fetch and analyze program • Recursively analyzing the phrase structure of source • Generating AST • Performing semantic analysis • Recursively via visitor • Execute program • Recursively by walking the decorated AST

  9. Recursive Interpreter for MiniTriangle public class MiniTriangleProcesser extends MiniTriangleState implements Visitor { public void fetchAnalyze () { //load the program into the code store after //performing syntactic and contextual analysis } public void run () { … // run the program public Object visit…Command (…Command com, Object arg) { //execute com, returning null (ignoring arg) } public Object visit…Expression (…Expression expr, Object arg) { //Evaluate expr, returning its result } public Object visit… }

  10. Recursive Interpreter for MiniTriangle public Object visitIfCommand (IfCommand com, Object arg) { BoolValue val = (BoolValue) com.E.visit(this, null); if (val.b) com.C1.visit(this, null); else com.C2.visit(this, null); return null; } public Object visitWhileCommand (WhileCommand com, Object arg) { for (;;) { BoolValue val = (BoolValue) com.E.visit(this, null) if (! Val.b) break; com.C.visit(this, null); } return null; }

  11. Interpreters are everywhere on the web Web-Client Database Server Web-Server HTML-Form (+JavaScript) Call PHP interpreter WWW DBMS Submit Data LAN Web-Browser PHP Script SQL commands Response Response Database Output Reply

More Related