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

Size: px
Start display at page:

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

Transcription

1 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

2 Administrivia Team assignments this evening for all those not listed as having one. HW#3 is now available, due next Tuesday morning (Monday is a holiday). 2/13/2008 Prof. Hilfinger CS164 Lecture 9 2

3 Remaining Issues How do we find a derivation of s? Ambiguity: what if there is more than one parse tree (erpretation) for some string s? rrors: what if there is no parse tree for some string s? Given a derivation, how do we construct an abstract syntax tree from it? 2/13/2008 Prof. Hilfinger CS164 Lecture 9 3

4 Ambiguity Grammar + * ( ) Strings + + * + 2/13/2008 Prof. Hilfinger CS164 Lecture 9 4

5 Ambiguity. xample The string + + has two parse trees is left-associative 2/13/2008 Prof. Hilfinger CS164 Lecture 9 5

6 Ambiguity. xample The string * + has two parse trees + * * + * has higher precedence than + 2/13/2008 Prof. Hilfinger CS164 Lecture 9 6

7 Ambiguity (Cont.) A grammar is ambiguous if it has more than one parse tree for some string quivalently, there is more than one rightmost or leftmost derivation for some string Ambiguity is bad Leaves meaning of some programs ill-defined Ambiguity is common in programming languages Arithmetic expressions IF-THN-LS 2/13/2008 Prof. Hilfinger CS164 Lecture 9 7

8 Dealing with Ambiguity There are several ways to handle ambiguity Most direct method is to rewrite the grammar unambiguously + T T T T * ( ) nforces precedence of * over + nforces left-associativity of + and * 2/13/2008 Prof. Hilfinger CS164 Lecture 9 8

9 Ambiguity. xample The * + has only one parse tree now + T * T + T * 2/13/2008 Prof. Hilfinger CS164 Lecture 9 9

10 Ambiguity: The Dangling lse Consider the grammar if then if then else OTHR This grammar is also ambiguous 2/13/2008 Prof. Hilfinger CS164 Lecture 9 10

11 The Dangling lse: xample The expression if 1 then if 2 then 3 else 4 has two parse trees if if 1 if 4 1 if Typically we want the second form 2/13/2008 Prof. Hilfinger CS164 Lecture 9 11

12 The Dangling lse: A Fix else matches the closest unmatched then We can describe this in the grammar (distinguish between matched and unmatched then ) MIF /* all then are matched */ UIF /* some then are unmatched */ MIF if then MIF else MIF OTHR UIF if then if then MIF else UIF Describes the same set of strings 2/13/2008 Prof. Hilfinger CS164 Lecture 9 12

13 The Dangling lse: xample Revisited The expression if 1 then if 2 then 3 else 4 if if 1 if 1 if A valid parse tree (for a UIF) Not valid because the then expression is not a MIF 2/13/2008 Prof. Hilfinger CS164 Lecture 9 13

14 Ambiguity Impossible to convert automatically an ambiguous grammar to an unambiguous one Used with care, ambiguity can simplify the grammar Sometimes allows more natural definitions But we need disambiguation mechanisms Instead of rewriting the grammar Use the more natural (ambiguous) grammar Along with disambiguating declarations Most tools allow precedence and associativity declarations to disambiguate grammars xamples 2/13/2008 Prof. Hilfinger CS164 Lecture 9 14

15 Associativity Declarations Consider the grammar + Ambiguous: two parse trees of Left-associativity declaration: %left + 2/13/2008 Prof. Hilfinger CS164 Lecture 9 15

16 Precedence Declarations Consider the grammar + * And the string + * * + + * 2/13/2008 Prof. Hilfinger CS164 Lecture 9 16 Precedence declarations: %left + %left *

17 How It s Done I: Intro to Top-Down Parsing Terminals are seen in order of appearance in the token stream: t 1 A B t 4 t 1 t 2 t 3 t 4 t 5 The parse tree is constructed C D From the top From left to right t 2 t 3 t 4 As for leftmost derivation 2/13/2008 Prof. Hilfinger CS164 Lecture 9 17

