Top Banner

Click here to load reader

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

Jan 04, 2016

ReportDownload

Documents

CS 153: Concepts of Compiler Design

CS 153: Concepts of Compiler DesignSeptember 16 Class MeetingDepartment of Computer ScienceSan Jose State University

Fall 2015Instructor: Ron Makwww.cs.sjsu.edu/~mak

1Computer Science Dept.Fall 2014: September 15CS 153: Concepts of Compiler Design R. Mak2FOR StatementExample:

Initial assignment.Node type GTfor TO and LTfor DOWNTO.DO statement.Increment/decrement:Node type ADD for TOand SUBTRACT forDOWNTO.FOR k := j TO 5 DO n := kComputer Science Dept.Fall 2014: September 15CS 153: Concepts of Compiler Design R. Mak3Pascal Syntax Checker II: FORDemo.

java -classpath classes Pascal compile -i for.txtjava -classpath classes Pascal compile -i forerrors.txtComputer Science Dept.Fall 2014: September 15CS 153: Concepts of Compiler Design R. Mak4IF Statement

Example:

Third child only ifthere is an ELSE.IF (i = j) THEN t := 200 ELSE f := -200;Computer Science Dept.Fall 2014: September 15CS 153: Concepts of Compiler Design R. Mak5The Dangling ELSEConsider:

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 15CS 153: Concepts of Compiler Design R. Mak6The Dangling ELSE, contdAccording 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 15CS 153: Concepts of Compiler Design R. Mak7Scanner and Parser Rules of ThumbScannerAt any point in the source file, extract the longest possible token.Example: