CSC 4181Compiler Construction. Context-Free Grammars Using grammars in parsers. Parsing Process. Context-Free Grammar

Similar documents
Languages. Languages. An Example Grammar. Grammars. Suppose we have an alphabet V. Then we can write:

This lecture covers Chapter 5 of HMU: Context-free Grammars

Syntactical analysis. Syntactical analysis. Syntactical analysis. Syntactical analysis

Bottom-Up Syntax Analysis

Context-free Grammars and Languages

CONTEXT FREE GRAMMAR AND

Lecture VII Part 2: Syntactic Analysis Bottom-up Parsing: LR Parsing. Prof. Bodik CS Berkley University 1

Ambiguity, Precedence, Associativity & Top-Down Parsing. Lecture 9-10

Parsing. Context-Free Grammars (CFG) Laura Kallmeyer. Winter 2017/18. Heinrich-Heine-Universität Düsseldorf 1 / 26

Lecture 11 Context-Free Languages

Administrivia. Test I during class on 10 March. Bottom-Up Parsing. Lecture An Introductory Example

Syntax Analysis Part I. Position of a Parser in the Compiler Model. The Parser. Chapter 4

CFLs and Regular Languages. CFLs and Regular Languages. CFLs and Regular Languages. Will show that all Regular Languages are CFLs. Union.

[4203] Compiler Theory Sheet # 3-Answers

Eng. Maha Talaat Page 1

Context Free Grammars

MA/CSSE 474 Theory of Computation

Top-Down Parsing and Intro to Bottom-Up Parsing

Syntax Analysis - Part 1. Syntax Analysis

Syntax Analysis Part I

Syntax Analysis Part I

Einführung in die Computerlinguistik

(NB. Pages are intended for those who need repeated study in formal languages) Length of a string. Formal languages. Substrings: Prefix, suffix.

Suppose h maps number and variables to ɛ, and opening parenthesis to 0 and closing parenthesis

Syntax Analysis Part III

TAFL 1 (ECS-403) Unit- III. 3.1 Definition of CFG (Context Free Grammar) and problems. 3.2 Derivation. 3.3 Ambiguity in Grammar

Introduction to Bottom-Up Parsing

Context-Free Grammars and Languages

CSE302: Compiler Design

Introduction to Bottom-Up Parsing

5 Context-Free Languages

Compiling Techniques

Harvard CS 121 and CSCI E-207 Lecture 9: Regular Languages Wrap-Up, Context-Free Grammars

Introduction to Bottom-Up Parsing

Parsing -3. A View During TD Parsing

Context-Free Grammars (and Languages) Lecture 7

Syntactic Analysis. Top-Down Parsing

Compiler Principles, PS4

CA Compiler Construction

CS20a: summary (Oct 24, 2002)

CMSC 330: Organization of Programming Languages. Pushdown Automata Parsing

Introduction to Bottom-Up Parsing

