CS 153: Concepts of Compiler Design September 16 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak www.cs.sjsu.edu/~mak.

Slides:



Advertisements
Similar presentations
CS 153: Concepts of Compiler Design Sept. 15 Class Meeting Department of Computer Science San Jose State University Fall 2009 Instructor: Ron Mak
Advertisements

Semantics Static semantics Dynamic semantics attribute grammars
Environments and Evaluation
CS 153: Concepts of Compiler Design August 25 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak
CS 146: Data Structures and Algorithms June 18 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
CS 152: Programming Language Paradigms April 9 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak
CS 153: Concepts of Compiler Design August 24 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 153: Concepts of Compiler Design September 2 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 280 Data Structures Professor John Peterson. How Does Parsing Work? You need to know where to start (“statement”) This grammar is constructed so that.
Interpretation Environments and Evaluation. CS 354 Spring Translation Stages Lexical analysis (scanning) Parsing –Recognizing –Building parse tree.
CS 153: Concepts of Compiler Design September 9 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 153: Concepts of Compiler Design October 5 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 153: Concepts of Compiler Design September 14 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 152: Programming Language Paradigms April 2 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak
CS 153: Concepts of Compiler Design August 26 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 153: Concepts of Compiler Design September 21 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 153: Concepts of Compiler Design October 7 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 153: Concepts of Compiler Design October 10 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 153: Concepts of Compiler Design October 21 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 153: Concepts of Compiler Design September 30 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
CS 46B: Introduction to Data Structures June 30 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak.
CS 152: Programming Language Paradigms April 7 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak
CS 153: Concepts of Compiler Design October 12 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 153: Concepts of Compiler Design September 23 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 152: Programming Language Paradigms May 12 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak
CS 153: Concepts of Compiler Design September 28 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 153: Concepts of Compiler Design September 14 Class Meeting
A Simple Syntax-Directed Translator
CS 153: Concepts of Compiler Design August 29 Class Meeting
CS 153: Concepts of Compiler Design September 12 Class Meeting
CS 153: Concepts of Compiler Design October 17 Class Meeting
CS 153: Concepts of Compiler Design October 5 Class Meeting
CS 432: Compiler Construction Lecture 10
CS 153: Concepts of Compiler Design September 7 Class Meeting
CS 153: Concepts of Compiler Design October 3 Class Meeting
CS 153: Concepts of Compiler Design November 30 Class Meeting
CMPE 152: Compiler Design December 5 Class Meeting
CMPE 152: Compiler Design September 25 Class Meeting
CMPE 152: Compiler Design September 4 Class Meeting
CMPE 152: Compiler Design September 6 Class Meeting
CS 432: Compiler Construction Lecture 7
CMPE 152: Compiler Design September 11 Class Meeting
CMPE 152: Compiler Design September 13 Class Meeting
CMPE 152: Compiler Design October 2 Class Meeting
CMPE 152: Compiler Design October 4 Class Meeting
CMPE 152: Compiler Design September 11/13 Lab
CMPE 152: Compiler Design October 4 Class Meeting
CMPE 152: Compiler Design August 23 Class Meeting
CMPE 152: Compiler Design August 21/23 Lab
Compiling Control Statements
CMPE 152: Compiler Design September 27 Class Meeting
CS 432: Compiler Construction Lecture 11
CMPE 152: Compiler Design February 14 Class Meeting
CMPE 152: Compiler Design February 28 Class Meeting
CMPE 152: Compiler Design March 7 Class Meeting
CMPE 152: Compiler Design January 29 Class Meeting
CMPE 152: Compiler Design February 12 Class Meeting
CMPE 152: Compiler Design February 7 Class Meeting
CMPE 152: Compiler Design February 21/26 Lab
CMPE 152: Compiler Design February 21 Class Meeting
CMPE 152: Compiler Design February 19 Class Meeting
CMPE 152: Compiler Design March 7 Class Meeting
CMPE 152: Compiler Design March 5 Class Meeting
CMPE 152: Compiler Design December 4 Class Meeting
CMPE 152: Compiler Design September 3 Class Meeting
CMPE 152: Compiler Design August 27 Class Meeting
CMPE 152: Compiler Design September 17 Class Meeting
CMPE 152: Compiler Design September 19 Class Meeting
Presentation transcript:

