CS 432: Compiler Construction Lecture 7

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

CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Environments and Evaluation
9/27/2006Prof. Hilfinger, Lecture 141 Parsing Odds and Ends Lecture 14 (P. N. Hilfinger, plus slides adapted from R. Bodik)
ERROR HANDLING Lecture on 27/08/2013 PPT: 11CS10037 SAHIL ARORA.
Java Programming: From the Ground Up
CS 153: Concepts of Compiler Design September 2 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
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
Joey Paquet, 2000, Lecture 5 Error Recovery Techniques in Top-Down Predictive Syntactic Analysis.
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 5: Introduction to C: More Control Flow.
CS 153: Concepts of Compiler Design September 16 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 10 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
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
CS 46B: Introduction to Data Structures June 30 Class Meeting Department of Computer Science San Jose State University Summer 2015 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
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Syntax Analysis Or Parsing. A.K.A. Syntax Analysis –Recognize sentences in a language. –Discover the structure of a document/program. –Construct (implicitly.
CS 153: Concepts of Compiler Design September 28 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
Information and Computer Sciences University of Hawaii, Manoa
CS 153: Concepts of Compiler Design September 14 Class Meeting
COMP261 Lecture 18 Parsing 3 of 4.
CS 432: Compiler Construction Lecture 5
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 432: Compiler Construction Lecture 10
Loop Structures.
CS 153: Concepts of Compiler Design September 7 Class Meeting
CS 153: Concepts of Compiler Design October 3 Class Meeting
CMPE 152: Compiler Design December 5 Class Meeting
CiS 260: App Dev I Chapter 4: Control Structures II.
CMPE 152: Compiler Design September 4 Class Meeting
CMPE 152: Compiler Design September 6 Class Meeting
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 September 11/13 Lab
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
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 March 7 Class Meeting
The Recursive Descent Algorithm
CMPE 152: Compiler Design January 29 Class Meeting
CMPE 152: Compiler Design February 12 Class Meeting
CMPE 152: Compiler Design February 21/26 Lab
CMPE 152: Compiler Design February 21 Class Meeting
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
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 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 432: Compiler Construction Lecture 7 Department of Computer Science Salisbury University Fall 2017 Instructor: Dr. Sophie Wang http://faculty.salisbury.edu/~xswang 11/16/2018

Pascal Control Statements Looping statements REPEAT UNTIL WHILE DO FOR TO FOR DOWNTO Conditional statements IF THEN IF THEN ELSE CASE 11/16/2018

Statement Syntax Diagram 11/16/2018

Pascal Statement Parsers New statement parser subclasses. RepeatStatementParser WhileStatementParser ForStatementParser IfStatementParser CaseStatementParser Each parse() method builds a parse subtree and returns the root node. _ 11/16/2018

Pascal Control Statements Looping statements REPEAT UNTIL WHILE DO FOR TO FOR DOWNTO Conditional statements IF THEN IF THEN ELSE CASE 11/16/2018

REPEAT Statement Example: REPEAT j := i; k := i UNTIL i <= j Keep looping until the boolean expression becomes true. Execute the loop at least once. Use LOOP and TEST nodes for source language independence. Exit the loop when the test expression evaluates to true. 11/16/2018

Syntax Error Handling Recall that syntax error handling in the front end is a three-step process. Detect the error. Flag the error. Recover from the error. Good syntax error handling is important! 11/16/2018

Options for Error Recovery Stop after the first error. No error recovery at all. Easiest for the compiler writer, annoying for the programmer. Worse case: The compiler crashes or hangs. Become hopelessly lost. Attempt to continue parsing the rest of the source program. Spew out lots of irrelevant and meaningless error messages. No error recovery here, either … … but the compiler writer doesn’t admit it! Skip tokens after the erroneous token until … The parser finds a token it recognizes, and It can safely resume syntax checking the rest of the source program. _ 11/16/2018

Parser Synchronization Skipping tokens to reach a safe, recognizable place to resume parsing is known as synchronizing. “Resynchronize the parser” after an error. Good error recovery with top-down parsers is more art than science. How many tokens should the parser skip? Skipping too many (the rest of the program?) can be considered “panic mode” recovery. For this class, we’ll take a rather simplistic approach to synchronization. 11/16/2018

Method synchronize() The synchronize() method of class PascalParserTD. Pass it an enumeration set of “good” token types. The method skips tokens until it finds one that is in the set. public Token synchronize(EnumSet syncSet) throws Exception { Token token = currentToken(); if (!syncSet.contains(token.getType())) { errorHandler.flag(token, UNEXPECTED_TOKEN, this); do { token = nextToken(); } while (!(token instanceof EofToken) && !syncSet.contains(token.getType())); } return token; Flag the first bad token. Recover by skipping tokens not in the synchronization set. Resume parsing at this token! (It’s the first token after the error that is in the synchronization set. 11/16/2018

Pascal Syntax Checker II: REPEAT Demo. java -classpath classes Pascal compile -i repeat.txt java -classpath classes Pascal compile –i repeaterrors.txt 11/16/2018

WHILE Statement Example: WHILE i > j DO k := i Exit the loop when the test expression evaluates to false. How can we eliminate the NOT node? 11/16/2018

Class WhileStatementParser From parent class StatementParser: // Synchronization set for starting a statement. protected static final EnumSet<PascalTokenType> STMT_START_SET = EnumSet.of(BEGIN, CASE, FOR, PascalTokenType.IF, REPEAT, WHILE, IDENTIFIER, SEMICOLON); // Synchronization set for following a statement. protected static final EnumSet<PascalTokenType> STMT_FOLLOW_SET = EnumSet.of(SEMICOLON, END, ELSE, UNTIL, DOT); In class WhileStatementParser: DO_SET contains all the tokens that can start a statement or follow a statement, plus the DO token. // Synchronization set for DO. private static final EnumSet<PascalTokenType> DO_SET = StatementParser.STMT_START_SET.clone(); static { DO_SET.add(DO); DO_SET.addAll(StatementParser.STMT_FOLLOW_SET); } 11/16/2018

Class WhileStatementParser, cont’d public ICodeNode parse(Token token) throws Exception { token = nextToken(); // consume the WHILE ICodeNode loopNode = ICodeFactory.createICodeNode(LOOP); ICodeNode testNode = ICodeFactory.createICodeNode(TEST); ICodeNode notNode = ICodeFactory.createICodeNode(ICodeNodeTypeImpl.NOT); loopNode.addChild(testNode); testNode.addChild(notNode); ExpressionParser expressionParser = new ExpressionParser(this); notNode.addChild(expressionParser.parse(token)); token = synchronize(DO_SET); if (token.getType() == DO) { token = nextToken(); // consume the DO } else { errorHandler.flag(token, MISSING_DO, this); StatementParser statementParser = new StatementParser(this); loopNode.addChild(statementParser.parse(token)); return loopNode; We’re in this method because the parser has already seen WHILE. Synchronize the parser here! If the current token is not DO, then skip tokens until we find a token that is in DO_SET. 11/16/2018

Pascal Syntax Checker II: WHILE We can recover (better) from syntax errors. Demo. java -classpath classes Pascal compile -i while.txt java -classpath classes Pascal compile -i whileerrors.txt 11/16/2018

FOR Statement Example: FOR k := j TO 5 DO n := k Increment/decrement: Node type ADD for TO and SUBTRACT for DOWNTO. Initial assignment. Node type GT for TO and LT for DOWNTO. DO statement. 11/16/2018

Pascal Syntax Checker II: FOR Demo. java -classpath classes Pascal compile -i for.txt java -classpath classes Pascal compile -i forerrors.txt 11/16/2018

IF Statement Example: IF (i = j) THEN t := 200 ELSE f := -200; Third child only if there is an ELSE. 11/16/2018

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: IF i = 3 THEN IF j = 2 THEN t := 500 ELSE f := -500 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. 11/16/2018

Scanner and Parser Rules of Thumb 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 11/16/2018

Pascal Syntax Checker II: IF Demo. java -classpath classes Pascal compile -i if.txt java -classpath classes Pascal compile -i iftest.txt 11/16/2018

CASE Statement Example: CASE i+1 OF 1: j := i; 4: j := 4*i; 5, 2, 3: j := 523*i; END Note that Pascal’s CASE statement does not use BREAK statements. 11/16/2018

CASE Statement, cont’d Example: CASE i+1 OF 1: j := i; 4: j := 4*i; 5, 2, 3: j := 523*i; END 11/16/2018