Ambiguity, Precedence, Associativity & TopDown Parsing. Lecture 910


 Elizabeth Williamson
 4 years ago
 Views:
Transcription
1 Ambiguity, Precedence, Associativity & TopDown Parsing Lecture 910 (From slides by G. Necula & R. Bodik) 9/18/06 Prof. Hilfinger CS164 Lecture 9 1
2 Administrivia Please let me know if there are continued problems with being able to see other people s stuff. Preliminary run of test data against any projects handed in by midnight Wednesday. Not final data sets, but may give you an indication. You can submit early and often! Will not test again until midnight Friday. 9/18/06 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? Today, we ll look at the first two. 9/18/06 Prof. Hilfinger CS164 Lecture 9 3
4 Ambiguity Grammar + * ( ) Strings + + * + 9/18/06 Prof. Hilfinger CS164 Lecture 9 4
5 Ambiguity. xample The string + + has two parse trees is leftassociative 9/18/06 Prof. Hilfinger CS164 Lecture 9 5
6 Ambiguity. xample The string * + has two parse trees + * * + * has higher precedence than + 9/18/06 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 illdefined Ambiguity is common in programming languages Arithmetic expressions IFTHNLS 9/18/06 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 leftassociativity of + and * 9/18/06 Prof. Hilfinger CS164 Lecture 9 8
9 Ambiguity. xample The * + has only one parse tree now + T * T + T * 9/18/06 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 9/18/06 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 9/18/06 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 9/18/06 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 9/18/06 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 9/18/06 Prof. Hilfinger CS164 Lecture 9 14
15 Associativity Declarations Consider the grammar + Ambiguous: two parse trees of Leftassociativity declaration: %left + 9/18/06 Prof. Hilfinger CS164 Lecture 9 15
16 Precedence Declarations Consider the grammar + * And the string + * * + + * 9/18/06 Prof. Hilfinger CS164 Lecture 9 16 Precedence declarations: %left + %left *
17 How It s Done I: Intro to TopDown 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 9/18/06 Prof. Hilfinger CS164 Lecture 9 17
18 Topdown DepthFirst Parsing Consider the grammar T + T T ( ) * T Token stream is: * Start with toplevel nonterminal Try the rules for in order 9/18/06 Prof. Hilfinger CS164 Lecture 9 18
19 DepthFirst 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 9/18/06 Prof. Hilfinger CS164 Lecture 9 19
20 DepthFirst 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 9/18/06 Prof. Hilfinger CS164 Lecture 9 20
21 DepthFirst Parsing Parsing: given a string of tokens t 1 t 2... t n, find a leftmost derivation (and thus, parse tree) Depthfirst parsing: Beginning with start symbol, try each production exhaustively on leftmost nonterminal in current sentential form and recurse. 9/18/06 Prof. Hilfinger CS164 Lecture 9 21
22 DepthFirst 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 nonterminals and terminals all matched (accept) or no more productions left (reject) 9/18/06 Prof. Hilfinger CS164 Lecture 9 22
23 When DepthFirst 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 reorder productions, but search will have lots of backtracking and general rule for ordering is complex Problem here is leftrecursive grammar: one that has a nonterminal S S + Sα for some α 9/18/06 Prof. Hilfinger CS164 Lecture 9 23
24 limination of Left Recursion Consider the leftrecursive grammar S S α β S generates all strings starting with a β and followed by a number of α Can rewrite using rightrecursion S β S S α S ε 9/18/06 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 ε 9/18/06 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 ε 9/18/06 Prof. Hilfinger CS164 Lecture 9 26
27 General Left Recursion The grammar S A α δ (1) A S β (2) is also leftrecursive 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. 9/18/06 Prof. Hilfinger CS164 Lecture 9 27
28 An Alternative Approach Instead of reordering or rewriting grammar, can use topdown breadthfirst search. S S a a String: aaa S S a a (string not all matched) S a a a a S a a a a a a 9/18/06 Prof. Hilfinger CS164 Lecture 9 28
29 Summary of TopDown Parsing So Far Simple and general parsing strategy Left recursion must be eliminated first but that can be done automatically Or can use breadthfirst search But backtracking (depthfirst) or maaining list of possible sentential forms (breadthfirst) can make it slow Often, though, we can avoid both 9/18/06 Prof. Hilfinger CS164 Lecture 9 29
30 Predictive Parsers Modification of depthfirst 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 lefttoright scan of input L means leftmost derivation k means predict based on k tokens of lookahead In practice, LL(1) is used 9/18/06 Prof. Hilfinger CS164 Lecture 9 30
31 LL(1) Languages Previously, for each nonterminal and input token there may be a choice of production LL(k) means that for each nonterminal and k tokens, there is only one production that could lead to success 9/18/06 Prof. Hilfinger CS164 Lecture 9 31
32 Recursive Descent: Grammar as Program In recursive descent, we think of a grammar as a program. ach nonterminal is turned o a procedure ach righthand side transliterated o part of the procedure body for its nonterminal First, define next() current token of input scan(t) check that next()=t (else RROR), and then read new token. 9/18/06 Prof. Hilfinger CS164 Lecture 9 32
33 Recursive Descent: xample P S $ S T S S + S ε T ( S ) ($ = end marker) def P (): S(); scan($) def S(): T(); S () def S (): But where do tests if next() == + : scan( + ); S() come from? elif next() in [ ), $]: pass else: RROR def T(): if next () == : scan() elif next() == ( : scan( ( ); S(); scan ( ) ) else: RROR 9/18/06 Prof. Hilfinger CS164 Lecture 9 33
34 Predicting Righthand Sides The iftests are conditions by which parser predicts which righthand 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 nonterminal to expand One dimension for next token A table entry contains one production 9/18/06 Prof. Hilfinger CS164 Lecture 9 34
35 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 leftfactored before use for predictive parsing 9/18/06 Prof. Hilfinger CS164 Lecture 9 35
36 LeftFactoring xample Starting with the grammar T + T T * T ( ) Factor out common prefixes of productions T X X + ε T ( ) Y Y * T ε 9/18/06 Prof. Hilfinger CS164 Lecture 9 36
37 LL(1) Parsing Table xample Leftfactored 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 ε ε ε 9/18/06 Prof. Hilfinger CS164 Lecture 9 37
38 LL(1) Parsing Table xample (Cont.) Consider the [, ] entry When current nonterminal is and next input is, use production T X This production can generate an in the first place Consider the [Y,+] entry When current nonterminal is Y and current token is +, get rid of Y We ll see later why this is so 9/18/06 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 nonterminal 9/18/06 Prof. Hilfinger CS164 Lecture 9 39
40 Using Parsing Tables Method similar to recursive descent, except For first nonterminal S We look at the next token a And choose the production shown at [S,a] We use a stack to keep track of pending nonterminals We reject when we encounter an error state We accept when we encounter endofinput 9/18/06 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 == < > 9/18/06 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 9/18/06 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 No table entry can be multiply defined Once we have the table Can create tabledriver or recursivedescent parser The parsing algorithms are simple and fast No backtracking is necessary We want to generate parsing tables from CFG 9/18/06 Prof. Hilfinger CS164 Lecture 9 43
44 TopDown Parsing. Review Topdown parsing expands a parse tree from the start symbol to the leaves Always expand the leftmost nonterminal T + * + 9/18/06 Prof. Hilfinger CS164 Lecture 9 44
45 TopDown Parsing. Review Topdown parsing expands a parse tree from the start symbol to the leaves Always expand the leftmost nonterminal 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 9/18/06 Prof. Hilfinger CS164 Lecture 9 45
46 TopDown Parsing. Review Topdown parsing expands a parse tree from the start symbol to the leaves Always expand the leftmost nonterminal T * * 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 9/18/06 Prof. Hilfinger CS164 Lecture 9 46
47 TopDown Parsing. Review Topdown parsing expands a parse tree from the start symbol to the leaves Always expand the leftmost nonterminal T * 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 * + 9/18/06 Prof. Hilfinger CS164 Lecture 9 47
48 Constructing Predictive Parsing Tables Consider the state S * βaγ With b the next token Trying to match βbδ There are two possibilities: 1. b belongs to an expansion of A Or Any A α can be used if b can start a string derived from α In this case we say that b First(α) 9/18/06 Prof. Hilfinger CS164 Lecture 9 48
49 Constructing Predictive Parsing Tables (Cont.) 2. b does not belong to an expansion of A The expansion of A is empty and b belongs to an expansion of γ (e.g., bω) Means that b can appear after A in a derivation of the form S * βabω We say that b Follow(A) in this case What productions can we use in this case? Any A α can be used if α can expand to ε We say that ε First(A) in this case 9/18/06 Prof. Hilfinger CS164 Lecture 9 49
50 Summary of Definitions For b T, the set of terminals; α a sequence of terminal & nonterminal symbols, S the start symbol, A N, the set of nonterminals: FIRST(α) T { ε } b FIRST(α) iff α * b ε FIRST(α) iff α * ε FOLLOW(A) T b FOLLOW(A) iff S * A b 9/18/06 Prof. Hilfinger CS164 Lecture 9 50
51 Computing First Sets Definition First(X) = { b X * bα} {ε X * ε}, X any grammar symbol. 1. First(b) = { b } 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) 9/18/06 Prof. Hilfinger CS164 Lecture 9 51
52 Computing First Sets, Contd. That takes care of singlesymbol case. In general: FIRST(X 1 X 2 X k ) = FIRST(X 1 ) FIRST(X 2 ) if ε FIRST(X 1 ) FIRST(X 2 ) if ε FIRST(X 1 X 2 X k1 )  { ε } unless ε FIRST(X i ) i 9/18/06 Prof. Hilfinger CS164 Lecture 9 52
53 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( * ) = { * } 9/18/06 Prof. Hilfinger CS164 Lecture 9 53
54 Computing Follow Sets Definition Follow(X) = { b S * β X b ω } 1. Compute the First sets for all nonterminals first 2. Add $ to Follow(S) (if S is the start nonterminal) 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) 9/18/06 Prof. Hilfinger CS164 Lecture 9 54
55 Follow Sets. xample For the grammar T X T ( ) Y Follow sets Follow( ) = {), $} Follow( X ) = {$, ) } Follow( Y ) = {+, ), $} Follow( T ) = {+, ), $} X + ε Y * T ε 9/18/06 Prof. Hilfinger CS164 Lecture 9 55
56 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] = α 9/18/06 Prof. Hilfinger CS164 Lecture 9 56
57 Constructing LL(1) Tables. xample For the grammar T X T ( ) Y X + ε Y * T ε Where in the line of Y do we put Y * T? In the lines of First( *T) = { * } Where in the line of Y do we put Y ε? In the lines of Follow(Y) = { $, +, ) } 9/18/06 Prof. Hilfinger CS164 Lecture 9 57
58 Notes on LL(1) Parsing Tables If any entry is multiply defined then G is not LL(1) If G is ambiguous If G is left recursive If G is not leftfactored And in other cases as well Most programming language grammars are not LL(1) There are tools that build LL(1) tables 9/18/06 Prof. Hilfinger CS164 Lecture 9 58
59 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. 9/18/06 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, you can write the parser by hand Next: a more powerful parsing strategy for grammars that are not LL(1) 9/18/06 Prof. Hilfinger CS164 Lecture 9 60
Ambiguity. Grammar E E + E E * E ( E ) int. The string int * int + int has two parse trees. * int
Administrivia Ambiguity, Precedence, Associativity & opdown Parsing eam assignments this evening for all those not listed as having one. HW#3 is now available, due next uesday morning (Monday is a holiday).
More informationAdministrativia. WA1 due on Thu PA2 in a week. Building a Parser III. Slides on the web site. CS164 3:305:00 TT 10 Evans.
Administrativia Building a Parser III CS164 3:305:00 10 vans WA1 due on hu PA2 in a week Slides on the web site I do my best to have slides ready and posted by the end of the preceding logical day yesterday,
More informationBuilding a Parser III. CS164 3:305:00 TT 10 Evans. Prof. Bodik CS 164 Lecture 6 1
Building a Parser III CS164 3:305:00 TT 10 Evans 1 Overview Finish recursive descent parser when it breaks down and how to fix it eliminating left recursion reordering productions Predictive parsers (aka
More informationCS1622. Today. A Recursive Descent Parser. Preliminaries. Lecture 9 Parsing (4)
CS1622 Lecture 9 Parsing (4) CS 1622 Lecture 9 1 Today Example of a recursive descent parser Predictive & LL(1) parsers Building parse tables CS 1622 Lecture 9 2 A Recursive Descent Parser. Preliminaries
More informationGrammars and ambiguity. CS164 3:305:00 TT 10 Evans. Prof. Bodik CS 164 Lecture 8 1
Grammars and ambiguity CS164 3:305:00 TT 10 vans 1 Overview derivations and parse trees different derivations produce may produce same parse tree ambiguous grammars what they are and how to fix them 2
More informationParsing: Derivations, Ambiguity, Precedence, Associativity. Lecture 8. Professor Alex Aiken Lecture #5 (Modified by Professor Vijay Ganesh)
Parsing: Derivations, Ambiguity, Precedence, Associativity Lecture 8 (Modified by Professor Vijay Ganesh) 1 Topics covered so far Regular languages and Finite automaton Parser overview Contextfree grammars
More informationAbstract Syntax Trees & TopDown Parsing
Review of Parsing Abstract Syntax Trees & TopDown Parsing Given a language L(G), a parser consumes a sequence of tokens s and produces a parse tree Issues: How do we recognize that s L(G)? A parse tree
More informationE E+E E E (E) id. id + id E E+E. id E + E id id + E id id + id. Overview. derivations and parse trees. Grammars and ambiguity. ambiguous grammars
Overview Grammars and ambiguity CS164 3:305:00 TT 10 vans derivations and parse trees dferent derivations produce may produce same parse tree ambiguous grammars what they are and how to fix them 1 2 Recall:
More informationAbstract Syntax Trees & TopDown Parsing
Abstract Syntax Trees & TopDown Parsing Review of Parsing Given a language L(G), a parser consumes a sequence of tokens s and produces a parse tree Issues: How do we recognize that s L(G)? A parse tree
More informationAbstract Syntax Trees & TopDown Parsing
Review of Parsing Abstract Syntax Trees & TopDown Parsing Given a language L(G), a parser consumes a sequence of tokens s and produces a parse tree Issues: How do we recognize that s L(G)? A parse tree
More informationTopDown Parsing and Intro to BottomUp Parsing. Lecture 7
TopDown Parsing and Intro to BottomUp Parsing Lecture 7 1 Predictive Parsers Like recursivedescent but parser can predict which production to use Predictive parsers are never wrong Always able to guess
More informationExtra Credit Question
TopDown Parsing #1 Extra Credit Question Given this grammar G: E E+T E T T T * int T int T (E) Is the string int * (int + int) in L(G)? Give a derivation or prove that it is not. #2 Revenge of Theory
More informationTopDown Parsing and Intro to BottomUp Parsing. Lecture 7
TopDown Parsing and Intro to BottomUp Parsing Lecture 7 1 Predictive Parsers Like recursivedescent but parser can predict which production to use Predictive parsers are never wrong Always able to guess
More informationOutline. Regular languages revisited. Introduction to Parsing. Parser overview. Contextfree grammars (CFG s) Lecture 5. Derivations.
Outline Regular languages revisited Introduction to Parsing Lecture 5 Parser overview Contextfree grammars (CFG s) Derivations Prof. Aiken CS 143 Lecture 5 1 Ambiguity Prof. Aiken CS 143 Lecture 5 2 Languages
More informationIntroduction to Parsing. Lecture 5. Professor Alex Aiken Lecture #5 (Modified by Professor Vijay Ganesh)
Introduction to Parsing Lecture 5 (Modified by Professor Vijay Ganesh) 1 Outline Regular languages revisited Parser overview Contextfree grammars (CFG s) Derivations Ambiguity 2 Languages and Automata
More information( ) i 0. Outline. Regular languages revisited. Introduction to Parsing. Parser overview. Contextfree grammars (CFG s) Lecture 5.
Outline Regular languages revisited Introduction to Parsing Lecture 5 Parser overview Contextfree grammars (CFG s) Derivations Prof. Aiken CS 143 Lecture 5 1 Ambiguity Prof. Aiken CS 143 Lecture 5 2 Languages
More informationIntroduction to Parsing Ambiguity and Syntax Errors
Introduction to Parsing Ambiguity and Syntax rrors Outline Regular languages revisited Parser overview Contextfree grammars (CFG s) Derivations Ambiguity Syntax errors Compiler Design 1 (2011) 2 Languages
More informationTop down vs. bottom up parsing
Parsing A grammar describes the strings that are syntactically legal A recogniser simply accepts or rejects strings A generator produces sentences in the language described by the grammar A parser constructs
More informationAmbiguity. Lecture 8. CS 536 Spring
Ambiguity Lecture 8 CS 536 Spring 2001 1 Announcement Reading Assignment ContextFree Grammars (Sections 4.1, 4.2) Programming Assignment 2 due Friday! Homework 1 due in a week (Wed Feb 21) not Feb 25!
More informationCompilers. Predictive Parsing. Alex Aiken
Compilers Like recursivedescent but parser can predict which production to use By looking at the next fewtokens No backtracking Predictive parsers accept LL(k) grammars L means lefttoright scan of input
More informationIntroduction to Parsing. Lecture 5
Introduction to Parsing Lecture 5 1 Outline Regular languages revisited Parser overview Contextfree grammars (CFG s) Derivations Ambiguity 2 Languages and Automata Formal languages are very important
More informationIntroduction to Parsing Ambiguity and Syntax Errors
Introduction to Parsing Ambiguity and Syntax rrors Outline Regular languages revisited Parser overview Contextfree grammars (CFG s) Derivations Ambiguity Syntax errors 2 Languages and Automata Formal
More information8 Parsing. Parsing. Top Down Parsing Methods. Parsing complexity. Top down vs. bottom up parsing. Top down vs. bottom up parsing
8 Parsing Parsing A grammar describes syntactically legal strings in a language A recogniser simply accepts or rejects strings A generator produces strings A parser constructs a parse tree for a string
More informationLL Parsing: A piece of cake after LR
LL Parsing: A piece of cake after LR Lecture 11 Dr. Sean Peisert ECS 142 Spring 2009 1 LL Parsing Still specified using a CFG Still reads lefttoright (Lx) Now is leftmost derivation (xl) rather than
More informationIntroduction to Parsing. Lecture 5
Introduction to Parsing Lecture 5 1 Outline Regular languages revisited Parser overview Contextfree grammars (CFG s) Derivations Ambiguity 2 Languages and Automata Formal languages are very important
More informationTypes of parsing. CMSC 430 Lecture 4, Page 1
Types of parsing Topdown parsers start at the root of derivation tree and fill in picks a production and tries to match the input may require backtracking some grammars are backtrackfree (predictive)
More informationParsing. Roadmap. > Contextfree grammars > Derivations and precedence > Topdown parsing > Leftrecursion > Lookahead > Tabledriven parsing
Roadmap > Contextfree grammars > Derivations and precedence > Topdown parsing > Leftrecursion > Lookahead > Tabledriven parsing The role of the parser > performs contextfree syntax analysis > guides
More information3. Parsing. Oscar Nierstrasz
3. Parsing Oscar Nierstrasz Thanks to Jens Palsberg and Tony Hosking for their kind permission to reuse and adapt the CS132 and CS502 lecture notes. http://www.cs.ucla.edu/~palsberg/ http://www.cs.purdue.edu/homes/hosking/
More informationMore BottomUp Parsing
More BottomUp Parsing Lecture 7 Dr. Sean Peisert ECS 142 Spring 2009 1 Status Project 1 Back By Wednesday (ish) savior lexer in ~cs142/s09/bin Project 2 Due Friday, Apr. 24, 11:55pm My office hours 3pm
More informationCS502: Compilers & Programming Systems
CS502: Compilers & Programming Systems Topdown Parsing Zhiyuan Li Department of Computer Science Purdue University, USA There exist two wellknown schemes to construct deterministic topdown parsers:
More informationOutline. Parser overview Contextfree grammars (CFG s) Derivations SyntaxDirected Translation
Outline Introduction to Parsing (adapted from CS 164 at Berkeley) Parser overview Contextfree grammars (CFG s) Derivations SyntaxDirected ranslation he Functionality of the Parser Input: sequence of
More informationNote that for recursive descent to work, if A ::= B1 B2 is a grammar rule we need First k (B1) disjoint from First k (B2).
LL(k) Grammars We need a bunch of terminology. For any terminal string a we write First k (a) is the prefix of a of length k (or all of a if its length is less than k) For any string g of terminal and
More informationIntro To Parsing. Step By Step
#1 Intro To Parsing Step By Step #2 SelfTest from Last Time Are practical parsers and scanners based on deterministic or nondeterministic automata? How can regular expressions be used to specify nested
More informationCS 314 Principles of Programming Languages
CS 314 Principles of Programming Languages Lecture 5: Syntax Analysis (Parsing) Zheng (Eddy) Zhang Rutgers University January 31, 2018 Class Information Homework 1 is being graded now. The sample solution
More informationIntroduction to Parsing. Lecture 8
Introduction to Parsing Lecture 8 Adapted from slides by G. Necula Outline Limitations of regular languages Parser overview Contextfree grammars (CFG s) Derivations Languages and Automata Formal languages
More informationCS 321 Programming Languages and Compilers. VI. Parsing
CS 321 Programming Languages and Compilers VI. Parsing Parsing Calculate grammatical structure of program, like diagramming sentences, where: Tokens = words Programs = sentences For further information,
More informationOutline. Limitations of regular languages. Introduction to Parsing. Parser overview. Contextfree grammars (CFG s)
Outline Limitations of regular languages Introduction to Parsing Parser overview Lecture 8 Adapted from slides by G. Necula Contextfree grammars (CFG s) Derivations Languages and Automata Formal languages
More informationFormal Languages and Compilers Lecture V: Parse Trees and Ambiguous Gr
Formal Languages and Compilers Lecture V: Parse Trees and Ambiguous Grammars Free University of BozenBolzano Faculty of Computer Science POS Building, Room: 2.03 artale@inf.unibz.it http://www.inf.unibz.it/
More information4 (c) parsing. Parsing. Top down vs. bo5om up parsing
4 (c) parsing Parsing A grammar describes syntac2cally legal strings in a language A recogniser simply accepts or rejects strings A generator produces strings A parser constructs a parse tree for a string
More informationAdministrativia. PA2 assigned today. WA1 assigned today. Building a Parser II. CS164 3:305:00 TT 10 Evans. First midterm. Grammars.
Administrativia Building a Parser II CS164 3:305:00 TT 10 Evans PA2 assigned today due in 12 days WA1 assigned today due in a week it s a practice for the exam First midterm Oct 5 will contain some projectinspired
More informationLexical and Syntax Analysis. TopDown Parsing
Lexical and Syntax Analysis TopDown Parsing Easy for humans to write and understand String of characters Lexemes identified String of tokens Easy for programs to transform Data structure Syntax A syntax
More informationBottomUp Parsing. Lecture 1112
BottomUp Parsing Lecture 1112 (From slides by G. Necula & R. Bodik) 2/20/08 Prof. Hilfinger CS164 Lecture 11 1 Administrivia Test I during class on 10 March. 2/20/08 Prof. Hilfinger CS164 Lecture 11
More informationParsing Part II. (Ambiguity, Topdown parsing, Leftrecursion Removal)
Parsing Part II (Ambiguity, Topdown parsing, Leftrecursion Removal) Ambiguous Grammars Definitions If a grammar has more than one leftmost derivation for a single sentential form, the grammar is ambiguous
More informationCSCI312 Principles of Programming Languages
Copyright 2006 The McGrawHill Companies, Inc. CSCI312 Principles of Programming Languages! LL Parsing!! Xu Liu Derived from Keith Cooper s COMP 412 at Rice University Recap Copyright 2006 The McGrawHill
More information1 Introduction. 2 Recursive descent parsing. Predicative parsing. Computer Language Implementation Lecture Note 3 February 4, 2004
CMSC 51086 Winter 2004 Computer Language Implementation Lecture Note 3 February 4, 2004 Predicative parsing 1 Introduction This note continues the discussion of parsing based on context free languages.
More informationLexical and Syntax Analysis
Lexical and Syntax Analysis (of Programming Languages) TopDown Parsing Lexical and Syntax Analysis (of Programming Languages) TopDown Parsing Easy for humans to write and understand String of characters
More informationCA Compiler Construction
CA4003  Compiler Construction David Sinclair A topdown parser starts with the root of the parse tree, labelled with the goal symbol of the grammar, and repeats the following steps until the fringe of
More informationSyntactic Analysis. TopDown Parsing
Syntactic Analysis TopDown Parsing Copyright 2017, Pedro C. Diniz, all rights reserved. Students enrolled in Compilers class at University of Southern California (USC) have explicit permission to make
More informationOutline. Limitations of regular languages Parser overview Contextfree grammars (CFG s) Derivations SyntaxDirected Translation
Outline Introduction to Parsing Lecture 8 Adapted from slides by G. Necula and R. Bodik Limitations of regular languages Parser overview Contextfree grammars (CG s) Derivations SyntaxDirected ranslation
More informationSometimes an ambiguous grammar can be rewritten to eliminate the ambiguity.
Eliminating Ambiguity Sometimes an ambiguous grammar can be rewritten to eliminate the ambiguity. Example: consider the following grammar stat if expr then stat if expr then stat else stat other One can
More informationLecture 8: Deterministic BottomUp Parsing
Lecture 8: Deterministic BottomUp Parsing (From slides by G. Necula & R. Bodik) Last modified: Fri Feb 12 13:02:57 2010 CS164: Lecture #8 1 Avoiding nondeterministic choice: LR We ve been looking at general
More informationLL(k) Parsing. Predictive Parsers. LL(k) Parser Structure. Sample Parse Table. LL(1) Parsing Algorithm. Push RHS in Reverse Order 10/17/2012
Predictive Parsers LL(k) Parsing Can we avoid backtracking? es, if for a given input symbol and given nonterminal, we can choose the alternative appropriately. his is possible if the first terminal of
More informationCS2210: Compiler Construction Syntax Analysis Syntax Analysis
Comparison with Lexical Analysis The second phase of compilation Phase Input Output Lexer string of characters string of tokens Parser string of tokens Parse tree/ast What Parse Tree? CS2210: Compiler
More informationLecture 7: Deterministic BottomUp Parsing
Lecture 7: Deterministic BottomUp Parsing (From slides by G. Necula & R. Bodik) Last modified: Tue Sep 20 12:50:42 2011 CS164: Lecture #7 1 Avoiding nondeterministic choice: LR We ve been looking at general
More informationTableDriven Parsing
TableDriven Parsing It is possible to build a nonrecursive predictive parser by maintaining a stack explicitly, rather than implicitly via recursive calls [1] The nonrecursive parser looks up the production
More informationBottomUp Parsing. Lecture 1112
BottomUp Parsing Lecture 1112 (From slides by G. Necula & R. Bodik) 9/22/06 Prof. Hilfinger CS164 Lecture 11 1 BottomUp Parsing Bottomup parsing is more general than topdown parsing And just as efficient
More informationCompiler Design 1. TopDown Parsing. Goutam Biswas. Lect 5
Compiler Design 1 TopDown Parsing Compiler Design 2 Nonterminal as a Function In a topdown parser a nonterminal may be viewed as a generator of a substring of the input. We may view a nonterminal
More informationCompilerconstructie. najaar Rudy van Vliet kamer 140 Snellius, tel rvvliet(at)liacs(dot)nl. college 3, vrijdag 22 september 2017
Compilerconstructie najaar 2017 http://www.liacs.leidenuniv.nl/~vlietrvan1/coco/ Rudy van Vliet kamer 140 Snellius, tel. 071527 2876 rvvliet(at)liacs(dot)nl college 3, vrijdag 22 september 2017 + werkcollege
More informationParsing. Lecture 11: Parsing. Recursive Descent Parser. Arithmetic grammar.  drops irrelevant details from parse tree
Parsing Lecture 11: Parsing CSC 131 Fall, 2014 Kim Bruce Build parse tree from an expression Interested in abstract syntax tree  drops irrelevant details from parse tree Arithmetic grammar ::=
More informationCompilers: CS31003 Computer Sc & Engg: IIT Kharagpur 1. TopDown Parsing. Lect 5. Goutam Biswas
Compilers: CS31003 Computer Sc & Engg: IIT Kharagpur 1 TopDown Parsing Compilers: CS31003 Computer Sc & Engg: IIT Kharagpur 2 Nonterminal as a Function In a topdown parser a nonterminal may be viewed
More informationParsing. Handle, viable prefix, items, closures, goto s LR(k): SLR(1), LR(1), LALR(1)
TD parsing  LL(1) Parsing First and Follow sets Parse table construction BU Parsing Handle, viable prefix, items, closures, goto s LR(k): SLR(1), LR(1), LALR(1) Problems with SLR Aho, Sethi, Ullman, Compilers
More informationParsing III. CS434 Lecture 8 Spring 2005 Department of Computer Science University of Alabama Joel Jones
Parsing III (Topdown parsing: recursive descent & LL(1) ) (Bottomup parsing) CS434 Lecture 8 Spring 2005 Department of Computer Science University of Alabama Joel Jones Copyright 2003, Keith D. Cooper,
More informationSyntax Analysis. Martin Sulzmann. Martin Sulzmann Syntax Analysis 1 / 38
Syntax Analysis Martin Sulzmann Martin Sulzmann Syntax Analysis 1 / 38 Syntax Analysis Objective Recognize individual tokens as sentences of a language (beyond regular languages). Example 1 (OK) Program
More informationCS Parsing 1
CS4142003403 Parsing 1 030: Parsing Once we have broken an input file into a sequence of tokens, the next step is to determine if that sequence of tokens forms a syntactically correct program parsing
More informationCompilers. Yannis Smaragdakis, U. Athens (original slides by Sam
Compilers Parsing Yannis Smaragdakis, U. Athens (original slides by Sam Guyer@Tufts) Next step text chars Lexical analyzer tokens Parser IR Errors Parsing: Organize tokens into sentences Do tokens conform
More informationIntroduction to BottomUp Parsing
Introduction to BottomUp Parsing Lecture 11 CS 536 Spring 2001 1 Outline he strategy: shiftreduce parsing Ambiguity and precedence declarations Next lecture: bottomup parsing algorithms CS 536 Spring
More informationSyntaxDirected Translation. Lecture 14
SyntaxDirected Translation Lecture 14 (adapted from slides by R. Bodik) 9/27/2006 Prof. Hilfinger, Lecture 14 1 Motivation: parser as a translator syntaxdirected translation stream of tokens parser ASTs,
More informationFall Compiler Principles Lecture 2: LL parsing. Roman Manevich BenGurion University of the Negev
Fall 20172018 Compiler Principles Lecture 2: LL parsing Roman Manevich BenGurion University of the Negev 1 Books Compilers Principles, Techniques, and Tools Alfred V. Aho, Ravi Sethi, Jeffrey D. Ullman
More informationParsing #1. Leonidas Fegaras. CSE 5317/4305 L3: Parsing #1 1
Parsing #1 Leonidas Fegaras CSE 5317/4305 L3: Parsing #1 1 Parser source file get next character scanner get token parser AST token A parser recognizes sequences of tokens according to some grammar and
More informationPrelude COMP 181 Tufts University Computer Science Last time Grammar issues Key structure meaning Tufts University Computer Science
Prelude COMP Lecture Topdown Parsing September, 00 What is the Tufts mascot? Jumbo the elephant Why? P. T. Barnum was an original trustee of Tufts : donated $0,000 for a natural museum on campus Barnum
More informationParsing Part II (Topdown parsing, leftrecursion removal)
Parsing Part II (Topdown parsing, leftrecursion removal) Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University have explicit
More informationCS415 Compilers. Syntax Analysis. These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University
CS415 Compilers Syntax Analysis These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University Limits of Regular Languages Advantages of Regular Expressions
More informationCS 406/534 Compiler Construction Parsing Part I
CS 406/534 Compiler Construction Parsing Part I Prof. Li Xu Dept. of Computer Science UMass Lowell Fall 2004 Part of the course lecture notes are based on Prof. Keith Cooper, Prof. Ken Kennedy and Dr.
More informationCompilers Course Lecture 4: Context Free Grammars
Compilers Course Lecture 4: Context Free Grammars Example: attempt to define simple arithmetic expressions using named regular expressions: num = [09]+ sum = expr "+" expr expr = "(" sum ")" num Appears
More informationSyntax Analysis. COMP 524: Programming Language Concepts Björn B. Brandenburg. The University of North Carolina at Chapel Hill
Syntax Analysis Björn B. Brandenburg The University of North Carolina at Chapel Hill Based on slides and notes by S. Olivier, A. Block, N. Fisher, F. HernandezCampos, and D. Stotts. The Big Picture Character
More informationBuilding a Parser II. CS164 3:305:00 TT 10 Evans. Prof. Bodik CS 164 Lecture 6 1
Building a Parser II CS164 3:305:00 TT 10 Evans 1 Grammars Programming language constructs have recursive structure. which is why our handwritten parser had this structure, too An expression is either:
More informationSyntax Analysis. The Big Picture. The Big Picture. COMP 524: Programming Languages Srinivas Krishnan January 25, 2011
Syntax Analysis COMP 524: Programming Languages Srinivas Krishnan January 25, 2011 Based in part on slides and notes by Bjoern Brandenburg, S. Olivier and A. Block. 1 The Big Picture Character Stream Token
More informationSyntax Analysis Check syntax and construct abstract syntax tree
Syntax Analysis Check syntax and construct abstract syntax tree if == = ; b 0 a b Error reporting and recovery Model using context free grammars Recognize using Push down automata/table Driven Parsers
More informationOutline. The strategy: shiftreduce parsing. Introduction to BottomUp Parsing. A key concept: handles
Outline Introduction to BottomUp Parsing Lecture Notes by Profs. Alex Aiken and George Necula (UCB) he strategy: reduce parsing A key concept: handles Ambiguity and precedence declarations CS780(Prasad)
More informationSYNTAX ANALYSIS 1. Define parser. Hierarchical analysis is one in which the tokens are grouped hierarchically into nested collections with collective meaning. Also termed as Parsing. 2. Mention the basic
More informationChapter 3. Parsing #1
Chapter 3 Parsing #1 Parser source file get next character scanner get token parser AST token A parser recognizes sequences of tokens according to some grammar and generates Abstract Syntax Trees (ASTs)
More informationParsing. Note by Baris Aktemur: Our slides are adapted from Cooper and Torczon s slides that they prepared for COMP 412 at Rice.
Parsing Note by Baris Aktemur: Our slides are adapted from Cooper and Torczon s slides that they prepared for COMP 412 at Rice. Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students
More informationContextFree Grammars
CFG2: Ambiguity ContextFree Grammars CMPT 379: Compilers Instructor: Anoop Sarkar anoopsarkar.github.io/compilersclass Ambiguity  /  / ( )  / /  160622 2 Ambiguity Grammar is ambiguous if more
More informationSyntax Analysis. Prof. James L. Frankel Harvard University. Version of 6:43 PM 6Feb2018 Copyright 2018, 2015 James L. Frankel. All rights reserved.
Syntax Analysis Prof. James L. Frankel Harvard University Version of 6:43 PM 6Feb2018 Copyright 2018, 2015 James L. Frankel. All rights reserved. ContextFree Grammar (CFG) terminals nonterminals start
More informationMonday, September 13, Parsers
Parsers Agenda Terminology LL(1) Parsers Overview of LR Parsing Terminology Grammar G = (Vt, Vn, S, P) Vt is the set of terminals Vn is the set of nonterminals S is the start symbol P is the set of productions
More informationWednesday, September 9, 15. Parsers
Parsers What is a parser A parser has two jobs: 1) Determine whether a string (program) is valid (think: grammatically correct) 2) Determine the structure of a program (think: diagramming a sentence) Agenda
More informationParsers. What is a parser. Languages. Agenda. Terminology. Languages. A parser has two jobs:
What is a parser Parsers A parser has two jobs: 1) Determine whether a string (program) is valid (think: grammatically correct) 2) Determine the structure of a program (think: diagramming a sentence) Agenda
More informationCompilation Lecture 3: Syntax Analysis: TopDown parsing. Noam Rinetzky
Compilation 03683133 Lecture 3: Syntax Analysis: TopDown parsing Noam Rinetzky 1 Recursive descent parsing Define a function for every nonterminal Every function work as follows Find applicable production
More informationCOP4020 Programming Languages. Syntax Prof. Robert van Engelen
COP4020 Programming Languages Syntax Prof. Robert van Engelen Overview Tokens and regular expressions Syntax and contextfree grammars Grammar derivations More about parse trees Topdown and bottomup
More informationParsers. Xiaokang Qiu Purdue University. August 31, 2018 ECE 468
Parsers Xiaokang Qiu Purdue University ECE 468 August 31, 2018 What is a parser A parser has two jobs: 1) Determine whether a string (program) is valid (think: grammatically correct) 2) Determine the structure
More informationBottomUp Parsing II (Different types of ShiftReduce Conflicts) Lecture 10. Prof. Aiken (Modified by Professor Vijay Ganesh.
BottomUp Parsing II Different types of ShiftReduce Conflicts) Lecture 10 Ganesh. Lecture 10) 1 Review: BottomUp Parsing Bottomup parsing is more general than topdown parsing And just as efficient Doesn
More informationReview: ShiftReduce Parsing. Bottomup parsing uses two actions: BottomUp Parsing II. Shift ABC xyz ABCx yz. Lecture 8. Reduce Cbxy ijk CbA ijk
Review: ShiftReduce Parsing Bottomup parsing uses two actions: BottomUp Parsing II Lecture 8 Shift ABC xyz ABCx yz Reduce Cbxy ijk CbA ijk Prof. Aiken CS 13 Lecture 8 1 Prof. Aiken CS 13 Lecture 8 2
More informationError Handling SyntaxDirected Translation Recursive Descent Parsing
Announcements rror Handling SyntaxDirected ranslation Lecture 6 PA1 & WA1 Due today at midnight PA2 & WA2 Assigned today Prof. Aiken CS 143 Lecture 6 1 Prof. Aiken CS 143 Lecture 6 2 Outline xtensions
More informationParsing III. (Topdown parsing: recursive descent & LL(1) )
Parsing III (Topdown parsing: recursive descent & LL(1) ) Roadmap (Where are we?) Previously We set out to study parsing Specifying syntax Contextfree grammars Ambiguity Topdown parsers Algorithm &
More informationLL(1) predictive parsing
LL(1) predictive parsing Informatics 2A: Lecture 11 John Longley School of Informatics University of Edinburgh jrl@staffmail.ed.ac.uk 13 October, 2011 1 / 12 1 LL(1) grammars and parse tables 2 3 2 / 12
More informationError Handling SyntaxDirected Translation Recursive Descent Parsing
Announcements rror Handling SyntaxDirected ranslation Lecture 6 PA1 & WA1 Due today at midnight PA2 & WA2 Assigned today Prof. Aiken CS 14 Lecture 6 1 Prof. Aiken CS 14 Lecture 6 2 Outline xtensions of
More informationAmbiguity and Errors SyntaxDirected Translation
Outline Ambiguity (revisited) Ambiguity and rrors SyntaxDirected Translation xtensions of CFG for parsing Precedence declarations rror handling Semantic actions Constructing a parse tree CS780(Prasad)
More informationTabledriven using an explicit stack (no recursion!). Stack can be viewed as containing both terminals and nonterminals.
Bottomup Parsing: Tabledriven using an explicit stack (no recursion!). Stack can be viewed as containing both terminals and nonterminals. Basic operation is to shift terminals from the input to the
More informationFall Compiler Principles Lecture 3: Parsing part 2. Roman Manevich BenGurion University
Fall 20142015 Compiler Principles Lecture 3: Parsing part 2 Roman Manevich BenGurion University Tentative syllabus Front End Intermediate Representation Optimizations Code Generation Scanning Lowering
More informationSyntax Analysis Part I
Syntax Analysis Part I Chapter 4: ContextFree Grammars Slides adapted from : Robert van Engelen, Florida State University Position of a Parser in the Compiler Model Source Program Lexical Analyzer Token,
More information