CS 153: Concepts of Compiler Design September 16 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak 1

Computer Science Dept. Fall 2014: September 15 CS 153: Concepts of Compiler Design © R. Mak 2 FOR Statement  Example: Initial assignment. Node type GT for TO and LT for DOWNTO. DO statement. Increment/decrement: Node type ADD for TO and SUBTRACT for DOWNTO. FOR k := j TO 5 DO n := k

Computer Science Dept. Fall 2014: September 15 CS 153: Concepts of Compiler Design © R. Mak 3 Pascal Syntax Checker II: FOR  Demo. java -classpath classes Pascal compile -i for.txt java -classpath classes Pascal compile -i forerrors.txt

Computer Science Dept. Fall 2014: September 15 CS 153: Concepts of Compiler Design © R. Mak 4 IF Statement  Example: Third child only if there is an ELSE. IF (i = j) THEN t := 200 ELSE f := -200;

Computer Science Dept. Fall 2014: September 15 CS 153: Concepts of Compiler Design © R. Mak 5 The “Dangling” ELSE  Consider: IF i = 3 THEN IF j = 2 THEN t := 500 ELSE f := -500  Which THEN does the ELSE pair with? Is it: IF i = 3 THEN IF j = 2 THEN t := 500 ELSE f := -500 Or is it: IF i = 3 THEN IF j = 2 THEN t := 500 ELSE f := -500

Computer Science Dept. Fall 2014: September 15 CS 153: Concepts of Compiler Design © R. Mak 6 The “Dangling” ELSE, cont’d  According to Pascal syntax, the nested IF statement is the THEN statement of the outer IF statement IF i = 3 THEN IF j = 2 THEN t := 500 ELSE f := -500  Therefore, the ELSE pairs with the closest (i.e., the second) THEN.

Computer Science Dept. Fall 2014: September 15 CS 153: Concepts of Compiler Design © R. Mak 7 Scanner and Parser Rules of Thumb  Scanner At any point in the source file, extract the longest possible token. Example:  <<= is one shift-left-assign token  Not a shift-left token followed by an assign token  Parser At any point in the source file, parse the longest possible statement. Example: IF i = 3 THEN IF j = 2 THEN t := 500 ELSE f := -500

Computer Science Dept. Fall 2014: September 15 CS 153: Concepts of Compiler Design © R. Mak 8 Pascal Syntax Checker II: IF  Demo. java -classpath classes Pascal compile -i if.txt java -classpath classes Pascal compile -i iftest.txt

Computer Science Dept. Fall 2014: September 15 CS 153: Concepts of Compiler Design © R. Mak 9 CASE Statement  Example: Note that Pascal’s CASE statement does not use BREAK statements. CASE i+1 OF 1: j := i; 4: j := 4*i; 5, 2, 3: j := 523*i; END

Computer Science Dept. Fall 2014: September 15 CS 153: Concepts of Compiler Design © R. Mak 10 CASE Statement, cont’d  Example: CASE i+1 OF 1: j := i; 4: j := 4*i; 5, 2, 3: j := 523*i; END

Computer Science Dept. Fall 2014: September 15 CS 153: Concepts of Compiler Design © R. Mak 11 Pascal Syntax Checker II: CASE  Demo. java -classpath classes Pascal compile -i case.txt java -classpath classes Pascal compile -i caseerrors.txt

Computer Science Dept. Fall 2014: September 15 CS 153: Concepts of Compiler Design © R. Mak 12 Top Down Recursive Descent Parsing  The term is very descriptive of how the parser works.  Start by parsing the topmost source language construct. For now it’s a statement. Later, it will be the program.

