Review 1.Lexical Analysis 2.Syntax Analysis 3.Semantic Analysis 4.Code Generation 5.Code Optimization.

Slides:



Advertisements
Similar presentations
A question from last class: construct the predictive parsing table for this grammar: S->i E t S e S | i E t S | a E -> B.
Advertisements

Lesson 8 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
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.
C O N T E X T - F R E E LANGUAGES ( use a grammar to describe a language) 1.
Pushdown Automata Consists of –Pushdown stack (can have terminals and nonterminals) –Finite state automaton control Can do one of three actions (based.
Lexical and Syntactic Analysis Here, we look at two of the tasks involved in the compilation process –Given source code, we need to first break it into.
Predictive Parsing l Find derivation for an input string, l Build a abstract syntax tree (AST) –a representation of the parsed program l Build a symbol.
9/27/2006Prof. Hilfinger, Lecture 141 Syntax-Directed Translation Lecture 14 (adapted from slides by R. Bodik)
6/12/2015Prof. Hilfinger CS164 Lecture 111 Bottom-Up Parsing Lecture (From slides by G. Necula & R. Bodik)
Bottom-Up Syntax Analysis Mooly Sagiv html:// Textbook:Modern Compiler Design Chapter
CS Summer 2005 Top-down and Bottom-up Parsing - a whirlwind tour June 20, 2005 Slide acknowledgment: Radu Rugina, CS 412.
Formal Aspects Term 2, Week4 LECTURE: LR “Shift-Reduce” Parsers: The JavaCup Parser-Generator CREATES LR “Shift-Reduce” Parsers, they are very commonly.
ISBN Chapter 4 Lexical and Syntax Analysis The Parsing Problem Recursive-Descent Parsing.
Parsing V Introduction to LR(1) Parsers. from Cooper & Torczon2 LR(1) Parsers LR(1) parsers are table-driven, shift-reduce parsers that use a limited.
Prof. Fateman CS 164 Lecture 91 Bottom-Up Parsing Lecture 9.
CS 330 Programming Languages 09 / 23 / 2008 Instructor: Michael Eckmann.
Bottom-Up Syntax Analysis Mooly Sagiv html:// Textbook:Modern Compiler Implementation in C Chapter 3.
LR(k) Grammar David Rodriguez-Velazquez CS6800-Summer I, 2009 Dr. Elise De Doncker.
1 Bottom-up parsing Goal of parser : build a derivation –top-down parser : build a derivation by working from the start symbol towards the input. builds.
Bottom-up parsing Goal of parser : build a derivation
Lexical and syntax analysis
CSC3315 (Spring 2009)1 CSC 3315 Lexical and Syntax Analysis Hamid Harroud School of Science and Engineering, Akhawayn University
Parsing IV Bottom-up Parsing Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University.
Syntax and Semantics Structure of programming languages.
Parsing. Goals of Parsing Check the input for syntactic accuracy Return appropriate error messages Recover if possible Produce, or at least traverse,
1 Natural Language Processing Lecture 11 Efficient Parsing Reading: James Allen NLU (Chapter 6)
10/13/2015IT 3271 Tow kinds of predictive parsers: Bottom-Up: The syntax tree is built up from the leaves Example: LR(1) parser Top-Down The syntax tree.
Parsing Jaruloj Chongstitvatana Department of Mathematics and Computer Science Chulalongkorn University.
LANGUAGE TRANSLATORS: WEEK 17 scom.hud.ac.uk/scomtlm/cis2380/ See Appel’s book chapter 3 for support reading Last Week: Top-down, Table driven parsers.
1 Compiler Construction Syntax Analysis Top-down parsing.
Syntactic Analysis Natawut Nupairoj, Ph.D. Department of Computer Engineering Chulalongkorn University.
Syntax and Semantics Structure of programming languages.
1Computer Sciences Department. Book: INTRODUCTION TO THE THEORY OF COMPUTATION, SECOND EDITION, by: MICHAEL SIPSER Reference 3Computer Sciences Department.
Bottom-Up Parsing David Woolbright. The Parsing Problem Produce a parse tree starting at the leaves The order will be that of a rightmost derivation The.
Top-Down Parsing CS 671 January 29, CS 671 – Spring Where Are We? Source code: if (b==0) a = “Hi”; Token Stream: if (b == 0) a = “Hi”; Abstract.
Top-down Parsing. 2 Parsing Techniques Top-down parsers (LL(1), recursive descent) Start at the root of the parse tree and grow toward leaves Pick a production.
Top-Down Parsing.
GRAMMARS & PARSING. Parser Construction Most of the work involved in constructing a parser is carried out automatically by a program, referred to as a.
CS 330 Programming Languages 09 / 25 / 2007 Instructor: Michael Eckmann.
Bernd Fischer RW713: Compiler and Software Language Engineering.
Bottom Up Parsing CS 671 January 31, CS 671 – Spring Where Are We? Finished Top-Down Parsing Starting Bottom-Up Parsing Lexical Analysis.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 6: LR grammars and automatic parser generators.
Lecture 5: LR Parsing CS 540 George Mason University.
Compilers: Bottom-up/6 1 Compiler Structures Objective – –describe bottom-up (LR) parsing using shift- reduce and parse tables – –explain how LR.
CMSC 330: Organization of Programming Languages Pushdown Automata Parsing.
Eliminating Left-Recursion Where some of a nonterminal’s productions are left-recursive, top-down parsing is not possible “Immediate” left-recursion can.
COMPILER CONSTRUCTION
Syntax and Semantics Structure of programming languages.
Parsing Bottom Up CMPS 450 J. Moloney CMPS 450.
Programming Languages Translator
Bottom-up parsing Goal of parser : build a derivation
Compiler design Bottom-up parsing Concepts
Bottom-Up Parsing.
Compiler Baojian Hua LR Parsing Compiler Baojian Hua
Unit-3 Bottom-Up-Parsing.
UNIT - 3 SYNTAX ANALYSIS - II
Parsing IV Bottom-up Parsing
Chapter 4 Syntax Analysis.
Bottom-Up Syntax Analysis
4 (c) parsing.
Subject Name:COMPILER DESIGN Subject Code:10CS63
Lexical and Syntax Analysis
Top-Down Parsing CS 671 January 29, 2008.
4d Bottom Up Parsing.
Parsing #2 Leonidas Fegaras.
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Compiler Design 7. Top-Down Table-Driven Parsing
Parsing #2 Leonidas Fegaras.
Kanat Bolazar February 16, 2010
Chap. 3 BOTTOM-UP PARSING
Presentation transcript:

Review 1.Lexical Analysis 2.Syntax Analysis 3.Semantic Analysis 4.Code Generation 5.Code Optimization

Syntax Analysis Often called parsing Groups tokens of source program into grammatical phrases that are used by the compiler to check for correct syntax and to help in generating code Creation of a hierarchical structure called a syntax tree –Tree helps us determine if program is syntactically correct –Also aids in the translation of source program to target language

Grammar Example San FranciscoSeattle  Lexical  Ogay orthnay eightay undrendhay ilesmay  Syntactical  Miles go hundred north eight  Logical  Go eight hundred miles (facing south)  Run-Time  Go eight hundred miles (facing West)

Grammars – Defining the Language Rules Terminals (tokens) Non-terminals - Syntactic variable. Contains groups of tokens that define some part of the language –Example: expression, if statement, etc Start symbol - A special non-terminal (i.e. a program) Productions –The manner in which terminals and non-terminals are combined to form statements –A non-terminal in LHS and a string of terminals and non- terminals in RHS

Example Variable Declaration –A type followed by one or more comma separated identifiers that end with a semi-colon. -> ->, and are non-terminals The comma is a terminal The two lines are called productions

Grammars We will be defining a grammar for the entire JO99 programming language. We will then have JCUP produce for us a parser that detects if the JO99 program is syntactically correct. In addition, the parser will create for us a tree that represents the program and allows us to be able to do other things like semantic checks and code generation.

Example Simple arithmetic expressions with + and * – – * 45.3 –( ) * (6.035 * -( )) Terminals (or tokens) –num for all the numbers –‘+’, ‘-’, ‘*’, ‘(‘, ‘)’ What is the grammar for all possible expressions?

Example   ( )  -  num  +  *

Categories of Parsers ()

–L - parse from left to right –R - parse from right to left ()

Categories of Parsers –L - leftmost derivation –R - rightmost derivation ()

Categories of Parsers –Number of look ahead characters ()

Categories of Parsers –Examples: LL(0) – Parse Left to Right, Derive the tree using a leftmost derivation (top down), no look ahead characters LR(1) – Parse Left to Right, Derive the tree using a rightmost derivation (bottom up), 1 look ahead character. –Each category of parsing handles a different type of language. –We will be learning about LR(k) parsers and will implement an LR(k) parser.

Why LR(k)? Virtually all programming language grammars can be parsed using a LR(k) technique Most general parsing method for programming grammars Can build a very efficient parser engine given just the syntax rules of the language. Can detect a syntactic error as soon as it is possible to do so Because its so general, programs have been written (JCUP) that produce the parser instead of writing it from scratch.

LR(k) Parser implementation Sometimes called a Shift-Reduce Parser Parse from left to right (get the tokens from left to right) Bottom up parsing (same as rightmost derivation)

Actions of a Shift-Reduce Parser Parse Tree

Actions of a Shift-Reduce Parser Parse Tree Parse Tree

Actions of a Shift-Reduce Parser Parse Tree Parse Tree

Actions of a Shift-Reduce Parser Parse Tree Parse Tree

Actions of a Shift-Reduce Parser Parse Tree Parse Tree

Actions of a Shift-Reduce Parser Parse Tree Parse Tree

Actions of a Shift-Reduce Parser Parse Tree Parse Tree

Actions of a Shift-Reduce Parser Parse Tree Parse Tree

Actions of a Shift-Reduce Parser Parse Tree

Actions of a Shift-Reduce Parser Parse Tree

Actions of a Shift-Reduce Parser Parse Tree

Actions of a Shift-Reduce Parser Parse Tree

Actions of a Shift-Reduce Parser How do we build this tree? As productions are recognized, a portion of the tree is created. This portion of the tree will be needed later to build bigger portions of the tree and therefore must be saved for future use. This requires a stack. The stack plus the next token read from the source program determines the action.

Actions of a Shift-Reduce Parser Stack Current Symbol stack Parser Action Parser Engine

Actions of a Shift-Reduce Parser Shift –Shift the current element into top of the stack –Move the current pointer (next token) Reduce –Apply a production (we recognize a part of the program) –Top of the stack should match the RHS of the grammar –Remove those symbols from the stack –Add the LHS non-terminal Accept –End of stream reached and stack only has the start symbol Reject –End of stream reached but stack has more than the start symbol

Shift-Reduce Parser Example *(+num)

Shift-Reduce Parser Example *(+num)   ( )  -  num  +  -  *

Shift-Reduce Parser Example *(+num)   ( )  -  num  +  -  *

Shift-Reduce Parser Example *(+num)   ( )  -  num  +  -  * num SHIFT

Shift-Reduce Parser Example *(+num)   ( )  -  num  +  -  * num REDUCE

Shift-Reduce Parser Example *(+num) num   ( )  -  num  +  -  * REDUCE

Shift-Reduce Parser Example (+num) num*   ( )  -  num  +  -  * * SHIFT

Shift-Reduce Parser Example (+num) num*   ( )  -  num  +  -  * * REDUCE

Shift-Reduce Parser Example (+num) num*   ( )  -  num  +  -  * REDUCE

Shift-Reduce Parser Example +num) num*(   ( )  -  num  +  -  * ( SHIFT

Shift-Reduce Parser Example *+num) num(   ( )  -  num  +  -  * ( num SHIFT

Shift-Reduce Parser Example *+num) num(   ( )  -  num  +  -  * ( num REDUCE

Shift-Reduce Parser Example *+num) num(   ( )  -  num  +  -  * ( REDUCE

Shift-Reduce Parser Example *num) num( +   ( )  -  num  +  -  * ( + SHIFT

Shift-Reduce Parser Example *num) num( +   ( )  -  num  +  -  * ( + REDUCE

Shift-Reduce Parser Example *num) num( +   ( )  -  num  +  -  * ( REDUCE

Shift-Reduce Parser Example *) num( +   ( )  -  num  +  -  * ( num SHIFT

Shift-Reduce Parser Example *) num( +   ( )  -  num  +  -  * ( num REDUCE

Shift-Reduce Parser Example *) num( +   ( )  -  num  +  -  * ( REDUCE

Shift-Reduce Parser Example *) num( +   ( )  -  num  +  -  * ( REDUCE

Shift-Reduce Parser Example *) num( +   ( )  -  num  +  -  * ( REDUCE

Shift-Reduce Parser Example *) num( +   ( )  -  num  +  -  * ( ) SHIFT

Shift-Reduce Parser Example *) num( +   ( )  -  num  +  -  * ( ) REDUCE

Shift-Reduce Parser Example *) num( +   ( )  -  num  +  -  * REDUCE

Shift-Reduce Parser Example *) num( + REDUCE   ( )  -  num  +  -  *

Shift-Reduce Parser Example *) num( + REDUCE   ( )  -  num  +  -  *

Shift-Reduce Parser Example *) num( + ACCEPT   ( )  -  num  +  -  *

What does the parser engine do? If the top symbols of the stack match the RHS of a production then do the reduction –Pop the RHS from the top of the stack –Push the LHS symbol onto the stack If no production is found do the shift – Push the current input into the stack If the input is empty –Accept if only the start symbol is on the stack –Reject otherwise Parser Engine

This is not that simple! Many choices of reductions if there are multiple RHS. Which LHS do we put on stack in its place? Choice between shift and reduce –Stack matches a RHS –But that may not be the right match –May need to shift an input onto stack and later find a different reduction

Shift-Reduce Parser Example   ( )  -  num  +  -  * Change in the Grammar

Shift-Reduce Parser Example   ( )  -  num  +  -  * Change in the Grammar

Shift-Reduce Parser Example   ( )  -  num  +  -  * Change in the Grammar

Shift-Reduce Parser Example   ( )  -  num  +  -  * Change in the Grammar

Shift-Reduce Parser Example -num   ( )  -  num  +  -  *

Shift-Reduce Parser Example -num   ( )  -  num  +  -  * num

Shift-Reduce Parser Example -num   ( )  -  num  +  -  * num SHIFT

num Shift-Reduce Parser Example -num   ( )  -  num  +  -  * num REDUCE

Shift-Reduce Parser Example -num   ( )  -  num  +  -  * num REDUCE

- Shift-Reduce Parser Example num   ( )  -  num  +  -  * num - SHIFT

- Shift-Reduce Parser Example num   ( )  -  num  +  -  * num - We have a choice!!! REDUCE

- Shift-Reduce Parser Example num   ( )  -  num  +  -  * num - But not the right thing to do!! REDUCE

Shift-Reduce Parser Example num   ( )  -  num  +  -  * num - REDUCE But not the right thing to do!!

num Shift-Reduce Parser Example num   ( )  -  num  +  -  * num - But not the right thing to do!! SHIFT

num Shift-Reduce Parser Example num   ( )  -  num  +  -  * num - But not the right thing to do!! REDUCE

Shift-Reduce Parser Example num   ( )  -  num  +  -  * num - But not the right thing to do!! REDUCE

Shift-Reduce Parser Example num   ( )  -  num  +  -  * num - But not the right thing to do!! No more actions!!! ERROR

Shift-Reduce Parser Example But this is perfectly valid input for the grammar We chose the wrong production and thus the wrong LHS Lets see what happens when we choose the right production and the right LHS

- Shift-Reduce Parser Example num   ( )  -  num  +  -  * num - We have a choice REDUCE The step before we went wrong

- Shift-Reduce Parser Example num   ( )  -  num  +  -  * num - Use the other production REDUCE

Shift-Reduce Parser Example num   ( )  -  num  +  -  * num - REDUCE Use the other production

num Shift-Reduce Parser Example num   ( )  -  num  +  -  * num - SHIFT

num Shift-Reduce Parser Example num   ( )  -  num  +  -  * num - REDUCE

Shift-Reduce Parser Example num   ( )  -  num  +  -  * num - REDUCE

Shift-Reduce Parser Example num   ( )  -  num  +  -  * num - REDUCE

Shift-Reduce Parser Example num   ( )  -  num  +  -  * num - ACCEPT

Shift-Reduce Parser Parser Engine is far more complicated that it appears. Requires it to know all possible productions that would match the top of the stack and the given input symbol.

Constructing a LR(k) Parser What is in the parse engine –decide between shift and reduce –decide on the right reduction

Constructing a LR(k) Parser Create a DFA –Encodes all the possible states that the parser can be in –DFA state transition occurs on terminals and non- terminals Create a Parser Table – From the DFA create a transition table that stores what action should be taken for the current state and current input character Maintain a stack of states in parallel with the stack of symbols

LR(k) Parser Engine Current Symbol Parser Action LR(k) Parser Engine Symbol Stack State Stack

Parser Tables Look-up [top of state stack] [ input symbol] in the parser table Carry-out the described action

Parser Tables Shift to sn –Push input token into the symbol stack –Push sn into state stack –Advance to next input symbol

Parser Tables Reduce (n) –Pop both stacks as many times as the number of symbols on the RHS of rule n –Push LHS of rule n into symbol stack –Lookup [top of the state stack][top of symbol stack] –Push that state (in goto k) into state stack

Parser Tables Accept –Stop parsing and report success

Parser Tables Error –Stop parsing and report failure

LR example The grammar  $(1)  ( )(2)  ( )(3)

Question The grammar  $(1)  ( )(2)  ( )(3)

Parser Table in Action The grammar  $(1)  ( )(2)  ( )(3)

Parser Table in Action Parser Table The grammar  $(1)  ( )(2)  ( )(3)

Parser Table in Action Parser Table The grammar  $(1)  ( )(2)  ( )(3) $

Parser Table in Action Parser Table The grammar  $(1)  ( )(2)  ( )(3) s0 $

Parser Table in Action Parser Table The grammar  $(1)  ( )(2)  ( )(3) s0 $ )(()$

Parser Table in Action Parser Table The grammar  $(1)  ( )(2)  ( )(3) s0 $ )()($

Parser Table in Action Parser Table The grammar  $(1)  ( )(2)  ( )(3) s0 $ )()($

Parser Table in Action Parser Table The grammar  $(1)  ( )(2)  ( )(3) s0 $ )()($

( s2 ( Parser Table in Action Parser Table The grammar  $(1)  ( )(2)  ( )(3) s0 $ ))($

s2 ( Parser Table in Action Parser Table The grammar  $(1)  ( )(2)  ( )(3) s0 $ ))(($

s2 ( Parser Table in Action Parser Table The grammar  $(1)  ( )(2)  ( )(3) s0 $ ))(($

s2 ( Parser Table in Action Parser Table The grammar  $(1)  ( )(2)  ( )(3) s0 $ ))(($

s2 ( ( Parser Table in Action Parser Table The grammar  $(1)  ( )(2)  ( )(3) s0 $ ))(($

s2 ( ( Parser Table in Action Parser Table The grammar  $(1)  ( )(2)  ( )(3) s0 $ )(()$

s2 ( ( Parser Table in Action Parser Table The grammar  $(1)  ( )(2)  ( )(3) s0 $ )(()$

s2 ( ( Parser Table in Action Parser Table The grammar  $(1)  ( )(2)  ( )(3) s0 $ )(()$

s5 ) s2 ( ( Parser Table in Action Parser Table The grammar  $(1)  ( )(2)  ( )(3) s0 $ )(()$

s5 ) s2 ( ( Parser Table in Action Parser Table The grammar  $(1)  ( )(2)  ( )(3) s0 $ )(()$

s5 ) s2 ( ( Parser Table in Action Parser Table The grammar  $(1)  ( )(2)  ( )(3) s0 $ )(()$

s5 ) s2 ( ( Parser Table in Action Parser Table The grammar  $(1)  ( )(2)  ( )(3) s0 $ )(()$

s5 ) s2 ( ( Parser Table in Action Parser Table The grammar  $(1)  ( )(2)  ( )(3) s0 $ )(()$

s5 ) s2 ( ( Parser Table in Action Parser Table The grammar  $(1)  ( )(2)  ( )(3) s0 $ )(() s5 ) s2 ( $

X ( Parser Table in Action Parser Table The grammar  $(1)  ( )(2)  ( )(3) s0 $ )(()$

X s2 ( Parser Table in Action Parser Table The grammar  $(1)  ( )(2)  ( )(3) s0 $ )(()$

X s2 ( Parser Table in Action Parser Table The grammar  $(1)  ( )(2)  ( )(3) s0 $ )(()$

s3 X s2 ( Parser Table in Action Parser Table The grammar  $(1)  ( )(2)  ( )(3) s0 $ )(()$

s3 X s2 ( Parser Table in Action Parser Table The grammar  $(1)  ( )(2)  ( )(3) s0 $ )(()$

s3 X s2 ( Parser Table in Action Parser Table The grammar  $(1)  ( )(2)  ( )(3) s0 $ )(()$

s3 X s2 ( Parser Table in Action Parser Table The grammar  $(1)  ( )(2)  ( )(3) s0 $ )(()$

s4 ) s3 X s2 ( Parser Table in Action Parser Table The grammar  $(1)  ( )(2)  ( )(3) s0 $ )(()$

s4 ) s3 X s2 ( Parser Table in Action Parser Table The grammar  $(1)  ( )(2)  ( )(3) s0 $ )(()$

s4 ) s3 X s2 ( Parser Table in Action Parser Table The grammar  $(1)  ( )(2)  ( )(3) s0 $ )(()$

s4 ) s3 X s2 ( Parser Table in Action Parser Table The grammar  $(1)  ( )(2)  ( )(3) s0 $ )(()$

s4 ) s3 X s2 ( Parser Table in Action Parser Table The grammar  $(1)  ( )(2)  ( )(3) s0 $ )(()$ s4 ) s3 X s2 (

X Parser Table in Action Parser Table The grammar  $(1)  ( )(2)  ( )(3) s0 $ )(()$

X Parser Table in Action Parser Table The grammar  $(1)  ( )(2)  ( )(3) s0 $ )(()$

X Parser Table in Action Parser Table The grammar  $(1)  ( )(2)  ( )(3) s0 $ )(()$

s1 X Parser Table in Action Parser Table The grammar  $(1)  ( )(2)  ( )(3) s0 $ )(()$

s1 X Parser Table in Action Parser Table The grammar  $(1)  ( )(2)  ( )(3) s0 $ )(()$

s1 X Parser Table in Action Parser Table The grammar  $(1)  ( )(2)  ( )(3) s0 $ )(()$

s1 X Parser Table in Action The grammar  $(1)  ( )(2)  ( )(3) s0 $ )(()$ Accept

Parser Table The table (DFA) and the stacks are called Push Down Automaton (PDA). There is an algorithm for converting a grammar to a PDA. Parser generators – given a grammar, produce a parser table with a stack (PDA) We will not study how this is done because its somewhat complicated. JCUP will convert our grammar into a shift-reduce parser.