lec16 - 16-1/29•Compilers and Interpreters•Parse Trees...

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 16-1/29•Compilers and Interpreters•Parse Trees and Abstract Syntax Trees (AST's)•Creating and Evaluating AST's•The Table ADT and Symbol Tables CSE 12Abstract Syntax Trees16-2/29Using Algorithms and Data StructuresDigital computers can do anything that can conceivably be done with information: they are general purpose information processing machinesYou get a computer to do something by programming it to run algorithms that manipulate data structuresPicking what algorithms and data structures are good to use for a particular application is something every good programmer needs to knowOne important application area is: writing programs that understand programs 16-3/29Compilers and interpretersThere are two main kinds of program-understanding programs:Compilers take a program written in a source language as input and translate it into a machine language, for later executionInterpreters take a source language program as input, and execute ("interpret") it immediately, line by line(You can think of a computer as an interpreter of machine language instructions; the Java Virtual Machine is an interpreter of Java bytecode instructions, which are generated by the Java compiler)Let’s look at how some ideas from this course (like recursion, and trees,) can be used to implement an interpreter for a language 16-4/29Towards an interpreterAn interpreter reads a line or statement at a time of a source language text, and then does what that line or statement says to doThe source language should have clearly defined syntax and semantics rules the syntax (grammar) rules say what strings legally belong to the source language, and what their structure is the semantics rules say what each legal string in the language means, based on its structureStart with clearly defined syntax and semantics, and writing an interpreter becomes much easier 16-5/29Tasks for an interpreterA language interpreter involves two phases for each line or statement in the input: syntactic analysisand semantic evaluationSyntactic analysis is done in accordance with the syntax rules for the language –the result of syntactic analysis is a tree data structure–corresponds to "compile time" in a compiled language Semantic evaluation is done in accordance with the semantic rules –evaluation uses the tree created in the syntactic analysisphase –corresponds to "run time" for compiled codeThe result of all this is: doing what the statement says! (For example, computing a value and returning it or printing it out) 16-6/29Syntax rules and BNFThe syntax rules (i.e., grammar) for the language can be written down in Backus-Naur Form (BNF) or a similar notationA BNF grammar is a list of rulesEach rule defines one "nonterminal symbol", which appears at the left of a " :=" sign in the ruleAlternative definitions of the nonterminal symbol appear to the right of the " :=" sign, separated by " |" signs16-7/29Syntax rules and BNFThe nonterminal symbol defined in the first rule of the grammar...
View Full Document

This note was uploaded on 06/15/2011 for the course ECON 1 taught by Professor Aben during the Fall '07 term at City College of San Francisco.

Page1 / 29

lec16 - 16-1/29•Compilers and Interpreters•Parse Trees...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online