Computer Science Dept. Fall 2014: September 15 CS 153: Concepts of Compiler Design © R. Mak 13 Top Down Recursive Descent Parsing  “Drill down” (descend) by parsing the sub-constructs. statement →assignment statement → expression →variable → etc.  Use recursion on the way down. statement → WHILE statement → statement → etc.

Computer Science Dept. Fall 2014: September 15 CS 153: Concepts of Compiler Design © R. Mak 14 Top Down Recursive Descent Parsing, cont’d  This is the technique for hand-coded parsers. Very easy to understand and write. The source language grammar is encoded in the structure of the parser code. Close correspondence between the parser code and the syntax diagrams.  Disadvantages Can be tedious coding. Ad hoc error handling. Big and slow!

Computer Science Dept. Fall 2014: September 15 CS 153: Concepts of Compiler Design © R. Mak 15 Top Down Recursive Descent Parsing, cont’d  Bottom-up parsers can be smaller and faster. Error handling can still be tricky. To be covered later this semester.

Computer Science Dept. Fall 2014: September 15 CS 153: Concepts of Compiler Design © R. Mak 16 Syntax and Semantics  Syntax refers to the “grammar rules” of a source language.  The rules prescribe the “proper form” of its programs.  Rules can be described by syntax diagrams.  Syntax checking: Does this sequence of tokens follow the syntax rules?

Computer Science Dept. Fall 2014: September 15 CS 153: Concepts of Compiler Design © R. Mak 17 Syntax and Semantics, cont’d  Semantics refers to the meaning of the token sequences according to the source language.  Example: Certain sequences of tokens constitute an IF statement according to the syntax rules.  The semantics of the statement determine how the statement will be executed by the interpreter, or what code will be generated for it by the compiler.

Computer Science Dept. Fall 2014: September 15 CS 153: Concepts of Compiler Design © R. Mak 18 Syntax and Semantics, cont’d  Semantic actions by the front end parser: Building symbol tables. Type checking (which we’ll do later). Building proper parse trees.  The parse trees encode type checking and operator precedence in their structures.  Semantic actions by the back end: Interpreter: The executor runs the program. Compiler: The code generator emits object code.

Computer Science Dept. Fall 2014: September 15 CS 153: Concepts of Compiler Design © R. Mak 19 Interpreter Design  Recall the design of our interpreter in the back end:

Computer Science Dept. Fall 2014: September 15 CS 153: Concepts of Compiler Design © R. Mak 20 Control Statement Executor Classes  New StatementExecutor subclasses: LoopExecutor IfExecutor SelectExecutor  The execute() method of each of these new subclasses executes the parse tree whose root node is passed to it. Each returns null. Only the execute() method of ExpressionExecutor returns a value.

Computer Science Dept. Fall 2014: September 15 CS 153: Concepts of Compiler Design © R. Mak 21 Executing a LOOP Parse Tree Pascal REPEAT loop Pascal WHILE loop Pascal FOR loop Note that we have the flexibility in the LOOP tree to have the TEST child be a middle child.

Computer Science Dept. Fall 2014: September 15 CS 153: Concepts of Compiler Design © R. Mak 22 Executing a LOOP Parse Tree, cont'd  Get all the children of the LOOP node.  Repeatedly execute all the child subtrees in order.  If a child is a TEST node, evaluate the node’s relational expression subtree. If the expression value is true, break out of the loop. If the expression value is false, continue executing the child statement subtrees.