18 Top-down Depth-First Parsing Consider the grammar T + T T ( ) * T Token stream is: * Start with top-level non-terminal Try the rules for in order 2/13/2008 Prof. Hilfinger CS164 Lecture 9 18

19 Depth-First Parsing. xample * Start with start symbol Try T + T + Then try a rule for T ( ) () + But ( input ; backtrack to T + Try T. Token matches. + But + input *; back to T + Try T * T *T+ But (skipping some steps) + can t be matched Must backtrack to 2/13/2008 Prof. Hilfinger CS164 Lecture 9 19

20 Depth-First Parsing. xample * Try T Follow same steps as before for T And succeed with T * T and T With the following parse tree T * T 2/13/2008 Prof. Hilfinger CS164 Lecture 9 20

21 Depth-First Parsing Parsing: given a string of tokens t 1 t 2... t n, find a leftmost derivation (and thus, parse tree) Depth-first parsing: Beginning with start symbol, try each production exhaustively on leftmost non-terminal in current sentential form and recurse. 2/13/2008 Prof. Hilfinger CS164 Lecture 9 21

22 Depth-First Parsing of t 1 t 2 t n At a given moment, have sentential form that looks like this: t 1 t 2 t k A, 0 k n Initially, k=0 and A is just start symbol Try a production for A: if A BC is a production, the new form is t 1 t 2 t k B C Backtrack when leading terminals aren t prefix of t 1 t 2 t n and try another production Stop when no more non-terminals and terminals all matched (accept) or no more productions left (reject) 2/13/2008 Prof. Hilfinger CS164 Lecture 9 22

23 When Depth-First Doesn t Work Well Consider productions S S a a: In the process of parsing S we try the above rules Applied consistently in this order, get infinite loop Could re-order productions, but search will have lots of backtracking and general rule for ordering is complex Problem here is left-recursive grammar: one that has a non-terminal S S + Sα for some α 2/13/2008 Prof. Hilfinger CS164 Lecture 9 23

24 limination of Left Recursion Consider the left-recursive grammar S S α β S generates all strings starting with a β and followed by any number of α s Can rewrite using right-recursion S β S S α S ε 2/13/2008 Prof. Hilfinger CS164 Lecture 9 24

25 limination of left Recursion. xample Consider the grammar S 1 S 0 ( β = 1 and α = 0 ) can be rewritten as S 1 S S 0 S ε 2/13/2008 Prof. Hilfinger CS164 Lecture 9 25

26 More limination of Left Recursion In general S S α 1 S α n β 1 β m All strings derived from S start with one of β 1,,β m and continue with several instances of α 1,,α n Rewrite as S β 1 S β m S S α 1 S α n S ε 2/13/2008 Prof. Hilfinger CS164 Lecture 9 26

27 General Left Recursion The grammar S A α δ (1) A S β (2) is also left-recursive because S + S β α This left recursion can also be eliminated by first substituting (2) o (1) There is a general algorithm (e.g. Aho, Sethi, Ullman 4.3) But personally, I d just do this by hand. 2/13/2008 Prof. Hilfinger CS164 Lecture 9 27

28 An Alternative Approach Instead of reordering or rewriting grammar, can use top-down breadth-first search. S S a a String: aaa S S a a a S a a S a a (not all matched) a a a a a 2/13/2008 Prof. Hilfinger CS164 Lecture 9 28

29 Summary of Top-Down Parsing So Far Simple and general parsing strategy Left recursion must be eliminated first but that can be done automatically Or can use breadth-first search But backtracking (depth-first) or maaining list of possible sentential forms (breadthfirst) can make it slow Often, though, we can avoid both 2/13/2008 Prof. Hilfinger CS164 Lecture 9 29

30 Predictive Parsers Modification of depth-first parsing in which parser predicts which production to use By looking at the next few tokens No backtracking Predictive parsers accept LL(k) grammars L means left-to-right scan of input L means leftmost derivation k means need k tokens of lookahead to predict In practice, LL(1) is used 2/13/2008 Prof. Hilfinger CS164 Lecture 9 30

31 Recursive Descent: Grammar as Program In recursive descent, we think of a grammar as a program. Here, we ll look at predictive version. ach non-terminal is turned o a procedure ach right-hand side transliterated o part of the procedure body for its non-terminal First, define next() current token of input scan(t) check that next()=t (else RROR), and then read new token. 2/13/2008 Prof. Hilfinger CS164 Lecture 9 31

32 Recursive Descent: xample P S $ S T S S + S ε T ( S ) ($ = end marker) def P (): S(); scan($) def S(): T(); S () def S (): predict if next() == + : scan( + ); S() elif next() in [ ), $]: pass else: RROR def T(): if next () == : scan() elif next() == ( : scan( ( ); S(); scan ( ) ) else: RROR But where do prediction tests come from? 2/13/2008 Prof. Hilfinger CS164 Lecture 9 32

33 Predicting Right-hand Sides The if-tests are conditions by which parser predicts which right-hand side to use. In our example, used only next symbol (LL(1)); but could use more. Can be specified as a 2D table One dimension for current non-terminal to expand One dimension for next token A table entry contains one production 2/13/2008 Prof. Hilfinger CS164 Lecture 9 33

34 But First: Left Factoring With the grammar T + T T * T ( ) Impossible to predict because For T two productions start with For it is not clear how to predict A grammar must be left-factored before use for predictive parsing 2/13/2008 Prof. Hilfinger CS164 Lecture 9 34

35 Left-Factoring xample Starting with the grammar T + T T * T ( ) Factor out common prefixes of productions T X X + ε T ( ) Y Y * T ε 2/13/2008 Prof. Hilfinger CS164 Lecture 9 35

36 Recursive Descent for Real So far, have presented a purist view. In fact, use of recursive descent makes life simpler in many ways if we cheat a bit. Here s how you really handle left recursion in recursive descent, for S S A R: def S(): R () while next() FIRST(A): A() It s a program: all kinds of shortcuts possible. 2/13/2008 Prof. Hilfinger CS164 Lecture 9 36

37 LL(1) Parsing Table xample Left-factored grammar T X T ( ) Y X + ε Y * T ε The LL(1) parsing table ($ is a special end marker): * + ( ) $ T Y ( ) T X T X X + ε ε Y * T ε ε ε 2/13/2008 Prof. Hilfinger CS164 Lecture 9 37

38 LL(1) Parsing Table xample (Cont.) Consider the [, ] entry Means When current non-terminal is and next input is, use production T X T X can generate string with in front Consider the [Y,+] entry When current non-terminal is Y and current token is +, get rid of Y We ll see later why this is right 2/13/2008 Prof. Hilfinger CS164 Lecture 9 38

39 LL(1) Parsing Tables. rrors Blank entries indicate error situations Consider the [,*] entry There is no way to derive a string starting with * from non-terminal 2/13/2008 Prof. Hilfinger CS164 Lecture 9 39

40 Using Parsing Tables ssentially table-driven recursive descent, except We use a stack to keep track of pending nonterminals We reject when we encounter an error state We accept when we encounter end-of-input 2/13/2008 Prof. Hilfinger CS164 Lecture 9 40

41 LL(1) Parsing Algorithm initialize stack = <S,$> repeat case stack of <X, rest> : if T[X,next()] == Y 1 Y n : stack <Y 1 Y n rest>; else: error (); <t, rest> : scan (t); stack <rest>; until stack == < > 2/13/2008 Prof. Hilfinger CS164 Lecture 9 41

42 LL(1) Parsing xample Stack Input Action $ * $ T X T X $ * $ Y Y X $ * $ terminal Y X $ * $ * T * T X $ * $ terminal T X $ $ Y Y X $ $ terminal Y X $ $ ε X $ $ ε $ $ ACCPT 2/13/2008 Prof. Hilfinger CS164 Lecture 9 42

43 Constructing Parsing Tables LL(1) languages are those definable by a parsing table for the LL(1) algorithm such that no table entry is multiply defined Once we have the table Can create table-driver or recursive-descent parser The parsing algorithms are simple and fast No backtracking is necessary We want to generate parsing tables from CFG 2/13/2008 Prof. Hilfinger CS164 Lecture 9 43

44 Predicting Productions I Top-down parsing expands a parse tree from the start symbol to the leaves Always expand the leftmost non-terminal T + * + 2/13/2008 Prof. Hilfinger CS164 Lecture 9 44

45 Predicting Productions II Top-down parsing expands a parse tree from the start symbol to the leaves Always expand the leftmost non-terminal T * * T + + The leaves at any po form a string βaγ β contains only terminals The input string is βbδ The prefix β matches The next token is b 2/13/2008 Prof. Hilfinger CS164 Lecture 9 45

46 Predicting Productions III Top-down parsing expands a parse tree from the start symbol to the leaves Always expand the leftmost non-terminal T * * T + T + The leaves at any po form a string βaγ (A=T, γ=ε) β contains only terminals γ contains any symbols The input string is βbδ (b=) So Aγ must derive bδ 2/13/2008 Prof. Hilfinger CS164 Lecture 9 46

47 Predicting Productions IV Top-down parsing expands a parse tree from the start symbol to the leaves Always expand the leftmost non-terminal T * * T + T + So choose production for T that can eventually derive something that starts with 2/13/2008 Prof. Hilfinger CS164 Lecture 9 47

48 Predicting Productions V Go back to previous grammar, with T X X + ε T ( ) Y Y * T ε Here, YX must match + T Y X Since + doesn t start with *, can t use Y * T + 2/13/2008 Prof. Hilfinger CS164 Lecture 9 48

49 Predicting Productions V Go back to previous grammar, with T X X + ε T ( ) Y Y * T ε T X But + can follow Y, so we choose Y ε Y + 2/13/2008 Prof. Hilfinger CS164 Lecture 9 49

50 FIRST and FOLLOW To summarize, if we are trying to predict how to expand A with b the next token, then either: b belongs to an expansion of A Any A α can be used if b can start a string derived from α In this case we say that b First(α) or b does not belong to an expansion of A, A has an expansion that derives ε, and b belongs to something that can follow A (so S * βabω) We say that b Follow(A) in this case. 2/13/2008 Prof. Hilfinger CS164 Lecture 9 50

51 Summary of Definitions For b T, the set of terminals; α a sequence of terminal & non-terminal symbols, S the start symbol, A N, the set of non-terminals: FIRST(α) T { ε } b FIRST(α) iff α * b ε FIRST(α) iff α * ε FOLLOW(A) T b FOLLOW(A) iff S * A b 2/13/2008 Prof. Hilfinger CS164 Lecture 9 51

52 Computing First Sets Definition First(X) = { b X * bα} {ε X * ε}, X any grammar symbol. 1. First(b) = { b } for b any terminal symbol 2. For all productions X A 1 A n Add First(A 1 ) {ε} to First(X). Stop if ε First(A 1 ) Add First(A 2 ) {ε} to First(X). Stop if ε First(A 2 ) Add First(A n ) {ε} to First(X). Stop if ε First(A n ) Add ε to First(X) 3. Repeat 2 until nothing changes ( compute fixed po ) 2/13/2008 Prof. Hilfinger CS164 Lecture 9 52

53 Computing First Sets, Contd. That takes care of single-symbol case. In general: FIRST(X 1 X 2 X k ) = FIRST(X 1 ) FIRST(X 2 ) if ε FIRST(X 1 ) FIRST(X k ) if ε FIRST(X 1 X 2 X k-1 ) - { ε } unless ε FIRST(X i ) i 2/13/2008 Prof. Hilfinger CS164 Lecture 9 53

54 First Sets. xample For the grammar T X T ( ) Y X + ε Y * T ε First sets First( ( ) = { ( } First( T ) = {, ( } First( ) ) = { ) } First( ) = {, ( } First( ) = { } First( X ) = {+, ε } First( + ) = { + } First( Y ) = {*, ε } First( * ) = { * } 2/13/2008 Prof. Hilfinger CS164 Lecture 9 54

55 Computing Follow Sets Definition Follow(X) = { b S * β X b ω } 1. Compute the First sets for all non-terminals first 2. Add $ to Follow(S) (if S is the start non-terminal) 3. For all productions Y X A 1 A n Add First(A 1 ) {ε} to Follow(X). Stop if ε First(A 1 ) Add First(A 2 ) {ε} to Follow(X). Stop if ε First(A 2 ) Add First(A n ) {ε} to Follow(X). Stop if ε First(A n ) Add Follow(Y) to Follow(X) 4. Repeat 3 until nothing changes. 2/13/2008 Prof. Hilfinger CS164 Lecture 9 55

56 Follow Sets. xample For the grammar T X T ( ) Y Follow sets Follow( ) = {), $} Follow( X ) = {$, ) } Follow( Y ) = {+, ), $} Follow( T ) = {+, ), $} X + ε Y * T ε 2/13/2008 Prof. Hilfinger CS164 Lecture 9 56

