Syntactic Analysis Natawut Nupairoj, Ph.D. Department of Computer Engineering Chulalongkorn University.

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.
Mooly Sagiv and Roman Manevich School of Computer Science
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.
1 Chapter 5: Bottom-Up Parsing (Shift-Reduce). 2 - attempts to construct a parse tree for an input string beginning at the leaves (the bottom) and working.
Pertemuan 12, 13, 14 Bottom-Up Parsing
By Neng-Fa Zhou Syntax Analysis lexical analyzer syntax analyzer semantic analyzer source program tokens parse tree parser tree.
CH4.1 CSE244 Sections 4.5,4.6 Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Box U-155 Storrs,
Chapter 4-2 Chang Chi-Chung Bottom-Up Parsing LR methods (Left-to-right, Rightmost derivation)  LR(0), SLR, Canonical LR = LR(1), LALR Other.
Prof. Fateman CS 164 Lecture 91 Bottom-Up Parsing Lecture 9.
CS 330 Programming Languages 09 / 23 / 2008 Instructor: Michael Eckmann.
Table-driven parsing Parsing performed by a finite state machine. Parsing algorithm is language-independent. FSM driven by table (s) generated automatically.
1 CIS 461 Compiler Design & Construction Fall 2012 slides derived from Tevfik Bultan, Keith Cooper, and Linda Torczon Lecture-Module #12 Parsing 4.
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
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.
Joey Paquet, 2000, 2002, 2012, Lecture 6 Bottom-Up Parsing.
410/510 1 of 21 Week 2 – Lecture 1 Bottom Up (Shift reduce, LR parsing) SLR, LR(0) parsing SLR parsing table Compiler Construction.
Review: –How do we define a grammar (what are the components in a grammar)? –What is a context free grammar? –What is the language defined by a grammar?
4 4 (c) parsing. Parsing A grammar describes the strings of tokens that are syntactically legal in a PL A recogniser simply accepts or rejects strings.
Parsing Jaruloj Chongstitvatana Department of Mathematics and Computer Science Chulalongkorn University.
 an efficient Bottom-up parser for a large and useful class of context-free grammars.  the “ L ” stands for left-to-right scan of the input; the “ R.