Computer Science Dept. Fall 2014: September 15 CS 153: Concepts of Compiler Design © R. Mak 23 ArrayList loopChildren = node.getChildren(); ExpressionExecutor expressionExecutor = new ExpressionExecutor(this); StatementExecutor statementExecutor = new StatementExecutor(this); while (!exitLoop) { ++executionCount; // count the loop statement itself for (ICodeNode child : loopChildren) { ICodeNodeTypeImpl childType = (ICodeNodeTypeImpl) child.getType(); if (childType == TEST) { if (exprNode == null) { exprNode = child.getChildren().get(0); } exitLoop = (Boolean) expressionExecutor.execute(exprNode); } else { statementExecutor.execute(child); } if (exitLoop) break; } } Executing a LOOP Parse Tree, cont’d Keep looping until exitLoop becomes true. Execute all the subtrees. TEST node: Evaluate the boolean expression and set exitLoop to its value. Statement subtree: Execute it. Break out of the for loop if exitLoop is true.

Computer Science Dept. Fall 2014: September 15 CS 153: Concepts of Compiler Design © R. Mak 24 Simple Interpreter II: Loops  Demos java –classpath classes Pascal execute repeat.txt java –classpath classes Pascal execute while.txt java –classpath classes Pascal execute for.txt

Computer Science Dept. Fall 2014: September 15 CS 153: Concepts of Compiler Design © R. Mak 25 Executing an IF Parse Tree  Evaluate the first child’s expression subtree.  If the expression value is true... Execute the second child’s statement subtree.  If the expression value is false … If there is a third child statement subtree, then execute it. If there isn’t a third child subtree, then we’re done with this tree.

Computer Science Dept. Fall 2014: September 15 CS 153: Concepts of Compiler Design © R. Mak 26 public Object execute(ICodeNode node) { ArrayList children = node.getChildren(); ICodeNode exprNode = children.get(0); ICodeNode thenStmtNode = children.get(1); ICodeNode elseStmtNode = children.size() > 2 ? children.get(2) : null; ExpressionExecutor expressionExecutor = new ExpressionExecutor(this); StatementExecutor statementExecutor = new StatementExecutor(this); boolean b = (Boolean) expressionExecutor.execute(exprNode); if (b) { statementExecutor.execute(thenStmtNode); } else if (elseStmtNode != null) { statementExecutor.execute(elseStmtNode); } ++executionCount; // count the IF statement itself return null; } Executing an IF Parse Tree, cont'd Get the IF node’s two or three children. Execute the boolean expression to determine which statement subtree child to execute next.

Computer Science Dept. Fall 2014: September 15 CS 153: Concepts of Compiler Design © R. Mak 27 Simple Interpreter II: IF  Demo java –classpath classes Pascal execute if.txt

Computer Science Dept. Fall 2014: September 15 CS 153: Concepts of Compiler Design © R. Mak 28 Assignment #3  Modify the parser code from Chapter 6: Parse Pascal set expressions.  Modify the interpreter code from Chapter 8: Execute set expressions.

Computer Science Dept. Fall 2014: September 15 CS 153: Concepts of Compiler Design © R. Mak 29 Assignment #3, cont’d  What does the syntax diagram for set values look like?  Where does the set value diagram fit in with the other expression syntax diagrams?

Computer Science Dept. Fall 2014: September 15 CS 153: Concepts of Compiler Design © R. Mak 30 Assignment #3, cont’d  What kinds of parse trees should you design?  What trees should the parser build when it parses: [3, 1, 4, 2] [high, mid..47, 2*low] s2 := evens - teens + [high, mid..47, 2*low]

Computer Science Dept. Fall 2014: September 15 CS 153: Concepts of Compiler Design © R. Mak 31 Assignment #3, cont’d  How does the executor in the back end evaluate set expressions at run time?  What does the executor do when it’s passed the root of a set value parse tree?  What Java data structure does the executor use to represent a set value?  What does it enter into a set variable’s symbol table entry as the variable’s value?

Computer Science Dept. Fall 2014: September 15 CS 153: Concepts of Compiler Design © R. Mak 32 Assignment #3, cont’d  How does the executor evaluate set expressions? union, intersection, difference equality, inequality contains, is contained by is a member of

Computer Science Dept. Fall 2014: September 15 CS 153: Concepts of Compiler Design © R. Mak 33 Assignment #3, cont’d  The AssignmentExecutor sends a message each time its execute() method executes an assignment statement. source line number target variable name value  The message listener is the main Pascal class. Do you need to modify the listener to print set values?

Computer Science Dept. Fall 2014: September 15 CS 153: Concepts of Compiler Design © R. Mak 34 Assignment #3, cont’d  Tutorial on Pascal sets:  Due Wednesday, September 30 at 11:59 PM.