CONTEXT FREE GRAMMAR AND
|
|
- Lenard Owen
- 6 years ago
- Views:
Transcription
1 CONTEXT FREE GRAMMAR AND PARSING STATIC ANALYSIS - PARSING Source language Scanner (lexical analysis) tokens Parser (syntax analysis) Syntatic structure Semantic Analysis (IC generator) Syntatic/sema ntic structure Code Generator Target language 2 Code Optimizer Syntax described formally Tokens organized into syntax tree that describes structure Error checking Symbol Table CS 540 Spri ng 2009 GM U 1
2 STATIC ANALYSIS - PARSING We can use context free grammars to specify the syntax of programming languages. 3 CS 540 Spri ng 2009 GM U CONTEXT FREE GRAMMARS Definition: A context free grammar is a formal model that consists of: A set of tokens or terminal symbols Vt A set of nonterminal symbols Vn Start symbol S (in Vn) Finite set of productions of form: A R1 Rn (n >= 0) where A in Vn, R in Vn Vt 4 2
3 CFG EXAMPLES Indicates a production V t = {+,-,0..9}, V n = {L,D}, s = {L} L L + D L D D D 0 9 Shorthand for multiple productions V t ={(,)}, V n = {L}, s = {L} L ( L ) L L e 5 LANGUAGES Regular Context free Context sensitive Type 0 A a B, C e A a aab agb a b 3
4 ANY REGULAR LANGUAGE CAN BE EXPREED USING A CFG Starting with a NFA: For each state Si in the NFA Create non-terminal Ai If transition (Si,a) = Sk, create production Ai a Ak If transition (Si,e) = Sk, create production Ai Ak If Si is a final state, create production Ai e If Si is the NFA start state, s = Ai What does the existence of this algorithm tell us about the relationship between regular and context free languages? 7 NFA TO CFG EXAMPLE ab*a a a A1 a A2 A2 b A2 A2 a A3 A3 e b 8 4
5 WRITING GRAMMARS When writing a grammar (or RE) for some language, the following must be true: All strings generated are in the language. Your grammar produces all strings in the language. 9 TRY THESE: Integers divisible by 2 Legal postfix expressions Floating point numbers with no extra zeros Strings of 0,1 where there are more 0 than
6 PARSING The task of parsing is figuring out what the parse tree looks like for a given input and language. If a string is in the given language, a parse tree must exist. However, just because a parse tree exists for some string in a given language doesn t mean a given algorithm can find it. 11 PARSE TREES The parse tree for some string in a language that is defined by the grammar G as follows: The root is the start symbol of G The leaves are terminals or e. When visited from left to right, the leaves form the input string The interior nodes are non-terminals of G For every non-terminal A in the tree with children B1 Bk, there is some production A B1 Bk 12 6
7 PARSE TREE FOR (())() L L ( L ) L L e 13 L L ( ( L ) L ) ( ) L L e e e e PARSE TREE FOR (())() L L ( L ) L L e 14 L L ( ( L ) L ) ( L ) L e e e e 7
8 PARSING Parsing algorithms are based on the idea of derivations. General Algorithms: LL (top down) LR (bottom up) 15 SINGLE STEP DERIVATION Definition: Given a A b (with a,b in (Vn Vt)*) and a production A g, a A b a g b is a single step derivation. Examples: L + D L D + D ( L ) ( L ) ( ( L ) L ) ( L ) L L - D L (L) L 16 8
9 DERIVATIONS Definition: A sequence of the form: w0 w1 wn is a derivation of wn from w0 (w0 * wn) L ( L ) L ( ) L ( ) production L ( L ) L production L e production L e L * ( ) If wi has non-terminal symbols, it is referred to as sentential form. 17 L * (( )) ( ) L ( L ) L ( L ) ( L ) L ( L ) ( L ) ( ( L ) L ) ( L ) ( ( ) L ) ( L ) ( ( ) L ) ( ) ( ( ) ) ( ) production L ( L ) L production L ( L ) L production L e production L ( L ) L production L e production L e production L e 18 9
10 L(G), the language generated by grammar G is {w in Vt*: s * w, for start symbol s} Example: We ve just shown that both () and (())() are in L(G) for the previous grammar. 19 LEFTMOST DERIVATIONS A derivation where the leftmost nonterminal is always chosen If a string is in a given language (i.e. a derivation exists), then a leftmost derivation must exist Rightmost derivation defined as you would expect 20 10
11 LEFTMOST DERIVATION FOR (())() L production L ( L ) L ( L ) L production L ( L ) L ( ( L ) L ) L production L e ( ( ) L ) L production L e ( ( ) ) L ( ( ) ) ( L ) L ( ( ) ) ( ) L ( ( ) ) ( ) production L e production L ( L ) L production L e RIGHTMOST DERIVATION FOR (())() L production L ( L ) L ( L ) L production L ( L ) L ( L ) ( L ) L production L e ( L ) ( L ) production L e ( L ) ( ) production L ( L ) L ( ( L ) L ) ( ) production L e ( ( L ) ) ( ) production L e ( ( ) ) ( ) 11
12 AMBIGUITY Two (or more) parse trees or leftmost derivations for some string in the language E E + E E E E E 0 9 E E E + E E - E E E E + E TWO LEFTMOST DERIVATIONS E E + E E E + E 2 E + E E E E E E E + E 2 E + E E
13 An ambiguous grammar can sometimes be made unambiguous: E E + T E T T T 0 9 Precedence can be specified as well: E E + T E T T T T * F T / F F F ( E ) 0 9 enforces the correct associativity 2 INPUT: { SIMPLESTMT; SIMPLESTMT; } P { } S ; e S simplestmt S { } P 26 S S { simplestmt ; simplestmt ; } e 13
14 TOP DOWN (LL) PARSING P { } S ; e S simplestmt S { } end P 27 { simplestmt ; simplestmt ; } TOP DOWN (LL) PARSING P { } S ; e S simplestmt S { } end P 28 S { simplestmt ; simplestmt; } 14
15 TOP DOWN (LL) PARSING P { } S ; e S simplestmt S { } end P 29 S { simplestmt ; simplestmt; } TOP DOWN (LL) PARSING P { } S ; e S simplestmt S { } end P 30 S S { simplestmt ; simplestmt; } 15
16 TOP DOWN (LL) PARSING P { } S ; e S simplestmt S { } end P 31 S S { simplestmt ; simplestmt; } TOP DOWN (LL) PARSING P { } S ; e S simplestmt S { } end P 32 S S e { simplestmt ; simplestmt; } 16
17 BOTTOMUP (LR) PARSING P { } S ; e S simplestmt S { } end 33 S { simplestmt ; simplestmt ; } BOTTOMUP (LR) PARSING P { } S ; e S simplestmt S { } end 34 S S { simplestmt ; simplestmt ; } 17
18 BOTTOMUP (LR) PARSING P { } S ; e S simplestmt S { } end 35 S S { simplestmt ; simplestmt ; } e CS 540 Sp BOTTOMUP (LR) PARSING P { } S ; e S simplestmt S { } end 36 S S e { simplestmt ; simplestmt ; } CS 540 Spri ng 2009 GM U 18
19 BOTTOMUP (LR) PARSING P { } S ; e S simplestmt S { } end 37 S S { simplestmt ; simplestmt ; } e BOTTOMUP (LR) PARSING P { } S ; e S simplestmt S { } end P 38 S S { simplestmt ; simplestmt ; } e 19
20 BACKUS-NAUR FORM (BNF) BNF is a meta-language i.e. a language used to describe another language invented by John Backus to describe ALGOL 58 used by Peter Naur to describe ALGOL 60 BNF is equivalent to context-free grammars a BNF grammar is defined by a set of terminal symbols, a set of nonterminal symbols a set of rules a start symbol (one of the terminal symbols) BNF ELEMENTS terminal symbols are the lexemes of the target PL e.g., while, (, ) nonterminal symbols represent classes of syntactic structures rules they act like syntactic variables e.g., <statement> define how a nonterminal symbol can by developed into a sequence of nonterminal and terminal symbols e.g., <while_stmt> while ( <logic_expr> ) <stmt> 20
21 BNF RULES A rule has a left-hand side (LHS) then a right-hand side (RHS) There can be several rules for one LHS <stmt> <assignment> <stmt> begin <stmt_list> end Syntactic lists are described using recursion <ident_list> ident <ident_list> ident, <ident_list> A grammar is a finite nonempty set of rules EBNF Extended BNF (EBNF) is most often used avoids having numerous rules for the same LHS Extra meta-symbols (in addition to ) [ ] enclosed symbols are optional (1 or 0 times) e.g., <if_stmt> if ( <exp> ) <stmt> [ else <stmt> ] { } enclosed symbols can be repeated (0 to n times) e.g., <ident_list> ident {, ident } choice of one of the symbol sequences separated by e.g., <stmt> <assignment> begin <stmt_list> end ( ) groups enclosed symbols 21
22 BNF VS. EBNF 43 BNF <expr> <expr> + <term> <expr> <expr> - <term> <expr> <term> <term> <term> * <factor> <term> <term> / <factor> <term> <factor> <factor> <exp> ** <factor> <factor> <exp> <exp> ( <expr> ) <exp> id EBNF <expr> <term> { ( + - ) <term> } <term> <factor> { ( * / ) <factor> } <factor> <exp> [ ** <factor> ] <exp> ( <expr> ) id AUGMENTED EBNF another meta-symbol ::= (equal) instead of meta-symbols for repetitions + means one or more times * means zero or more times <ident> = <letter> + ( <letter> <digit> ) * rules can use iteration instead of recursion e.g.: <stmt_list> <stmt> <stmt> ; <stmt_list> can be formulated as <stmt_list> ::= <stmt> ( ; <stmt> ) * 22
23 A SMALL LANGUAGE IN EBNF <program> ::= begin <stmt_list> end <stmt_list> ::= <stmt> <stmt> ; <stmt_list> <stmt> ::= <var> = <expr> <expr> ::= <term> + <term> <term> - <term> <term> ::= <var> const <var> ::= a b c PARSING General Algorithms: LL (top down) LR (bottom up) Both algorithms are driven by the input grammar and the input to be parsed Two important sets: FIRST and FOLLOW 46 23
24 FIRST SETS FIRST(a) is the set of all terminal symbols that can begin some sentential form in a derivation that starts with a a a b FIRST(a) = {a in V t a * ab } { e } if a * e Example: S simple { S } FIRST(S) = { simple, { } 47 To compute FIRST across strings of terminals and non-terminals: FIRST(e) = { e } FIRST(Aa) = A if A is a terminal FIRST(A) FIRST(a) If A * e FIRST(A) otherwise 48 24
25 COMPUTING FIRST SETS Initially FIRST(A) (for all A) is empty 1. For productions A c b, where c in V t Add { c } to FIRST(A) 2. For productions A e Add { e } to FIRST(A) 3. For productions A a B b, where a * e and NOT (B * e) Add FIRST(aB) to FIRST(A) 4. For productions A a, where a * e Add FIRST(a) and { e } to FIRST(A) same thing as e FIRST(B) 49 EXAMPLE 1 S a S e S B B b B e B C C c C e C d FIRST(C) = {c,d} FIRST(B) = FIRST(S) = Start with the simplest non-terminal 50 25
26 EXAMPLE 1 S a S e S B B b B e B C C c C e C d FIRST(C) = {c,d} FIRST(B) = {b,c,d} FIRST(S) = CS 540 Spring 2009 GMU Now that we know FIRST(C) 51 EXAMPLE 1 S a S e S B B b B e B C C c C e C d FIRST(C) = {c,d} FIRST(B) = {b,c,d} FIRST(S) = {a,b,c,d} 52 26
27 EXAMPLE 2 P i c n T S Q P a S d S c S T R b e S e R n e T R S q FIRST(P) = {i,c,n} FIRST(Q) = FIRST(R) = {b,e} FIRST(S) = FIRST(T) = 53 EXAMPLE 2 P i c n T S Q P a S d S c S T R b e S e R n e T R S q FIRST(P) = {i,c,n} FIRST(Q) = {i,c,n,a,d} FIRST(R) = {b,e} FIRST(S) = FIRST(T) = 54 27
28 EXAMPLE 2 P i c n T S Q P a S d S c S T R b e S e R n e T R S q FIRST(P) = {i,c,n} FIRST(Q) = {i,c,n,a,d} FIRST(R) = {b,e} FIRST(S) = {e,b,n,e} FIRST(T) = 55 EXAMPLE 2 P i c n T S Q P a S d S c S T R b e S e R n e T R S q FIRST(P) = {i,c,n} FIRST(Q) = {i,c,n,a,d} FIRST(R) = {b,e} FIRST(S) = {e,b,n,e} FIRST(T) = {b,c,n,q} 56 28
29 EXAMPLE 3 S a S e S T S T R S e Q R r S r e Q S T e FIRST(S) = FIRST(R) = FIRST(T) = FIRST(Q) = 57 EXAMPLE 3 S a S e S T S T R S e Q R r S r e Q S T e FIRST(S) = {a} FIRST(R) = {r, e} FIRST(T) = {r,a, e} FIRST(Q) = {a, e} 58 29
30 FOLLOW SETS FOLLOW(A) is the set of terminals (including end of file - $) that may follow non-terminal A in some sentential form. FOLLOW(A) = {c in Vt S + Ac } {$} if S + A For example, consider L + (())(L)L Both ) and end of file can follow L NOTE: e is never in FOLLOW sets 59 COMPUTING FOLLOW(A) If A is start symbol, put $ in FOLLOW(A) Productions of the form B a A b, Add FIRST(b) {e} to FOLLOW(A) INTUITION: Suppose B AX and FIRST(X) = {c} S + a B b a A X b + a A c d b 60 30
31 3. Productions of the form B a A or B a A b where b * e Add FOLLOW(B) to FOLLOW(A) INTUITION: Suppose B Y A S + a B b a Y A b FOLLOW(B) Suppose B A X and X * e S + a B b a A X b * a A b FOLLOW(B) 61 Assume the first non-terminal is the start symbol EXAMPLE 4 S a S e B B b B C f C C c C g d e FIRST(C) = {c,d,e} FIRST(B) = {b,c,d,e} FIRST(S) = {a,b,c,d,e} FOLLOW(C) = FOLLOW(B) = FOLLOW(S) {$} Using rule #
32 EXAMPLE 4 S a S e B B b B C f C C c C g d e FIRST(C) = {c,d,e} FIRST(B) = {b,c,d,e} FIRST(S) = {a,b,c,d,e} FOLLOW(C) {f,g} FOLLOW(B) {c,d,f} FOLLOW(S) {$,e} Using rule #2 63 EXAMPLE 4 S a S e B B b B C f C C c C g d e FIRST(C) = {c,d,e} FIRST(B) = {b,c,d,e} FIRST(S) = {a,b,c,d,e} FOLLOW(S) = {$} FOLLOW(B) = FIRST(Cf) {$} = {c,d,e,} {$} = {c,d,e,$} FOLLOW(C) = FOLLOW(B) {f,g} = {c,d,e,g,$} 32
33 EXAMPLE 5 S A B C A D A e a A B b c e C D d C D e b f c FOLLOW(S) = FOLLOW(A) = FOLLOW(B) = FOLLOW(C) = FOLLOW(D) = FIRST(D) = {e,f} FIRST(C) = {e,f} FIRST(B) = {b,c,e} FIRST(A) = {a, e} FIRST(S) = {a,b,c,e,f} 65 EXAMPLE 5 S A B C A D A e a A B b c e C D d C D e b f c FOLLOW(S) = {$} FOLLOW(A) = {b,c,e,f} FOLLOW(B) = {e,f} FOLLOW(C) = {$} FOLLOW(D) = {$} FIRST(D) = {e,f} FIRST(C) = {e,f} FIRST(B) = {b,c,e} FIRST(A) = {a, e} FIRST(S) = {a,b,c,e,f} 66 33
34 EXAMPLE 6 S ( A) e A T E E & T E e T ( A ) a b c FOLLOW(S) = FOLLOW(A) = FOLLOW(E) = FOLLOW(T) = FIRST(T) = {(,a,b,c} FIRST(E) = {&, e } FIRST(A) = {(,a,b,c} FIRST(S) = {(, e} 67 EXAMPLE 6 S ( A) e A T E E & T E e T ( A ) a b c FIRST(T) = {(,a,b,c} FIRST(E) = {&, e } FIRST(A) = {(,a,b,c} FIRST(S) = {(, e} FOLLOW(S) = {$} FOLLOW(A) = { ) } FOLLOW(E) = FOLLOW(A) = { ) } FOLLOW(T) = FIRST(E) FOLLOW(A) FOLLOW(E) = {&, )} CS 540 Spring 2009 GMU 68 34
35 EXAMPLE 7 E T E E + T E e T F T T * F T e F ( E ) id FOLLOW(E) = FOLLOW(E ) = FOLLOW(T) = FOLLOW(T ) = FOLLOW(F) = FIRST(F) = FIRST(T) = FIRST(E) = {(,id} FIRST(T ) = {*,e} FIRST(E ) = {+,e} 69 EXAMPLE 7 E T E E + T E e T F T T * F T e F ( E ) id FOLLOW(E) = {$,)} FOLLOW(E ) = FOLLOW(E) = {$,)} FOLLOW(T) = FIRST(E ) FOLLOW(E) FOLLOW(E ) = {+,$,)} FOLLOW(T ) = FOLLOW(T) = {+,$,)} FOLLOW(F) = FIRST(T ) FOLLOW(T) FOLLOW(T ) = {*,+,$,)} FIRST(F) = FIRST(T) = FIRST(E) = {(,id} FIRST(T ) = {*,e} FIRST(E ) = {+,e} 70 35
Syntax Analysis Part I
1 Syntax Analysis Part I Chapter 4 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007-2013 2 Position of a Parser in the Compiler Model Source Program Lexical Analyzer
More informationSyntax Analysis Part I
1 Syntax Analysis Part I Chapter 4 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007-2013 2 Position of a Parser in the Compiler Model Source Program Lexical Analyzer
More informationSyntax Analysis Part III
Syntax Analysis Part III Chapter 4: Top-Down Parsing Slides adapted from : Robert van Engelen, Florida State University Eliminating Ambiguity stmt if expr then stmt if expr then stmt else stmt other The
More informationTop-Down Parsing and Intro to Bottom-Up Parsing
Predictive Parsers op-down Parsing and Intro to Bottom-Up Parsing Lecture 7 Like recursive-descent but parser can predict which production to use By looking at the next few tokens No backtracking Predictive
More informationSyntactic Analysis. Top-Down Parsing
Syntactic Analysis Top-Down Parsing Copyright 2015, Pedro C. Diniz, all rights reserved. Students enrolled in Compilers class at University of Southern California (USC) have explicit permission to make
More informationn Top-down parsing vs. bottom-up parsing n Top-down parsing n Introduction n A top-down depth-first parser (with backtracking)
Announcements n Quiz 1 n Hold on to paper, bring over at the end n HW1 due today n HW2 will be posted tonight n Due Tue, Sep 18 at 2pm in Submitty! n Team assignment. Form teams in Submitty! n Top-down
More informationCompiling Techniques
Lecture 6: 9 October 2015 Announcement New tutorial session: Friday 2pm check ct course webpage to find your allocated group Table of contents 1 2 Ambiguity s Bottom-Up Parser A bottom-up parser builds
More informationIntroduction to Bottom-Up Parsing
Introduction to Bottom-Up Parsing Outline Review LL parsing Shift-reduce parsing The LR parsing algorithm Constructing LR parsing tables 2 Top-Down Parsing: Review Top-down parsing expands a parse tree
More informationLecture 11 Context-Free Languages
Lecture 11 Context-Free Languages COT 4420 Theory of Computation Chapter 5 Context-Free Languages n { a b : n n { ww } 0} R Regular Languages a *b* ( a + b) * Example 1 G = ({S}, {a, b}, S, P) Derivations:
More informationThe Parser. CISC 5920: Compiler Construction Chapter 3 Syntactic Analysis (I) Grammars (cont d) Grammars
The Parser CISC 5920: Compiler Construction Chapter 3 Syntactic Analysis (I) Arthur. Werschulz Fordham University Department of Computer and Information Sciences Copyright c Arthur. Werschulz, 2017. All
More informationIntroduction to Bottom-Up Parsing
Introduction to Bottom-Up Parsing Outline Review LL parsing Shift-reduce parsing The LR parsing algorithm Constructing LR parsing tables Compiler Design 1 (2011) 2 Top-Down Parsing: Review Top-down parsing
More informationCSC 4181Compiler Construction. Context-Free Grammars Using grammars in parsers. Parsing Process. Context-Free Grammar
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
More informationIntroduction to Bottom-Up Parsing
Outline Introduction to Bottom-Up Parsing Review LL parsing Shift-reduce parsing he LR parsing algorithm Constructing LR parsing tables 2 op-down Parsing: Review op-down parsing expands a parse tree from
More informationIntroduction to Bottom-Up Parsing
Outline Introduction to Bottom-Up Parsing Review LL parsing Shift-reduce parsing he LR parsing algorithm Constructing LR parsing tables Compiler Design 1 (2011) 2 op-down Parsing: Review op-down parsing
More informationFollow sets. LL(1) Parsing Table
Follow sets. LL(1) Parsing Table Exercise Introducing Follow Sets Compute nullable, first for this grammar: stmtlist ::= ε stmt stmtlist stmt ::= assign block assign ::= ID = ID ; block ::= beginof ID
More informationComputing if a token can follow
Computing if a token can follow first(b 1... B p ) = {a B 1...B p... aw } follow(x) = {a S......Xa... } There exists a derivation from the start symbol that produces a sequence of terminals and nonterminals
More informationSyntax Analysis Part I. Position of a Parser in the Compiler Model. The Parser. Chapter 4
1 Syntax Analysis Part I Chapter 4 COP5621 Compiler Construction Copyright Robert van ngelen, Flora State University, 2007 Position of a Parser in the Compiler Model 2 Source Program Lexical Analyzer Lexical
More informationCS415 Compilers Syntax Analysis Top-down Parsing
CS415 Compilers Syntax Analysis Top-down Parsing These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University Announcements Midterm on Thursday, March 13
More informationEXAM. Please read all instructions, including these, carefully NAME : Problem Max points Points 1 10 TOTAL 100
EXAM Please read all instructions, including these, carefully There are 7 questions on the exam, with multiple parts. You have 3 hours to work on the exam. The exam is open book, open notes. Please write
More informationLL(1) Grammar and parser
Crafting a Compiler with C (XI) 資科系 林偉川 LL(1) Grammar and parser LL(1) grammar is the class of CFG and is suitable for RDP. Define LL(1) parsers which use an LL(1) parse table rather than recursive procedures
More informationCompiler Principles, PS4
Top-Down Parsing Compiler Principles, PS4 Parsing problem definition: The general parsing problem is - given set of rules and input stream (in our case scheme token input stream), how to find the parse
More informationPredictive parsing as a specific subclass of recursive descent parsing complexity comparisons with general parsing
Plan for Today Recall Predictive Parsing when it works and when it doesn t necessary to remove left-recursion might have to left-factor Error recovery for predictive parsers Predictive parsing as a specific
More informationCompiler Design. Spring Syntactic Analysis. Sample Exercises and Solutions. Prof. Pedro C. Diniz
Compiler Design Spring 2015 Syntactic Analysis Sample Exercises and Solutions Prof. Pedro C. Diniz USC / Information Sciences Institute 4676 Admiralty Way, Suite 1001 Marina del Rey, California 90292 pedro@isi.edu
More informationSyntax Analysis - Part 1. Syntax Analysis
Syntax Analysis Outline Context-Free Grammars (CFGs) Parsing Top-Down Recursive Descent Table-Driven Bottom-Up LR Parsing Algorithm How to Build LR Tables Parser Generators Grammar Issues for Programming
More informationHarvard CS 121 and CSCI E-207 Lecture 9: Regular Languages Wrap-Up, Context-Free Grammars
Harvard CS 121 and CSCI E-207 Lecture 9: Regular Languages Wrap-Up, Context-Free Grammars Salil Vadhan October 2, 2012 Reading: Sipser, 2.1 (except Chomsky Normal Form). Algorithmic questions about regular
More information(NB. Pages are intended for those who need repeated study in formal languages) Length of a string. Formal languages. Substrings: Prefix, suffix.
(NB. Pages 22-40 are intended for those who need repeated study in formal languages) Length of a string Number of symbols in the string. Formal languages Basic concepts for symbols, strings and languages:
More informationCS 314 Principles of Programming Languages
CS 314 Principles of Programming Languages Lecture 7: LL(1) Parsing Zheng (Eddy) Zhang Rutgers University February 7, 2018 Class Information Homework 3 will be posted this coming Monday. 2 Review: Top-Down
More informationEXAM. CS331 Compiler Design Spring Please read all instructions, including these, carefully
EXAM Please read all instructions, including these, carefully There are 7 questions on the exam, with multiple parts. You have 3 hours to work on the exam. The exam is open book, open notes. Please write
More informationSyntax Analysis: Context-free Grammars, Pushdown Automata and Parsing Part - 3. Y.N. Srikant
Syntax Analysis: Context-free Grammars, Pushdown Automata and Part - 3 Department of Computer Science and Automation Indian Institute of Science Bangalore 560 012 NPTEL Course on Principles of Compiler
More informationAmbiguity, Precedence, Associativity & Top-Down Parsing. Lecture 9-10
Ambiguity, Precedence, Associativity & Top-Down Parsing Lecture 9-10 (From slides by G. Necula & R. Bodik) 2/13/2008 Prof. Hilfinger CS164 Lecture 9 1 Administrivia Team assignments this evening for all
More informationCSE302: Compiler Design
CSE302: Compiler Design Instructor: Dr. Liang Cheng Department of Computer Science and Engineering P.C. Rossin College of Engineering & Applied Science Lehigh University February 27, 2007 Outline Recap
More informationCMSC 330: Organization of Programming Languages. Pushdown Automata Parsing
CMSC 330: Organization of Programming Languages Pushdown Automata Parsing Chomsky Hierarchy Categorization of various languages and grammars Each is strictly more restrictive than the previous First described
More informationBottom-Up Parsing. Ÿ rm E + F *idÿ rm E +id*idÿ rm T +id*id. Ÿ rm F +id*id Ÿ rm id + id * id
Bottom-Up Parsing Attempts to traverse a parse tree bottom up (post-order traversal) Reduces a sequence of tokens to the start symbol At each reduction step, the RHS of a production is replaced with LHS
More informationContext free languages
Context free languages Syntatic parsers and parse trees E! E! *! E! (! E! )! E + E! id! id! id! 2 Context Free Grammars The CF grammar production rules have the following structure X α being X N and α
More informationCYK Algorithm for Parsing General Context-Free Grammars
CYK Algorithm for Parsing General Context-Free Grammars Why Parse General Grammars Can be difficult or impossible to make grammar unambiguous thus LL(k) and LR(k) methods cannot work, for such ambiguous
More informationCA Compiler Construction
CA4003 - Compiler Construction Bottom Up Parsing David Sinclair Bottom Up Parsing LL(1) parsers have enjoyed a bit of a revival thanks to JavaCC. LL(k) parsers must predict which production rule to use
More informationParsing -3. A View During TD Parsing
Parsing -3 Deterministic table-driven parsing techniques Pictorial view of TD and BU parsing BU (shift-reduce) Parsing Handle, viable prefix, items, closures, goto s LR(k): SLR(1), LR(1) Problems with
More informationLanguages. Languages. An Example Grammar. Grammars. Suppose we have an alphabet V. Then we can write:
Languages A language is a set (usually infinite) of strings, also known as sentences Each string consists of a sequence of symbols taken from some alphabet An alphabet, V, is a finite set of symbols, e.g.
More informationCS153: Compilers Lecture 5: LL Parsing
CS153: Compilers Lecture 5: LL Parsing Stephen Chong https://www.seas.harvard.edu/courses/cs153 Announcements Proj 1 out Due Thursday Sept 20 (2 days away) Proj 2 out Due Thursday Oct 4 (16 days away)
More informationParsing Algorithms. CS 4447/CS Stephen Watt University of Western Ontario
Parsing Algorithms CS 4447/CS 9545 -- Stephen Watt University of Western Ontario The Big Picture Develop parsers based on grammars Figure out properties of the grammars Make tables that drive parsing engines
More informationBottom up parsing. General idea LR(0) SLR LR(1) LALR To best exploit JavaCUP, should understand the theoretical basis (LR parsing);
Bottom up parsing General idea LR(0) SLR LR(1) LALR To best exploit JavaCUP, should understand the theoretical basis (LR parsing); 1 Top-down vs Bottom-up Bottom-up more powerful than top-down; Can process
More informationINF5110 Compiler Construction
INF5110 Compiler Construction Parsing Spring 2016 1 / 84 Overview First and Follow set: general concepts for grammars textbook looks at one parsing technique (top-down) [Louden, 1997, Chap. 4] before studying
More informationParsing. Context-Free Grammars (CFG) Laura Kallmeyer. Winter 2017/18. Heinrich-Heine-Universität Düsseldorf 1 / 26
Parsing Context-Free Grammars (CFG) Laura Kallmeyer Heinrich-Heine-Universität Düsseldorf Winter 2017/18 1 / 26 Table of contents 1 Context-Free Grammars 2 Simplifying CFGs Removing useless symbols Eliminating
More informationCompiling Techniques
Lecture 5: Top-Down Parsing 26 September 2017 The Parser Context-Free Grammar (CFG) Lexer Source code Scanner char Tokeniser token Parser AST Semantic Analyser AST IR Generator IR Errors Checks the stream
More informationContext Free Grammars
Automata and Formal Languages Context Free Grammars Sipser pages 101-111 Lecture 11 Tim Sheard 1 Formal Languages 1. Context free languages provide a convenient notation for recursive description of languages.
More informationContext-free Grammars and Languages
Context-free Grammars and Languages COMP 455 002, Spring 2019 Jim Anderson (modified by Nathan Otterness) 1 Context-free Grammars Context-free grammars provide another way to specify languages. Example:
More informationCompiling Techniques
Lecture 5: Top-Down Parsing 6 October 2015 The Parser Context-Free Grammar (CFG) Lexer Source code Scanner char Tokeniser token Parser AST Semantic Analyser AST IR Generator IR Errors Checks the stream
More informationBottom-Up Syntax Analysis
Bottom-Up Syntax Analysis Mooly Sagiv http://www.cs.tau.ac.il/~msagiv/courses/wcc13.html Textbook:Modern Compiler Design Chapter 2.2.5 (modified) 1 Pushdown automata Deterministic fficient Parsers Report
More informationComputer Science 160 Translation of Programming Languages
Computer Science 160 Translation of Programming Languages Instructor: Christopher Kruegel Top-Down Parsing Top-down Parsing Algorithm Construct the root node of the parse tree, label it with the start
More informationCreating a Recursive Descent Parse Table
Creating a Recursive Descent Parse Table Recursive descent parsing is sometimes called LL parsing (Left to right examination of input, Left derivation) Consider the following grammar E TE' E' +TE' T FT'
More informationContext-Free Grammars (and Languages) Lecture 7
Context-Free Grammars (and Languages) Lecture 7 1 Today Beyond regular expressions: Context-Free Grammars (CFGs) What is a CFG? What is the language associated with a CFG? Creating CFGs. Reasoning about
More informationThis lecture covers Chapter 5 of HMU: Context-free Grammars
This lecture covers Chapter 5 of HMU: Context-free rammars (Context-free) rammars (Leftmost and Rightmost) Derivations Parse Trees An quivalence between Derivations and Parse Trees Ambiguity in rammars
More informationAnnouncement: Midterm Prep
Announcement: Midterm Prep Midterm is Tuesday, 3/13 at 11 in three classrooms - Last name A-C Van Vleck B115 - Last name D-J Van Vleck B135 - Last name K-Z CS1240 List of topics Up to and including lecture
More informationCompiling Techniques
Lecture 3: Introduction to 22 September 2017 Reminder Action Create an account and subscribe to the course on piazza. Coursework Starts this afternoon (14.10-16.00) Coursework description is updated regularly;
More informationAdministrivia. Test I during class on 10 March. Bottom-Up Parsing. Lecture An Introductory Example
Administrivia Test I during class on 10 March. Bottom-Up Parsing Lecture 11-12 From slides by G. Necula & R. Bodik) 2/20/08 Prof. Hilfinger CS14 Lecture 11 1 2/20/08 Prof. Hilfinger CS14 Lecture 11 2 Bottom-Up
More informationOutline. CS21 Decidability and Tractability. Machine view of FA. Machine view of FA. Machine view of FA. Machine view of FA.
Outline CS21 Decidability and Tractability Lecture 5 January 16, 219 and Languages equivalence of NPDAs and CFGs non context-free languages January 16, 219 CS21 Lecture 5 1 January 16, 219 CS21 Lecture
More informationContext-Free Grammars and Languages
Context-Free Grammars and Languages Seungjin Choi Department of Computer Science and Engineering Pohang University of Science and Technology 77 Cheongam-ro, Nam-gu, Pohang 37673, Korea seungjin@postech.ac.kr
More informationh>p://lara.epfl.ch Compiler Construc/on 2011 CYK Algorithm and Chomsky Normal Form
h>p://lara.epfl.ch Compiler Construc/on 2011 CYK Algorithm and Chomsky Normal Form S à N ( N S) N ( N ) S S Parsing an Input N S) à S N ) N ( à ( N ) à ) 7 6 5 4 3 2 1 ambiguity N ( N ( N ) N ( N ) N (
More information5/10/16. Grammar. Automata and Languages. Today s Topics. Grammars Definition A grammar G is defined as G = (V, T, P, S) where:
Grammar Automata and Languages Grammar Prof. Mohamed Hamada oftware Engineering Lab. The University of Aizu Japan Regular Grammar Context-free Grammar Context-sensitive Grammar Left-linear Grammar right-linear
More informationShift-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.
Bottom-up Parsing Bottom-up Parsing Until now we started with the starting nonterminal S and tried to derive the input from it. In a way, this isn t the natural thing to do. It s much more logical to start
More informationFormal Languages, Grammars and Automata Lecture 5
Formal Languages, Grammars and Automata Lecture 5 Helle Hvid Hansen helle@cs.ru.nl http://www.cs.ru.nl/~helle/ Foundations Group Intelligent Systems Section Institute for Computing and Information Sciences
More informationExercises. Exercise: Grammar Rewriting
Exercises Text adapted from : Alessandro Artale, Free University of Bolzano Exercise: Grammar Rewriting Consider the following grammar for Boolean expressions: Bexp Bexp or Bterm Bterm Bterm Bterm and
More informationContext-Free Grammars and Languages. Reading: Chapter 5
Context-Free Grammars and Languages Reading: Chapter 5 1 Context-Free Languages The class of context-free languages generalizes the class of regular languages, i.e., every regular language is a context-free
More informationEXAM. CS331 Compiler Design Spring Please read all instructions, including these, carefully
EXAM Please read all instructions, including these, carefully There are 7 questions on the exam, with multiple parts. You have 3 hours to work on the exam. The exam is open book, open notes. Please write
More informationINF5110 Compiler Construction
INF5110 Compiler Construction Spring 2017 1 / 330 Outline 1. Parsing First and follow sets Top-down parsing Bottom-up parsing References 2 / 330 INF5110 Compiler Construction Parsing Spring 2017 3 / 330
More informationSyntactical analysis. Syntactical analysis. Syntactical analysis. Syntactical analysis
Context-free grammars Derivations Parse Trees Left-recursive grammars Top-down parsing non-recursive predictive parsers construction of parse tables Bottom-up parsing shift/reduce parsers LR parsers GLR
More informationChapter 4: Context-Free Grammars
Chapter 4: Context-Free Grammars 4.1 Basics of Context-Free Grammars Definition A context-free grammars, or CFG, G is specified by a quadruple (N, Σ, P, S), where N is the nonterminal or variable alphabet;
More informationSyntax Analysis, VI Examples from LR Parsing. Comp 412
COMP 412 FALL 2017 Syntax Analysis, VI Examples from LR Parsing Comp 412 source code IR IR target code Front End Optimizer Back End Copyright 2017, Keith D. Cooper & Linda Torczon, all rights reserved.
More informationFrom EBNF to PEG. Roman R. Redziejowski. Concurrency, Specification and Programming Berlin 2012
Concurrency, Specification and Programming Berlin 2012 EBNF: Extended Backus-Naur Form A way to define grammar. EBNF: Extended Backus-Naur Form A way to define grammar. Literal = Decimal Binary Decimal
More informationDefinition: A grammar G = (V, T, P,S) is a context free grammar (cfg) if all productions in P have the form A x where
Recitation 11 Notes Context Free Grammars Definition: A grammar G = (V, T, P,S) is a context free grammar (cfg) if all productions in P have the form A x A V, and x (V T)*. Examples Problem 1. Given the
More informationCompiler Principles, PS7
Top-Down Parsing Compiler Principles, PS7 Parsing problem definition: The general parsing problem is - given set of rules and input stream (in our case scheme token input stream), how to find the parse
More informationTop-Down Parsing, Part II
op-down Parsing, Part II Announcements Programming Project 1 due Friday, 11:59PM Office hours every day until then. ubmission instructions will be emailed out tonight. Where We Are ource Code Lexical Analysis
More informationBottom-up syntactic parsing. LR(k) grammars. LR(0) grammars. Bottom-up parser. Definition Properties
Course 9-10 1 Bottom-up syntactic parsing Bottom-up parser LR(k) grammars Definition Properties LR(0) grammars Characterization theorem for LR(0) LR(0) automaton 2 a 1... a i... a n # X 1 X 1 Control Parsing
More informationIntroduction to Theory of Computing
CSCI 2670, Fall 2012 Introduction to Theory of Computing Department of Computer Science University of Georgia Athens, GA 30602 Instructor: Liming Cai www.cs.uga.edu/ cai 0 Lecture Note 3 Context-Free Languages
More informationTAFL 1 (ECS-403) Unit- III. 3.1 Definition of CFG (Context Free Grammar) and problems. 3.2 Derivation. 3.3 Ambiguity in Grammar
TAFL 1 (ECS-403) Unit- III 3.1 Definition of CFG (Context Free Grammar) and problems 3.2 Derivation 3.3 Ambiguity in Grammar 3.3.1 Inherent Ambiguity 3.3.2 Ambiguous to Unambiguous CFG 3.4 Simplification
More informationCourse Script INF 5110: Compiler construction
Course Script INF 5110: Compiler construction INF5110, spring 2018 Martin Steffen ii Contents Contents 4 Parsing 1 4.1 Introduction to parsing........................ 1 4.2 Top-down parsing...........................
More informationCompilerconstructie. najaar Rudy van Vliet kamer 124 Snellius, tel rvvliet(at)liacs.
Compilerconstructie najaar 2012 http://www.liacs.nl/home/rvvliet/coco/ Rudy van Vliet kamer 124 Snellius, tel. 071-527 5777 rvvliet(at)liacs.nl werkcollege 9, dinsdag 27 november 2012 SLR Parsing / Backpatching
More informationCFLs and Regular Languages. CFLs and Regular Languages. CFLs and Regular Languages. Will show that all Regular Languages are CFLs. Union.
We can show that every RL is also a CFL Since a regular grammar is certainly context free. We can also show by only using Regular Expressions and Context Free Grammars That is what we will do in this half.
More informationComputer Science 160 Translation of Programming Languages
Computer Science 160 Translation of Programming Languages Instructor: Christopher Kruegel Building a Handle Recognizing Machine: [now, with a look-ahead token, which is LR(1) ] LR(k) items An LR(k) item
More informationProf. Mohamed Hamada Software Engineering Lab. The University of Aizu Japan
Language Processing Systems Prof. Mohamed Hamada Software Engineering La. The University of izu Japan Syntax nalysis (Parsing) 1. Uses Regular Expressions to define tokens 2. Uses Finite utomata to recognize
More informationLecture VII Part 2: Syntactic Analysis Bottom-up Parsing: LR Parsing. Prof. Bodik CS Berkley University 1
Lecture VII Part 2: Syntactic Analysis Bottom-up Parsing: LR Parsing. Prof. Bodik CS 164 -- Berkley University 1 Bottom-Up Parsing Bottom-up parsing is more general than topdown parsing And just as efficient
More information5 Context-Free Languages
CA320: COMPUTABILITY AND COMPLEXITY 1 5 Context-Free Languages 5.1 Context-Free Grammars Context-Free Grammars Context-free languages are specified with a context-free grammar (CFG). Formally, a CFG G
More informationTalen en Compilers. Johan Jeuring , period 2. October 29, Department of Information and Computing Sciences Utrecht University
Talen en Compilers 2015-2016, period 2 Johan Jeuring Department of Information and Computing Sciences Utrecht University October 29, 2015 12. LL parsing 12-1 This lecture LL parsing Motivation Stack-based
More informationCS 301. Lecture 18 Decidable languages. Stephen Checkoway. April 2, 2018
CS 301 Lecture 18 Decidable languages Stephen Checkoway April 2, 2018 1 / 26 Decidable language Recall, a language A is decidable if there is some TM M that 1 recognizes A (i.e., L(M) = A), and 2 halts
More informationSyntax Analysis, VII The Canonical LR(1) Table Construction. Comp 412
COMP 412 FALL 2018 Syntax Analysis, VII The Canonical LR(1) Table Construction Comp 412 source code IR IR target Front End Optimizer Back End code Copyright 2018, Keith D. Cooper & Linda Torczon, all rights
More informationAn Efficient Context-Free Parsing Algorithm. Speakers: Morad Ankri Yaniv Elia
An Efficient Context-Free Parsing Algorithm Speakers: Morad Ankri Yaniv Elia Yaniv: Introduction Terminology Informal Explanation The Recognizer Morad: Example Time and Space Bounds Empirical results Practical
More informationIntroduction and Motivation. Introduction and Motivation. Introduction to Computability. Introduction and Motivation. Theory. Lecture5: Context Free
ntroduction to Computability Theory Lecture5: Context Free Languages Prof. Amos sraeli 1 ntroduction and Motivation n our study of RL-s we Covered: 1. Motivation and definition of regular languages. 2.
More informationParsing with CFGs L445 / L545 / B659. Dept. of Linguistics, Indiana University Spring Parsing with CFGs. Direction of processing
L445 / L545 / B659 Dept. of Linguistics, Indiana University Spring 2016 1 / 46 : Overview Input: a string Output: a (single) parse tree A useful step in the process of obtaining meaning We can view the
More informationINF5110 Compiler Construction
INF5110 Compiler Construction Parsing Spring 2016 1 / 131 Outline 1. Parsing Bottom-up parsing Bibs 2 / 131 Outline 1. Parsing Bottom-up parsing Bibs 3 / 131 Bottom-up parsing: intro LR(0) SLR(1) LALR(1)
More informationParsing with CFGs. Direction of processing. Top-down. Bottom-up. Left-corner parsing. Chart parsing CYK. Earley 1 / 46.
: Overview L545 Dept. of Linguistics, Indiana University Spring 2013 Input: a string Output: a (single) parse tree A useful step in the process of obtaining meaning We can view the problem as searching
More informationUNIT II REGULAR LANGUAGES
1 UNIT II REGULAR LANGUAGES Introduction: A regular expression is a way of describing a regular language. The various operations are closure, union and concatenation. We can also find the equivalent regular
More informationTheory of Computation - Module 3
Theory of Computation - Module 3 Syllabus Context Free Grammar Simplification of CFG- Normal forms-chomsky Normal form and Greibach Normal formpumping lemma for Context free languages- Applications of
More informationType 3 languages. Type 2 languages. Regular grammars Finite automata. Regular expressions. Type 2 grammars. Deterministic Nondeterministic.
Course 7 1 Type 3 languages Regular grammars Finite automata Deterministic Nondeterministic Regular expressions a, a, ε, E 1.E 2, E 1 E 2, E 1*, (E 1 ) Type 2 languages Type 2 grammars 2 Brief history
More informationSyntax Directed Transla1on
Syntax Directed Transla1on Syntax Directed Transla1on CMPT 379: Compilers Instructor: Anoop Sarkar anoopsarkar.github.io/compilers-class Syntax directed Translation Models for translation from parse trees
More informationSimplification and Normalization of Context-Free Grammars
implification and Normalization, of Context-Free Grammars 20100927 Slide 1 of 23 Simplification and Normalization of Context-Free Grammars 5DV037 Fundamentals of Computer Science Umeå University Department
More informationChapter 5: Context-Free Languages
Chapter 5: Context-Free Languages Peter Cappello Department of Computer Science University of California, Santa Barbara Santa Barbara, CA 93106 cappello@cs.ucsb.edu Please read the corresponding chapter
More informationContext-Free Languages
CS:4330 Theory of Computation Spring 2018 Context-Free Languages Non-Context-Free Languages Haniel Barbosa Readings for this lecture Chapter 2 of [Sipser 1996], 3rd edition. Section 2.3. Proving context-freeness
More informationContext-Free Grammars and Languages. We have seen that many languages cannot be regular. Thus we need to consider larger classes of langs.
Context-Free Grammars and Languages We have seen that many languages cannot be regular. Thus we need to consider larger classes of langs. Contex-Free Languages (CFL s) played a central role natural languages
More informationSuppose h maps number and variables to ɛ, and opening parenthesis to 0 and closing parenthesis
1 Introduction Parenthesis Matching Problem Describe the set of arithmetic expressions with correctly matched parenthesis. Arithmetic expressions with correctly matched parenthesis cannot be described
More informationPushdown Automata. Reading: Chapter 6
Pushdown Automata Reading: Chapter 6 1 Pushdown Automata (PDA) Informally: A PDA is an NFA-ε with a infinite stack. Transitions are modified to accommodate stack operations. Questions: What is a stack?
More information