1 Compiler Construction Syntax Analysis Top-down parsing.
CH4.1 CSE244 Sections 4.5,4.6 Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Box U-155 Storrs,
CMSC 331, Some material © 1998 by Addison Wesley Longman, Inc. 1 Chapter 4 Chapter 4 Bottom Up Parsing.
1 LR Parsers  The most powerful shift-reduce parsing (yet efficient) is: LR(k) parsing. LR(k) parsing. left to right right-most k lookhead scanning derivation.
Syntax and Semantics Structure of programming languages.
1 Bottom-Up Parsing  “Shift-Reduce” Parsing  Reduce a string to the start symbol of the grammar.  At every step a particular substring is matched (in.
4 4 (c) parsing. Parsing A grammar describes syntactically legal strings in a language A recogniser simply accepts or rejects strings A generator produces.
Chapter 5: Bottom-Up Parsing (Shift-Reduce)
COP4020 Programming Languages Parsing Prof. Xin Yuan.
111 Chapter 6 LR Parsing Techniques Prof Chung. 1.
1 Compiler Construction Syntax Analysis Top-down parsing.
1 Syntax Analysis Part II Chapter 4 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
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.
10/10/2002© 2002 Hal Perkins & UW CSED-1 CSE 582 – Compilers LR Parsing Hal Perkins Autumn 2002.
CS 330 Programming Languages 09 / 25 / 2007 Instructor: Michael Eckmann.
UMBC  CSEE   1 Chapter 4 Chapter 4 (b) parsing.
Bottom Up Parsing CS 671 January 31, CS 671 – Spring Where Are We? Finished Top-Down Parsing Starting Bottom-Up Parsing Lexical Analysis.
1 Syntax Analysis Part II Chapter 4 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007.
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.
1 Chapter 6 Bottom-Up Parsing. 2 Bottom-up Parsing A bottom-up parsing corresponds to the construction of a parse tree for an input tokens beginning at.
Chapter 8. LR Syntactic Analysis Sung-Dong Kim, Dept. of Computer Engineering, Hansung University.
Syntax and Semantics Structure of programming languages.
Programming Languages Translator
Bottom-up parsing Goal of parser : build a derivation
Compiler design Bottom-up parsing Concepts
lec04-bottomupparser June 6, 2018 Bottom-Up Parsing
Unit-3 Bottom-Up-Parsing.
UNIT - 3 SYNTAX ANALYSIS - II
CS 488 Spring 2012 Lecture 4 Bapa Rao Cal State L.A.
Parsing IV Bottom-up Parsing
Table-driven parsing Parsing performed by a finite state machine.
Fall Compiler Principles Lecture 4: Parsing part 3
Bottom-Up Syntax Analysis
Subject Name:COMPILER DESIGN Subject Code:10CS63
Top-Down Parsing CS 671 January 29, 2008.
4d Bottom Up Parsing.
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Compiler Design 7. Top-Down Table-Driven Parsing
Bottom Up Parsing.
Parsing IV Bottom-up Parsing
4d Bottom Up Parsing.
Kanat Bolazar February 16, 2010
4d Bottom Up Parsing.
4d Bottom Up Parsing.
4d Bottom Up Parsing.
Presentation transcript:

Syntactic Analysis Natawut Nupairoj, Ph.D. Department of Computer Engineering Chulalongkorn University

Outline Overview. Bottom-Up Parsing. LR Parsing. Examples.

Front-End Components Scanner Source program (text stream) Parser Intermediate Representation (file or in memory) Semantic Analyzer Front-End Construct parse tree. Group token. next-token token Symbol Table main(){ Check semantic/contextual. identifier main symbol ( parse-tree

Parsing Techniques Top-down parsing  LL(1) grammars  Left-to-right scanning, Leftmost derivation, 1 symbol lookahead Bottom-up parsing  LR(k) grammars  Left-to-right scanning, Rightmost derivation, k symbols lookahead

Top-Down Parsing Recursive Decent Parser  simple top-down parser with Backtracking Predictive Parser  non-backtracking  use FIRST and FOLLOW sets

Basic Terminologies Sentential Form. Sentential form (left/right)

Example: Left Sentential Form (1) E ฎ E + T (2) E ฎ T (3) T ฎ T * F (4) T ฎ F (5) F ฎ (E) (6) F ฎ id m + n * k id + id * id E => E + T => T + T => id + T => id + T * F => id + F * F => id + id * F => id + id * id

Example: Right Sentential Form (1) E ฎ E + T (2) E ฎ T (3) T ฎ T * F (4) T ฎ F (5) F ฎ (E) (6) F ฎ id E => E + T => E + T * F => E + T * id => E + id * id => T + id * id => F + id * id => id + id * id Rightmost derivation is also called “canonical derivation”.

Bottom-Up Parsing Starting from the bottom of the parse tree and reduce all terminals until getting only one starting symbol. Characteristics  Rightmost derivation in reverse.  Find the “handle” and reduce.

Rightmost Derivation in Reverse (1) E ฎ E + T (2) E ฎ T (3) T ฎ T * F (4) T ฎ F (5) F ฎ (E) (6) F ฎ id E => E + T => E + T * F => E + T * id => E + id * id => T + id * id => F + id * id => id + id * id During parsing: id + id * idF + id * idT + id * id

Basic Terminologies Handle  A substring that matches the right side of a production.  Whose reduction (with that production) will eventually lead to the starting symbol.

Example: Handle (1) E ฎ E + T (2) E ฎ T (3) T ฎ T * F (4) T ฎ F (5) F ฎ (E) (6) F ฎ id E => E + T => E + T * F => E + T * id => E + id * id => T + id * id => F + id * id => id + id * id Note: for right-sentential form, the string on the right of a handle contains only terminals. Not a handle Handle

Shift-Reduce Parsing shift input string on to the stack. reduce the handle on the stack to a non- terminal. try to reduce input to the starting variable.

Model of Shift-Reduce Parsing Stack + input = current right-sentential form. Locate the handle during the parsing:  shift zero or more input onto the stack until a handle is  on top of the stack. Replace the handle with a proper non- terminal (Handle Pruning):  reduce  to A where A ฎ 

Example $id + id * id$ $F + id * id$ $T + id * id$ $E + id * id$ $E + F * id$ $E + T * id$ $E + T * F $ $E + T $ $E $ Shift Reduce (F->id) Reduce (T->F) Reduce (E->T) Shift Reduce (F->id) Reduce (T->F) Shift Reduce (F->id) Reduce (T->T*F) Reduce (E->E+T) Accept

LR Parsing Algorithms Use grammar to construct a parsing table. Three techniques:  Simple LR (SLR)  Canonical LR (LR)  Look Ahead LR (LALR) Same algorithm but different ways to construct a parsing table.

Model of an LR Parser

LR Parsing Tables Two tables: action and goto. action[s m, a i ]  shift s  reduce A ฎ    accept  error goto[s m, X i ] = target state (If action[s m, a i ] = shift s, goto[s m, a i ] = s)

Example: Action Table Example:  Input = id, state = 0  Next action = Shift  Input = +, state = 3  Next action = Reduce  Input = $, state = 1  Next action = Accept  Input = id, state = 1  Next action = Error Stateid+ … $ 0s 1sa 2rr 3rr...

Configuration Stack contents and unread input: (s 0 X 1 s 1 X 2 s 2 … X m s m, a i a i+1 … a n $) This represents right-sentential form: X 1 X 2 … X m a i a i+1 … a n

LR Parser Movements If action[s m, a i ] = shift s, shift move: (s 0 X 1 s 1 X 2 s 2 … X m s m, a i a i+1 … a n $) (s 0 X 1 s 1 X 2 s 2 … X m s m a i s, a i+1 … a n $)

LR Parser Movements If action[s m, a i ] = reduce A ฎ , reduce move: (s 0 X 1 s 1 X 2 s 2 … X m s m, a i a i+1 … a n $) (s 0 X 1 s 1 X 2 s 2 … X m-r s m-r A s, a i a i+1 … a n $) s = goto[s m-r, A]r = |  |

LR Parser Movements If action[s m, a i ] = accept, done. If action[s m, a i ] = error, error.

Example (1) E ฎ E + T (2) E ฎ T (3) T ฎ T * F (4) T ฎ F (5) F ฎ (E) (6) F ฎ id

Stateid+*()$ETF 0s5s4123 1s6acc 2r2s7r2 3r4 4s5s4823 5r6 6s5s493 7s5s410 8s6s11 9r1s7r1 10r3 11r5

Conflicts Parser cannot decide:  shift/reduce conflict can either shift or reduce.  reduce/reduce conflict more than one production is eligible. Usually ambiguous or non-LR grammars.

Example: Shift/Reduce Conflict stmt ฎ  if expr then stmt | if expr then stmt else stmt |... STACKINPUT $ … if expr then stmtelse … $

Example: Reduce/Reduce Conflict E ฎ  T | F T ฎ  id F ฎ  id STACKINPUT $ id… $