Parsing Discrete Mathematics and Its Applications Baojian Hua

Slides:



Advertisements
Similar presentations
Parsing 4 Dr William Harrison Fall 2008
Advertisements

Translator Architecture Code Generator ParserTokenizer string of characters (source code) string of tokens abstract program string of integers (object.
Mooly Sagiv and Roman Manevich School of Computer Science
By Neng-Fa Zhou Syntax Analysis lexical analyzer syntax analyzer semantic analyzer source program tokens parse tree parser tree.
Chapter 4 Lexical and Syntax Analysis Sections 1-4.
Abstract Syntax Trees Compiler Baojian Hua
ISBN Chapter 4 Lexical and Syntax Analysis.
Parsing Compiler Baojian Hua Front End source code abstract syntax tree lexical analyzer parser tokens IR semantic analyzer.
CS Summer 2005 Top-down and Bottom-up Parsing - a whirlwind tour June 20, 2005 Slide acknowledgment: Radu Rugina, CS 412.
Context-Free Grammars Lecture 7
ISBN Chapter 4 Lexical and Syntax Analysis.
Syntax Analysis Mooly Sagiv html:// Textbook:Modern Compiler Implementation in C Chapter 3.
ISBN Chapter 4 Lexical and Syntax Analysis The Parsing Problem Recursive-Descent Parsing.
Syntax Analysis Mooly Sagiv html:// Textbook:Modern Compiler Design Chapter 2.2 (Partial) Hashlama 11:00-14:00.
1 Foundations of Software Design Lecture 23: Finite Automata and Context-Free Grammars Marti Hearst Fall 2002.
Parsing — Part II (Ambiguity, Top-down parsing, Left-recursion Removal)
COP4020 Programming Languages
CPSC Compiler Tutorial 3 Parser. Parsing The syntax of most programming languages can be specified by a Context-free Grammar (CGF) Parsing: Given.
(2.1) Grammars  Definitions  Grammars  Backus-Naur Form  Derivation – terminology – trees  Grammars and ambiguity  Simple example  Grammar hierarchies.
CSC3315 (Spring 2009)1 CSC 3315 Lexical and Syntax Analysis Hamid Harroud School of Science and Engineering, Akhawayn University
1 Introduction to Parsing Lecture 5. 2 Outline Regular languages revisited Parser overview Context-free grammars (CFG’s) Derivations.
LR Parsing Compiler Baojian Hua
BİL 744 Derleyici Gerçekleştirimi (Compiler Design)1 Syntax Analyzer Syntax Analyzer creates the syntactic structure of the given source program. This.
CS 330 Programming Languages 09 / 26 / 2006 Instructor: Michael Eckmann.
CISC 471 First Exam Review Game Questions. Overview 1 Draw the standard phases of a compiler for compiling a high level language to machine code, showing.
Introduction to Parsing Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University.
CS 461 – Oct. 7 Applications of CFLs: Compiling Scanning vs. parsing Expression grammars –Associativity –Precedence Programming language (handout)
Profs. Necula CS 164 Lecture Top-Down Parsing ICOM 4036 Lecture 5.
Lesson 3 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Lexical Analysis (I) Compiler Baojian Hua
Joey Paquet, Lecture 12 Review. Joey Paquet, Course Review Compiler architecture –Lexical analysis, syntactic analysis, semantic.
1 Syntax In Text: Chapter 3. 2 Chapter 3: Syntax and Semantics Outline Syntax: Recognizer vs. generator BNF EBNF.
Bernd Fischer RW713: Compiler and Software Language Engineering.
CPS 506 Comparative Programming Languages Syntax Specification.
LL(k) Parsing Compiler Baojian Hua
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 3: Introduction to Syntactic Analysis.
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.
Syntax Analysis - Parsing Compiler Design Lecture (01/28/98) Computer Science Rensselaer Polytechnic.
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.
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
Top-Down Parsing.
Syntax Analyzer (Parser)
CSE 5317/4305 L3: Parsing #11 Parsing #1 Leonidas Fegaras.
1 Introduction to Parsing. 2 Outline l Regular languages revisited l Parser overview Context-free grammars (CFG ’ s) l Derivations.
Copyright © 2004 Pearson Addison-Wesley. All rights reserved.3-1 Language Specification and Translation Lecture 8.
1 Topic #4: Syntactic Analysis (Parsing) CSC 338 – Compiler Design and implementation Dr. Mohamed Ben Othman ( )
Compiler Construction Lecture Five: Parsing - Part Two CSC 2103: Compiler Construction Lecture Five: Parsing - Part Two Joyce Nakatumba-Nabende 1.
Syntax Analysis By Noor Dhia Syntax analysis:- Syntax analysis or parsing is the most important phase of a compiler. The syntax analyzer considers.
CS 2130 Lecture 18 Bottom-Up Parsing or Shift-Reduce Parsing Warning: The precedence table given for the Wff grammar is in error.
Chapter 3 – Describing Syntax CSCE 343. Syntax vs. Semantics Syntax: The form or structure of the expressions, statements, and program units. Semantics:
Introduction to Parsing
4.1 Introduction - Language implementation systems must analyze
Chapter 4 - Parsing CSCE 343.
Programming Languages Translator
Compiler Baojian Hua LR Parsing Compiler Baojian Hua
Introduction to Parsing (adapted from CS 164 at Berkeley)
Textbook:Modern Compiler Design
Parsing IV Bottom-up Parsing
4 (c) parsing.
Compiler Design 4. Language Grammars
Lexical and Syntax Analysis
Top-Down Parsing CS 671 January 29, 2008.
R.Rajkumar Asst.Professor CSE
Introduction to Parsing
Introduction to Parsing
Lecture 18 Bottom-Up Parsing or Shift-Reduce Parsing
4.1 Introduction - Language implementation systems must analyze
Parsing CSCI 432 Computer Science Theory
Presentation transcript:

Parsing Discrete Mathematics and Its Applications Baojian Hua

Derivations A string is valid in a language if and only if there exists a derivation from the start state which produces it Begin with the start symbol, and apply grammar rules until you produce the string Note that the final string (sentence) consists of only terminals

Question Given a formal grammar G and a sentence (program) p, is p derivable from grammar G ? Or equivalently, is a given program p valid according to some language ’ s syntax (say C)?

Example: Context-Free Grammar S ::= x A | y B A ::= u C | v C B ::= t C ::= w | z // derivable? xum

Example: Context-Free Grammar // derivable? xum xuwz S ::= x A | y B A ::= u C | v C B ::= t C ::= w | z

Example: Context-Free Grammar // derivable? xum xuwz xwu S ::= x A | y B A ::= u C | v C B ::= t C ::= w | z

Example: Context-Free Grammar // derivable? xum xuwz xwu xuz S ::= x A | y B A ::= u C | v C B ::= t C ::= w | z

Lexical Analyzer The lexical analyzer translates the source program into a stream of lexical tokens Source program: stream of (ASCII or Unicode) characters Lexical token: compiler data structure that represents the occurrence of a terminal symbol Valid sentence consists of only allowable terminals

Example: Context-Free Grammar // all terminals T={x, y, u, v, t, w, z} S ::= x A | y B A ::= u C | v C B ::= t C ::= w | z

Example: Context-Free Grammar // all terminals T={x, y, u, v, t, w, z} // allowable strings T* S ::= x A | y B A ::= u C | v C B ::= t C ::= w | z

Predictive Parsing Parsing: recognizing a string and do something useful The most na ï ve approach to use when implementing a parser is to use recursive descent A form of top-down parsing Not as powerful as other methods, but easy enough to implement by hand

Predictive Parsing // Valid? xum xuwz xwu xuz S ::= x A | y B A ::= u C | v C B ::= t C ::= w | z

A Predictive Parser in C (Sketch) tokenTy token; void parseS () { switch (token.kind) { case x: token = nextToken (); parseA (); break; case y: token = nextToken (); parseB (); break; default: error (…); } // other functions are similar

Output: Abstract Syntax Tree xuz S xA uC z

A Predictive Parser Emitting AST in C (Sketch) tokenTy token; S parseS () { switch (token.kind) { case x: token = nextToken (); a=parseA (); return newS1 (x, a); case y: token = nextToken (); b=parseB (); return newS2 (y, b); default: error (…); } // other functions are similar

Predictive Parsing Difficulties // derivable? xuz S ::= x A | x B A ::= u C | v C B ::= t C ::= w | z

E By 4 => E * E By 5 => E * (E + E) By 2 => E * (E + 4) By 2 => E * (3 + 4) By 2 => 15 * (3 + 4) Or Even Worse 1 E ::= id 2 | num 3 | E + E 4 | E * E 5 | ( E ) 15*(3+4)

E E * E E * (E + E) E * (E + 4) E * (3 + 4) 15 * (3 + 4) Or Even Worse 15*(3+4) E E * E 15 * E 15 * (E + E) 15 * (3 + E) 15 * (3 + 4) rightmost derivationleftmost derivation

Ambiguous grammars A grammar is ambiguous if there is a sentence with >1 parse tree 15 * E E*E 15 E +E 3 4 E E+E E *E 3

Eliminating ambiguity In programming language syntax, ambiguity often arises from missing operator precedence or associativity * higher precedence than +? * and + are left associative? Can sometimes rewrite the grammar to disambiguate this Beyond the scope of this course

Unambiguous Grammar E ::= id | num | E + E | E * E | ( E ) E ::= E + T | T T ::= T * F | F F ::= id | num | ( E ) Accepts the same language, but parses unambiguously

Limitations with Predictive Parsing Rewriting grammar: to resolve ambiguity Grammars/trees are ugly But … easy to write code by hand, and very good for error reporting

Doing better We can do better We can use a parsing algorithm that can handle all context-free languages (though not all context-free grammars) Remember: a context-free language might have many different context-free grammars

The Yacc Tool semantic analyzer specification parser Yacc Originally developed for C, and now almost every main-stream language has its own Yacc-tool: bison (C), ml-yacc (SML), Cup (Java), GPPG (C#), …

Whole Structure source code abstract syntax tree lexical analyzer parser tokens Pentiu m other part