Shift-Reduce parser E + (E + (E) E [a-z] In each stage, we shift a symbol from the input to the stack, or reduce according to one of the rules.

Compiling Techniques

Prof. Mohamed Hamada Software Engineering Lab. The University of Aizu Japan

n Top-down parsing vs. bottom-up parsing n Top-down parsing n Introduction n A top-down depth-first parser (with backtracking)

THEORY OF COMPUTATION (AUBER) EXAM CRIB SHEET

Parsing Algorithms. CS 4447/CS Stephen Watt University of Western Ontario

Bottom-Up Parsing. Ÿ rm E + F *idÿ rm E +id*idÿ rm T +id*id. Ÿ rm F +id*id Ÿ rm id + id * id

CPS 220 Theory of Computation

Introduction to Theory of Computing

Context-Free Grammars. 2IT70 Finite Automata and Process Theory

Context-Free Grammars and Languages. Reading: Chapter 5

An Efficient Context-Free Parsing Algorithm. Speakers: Morad Ankri Yaniv Elia

Notes for Comp 497 (Comp 454) Week 10 4/5/05

The Parser. CISC 5920: Compiler Construction Chapter 3 Syntactic Analysis (I) Grammars (cont d) Grammars

CS 406: Bottom-Up Parsing

Context-Free Grammars and Languages. We have seen that many languages cannot be regular. Thus we need to consider larger classes of langs.

5/10/16. Grammar. Automata and Languages. Today s Topics. Grammars Definition A grammar G is defined as G = (V, T, P, S) where:

Parsing. Probabilistic CFG (PCFG) Laura Kallmeyer. Winter 2017/18. Heinrich-Heine-Universität Düsseldorf 1 / 22

Compiling Techniques

CS153: Compilers Lecture 5: LL Parsing

Chapter 4: Context-Free Grammars

Chapter 5: Context-Free Languages

Predictive parsing as a specific subclass of recursive descent parsing complexity comparisons with general parsing

LR2: LR(0) Parsing. LR Parsing. CMPT 379: Compilers Instructor: Anoop Sarkar. anoopsarkar.github.io/compilers-class

Compiling Techniques

Section 1 (closed-book) Total points 30

Bottom up parsing. General idea LR(0) SLR LR(1) LALR To best exploit JavaCUP, should understand the theoretical basis (LR parsing);

Notes for Comp 497 (454) Week 10

Functions on languages:

Computer Science 160 Translation of Programming Languages

Definition: A grammar G = (V, T, P,S) is a context free grammar (cfg) if all productions in P have the form A x where

FLAC Context-Free Grammars

Creating a Recursive Descent Parse Table

CS 314 Principles of Programming Languages

EXAM. Please read all instructions, including these, carefully NAME : Problem Max points Points 1 10 TOTAL 100

Solutions to Problem Set 3

A* Search. 1 Dijkstra Shortest Path

Pushdown Automata: Introduction (2)

Formal Languages, Grammars and Automata Lecture 5

d(ν) = max{n N : ν dmn p n } N. p d(ν) (ν) = ρ.

Type 3 languages. Type 2 languages. Regular grammars Finite automata. Regular expressions. Type 2 grammars. Deterministic Nondeterministic.

60-354, Theory of Computation Fall Asish Mukhopadhyay School of Computer Science University of Windsor

Foundations of Informatics: a Bridging Course

Talen en Compilers. Johan Jeuring , period 2. October 29, Department of Information and Computing Sciences Utrecht University

Lecture 11 Sections 4.5, 4.7. Wed, Feb 18, 2009

Compiler Construction Lent Term 2015 Lectures (of 16)

Compiler Construction Lent Term 2015 Lectures (of 16)

INF5110 Compiler Construction

What Is a Language? Grammars, Languages, and Machines. Strings: the Building Blocks of Languages

Everything You Always Wanted to Know About Parsing

Pushdown Automata. We have seen examples of context-free languages that are not regular, and hence can not be recognized by finite automata.

Context Free Languages (CFL) Language Recognizer A device that accepts valid strings. The FA are formalized types of language recognizer.

Compiler Principles, PS7

Parsing with Context-Free Grammars

EXAM. CS331 Compiler Design Spring Please read all instructions, including these, carefully

CS5371 Theory of Computation. Lecture 7: Automata Theory V (CFG, CFL, CNF)

Propositional Logic: Part II - Syntax & Proofs 0-0

Automata Theory CS F-08 Context-Free Grammars

Transcription:

CSC 4181Compiler Construction Context-ree Grammars Using grammars in parsers CG 1 Parsing Process Call the scanner to get tokens Build a parse tree from the stream of tokens A parse tree shows the syntactic structure of the source program. Add information about entifiers in the symbol table Report error, when found, and recover from thee error CG 2 Context-ree Grammar a tuple (V, T, P, S) where V is a finite set of nonterminals, containing S, T is a finite set of terminals, P is a set of production rules in the form of aβ where ais in V and β is in (V UT ), and S is the start symbol. Any string in (V U T) is called a sentential form. CG 3 1

xamples O () O O - O O / S SS S (S)S S () S CG 4 Backus-Naur orm (BN) Nonterminals are in < >. Terminals are any other symbols. ::= means. means or. xamples: <> ::= <><O><> (<>) ID <O> ::= - / <S> ::= <S><S> (<S>)<S> () CG 5 Derivation A sequence of replacement of a substring in a sentential form. Definition Let G = (V, T, P, S ) be a CG, a,, be strings in (V U T) and A is in V. aa G a if A is in P. G denotes a derivation in zero step or more. CG 6 2

xamples S SS (S)S () S SS (S)SS (S)S(S)S (S)S(())S ((S)S)S(())S ((S)())S(())S ((())())S(())S ((())()) (())S ((())())(()) O () O - / O () O ( O ) O (( O ) O ) O (( O )) O ) O (( )) O ) O (( )) O ) O (( )) ) CG 7 Leftmost Derivation ach step of the derivation is a replacement of the leftmost nonterminals in a sentential form. Rightmost Derivation ach step of the derivation is a replacement of the rightmost nonterminals in a sentential form. O () O ( O ) O ( O ) O ( ) O ( ) O ( ) ( ) O O () ( O ) ( O ) ( ) ( ) CG 8 Right/Left Recursive A grammar is a left recursive if its production rules can generate a derivation of the form A A. xamples: O () () A grammar is a right recursive if its production rules can generate a derivation of the form A A. xamples: O () () CG 9 3

