Download presentation
Presentation is loading. Please wait.
Published byGlenna Tan Modified over 5 years ago
1
CMPE 152: Compiler Design September 19 Class Meeting
Department of Computer Engineering San Jose State University Fall 2019 Instructor: Ron Mak
2
Tutoring Hours for Compiler Design
Eddie Broeder is your instructional student assistant (ISA). He is a grad student who took compiler design from me last year, so he’s familiar with the material. He grades your assignments. He will also provide two hours/week of tutoring for compiler design students: Mondays and Wednesdays, 5:00 – 6:00 PM Room ENG 325
3
Chapter 8 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?
4
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 interpreter will execute the statement, or How the compiler will generate object code for the statement.
5
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.
6
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.
7
Executing a LOOP Parse Tree
Pascal REPEAT loop Pascal WHILE loop Note that we have the flexibility in the LOOP tree to have the TEST child be a middle child. Pascal FOR loop
8
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.
9
Executing a LOOP Parse Tree, cont’d
bool exit_loop = false; ICodeNode *expr_node = nullptr; vector<ICodeNode *> loop_children = node->get_children(); ExpressionExecutor expression_executor(this); StatementExecutor statement_executor(this); while (!exit_loop) { ++execution_count; // count the loop statement itself for (ICodeNode *child : loop_children) { ICodeNodeTypeImpl child_type = (ICodeNodeTypeImpl) child->get_type(); if (child_type == NT_TEST) { if (expr_node == nullptr) { expr_node = child->get_children()[0]; } Object data_value = expression_executor.execute(expr_node); exit_loop = cast(data_value, bool); } else statement_executor.execute(child); if (exit_loop) break; } } wci/backend/interpreter/executors/LoopExecutor.cpp Keep looping until exit_loop 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.
10
Simple Interpreter II: Loops
Demos ./Chapter8cpp Pascal execute repeat.txt ./Chapter8cpp Pascal execute while.txt ./Chapter8cpp Pascal execute for.txt
11
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.
12
Executing an IF Parse Tree, cont'd
wci/backend/interpreter/executors/IfExecutor.cpp Object IfExecutor::execute(ICodeNode *node) { vector<ICodeNode *> children = node->get_children(); ICodeNode *expr_node = children[0]; ICodeNode *then_stmt_node = children[1]; ICodeNode *else_stmt_node = children.size() > 2 ? children[2] : nullptr; ExpressionExecutor expression_executor(this); StatementExecutor statement_executor(this); Object data_value = expression_executor.execute(expr_node); if (cast(data_value, bool)) { statement_executor.execute(then_stmt_node); } else if (else_stmt_node != nullptr) statement_executor.execute(else_stmt_node); ++execution_count; // count the IF statement itself return Object(); // empty } Get the IF node’s two or three children. Execute the boolean expression to determine which statement subtree child to execute next.
13
Simple Interpreter II: IF
Demo ./Chapter8cpp Pascal execute if.txt
14
Scripting Engine We now have a simple scripting engine!
We can execute: Expressions Assignment statements Control statements Compound statements Variables that are untyped
15
What’s Next? Parse Pascal declarations Type checking
Parse procedure and function declarations Runtime memory management Interpret entire Pascal programs.
16
Chapter 9 Parsing Declarations The declarations of a programming language are often the most challenging to parse. Declarations syntax can be difficult. Declarations often include recursive definitions. You must keep of track of diverse information. Many new items to enter into the symbol table.
17
Pascal Declarations Classic Pascal declarations consist of 5 parts, each optional, but always in this order: Label declarations Constant definitions Type definitions Variable declarations Procedure and function declarations We will examine 2, 3, and 4 next. We’ll do procedures and functions in a couple of weeks.
18
Pascal Declarations The CONST, TYPE, and VAR parts are optional, but they must come in this order. Note that constants and types are defined, but variables are declared. Collectively, you refer to all of them as declarations.
19
Pascal Constant Definitions
Example constant definition part: CONST factor = 8; epsilon = 1.0e-6; ch = 'x'; limit = -epsilon; message = 'Press the OK button to confirm your selection.'; Classic Pascal only allows a constant value after the = sign. No constant expressions.
20
Pascal Type Definitions
A Pascal simple type can be: scalar (integer, real, boolean, char) enumeration subrange Not reserved words!
21
Pascal Simple Type Definitions
Examples of subrange and enumeration type definitions: CONST factor = 8; TYPE range1 = 0..factor; {subrange of integer (factor is constant)} range2 = 'a'..'q'; {subrange of char} range3 = range1; {type identifier} grades = (A, B, C, D, F); {enumeration} passing = A..D; {subrange of enumeration} week = (monday, tuesday, wednesday, thursday, friday, saturday, sunday); weekday = monday..friday; weekend = saturday..sunday;
22
Lab #4: Execute LOOP AGAIN
Verify that your parser from Assignment #3 works correctly. Does it build correct parse trees? Merge all your changes into the compiler code from Chapter 8. Execute the following Pascal+ statements that include a LOOP AGAIN statement that contains two WHEN statements. You should get the runtime message from each assignment statement.
23
Lab #4, cont’d Due Friday, September 27 at 11:59 PM. LoopTest2.txt
BEGIN i := 0; LOOP i := i + 1; WHEN i > 10 ==>; i2 := i*i; WHEN i2 > 50 ==>; AGAIN END Due Friday, September 27 at 11:59 PM.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.