57 Constructing LL(1) Parsing Tables Construct a parsing table T for CFG G For each production A α in G do: For each terminal b First(α) do T[A, b] = α If α * ε, for each b Follow(A) do T[A, b] = α 2/13/2008 Prof. Hilfinger CS164 Lecture 9 57

58 LL(1) Parsing Table xample T X T ( ) Y X + ε Y * T ε * + ( ) $ T Y ( ) T X T X X + ε ε Y * T ε ε ε Follow( ) = {), $} Follow( X ) = {$, ) } Follow( Y ) = {+, ), $} Follow( T ) = {+, ), $} First( T ) = {, ( } First( ) = {, ( } First( X ) = {+, ε } First( Y ) = {*, ε } 2/13/2008 Prof. Hilfinger CS164 Lecture 9 58

59 Notes on LL(1) Parsing Tables If any entry is multiply defined then G is not LL(1). This happens If G is ambiguous If G is left recursive If G is not left-factored And in other cases as well Most programming language grammars are not LL(1) There are tools that build LL(1) tables 2/13/2008 Prof. Hilfinger CS164 Lecture 9 59

60 Review For some grammars there is a simple parsing strategy Predictive parsing (LL(1)) Once you build the LL(1) table (or know the FIRST and FOLLOW sets), you can write the parser by hand: recursive descent. Next: a more powerful parsing strategy for grammars that are not LL(1) 2/13/2008 Prof. Hilfinger CS164 Lecture 9 60

Top-Down Parsing and Intro to Bottom-Up Parsing

Top-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 information

Introduction to Bottom-Up Parsing

Introduction 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 information

Introduction to Bottom-Up Parsing

Introduction 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 information

Introduction to Bottom-Up Parsing

Introduction 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 information

Introduction to Bottom-Up Parsing

Introduction 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 information

Administrivia. 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 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 information

Syntax Analysis Part III

Syntax 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 information

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

n 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 information

CSE302: Compiler Design

CSE302: 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 information

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

Predictive 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 information

Syntax Analysis Part I

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 information

Syntactic Analysis. Top-Down Parsing

Syntactic 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 information

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

Parsing 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 information

Syntax Analysis Part I

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 information

Parsing -3. A View During TD Parsing

Parsing -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 information

CS415 Compilers Syntax Analysis Top-down Parsing

CS415 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 information

CS 314 Principles of Programming Languages

CS 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 information

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

Syntax 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 information

Syntax Analysis: Context-free Grammars, Pushdown Automata and Parsing Part - 3. Y.N. Srikant

Syntax 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 information

Creating a Recursive Descent Parse Table

Creating 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 information

Compiling Techniques

Compiling 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 information

CMSC 330: Organization of Programming Languages. Pushdown Automata Parsing

CMSC 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 information

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

CSC 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 information

Lecture 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 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 information

CS153: Compilers Lecture 5: LL Parsing

CS153: 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 information

Compiler Principles, PS4

Compiler 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 information

Follow sets. LL(1) Parsing Table

Follow 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 information

Compiling Techniques

Compiling 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 information

Compiling Techniques

Compiling 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 information

The 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) 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 information

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

Languages. 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 information

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

Prof. 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 information

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

This 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 information

Syntactical analysis. Syntactical analysis. Syntactical analysis. Syntactical analysis

Syntactical 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 information

Computer Science 160 Translation of Programming Languages

Computer 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 information

Exercises. Exercise: Grammar Rewriting

Exercises. 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 information

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

Talen 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 information

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

CFLs 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 information

Syntax Analysis - Part 1. Syntax Analysis

Syntax 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 information

Parsing VI LR(1) Parsers

Parsing VI LR(1) Parsers Parsing VI LR(1) Parsers N.B.: This lecture uses a left-recursive version of the SheepNoise grammar. The book uses a rightrecursive version. The derivations (& the tables) are different. Copyright 2005,

More information

Bottom-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. Ÿ 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 information

Top-Down Parsing, Part II

Top-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 information

CA Compiler Construction

CA 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 information

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

EXAM. 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 information

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

EXAM. 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 information

CONTEXT FREE GRAMMAR AND

CONTEXT FREE GRAMMAR AND 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

More information

CS 406: Bottom-Up Parsing

CS 406: Bottom-Up Parsing CS 406: Bottom-Up Parsing Stefan D. Bruda Winter 2016 BOTTOM-UP PUSH-DOWN AUTOMATA A different way to construct a push-down automaton equivalent to a given grammar = shift-reduce parser: Given G = (N,

More information

Computing if a token can follow

Computing 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 information

Compiler Construction Lent Term 2015 Lectures (of 16)

Compiler Construction Lent Term 2015 Lectures (of 16) Compiler Construction Lent Term 2015 Lectures 13 --- 16 (of 16) 1. Return to lexical analysis : application of Theory of Regular Languages and Finite Automata 2. Generating Recursive descent parsers 3.

More information

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

CS5371 Theory of Computation. Lecture 7: Automata Theory V (CFG, CFL, CNF) CS5371 Theory of Computation Lecture 7: Automata Theory V (CFG, CFL, CNF) Announcement Homework 2 will be given soon (before Tue) Due date: Oct 31 (Tue), before class Midterm: Nov 3, (Fri), first hour

More information

Compiler Construction Lent Term 2015 Lectures (of 16)

Compiler Construction Lent Term 2015 Lectures (of 16) Compiler Construction Lent Term 2015 Lectures 13 --- 16 (of 16) 1. Return to lexical analysis : application of Theory of Regular Languages and Finite Automata 2. Generating Recursive descent parsers 3.

More information

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

An 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 information

Computer Science 160 Translation of Programming Languages

Computer 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 information

Lecture 11 Context-Free Languages

Lecture 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 information

MA/CSSE 474 Theory of Computation

MA/CSSE 474 Theory of Computation MA/CSSE 474 Theory of Computation CFL Hierarchy CFL Decision Problems Your Questions? Previous class days' material Reading Assignments HW 12 or 13 problems Anything else I have included some slides online

More information

Syntax Analysis, VI Examples from LR Parsing. Comp 412

Syntax 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 information

Context-Free Grammars and Languages

Context-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 information

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.

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. 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 information

CS 301. Lecture 18 Decidable languages. Stephen Checkoway. April 2, 2018

CS 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 information

Lecture Notes on Inductive Definitions

Lecture Notes on Inductive Definitions Lecture Notes on Inductive Definitions 15-312: Foundations of Programming Languages Frank Pfenning Lecture 2 August 28, 2003 These supplementary notes review the notion of an inductive definition and give

More information

Compiler Principles, PS7

Compiler 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 information

INF5110 Compiler Construction

INF5110 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 information

From EBNF to PEG. Roman R. Redziejowski. Concurrency, Specification and Programming Berlin 2012

From 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 information

I 1 : {S S } I 2 : {S X ay, Y X } I 3 : {S Y } I 4 : {X b Y, Y X, X by, X c} I 5 : {X c } I 6 : {S Xa Y, Y X, X by, X c} I 7 : {X by } I 8 : {Y X }

I 1 : {S S } I 2 : {S X ay, Y X } I 3 : {S Y } I 4 : {X b Y, Y X, X by, X c} I 5 : {X c } I 6 : {S Xa Y, Y X, X by, X c} I 7 : {X by } I 8 : {Y X } Let s try building an SLR parsing table for another simple grammar: S XaY Y X by c Y X S XaY Y X by c Y X Canonical collection: I 0 : {S S, S XaY, S Y, X by, X c, Y X} I 1 : {S S } I 2 : {S X ay, Y X }

More information

Lecture Notes on Inductive Definitions

Lecture Notes on Inductive Definitions Lecture Notes on Inductive Definitions 15-312: Foundations of Programming Languages Frank Pfenning Lecture 2 September 2, 2004 These supplementary notes review the notion of an inductive definition and

More information

Bottom-Up Syntax Analysis

Bottom-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 information

Context-free Grammars and Languages

Context-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 information

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

LR2: LR(0) Parsing. LR Parsing. CMPT 379: Compilers Instructor: Anoop Sarkar. anoopsarkar.github.io/compilers-class LR2: LR(0) Parsing LR Parsing CMPT 379: Compilers Instructor: Anoop Sarkar anoopsarkar.github.io/compilers-class Parsing - Roadmap Parser: decision procedure: builds a parse tree Top-down vs. bottom-up

More information

Parsing with Context-Free Grammars

Parsing with Context-Free Grammars Parsing with Context-Free Grammars Berlin Chen 2005 References: 1. Natural Language Understanding, chapter 3 (3.1~3.4, 3.6) 2. Speech and Language Processing, chapters 9, 10 NLP-Berlin Chen 1 Grammars

More information

CISC4090: Theory of Computation

CISC4090: Theory of Computation CISC4090: Theory of Computation Chapter 2 Context-Free Languages Courtesy of Prof. Arthur G. Werschulz Fordham University Department of Computer and Information Sciences Spring, 2014 Overview In Chapter

More information

Compiler Construction Lectures 13 16

Compiler Construction Lectures 13 16 Compiler Construction Lectures 13 16 Lent Term, 2013 Lecturer: Timothy G. Griffin 1 Generating Lexical Analyzers Source Program Lexical Analyzer tokens Parser Lexical specification Scanner Generator LEX

More information

Announcement: Midterm Prep

Announcement: 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 information

Compiler Design Spring 2017

Compiler Design Spring 2017 Compiler Design Spring 2017 3.4 Bottom-up parsing Dr. Zoltán Majó Compiler Group Java HotSpot Virtual Machine Oracle Corporation 1 Bottom up parsing Goal: Obtain rightmost derivation in reverse w S Reduce

More information

Context-Free Grammars (and Languages) Lecture 7

Context-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 information

Compiler construction

Compiler construction Compiler construction Martin Steffen February 20, 2017 Contents 1 Abstract 1 1.1 Parsing.................................................. 1 1.1.1 First and follow sets.....................................

More information

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

Parsing. 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 information

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

60-354, Theory of Computation Fall Asish Mukhopadhyay School of Computer Science University of Windsor 60-354, Theory of Computation Fall 2013 Asish Mukhopadhyay School of Computer Science University of Windsor Pushdown Automata (PDA) PDA = ε-nfa + stack Acceptance ε-nfa enters a final state or Stack is

More information

Review. Earley Algorithm Chapter Left Recursion. Left-Recursion. Rule Ordering. Rule Ordering

Review. Earley Algorithm Chapter Left Recursion. Left-Recursion. Rule Ordering. Rule Ordering Review Earley Algorithm Chapter 13.4 Lecture #9 October 2009 Top-Down vs. Bottom-Up Parsers Both generate too many useless trees Combine the two to avoid over-generation: Top-Down Parsing with Bottom-Up

More information

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

EXAM. 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 information

LL(1) Grammar and parser

LL(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 information

Bottom-up syntactic parsing. LR(k) grammars. LR(0) grammars. Bottom-up parser. Definition Properties

Bottom-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 information

Context Free Grammars

Context 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 information

CS20a: summary (Oct 24, 2002)

CS20a: summary (Oct 24, 2002) CS20a: summary (Oct 24, 2002) Context-free languages Grammars G = (V, T, P, S) Pushdown automata N-PDA = CFG D-PDA < CFG Today What languages are context-free? Pumping lemma (similar to pumping lemma for

More information

Announcements. H6 posted 2 days ago (due on Tue) Midterm went well: Very nice curve. Average 65% High score 74/75

Announcements. H6 posted 2 days ago (due on Tue) Midterm went well: Very nice curve. Average 65% High score 74/75 Announcements H6 posted 2 days ago (due on Tue) Mterm went well: Average 65% High score 74/75 Very nice curve 80 70 60 50 40 30 20 10 0 1 6 11 16 21 26 31 36 41 46 51 56 61 66 71 76 81 86 91 96 101 106

More information

Context free languages

Context 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 information

Fundamentele Informatica II

Fundamentele Informatica II Fundamentele Informatica II Answer to selected exercises 5 John C Martin: Introduction to Languages and the Theory of Computation M.M. Bonsangue (and J. Kleijn) Fall 2011 5.1.a (q 0, ab, Z 0 ) (q 1, b,

More information

From EBNF to PEG. Roman R. Redziejowski. Giraf s Research

From EBNF to PEG. Roman R. Redziejowski. Giraf s Research From EBNF to PEG Roman R. Redziejowski Giraf s Research roman.redz@swipnet.se Abstract. Parsing Expression Grammar (PEG) is a way to define a recursive-descent parser with limited backtracking. Its properties

More information

FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY

FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY 15-453 FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY Chomsky Normal Form and TURING MACHINES TUESDAY Feb 4 CHOMSKY NORMAL FORM A context-free grammar is in Chomsky normal form if every rule is of the form:

More information

Accept or reject. Stack

Accept or reject. Stack Pushdown Automata CS351 Just as a DFA was equivalent to a regular expression, we have a similar analogy for the context-free grammar. A pushdown automata (PDA) is equivalent in power to contextfree grammars.

More information

THEORY OF COMPUTATION (AUBER) EXAM CRIB SHEET

THEORY OF COMPUTATION (AUBER) EXAM CRIB SHEET THEORY OF COMPUTATION (AUBER) EXAM CRIB SHEET Regular Languages and FA A language is a set of strings over a finite alphabet Σ. All languages are finite or countably infinite. The set of all languages

More information

Parsing with CFGs L445 / L545 / B659. Dept. of Linguistics, Indiana University Spring Parsing with CFGs. Direction of processing

Parsing 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 information

Parsing with CFGs. Direction of processing. Top-down. Bottom-up. Left-corner parsing. Chart parsing CYK. Earley 1 / 46.

Parsing 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 information

THEORY OF COMPILATION

THEORY OF COMPILATION Lecture 04 Syntax analysis: top-down and bottom-up parsing THEORY OF COMPILATION EranYahav 1 You are here Compiler txt Source Lexical Analysis Syntax Analysis Parsing Semantic Analysis Inter. Rep. (IR)

More information

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

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 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 information

Syntax Analysis, VII The Canonical LR(1) Table Construction. Comp 412

Syntax 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 information

Recursive descent for grammars with contexts

Recursive descent for grammars with contexts 39th International Conference on Current Trends in Theory and Practice of Computer Science Špindleruv Mlýn, Czech Republic Recursive descent parsing for grammars with contexts Ph.D. student, Department

More information

Eng. Maha Talaat Page 1

Eng. Maha Talaat Page 1 El-Shorouk Academy Acad. Year : 2013 / 2014 Higher Institute for Computer & Term : 2nd Information Technology Year : 4th Computer Science Department Supervisor: Prof. Dr. Ahmed Abbassy Section contents:

More information

Chomsky Normal Form and TURING MACHINES. TUESDAY Feb 4

Chomsky Normal Form and TURING MACHINES. TUESDAY Feb 4 Chomsky Normal Form and TURING MACHINES TUESDAY Feb 4 CHOMSKY NORMAL FORM A context-free grammar is in Chomsky normal form if every rule is of the form: A BC A a S ε B and C aren t start variables a is

More information

Course Script INF 5110: Compiler construction

Course 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 information

Einführung in die Computerlinguistik

Einführung in die Computerlinguistik Einführung in die Computerlinguistik Context-Free Grammars (CFG) Laura Kallmeyer Heinrich-Heine-Universität Düsseldorf Summer 2016 1 / 22 CFG (1) Example: Grammar G telescope : Productions: S NP VP NP

More information