CS 153: Concepts of Compiler Design December 5 Class Meeting

Slides:



Advertisements
Similar presentations
Bottom up Parsing Bottom up parsing trys to transform the input string into the start symbol. Moves through a sequence of sentential forms (sequence of.
Advertisements

CS 153: Concepts of Compiler Design August 25 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak
LEX and YACC work as a team
CS 153: Concepts of Compiler Design August 24 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
Chapter 10: Compilers and Language Translation Invitation to Computer Science, Java Version, Third Edition.
Compiler course 1. Introduction. Outline Scope of the course Disciplines involved in it Abstract view for a compiler Front-end and back-end tasks Modules.
Review 1.Lexical Analysis 2.Syntax Analysis 3.Semantic Analysis 4.Code Generation 5.Code Optimization.
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 August 26 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
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
1 Compiler Design (40-414)  Main Text Book: Compilers: Principles, Techniques & Tools, 2 nd ed., Aho, Lam, Sethi, and Ullman, 2007  Evaluation:  Midterm.
CS 153: Concepts of Compiler Design October 10 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
Compiler Principle and Technology Prof. Dongming LU Mar. 26th, 2014.
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 December 7 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
PL&C Lab, DongGuk University Compiler Lecture Note, MiscellaneousPage 1 Yet Another Compiler-Compiler Stephen C. Johnson July 31, 1978 YACC.
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
Overview of Compilation Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 2.
CS 154 Formal Languages and Computability March 22 Class Meeting Department of Computer Science San Jose State University Spring 2016 Instructor: Ron Mak.
CS 3304 Comparative Languages
CS 153: Concepts of Compiler Design September 14 Class Meeting
Intro to compilers Based on end of Ch. 1 and start of Ch. 2 of textbook, plus a few additional references.
Compiler Design (40-414) Main Text Book:
CS 3304 Comparative Languages
CS 326 Programming Languages, Concepts and Implementation
Programming Languages Translator
Chapter 2 :: Programming Language Syntax
CS 153: Concepts of Compiler Design October 17 Class Meeting
CS 153: Concepts of Compiler Design October 5 Class Meeting
Table-driven parsing Parsing performed by a finite state machine.
PROGRAMMING LANGUAGES
Context-free Languages
CS 153: Concepts of Compiler Design October 3 Class Meeting
Compiler Lecture 1 CS510.
CMPE 152: Compiler Design December 5 Class Meeting
CS416 Compiler Design lec00-outline September 19, 2018
CS 3304 Comparative Languages
Lexical and Syntax Analysis
Top-Down Parsing CS 671 January 29, 2008.
Introduction CI612 Compiler Design CI612 Compiler Design.
CPSC 388 – Compiler Design and Construction
CMPE 152: Compiler Design September 18 Class Meeting
CMPE 152: Compiler Design September 13 Class Meeting
CMPE 152: Compiler Design October 4 Class Meeting
CMPE 152: Compiler Design October 4 Class Meeting
CS 3304 Comparative Languages
CMPE 152: Compiler Design August 21/23 Lab
CS 3304 Comparative Languages
Subject: Language Processor
CMPE 152: Compiler Design December 6 Class Meeting
CS 153: Concepts of Compiler Design October 30 Class Meeting
CS 153: Concepts of Compiler Design November 6 Class Meeting
CS416 Compiler Design lec00-outline February 23, 2019
Chapter 2 :: Programming Language Syntax
Compiler Lecture Note, Miscellaneous
CMPE 152: Compiler Design March 7 Class Meeting
CMPE 152: Compiler Design April 9 Class Meeting
Chapter 2 :: Programming Language Syntax
CMPE 152: Compiler Design March 21 Class Meeting
Chapter 10: Compilers and Language Translation
Compiler Design Yacc Example "Yet Another Compiler Compiler"
LL and Recursive-Descent Parsing Hal Perkins Winter 2008
Lec00-outline May 18, 2019 Compiler Design CS416 Compiler Design.
CMPE 152: Compiler Design December 4 Class Meeting
CMPE 152: Compiler Design March 19 Class Meeting
COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 2, 09/04/2003 Prof. Roy Levow.
CMPE 152: Compiler Design September 17 Class Meeting
Parsing CSCI 432 Computer Science Theory
Presentation transcript:

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

Presentation Schedule Thursday, Dec. 7 Alex Kong No Name 1 No Name 3 No Name 4

Final Exam Tuesday, December 19 It will be similar to the midterm. 7:15-9:30 AM in MH 222 It will be similar to the midterm. Covers the entire semester. Emphasis on the second half.

Context-Free Grammars Every production rule has a single nonterminal for its left-hand side. Example: <simple expression> ::= <term> + <term> Whenever the parser matches the right-hand side of the rule, it can freely reduce it to the nonterminal symbol. Regardless of the context of where the match occurs. A language is context-free if it can be defined by a context-free grammar. Context-free grammars are a subset of context-sensitive grammars. CONTEXT SENSITIVE CONTEXT FREE REGULAR

Context-Sensitive Grammars Context-sensitive grammars are more powerful than context-free grammars. They can define more languages. Production rules can be of the form <A><B><C> ::= <A><b><C> The parser is allowed to reduce <b> to <B> only in the context of <A> and <C>.

Context-Sensitive Grammars: Example <A><B><C> ::= <A><b><C> We can attempt to capture the language rule: “An identifier must have been previously declared to be a variable before it can appear in an expression.” In an expression, the parser can reduce <identifier> to <variable> only in the context of a prior <variable declaration> for that identifier.

Context-Sensitive Grammars, cont’d Context-sensitive grammars are extremely unwieldy for writing compilers. Alternative: Use context-free grammars and rely on semantic actions such as building symbol tables to provide the context.

Top-Down Parsers The parser we hand-wrote for the Pascal interpreter and the parser that JavaCC generates are top-down. Start with the topmost nonterminal grammar symbol such as <PROGRAM> and work your way down recursively. Top-down recursive-descent parser Easy to understand and write, but are generally BIG and slow.

Top-Down Parsers, cont’d Write a parse method for a production (grammar) rule. Each parse method “expects” to see tokens from the source program that match its production rule. Example: IF ... THEN ... ELSE A parse method calls other parse methods that implement lower production rules. Parse methods consume tokens that match the production rules.

Top-Down Parsers, cont’d A parse is successful if it’s able to derive the input string (i.e., the source program) from the production rules. All the tokens match the production rules and are consumed.

Bottom-Up Parsers A popular type of bottom-up parser is the shift-reduce parser. A bottom-up parser starts with the input tokens from the source program. A shift-reduce parser uses a parse stack. The stack starts out empty. The parser shifts (pushes) each input token (terminal symbol) from the scanner onto the stack.

Bottom-Up Parsers, cont’d When what’s on top of the parse stack matches the longest right hand side of a production rule: The parser pops off the matching symbols and … … reduces (replaces) them with the nonterminal symbol at the left hand side of the matching rule. Example: <term> ::= <factor> * <factor> Pop off <factor> * <factor> and replace by <term>

Bottom-Up Parsers, cont’d Repeat until the parse stack is reduced to the topmost nonterminal symbol. Example: <PROGRAM> The parser accepts the input source as being syntactically correct. The parse was successful.

Example: Shift-Reduce Parsing Action Input Parse stack (top at right) Parse the expression a + b*c given the production rules: a + b*c shift + b*c a reduce + b*c <identifier> reduce <variable> + b*c reduce + b*c <factor> reduce + b*c <term> shift <expression> ::= <simple expression> <simple expression> ::= <term + <term> <term> ::= <factor> | <factor> * <factor> <factor> ::= <variable> <variable> ::= <identifier> <identifier> ::= a | b | c b*c <term> + shift *c <term> + b reduce *c <term> + <identifier> reduce *c <term> + <variable> reduce *c <term> + <factor> shift c <term> + <factor> * shift <term> + <factor> * c reduce <term> + <factor> * <identifier> reduce In this grammar, the topmost nonterminal symbol is <expression> <term> + <factor> * <variable> reduce <term> + <factor> * <factor> reduce <term> + <term> reduce <simple expression> reduce <expression> accept

Why Bottom-Up Parsing? The shift-reduce actions can be driven by a table. The table is based on the production rules. It is almost always generated by a compiler-compiler. Like a table-driven scanner, a table-driven parser can be very compact and extremely fast. However, for a significant grammar, the table can be nearly impossible for a human to follow.

Why Bottom-Up Parsing? Error recovery can be especially tricky. It can be very hard to debug the parser if something goes wrong. It’s usually an error in the grammar (of course!).

Lex and Yacc Lex and Yacc “Standard” compiler-compiler for Unix and Linux systems. Lex automatically generates a scanner written in C. Flex: free GNU version Yacc (“Yet another compiler-compiler”) automatically generates a parser written in C. Bison: free GNU version Generates a bottom-up shift-reduce parser.

Example: Simple Interpretive Calculator Yacc file (production rules): calc.y ... %token NUMBER %left '+' '-' /* left associative, same precedence */ %left '*' '/' /* left associative, higher precedence */ %% exprlist: /* empty list */ | exprlist '\n' | exprlist expr '\n' {printf("\t%lf\n", $2);} ; expr: NUMBER {$$ = $1;} | expr '+' expr {$$ = $1 + $3;} | expr '-' expr {$$ = $1 - $3;} | expr '*' expr {$$ = $1 * $3;} | expr '/' expr {$$ = $1 / $3;} | '(' expr ')' {$$ = $2;} We’ll need to define the NUMBER token. #include <stdio.h> #include <ctype.h> int main(int argc, char *argv[]) { progname = argv[0]; yyparse(); }

Example: Simple Calculator, cont’d Lex file (token definitions): calc.l %{ #include "calc.tab.h" extern lineno; %} %option noyywrap %% [ \t] {;} /* skip blanks and tabs */ [0-9]+\.?|[0-9]*\.[0-9]+ {sscanf(yytext, "%lf", &yylval); return NUMBER;} \n {lineno++; return '\n';} . {return yytext[0];} /* everything else */ Commands: yacc –d calc.y lex calc.l cc –c *.c cc –o calc *.o ./calc Demo

Course Review Lectures and PowerPoint slide sets Reading assignments Homework assignments Compiler project

Course Review Good understanding of compiler concepts Front end: parser, scanner, and tokens Intermediate tier: symbol table and parse trees Back end: interpreter and code generator The ANTLR 4 compiler-compiler Basic understanding of Pascal

Course Review What is the overall architecture of a compiler or an interpreter? What are the source language-independent and -dependent parts? What are the target machine-independent and -dependent parts? How can we manage the size and complexity of a compiler or an interpreter during its development?

Course Review What are the main characteristics of a top-down recursive-descent parser? Of a bottom-up parser? What is the basic control flow through an interpreter as a source program is read, translated, and executed? Through a compiler for code generation?

Course Review How do the various components work with each other? parser  scanner scanner  source program parser  symbol table parser  parse tree executor code generator symbol table parse tree 

Course Review What information is kept in a symbol table? When is a symbol table created? How is this information structured? How is this information accessed? What information is kept in a parse tree? When is a parse tree created?

Course Review What is the purpose of the symbol table stack runtime stack runtime display operand stack parse stack

Course Review Define or explain syntax and semantics syntax diagrams and BNF syntax error handling runtime error handling type checking

Course Review Deterministic finite automaton (DFA) start state accepting state transitions state transition table table-driven DFA scanner

Course Review What information is kept in an activation record or stack frame? How is this information initialized? What happens during a procedure or function call? How to pass parameters by value by reference ... with an interpreter vs. with generated object code.

Course Review The Java Virtual Machine (JVM) architecture Runtime stack Stack frame operand stack local variables array program counter

Course Review The Jasmin assembly language instructions explicit operands operands on the stack standard and “short cut” type descriptors

Course Review Jasmin assembler directives: .class .super .limit .field .var .method .line .end

Course Review Basic concepts of the ANTLR 4 compiler-compiler Tokens specification with regular expressions Production rules labelled alternates Tree node visitors Overriding visit methods.

Course Review Code generation and code templates expressions assignment statements conditional statements looping statements arrays and records

Course Review Compiling procedures and functions fields and local variables call and return passing parameters

Course Review Multipass compilers type checking pass with the visitor pattern optimization pass code generation pass with the visitor pattern

Course Review Integrating Jasmin routines with Java routines Pascal runtime library Instruction selection Instruction scheduling Register allocation spilling values live variables

Course Review Optimization for performance constant folding constant propagation strength reduction dead code elimination loop unrolling common subexpression elimination

Course Review Was this course “deep” enough?