Parse Tree A labeled tree in which the interior nodes are labeled by nonterminals leaf nodes are labeled by terminals the children of an interior node represent a replacement of the associated nonterminal in a derivation corresponding to a derivation CG 10 Parse Trees and Derivations 1 3 2 4 5 Preorder numbering 1 2 5 4 3 Reverse or postorder numbering (1) (2) (3) (4) (5) (1) (2) (3) (4) (5) CG 11 List of statements: No statement One statement: s; More than one statement: s; s; s; A statement can be a block of statements. {s; s; s;} Is the following correct? { {s; {s; s;} s; {}} s; } Grammar: xample <St> ::= s; s; <St> { <St> } <St> <St> { <St> } <St> { { <St> } <St> } <St> { { s; <St> } <St>} <St> { { s; { <St> } <St> } <St>} <St> { { s; { s; <St> } <St> } <St>} <St> { { s; { s; s; } <St> } <St>} <St> { { s; { s; s; } s; <St> } <St>} <St> { { s; { s; s; } s; { <St> } <St> } <St>} <St> { { s; { s; s; } s; { } <St> } <St>} <St> { { s; { s; s; } s; { } } <St>} <St> { { s; { s; s;} s; { } } s; } <St> { { s; { s; s;} s; { } } s; } CG 12 4

Abstract Syntax Tree Representation of actual source tokens Interior nodes represent operators. Leaf nodes represent operands. CG 13 Abstract Syntax Tree for xpression CG 14 Abstract Syntax Tree for If Statement st ifstatement if if ( exp ) st elsepart true st return true else st return CG 15 5

Ambiguous Grammar A grammar is ambiguous if it can generate two different parse trees for one string. Ambiguous grammars can cause inconsistency in parsing. CG 16 xample: Ambiguous Grammar - / CG 17 Ambiguity in xpressions Which operation is to be done first? solved by precedence An operator with higher precedence is done before one with lower precedence. An operator with higher precedence is placed in a rule (logically) further from the start symbol. solved by associativity If an operator is right-associative (or left-associative), an operand in between 2 operators is associated to the operator to the right (left). Right-associated : W ( (Y Z)) Left-associated : ((W ) Y) Z CG 18 6

Precedence - / - / CG 19 Precedence (cont d) - / ( ) ( ) 4 ( ) 4 CG 20 Associativity Left-associative operators - / ( ) ( ) / 4 = ((( ) ) / 4) CG 21 ( ) / 4 7

Ambiguity in Dangling lse St IfSt... IfSt if ( ) St if ( ) St else St 0 1 IfSt if ( ) St { if (0) { if (1) St else St } } 0 IfSt if ( ) St else St 1 { if (0) { if (1) St } else St } IfSt if ( ) St else St IfSt if ( ) St CG 22 0 1 Disambiguating Rules for Dangling lse St MatchedSt UnmatchedSt UnmatchedSt if () St if () MatchedSt else UnmatchedSt St MatchedSt if () MatchedSt else MatchedSt UnmatchedSt... 0 1 if ( ) St MatchedSt if (0) if (1) St else St = if (0) if (1) St else St if ( ) MatchedSt else MatchedSt CG 23 xtended Backus-Naur orm (BN) Kleene s Star/ Kleene s Closure Seq ::= St {; St} Seq ::= {St ;} St Optional Part IfSt ::= if ( ) St [else St] ::= [ ] [- ] CG 24 8

Syntax Diagrams Graphical representation of BN rules IfSt nonterminals: terminals: sequences and choices: xamples ::= () Seq ::= {St ;} St ::= [ ] ( ) CG 25 ; St St 9