Syntax Analysis - Part 1. Syntax Analysis

Size: px
Start display at page:

Download "Syntax Analysis - Part 1. Syntax Analysis"

Transcription

1 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 Languages Harry H. Porter,

2 Top-Down Parsing LL Grammars - A subclass of all CFGs Recursive-Descent Parsers - Programmed by hand Non-Recursive Predictive Parsers - Table Driven Simple, Easy to Build, Better Error Handling Bottom-Up Parsing LR Grammars - A larger subclass of CFGs Complex Parsing Algorithms - Table Driven Table Construction Techniques Parser Generators use this technique Faster Parsing, Larger Set of Grammars Complex Error Reporting is Tricky Harry H. Porter,

3 Succeed is string is recognized... and fail if syntax errors Syntax Errors? Good, descriptive, helpful message! Recover and continue parsing! Output of Parser? Build a Parse Tree (also called derivation tree ) Build Abstract Syntax Tree (AST) In memory (with objects, pointers) Output to a file Execute Semantic Actions Build AST Type Checking Generate Code Don t build a tree at all! * x + y 1 Harry H. Porter,

4 Lexical if x<1 thenn y = 5: Typos Syntactic if ((x<1) & (y>5)))... {... { } Semantic if (x+5) then... Type Errors Undefined IDs, etc. Errors in Programs Logical Errors if (i<9) then... Should be <= not < Bugs Compiler cannot detect Logical Errors Harry H. Porter,

5 Compiler Always halts Any checks guaranteed to terminate Decidable Other Program Checking Techniques Debugging Testing Correctness Proofs Partially Decidable Okay? The test terminates. Not Okay? The test may not terminate! You may need to run some programs to see if they are okay. Harry H. Porter,

6 Requirements Detect All Errors (Except Logical!) Messages should be helpful. Difficult to produce clear messages! Example: Syntax Error Example: Line 23: Unmatched Paren if ((x == 1) then ^ Compiler Should Recover Keep going to find more errors Example: x := (a + 5)) * (b + 7)); We re in the middle of a statement This error missed Skip tokens until we see a ; Error detected here Resume Parsing Misses a second error... Oh, well... Checks most of the source Harry H. Porter,

7 Difficult to generate clear and accurate error messages. Example function foo () {... if (...) {... } else { } <eof> Missing } here Not detected until here Example var myvarr: Integer;... x := myvar; Misspelled ID here... Detected here as Undeclared ID Harry H. Porter,

8 For Mature Languages Catalog common errors Statistical studies Tailor compiler to handle common errors well Statement terminators versus separators Terminators: C, Java, PCAT {A;B;C;} Separators: Pascal, Smalltalk, Haskell Pascal Examples begin var t: Integer; t := x; x := y; y := t end if (...) then x := 1 else y := 2; z := 3; Tend to insert a ; here Tend to insert a ; here function foo (x: Integer; y: Integer)... Tend to put a comma here Harry H. Porter,

9 Error-Correcting Compilers Issue an error message Fix the problem Produce an executable Example Error on line 23: myvarr undefined. myvar was used. Is this a good idea??? Compiler guesses the programmer s intent A shifting notion of what constitutes a correct / legal / valid program May encourage programmers to get sloppy Declarations provide redundancy Increased reliability Harry H. Porter,

10 Error Avalanche One error generates a cascade of messages Example x := 5 while ( a == b ) do ^ Expecting ; ^ Expecting ; ^ Expecting ; The real messages may be buried under the avalanche. Missing #include or import will also cause an avalanche. Approaches: Only print 1 message per token [ or per line of source ] Only print a particular message once Error: Variable myvarr is undeclared All future notices for this ID have been suppressed Abort the compiler after 50 errors. Harry H. Porter,

11 Error Recovery Approaches: Panic Mode Discard tokens until we see a synchronizing token. Example Skip to next occurrence of } end ; Resume by parsing the next statement Simple to implement Commonly used The key... Good set of synchronizing tokens Knowing what to do then May skip over large sections of source Harry H. Porter,

12 Error Recovery Approaches: Phrase-Level Recovery Compiler corrects the program by deleting or inserting tokens...so it can proceed to parse from where it was. Example while (x = 4) y := a+b;... Insert do to fix the statement. The key... Don t get into an infinite loop...constantly inserting tokens...and never scanning the actual source Harry H. Porter,

13 Error Recovery Approaches: Error Productions Augment the CFG with Error Productions Now the CFG accepts anything! If error productions are used... Their actions: { print ( Error... ) } Used with... LR (Bottom-up) parsing Parser Generators Error Recovery Approaches: Global Correction Theoretical Approach Find the minimum change to the source to yield a valid program (Insert tokens, delete tokens, swap adjacent tokens) Impractical algorithms - too time consuming Harry H. Porter,

14 CFG: Context Free Grammars Example Rule: Stmt if Expr then Stmt else Stmt Terminals Keywords else else Token Classes ID INTEGER REAL Punctuation ; ; ; Non-terminals Any symbol appearing on the lefthand side of any rule Start Symbol Usually the non-terminal on the lefthand side of the first rule Rules (or Productions ) BNF: Backus-Naur Form / Backus-Normal Form Stmt ::= if Expr then Stmt else Stmt Harry H. Porter,

15 E E + E E ( E ) E - E E ID Rule Alternatives E E E + E ( E ) - E ID E + E ( E ) - E ID All Notations are Equivalent E E + E ( E ) - E ID Harry H. Porter,

16 Terminals a b c... Nonterminals A B C... S Expr Notational Conventions Grammar Symbols (Terminals or Nonterminals) X Y Z U V W... Strings of Symbols α β γ... Strings of Terminals x y z u v w... A sequence of zero Or more terminals And nonterminals Including ε Examples A α B A rule whose righthand side ends with a nonterminal A x α A rule whose righthand side begins with a string of terminals (call it x ) Harry H. Porter,

17 Derivations 1. E E + E 2. E * E 3. ( E ) 4. - E 5. ID A Derivation of (id*id) E (E) (E*E) (id*e) (id*id) Sentential Forms A sequence of terminals and nonterminals in a derivation (id*e) Harry H. Porter,

18 Derives in one step If A β is a rule, then we can write αaγ αβγ Any sentential form containing a nonterminal (call it A)... such that A matches the nonterminal in some rule. Derives in zero-or-more steps * E * (id*id) If α * β and β γ, then α * γ Derives in one-or-more steps + Harry H. Porter,

19 Given G S A grammar The Start Symbol Define L(G) The language generated L(G) = { w S + w} Equivalence of CFG s If two CFG s generate the same language, we say they are equivalent. G 1 G 2 whenever L(G 1 ) = L(G 2 ) In making a derivation... Choose which nonterminal to expand Choose which rule to apply Harry H. Porter,

20 Leftmost Derivations In a derivation... always expand the leftmost nonterminal. E E+E (E)+E (E*E)+E (id*e)+e (id*id)+e (id*id)+id 1. E E + E 2. E * E 3. ( E ) 4. - E 5. ID Let LM denote a step in a leftmost derivation ( LM * means zero-or-more steps ) At each step in a leftmost derivation, we have waγ LM wβγ where A β is a rule (Recall that w is a string of terminals.) Each sentential form in a leftmost derivation is called a left-sentential form. If S LM * α then we say α is a left-sentential form. Harry H. Porter,

21 Rightmost Derivations In a derivation... always expand the rightmost nonterminal. E E+E 1. E E + E E+id 2. E * E (E)+id 3. ( E ) (E*E)+id 4. - E (E*id)+id 5. ID (id*id)+id Let RM denote a step in a rightmost derivation ( RM * means zero-or-more steps ) At each step in a rightmost derivation, we have αaw RM αβw (Recall that w is a string of terminals.) where A β is a rule Each sentential form in a rightmost derivation is called a right-sentential form. If S RM * α then we say α is a right-sentential form. Harry H. Porter,

22 Bottom-Up Parsing Bottom-up parsers discover rightmost derivations! Parser moves from input string back to S. Follow S RM * w in reverse. At each step in a rightmost derivation, we have αaw RM αβw where A β is a rule String of terminals (i.e., the rest of the input, which we have not yet seen) Harry H. Porter,

23 Parse Trees Two choices at each step in a derivation... Which non-terminal to expand Which rule to use in replacing it Leftmost Derivation: E E+E (E)+E (E*E)+E (id*e)+e (id*id)+e (id*id)+id E The parse tree remembers only this E + E 1. E E + E 2. E * E 3. ( E ) 4. - E 5. ID ( E id E * ) E id id Harry H. Porter,

24 Parse Trees Two choices at each step in a derivation... Which non-terminal to expand Which rule to use in replacing it E The parse tree remembers only this Rightmost Derivation: E E+E E+id (E)+id (E*E)+id (E*id)+id (id*id)+id E + E 1. E E + E 2. E * E 3. ( E ) 4. - E 5. ID ( E id E * ) E id id Harry H. Porter,

25 Parse Trees Two choices at each step in a derivation... Which non-terminal to expand Which rule to use in replacing it Leftmost Derivation: E E+E (E)+E (E*E)+E (id*e)+e (id*id)+e (id*id)+id E The parse tree remembers only this Rightmost Derivation: E E+E E+id (E)+id (E*E)+id (E*id)+id (id*id)+id E + E 1. E E + E 2. E * E 3. ( E ) 4. - E 5. ID ( E id E * E ) id id Harry H. Porter,

26 Given a leftmost derivation, we can build a parse tree. Given a rightmost derivation, we can build a parse tree. Lefttmost Derivation of (id*id)+id Rightmost Derivation of (id*id)+id Same Parse Tree Every parse tree corresponds to... A single, unique leftmost derivation A single, unique rightmost derivation Ambiguity: However, one input string may have several parse trees!!! Therefore: Several leftmost derivations Several rightmost derivations Harry H. Porter,

27 Leftmost Derivation #1 E E+E id+e id+e*e id+id*e id+id*id Ambuiguous Grammars E id E + E id E * E id 1. E E + E 2. E * E 3. ( E ) 4. - E 5. ID Input: id+id*id Leftmost Derivation #2 E E*E E+E*E id+e*e id+id*e id+id*id E E + E * E E id id id Harry H. Porter,

28 Ambiguous Grammar More than one Parse Tree for some sentence. The grammar for a programming language may be ambiguous Need to modify it for parsing. Also: Grammar may be left recursive. Need to modify it for parsing. Harry H. Porter,

29 Translating a Regular Expression into a CFG First build the NFA. For every state in the NFA... Make a nonterminal in the grammar For every edge labeled c from A to B... Add the rule A cb For every edge labeled ε from A to B... Add the rule A B For every final state B... Add the rule B ε Harry H. Porter,

30 Recursive Transition Networks Regular Expressions NFA DFA Context-Free Grammar Recursive Transition Networks Exactly as expressive a CFGs... But clearer for humans! IfStmt if Expr then Stmt Expr... elseif Expr then Stmt Stmt else Stmt... end ; Terminal Symbols: Nonterminal Symbols: Harry H. Porter,

31 The Dangling Else Problem This grammar is ambiguous! Stmt if Expr then Stmt if Expr then Stmt else Stmt...Other Stmt Forms... Example String: if E 1 then if E 2 then S 1 else S 2 Interpretation #1: if E 1 then (if E 2 then S 1 else S 2 ) S if E 1 then S if E 2 then S 1 else S 2 Interpretation #2: if E 1 then (if E 2 then S 1 ) else S 2 S if E 1 then S else S 2 if E 2 then S 1 Harry H. Porter,

32 Goal: Match else-clause to the closest if without an else-clause already. Solution: Stmt WithElse The Dangling Else Problem if Expr then Stmt if Expr then WithElse else Stmt...Other Stmt Forms... if Expr then WithElse else WithElse...Other Stmt Forms... Any Stmt occurring between then and else must have an else. i.e., the Stmt must not end with then Stmt. Interpretation #1: if E 1 then (if E 2 then S 1 else S 2 ) Stmt if E 1 then Stmt if then WithElse else WithElse E 2 S 1 S 2 Harry H. Porter,

33 Goal: Match else-clause to the closest if without an else-clause already. Solution: Stmt WithElse The Dangling Else Problem if Expr then Stmt if Expr then WithElse else Stmt...Other Stmt Forms... if Expr then WithElse else WithElse...Other Stmt Forms... Any Stmt occurring between then and else must have an else. i.e., the Stmt must not end with then Stmt. Interpretation #2: if E 1 then (if E 2 then S 1 ) else S 2 Stmt if E 1 then WithElse else Stmt Harry H. Porter,

34 Goal: Match else-clause to the closest if without an else-clause already. Solution: Stmt WithElse The Dangling Else Problem if Expr then Stmt if Expr then WithElse else Stmt...Other Stmt Forms... if Expr then WithElse else WithElse...Other Stmt Forms... Any Stmt occurring between then and else must have an else. i.e., the Stmt must not end with then Stmt. Interpretation #2: if E 1 then (if E 2 then S 1 ) else S 2 Stmt if E 1 then WithElse else Stmt if then WithElse else WithElse E 2 Harry H. Porter,

35 Goal: Match else-clause to the closest if without an else-clause already. Solution: Stmt WithElse The Dangling Else Problem if Expr then Stmt if Expr then WithElse else Stmt...Other Stmt Forms... if Expr then WithElse else WithElse...Other Stmt Forms... Any Stmt occurring between then and else must have an else. i.e., the Stmt must not end with then Stmt. Interpretation #2: if E 1 then (if E 2 then S 1 ) else S 2 Stmt if E 1 then WithElse else Stmt if then WithElse else WithElse E 2 Oops, No Match! Harry H. Porter,

36 Top-Down Parsing Find a left-most derivation Find (build) a parse tree Start building from the root and work down... As we search for a derivation... Must make choices: Which rule to use Where to use it May run into problems! Option 1: Backtracking Made a bad decision Back up and try another choice Option 2: Always make the right choice. Never have to backtrack: Predictive Parser Possible for some grammars (LL Grammars) May be able to fix some grammars (but not others) Eliminate Left Recursion Left-Factor the Rules Harry H. Porter,

37 Input: aabbde S Backtracking 1. S Aa 2. Ce 3. A aab 4. aaba 5. B bbb 6. C aad 7. D bbd Harry H. Porter,

38 Input: aabbde A S a Backtracking 1. S Aa 2. Ce 3. A aab 4. aaba 5. B bbb 6. C aad 7. D bbd Harry H. Porter,

39 Input: aabbde A S a Backtracking 1. S Aa 2. Ce 3. A aab 4. aaba 5. B bbb 6. C aad 7. D bbd a a B Harry H. Porter,

40 Input: aabbde A S a Backtracking 1. S Aa 2. Ce 3. A aab 4. aaba 5. B bbb 6. C aad 7. D bbd a a B Harry H. Porter,

41 Input: aabbde A S a Backtracking 1. S Aa 2. Ce 3. A aab 4. aaba 5. B bbb 6. C aad 7. D bbd a a B Harry H. Porter,

42 Input: aabbde A S a Backtracking 1. S Aa 2. Ce 3. A aab 4. aaba 5. B bbb 6. C aad 7. D bbd a a B b b b Harry H. Porter,

43 Input: aabbde A S a Backtracking 1. S Aa 2. Ce 3. A aab 4. aaba 5. B bbb 6. C aad 7. D bbd a a B b b b Harry H. Porter,

44 Input: aabbde A S a Backtracking 1. S Aa 2. Ce 3. A aab 4. aaba 5. B bbb 6. C aad 7. D bbd a a B b b b Harry H. Porter,

45 Input: aabbde A S a Backtracking 1. S Aa 2. Ce 3. A aab 4. aaba 5. B bbb 6. C aad 7. D bbd a a B b b b Failure Occurs Here!!! Harry H. Porter,

46 Input: aabbde A S a Backtracking 1. S Aa 2. Ce 3. A aab 4. aaba 5. B bbb 6. C aad 7. D bbd a a B We need an ability to back up in the input!!! Harry H. Porter,

47 Input: aabbde A S a Backtracking 1. S Aa 2. Ce 3. A aab 4. aaba 5. B bbb 6. C aad 7. D bbd Harry H. Porter,

48 Input: aabbde A S a Backtracking 1. S Aa 2. Ce 3. A aab 4. aaba 5. B bbb 6. C aad 7. D bbd a a b a Harry H. Porter,

49 Input: aabbde A S a Backtracking 1. S Aa 2. Ce 3. A aab 4. aaba 5. B bbb 6. C aad 7. D bbd a a b a Harry H. Porter,

50 Input: aabbde A S a Backtracking 1. S Aa 2. Ce 3. A aab 4. aaba 5. B bbb 6. C aad 7. D bbd a a b a Harry H. Porter,

51 Input: aabbde A S a Backtracking 1. S Aa 2. Ce 3. A aab 4. aaba 5. B bbb 6. C aad 7. D bbd a a b a Harry H. Porter,

52 Input: aabbde A S a Backtracking 1. S Aa 2. Ce 3. A aab 4. aaba 5. B bbb 6. C aad 7. D bbd a a b a Failure Occurs Here!!! Harry H. Porter,

53 Input: aabbde A S a Backtracking 1. S Aa 2. Ce 3. A aab 4. aaba 5. B bbb 6. C aad 7. D bbd Harry H. Porter,

54 Input: aabbde S Backtracking 1. S Aa 2. Ce 3. A aab 4. aaba 5. B bbb 6. C aad 7. D bbd Harry H. Porter,

55 Input: aabbde C S e Backtracking 1. S Aa 2. Ce 3. A aab 4. aaba 5. B bbb 6. C aad 7. D bbd Harry H. Porter,

56 Input: aabbde S C e Backtracking 1. S Aa 2. Ce 3. A aab 4. aaba 5. B bbb 6. C aad 7. D bbd a a D Harry H. Porter,

57 Input: aabbde C S e Backtracking 1. S Aa 2. Ce 3. A aab 4. aaba 5. B bbb 6. C aad 7. D bbd a a D b b d Harry H. Porter,

58 Input: aabbde C S e Backtracking 1. S Aa 2. Ce 3. A aab 4. aaba 5. B bbb 6. C aad 7. D bbd a a D b b d Harry H. Porter,

59 Will never backtrack! Predictive Parsing Requirement: For every rule: A α 1 α 2 α 3... α N We must be able to choose the correct alternative by looking only at the next symbol May peek ahead to the next symbol (token). Example A ab cd E Assuming a,c FIRST (E) Example Stmt if Expr... for LValue... while Expr... return Expr... ID... Harry H. Porter,

60 Predictive Parsing LL(1) Grammars Can do predictive parsing Can select the right rule Looking at only the next 1 input symbol Harry H. Porter,

61 Predictive Parsing LL(1) Grammars Can do predictive parsing Can select the right rule Looking at only the next 1 input symbol LL(k) Grammars Can do predictive parsing Can select the right rule Looking at only the next k input symbols Harry H. Porter,

62 Predictive Parsing LL(1) Grammars Can do predictive parsing Can select the right rule Looking at only the next 1 input symbol LL(k) Grammars Can do predictive parsing Can select the right rule Looking at only the next k input symbols Techniques to modify the grammar: Left Factoring Removal of Left Recursion Harry H. Porter,

63 Predictive Parsing LL(1) Grammars Can do predictive parsing Can select the right rule Looking at only the next 1 input symbol LL(k) Grammars Can do predictive parsing Can select the right rule Looking at only the next k input symbols Techniques to modify the grammar: Left Factoring Removal of Left Recursion But these may not be enough! Harry H. Porter,

64 Predictive Parsing LL(1) Grammars Can do predictive parsing Can select the right rule Looking at only the next 1 input symbol LL(k) Grammars Can do predictive parsing Can select the right rule Looking at only the next k input symbols Techniques to modify the grammar: Left Factoring Removal of Left Recursion But these may not be enough! LL(k) Language Can be described with an LL(k) grammar. Harry H. Porter,

65 Problem: Stmt Left-Factoring if Expr then Stmt else Stmt if Expr then Stmt OtherStmt With predictive parsing, we need to know which rule to use! (While looking at just the next token) Harry H. Porter,

66 Problem: Stmt Left-Factoring if Expr then Stmt else Stmt if Expr then Stmt OtherStmt With predictive parsing, we need to know which rule to use! (While looking at just the next token) Solution: Stmt if Expr then Stmt ElsePart OtherStmt ElsePart else Stmt ε Harry H. Porter,

67 Problem: Stmt Left-Factoring if Expr then Stmt else Stmt if Expr then Stmt OtherStmt With predictive parsing, we need to know which rule to use! (While looking at just the next token) Solution: Stmt if Expr then Stmt ElsePart OtherStmt ElsePart else Stmt ε General Approach: Before: A αβ 1 αβ 2 αβ 3... δ 1 δ 2 δ 3... After: A αc δ 1 δ 2 δ 3... C β 1 β 2 β 3... Harry H. Porter,

68 Problem: Stmt A Left-Factoring if Expr then Stmt else Stmt α if Expr then Stmt ε α OtherStmt β 2 β 1 δ 1 With predictive parsing, we need to know which rule to use! (While looking at just the next token) Solution: Stmt if Expr then Stmt ElsePart OtherStmt ElsePart else Stmt ε General Approach: Before: A αβ 1 αβ 2 αβ 3... δ 1 δ 2 δ 3... After: A αc δ 1 δ 2 δ 3... C β 1 β 2 β 3... Harry H. Porter,

69 Problem: Stmt A Left-Factoring if Expr then Stmt else Stmt α if Expr then Stmt ε α OtherStmt β 2 β 1 With predictive parsing, we need to know which rule to use! (While looking at just the next token) Solution: Stmt if Expr then Stmt ElsePart A δ 1 α OtherStmt δ 1 ElsePart else Stmt ε C β General Approach: 1 β 2 Before: A αβ 1 αβ 2 αβ 3... δ 1 δ 2 δ 3... After: A αc δ 1 δ 2 δ 3... C β 1 β 2 β 3... C Harry H. Porter,

70 Whenever Left Recursion A + Aα Simplest Case: Immediate Left Recursion Given: A Aα β Transform into: A βa' A' αa' ε where A' is a new nonterminal More General (but still immediate): A Aα 1 Aα 2 Aα 3... β 1 β 2 β 3... Transform into: A β 1 A' β 2 A' β 3A'... A' α 1 A' α 2 A' α 3 A'... ε Harry H. Porter,

71 Example: S Af b A Ac Sd e Is A left recursive? Yes. Left Recursion in More Than One Step Harry H. Porter,

72 Example: S Af b A Ac Sd e Is A left recursive? Yes. Is S left recursive? Left Recursion in More Than One Step Harry H. Porter,

73 Left Recursion in More Than One Step Example: S Af b A Ac Sd e Is A left recursive? Yes. Is S left recursive? Yes, but not immediate left recursion. S Af Sdf Harry H. Porter,

74 Left Recursion in More Than One Step Example: S Af b A Ac Sd e Is A left recursive? Yes. Is S left recursive? Yes, but not immediate left recursion. S Af Sdf Approach: Look at the rules for S only (ignoring other rules)... No left recursion. Harry H. Porter,

75 Left Recursion in More Than One Step Example: S Af b A Ac Sd e Is A left recursive? Yes. Is S left recursive? Yes, but not immediate left recursion. S Af Sdf Approach: Look at the rules for S only (ignoring other rules)... No left recursion. Look at the rules for A... Harry H. Porter,

76 Left Recursion in More Than One Step Example: S Af b A Ac Sd e Is A left recursive? Yes. Is S left recursive? Yes, but not immediate left recursion. S Af Sdf Approach: Look at the rules for S only (ignoring other rules)... No left recursion. Look at the rules for A... Do any of A s rules start with S? Yes. A Sd Harry H. Porter,

77 Left Recursion in More Than One Step Example: S Af b A Ac Sd e Is A left recursive? Yes. Is S left recursive? Yes, but not immediate left recursion. S Af Sdf Approach: Look at the rules for S only (ignoring other rules)... No left recursion. Look at the rules for A... Do any of A s rules start with S? Yes. A Sd Get rid of the S. Substitute in the righthand sides of S. A Afd bd Harry H. Porter,

78 Left Recursion in More Than One Step Example: S Af b A Ac Sd e Is A left recursive? Yes. Is S left recursive? Yes, but not immediate left recursion. S Af Sdf Approach: Look at the rules for S only (ignoring other rules)... No left recursion. Look at the rules for A... Do any of A s rules start with S? Yes. A Sd Get rid of the S. Substitute in the righthand sides of S. A Afd bd The modified grammar: S Af b A Ac Afd bd e Harry H. Porter,

79 Left Recursion in More Than One Step Example: S Af b A Ac Sd e Is A left recursive? Yes. Is S left recursive? Yes, but not immediate left recursion. S Af Sdf Approach: Look at the rules for S only (ignoring other rules)... No left recursion. Look at the rules for A... Do any of A s rules start with S? Yes. A Sd Get rid of the S. Substitute in the righthand sides of S. A Afd bd The modified grammar: S Af b A Ac Afd bd e Now eliminate immediate left recursion involving A. S Af b A bda' ea' A' ca' fda' ε Harry H. Porter,

80 Left Recursion in More Than One Step The Original Grammar: S Af b A Ac Sd e Harry H. Porter,

81 The Original Grammar: S Af b A Ac Sd Be B Ag Sh k Left Recursion in More Than One Step Assume there are still more nonterminals; Look at the next one... Harry H. Porter,

82 Left Recursion in More Than One Step The Original Grammar: S Af b A Ac Sd Be B Ag Sh k So Far: S Af b A bda' BeA' A' ca' fda' ε Harry H. Porter,

83 Left Recursion in More Than One Step The Original Grammar: S Af b A Ac Sd Be B Ag Sh k So Far: S Af b A bda' BeA' A' ca' fda' ε B Ag Sh k Look at the B rules next; Does any righthand side start with S? Harry H. Porter,

84 Left Recursion in More Than One Step The Original Grammar: S Af b A Ac Sd Be B Ag Sh k So Far: S Af b A bda' BeA' A' ca' fda' ε B Ag Afh bh k Substitute, using the rules for S Af... b... Harry H. Porter,

85 Left Recursion in More Than One Step The Original Grammar: S Af b A Ac Sd Be B Ag Sh k So Far: S Af b A bda' BeA' A' ca' fda' ε B Ag Afh bh k Does any righthand side start with A? Harry H. Porter,

86 Left Recursion in More Than One Step The Original Grammar: S Af b A Ac Sd Be B Ag Sh k So Far: S Af b A bda' BeA' A' ca' fda' ε B Ag Afh bh k Do this one first. Harry H. Porter,

87 Left Recursion in More Than One Step The Original Grammar: S Af b A Ac Sd Be B Ag Sh k So Far: S Af b A bda' BeA' A' ca' fda' ε B bda'g BeA'g Afh bh k Substitute, using the rules for A bda'... BeA'... Harry H. Porter,

88 Left Recursion in More Than One Step The Original Grammar: S Af b A Ac Sd Be B Ag Sh k So Far: S Af b A bda' BeA' A' ca' fda' ε B bda'g BeA'g Afh bh k Do this one next. Harry H. Porter,

89 Left Recursion in More Than One Step The Original Grammar: S Af b A Ac Sd Be B Ag Sh k So Far: S Af b A bda' BeA' A' ca' fda' ε B bda'g BeA'g bda'fh BeA'fh bh k Substitute, using the rules for A bda'... BeA'... Harry H. Porter,

90 Left Recursion in More Than One Step The Original Grammar: S Af b A Ac Sd Be B Ag Sh k So Far: S Af b A bda' BeA' A' ca' fda' ε B bda'g BeA'g bda'fh BeA'fh bh k Finally, eliminate any immediate Left recursion involving B Harry H. Porter,

91 Left Recursion in More Than One Step The Original Grammar: S Af b A Ac Sd Be B Ag Sh k So Far: S Af b A bda' BeA' A' ca' fda' ε B bda'gb' bda'fhb' bhb' kb' B' ea'gb' ea'fhb' ε Finally, eliminate any immediate Left recursion involving B Harry H. Porter,

92 Left Recursion in More Than One Step The Original Grammar: S Af b A Ac Sd Be C If there is another nonterminal, B Ag Sh k then do it next. C BkmA AS j So Far: S Af b A bda' BeA' CA' A' ca' fda' ε B bda'gb' bda'fhb' bhb' kb' CA'gB' CA'fhB' B' ea'gb' ea'fhb' ε Harry H. Porter,

93 Algorithm to Eliminate Left Recursion Assume the nonterminals are ordered A 1, A 2, A 3,... (In the example: S, A, B) for each nonterminal A i (for i = 1 to N) do for each nonterminal A j (for j = 1 to i-1) do Let A j β 1 β 2 β 3... β N be all the rules for A j if there is a rule of the form A i A j α then replace it by A i β 1 α β 2 α β 3 α... β N α endif endfor Eliminate immediate left recursion among the A i rules endfor A 1 A 2 A 3... A j... A 7 A i Inner Loop Outer Loop Harry H. Porter,

94 Table-Driven Predictive Parsing Algorithm Assume that the grammar is LL(1) i.e., Backtracking will never be needed Always know which righthand side to choose (with one look-ahead) No Left Recursion Grammar is Left-Factored. Example E' + T E' ε T' * F T' ε F ( E ) id Term... +Term +Term Term Factor... * Factor * Factor *... * Factor Step 1: Step 2: From grammar, construct table. Use table to parse strings. Harry H. Porter,

95 Table-Driven Predictive Parsing Algorithm X Y Z $ Stack of grammar symbols ( a * 4 ) + 3 $ Input String Algorithm Output E' + T E' ε T' * F T' ε F ( E ) id Pre-Computed Table: Harry H. Porter,

96 Table-Driven Predictive Parsing Algorithm X Y Z $ Stack of grammar symbols ( a * 4 ) + 3 $ Input String Algorithm Output E' + T E' ε T' * F T' ε F ( E ) id Pre-Computed Table: Input Symbols, plus $ id + * ( ) $ E E TE' E TE' E' E' +TE' E' ε E' ε Nonterminals T T FT' T FT' T' T' ε T' *FT' T' ε T' ε F F id F (E) Harry H. Porter,

97 Table-Driven Predictive Parsing Algorithm X Y Z $ Stack of grammar symbols Pre-Computed Table: Nonterminals ( a * 4 ) + 3 $ Input String Algorithm Input Symbols, plus $ id + * ( ) $ E E TE' E TE' E' E' +TE' E' ε E' ε T T FT' T FT' T' T' ε T' *FT' T' ε T' ε F F id F (E) Output E' + T E' ε T' * F T' ε F ( E ) id Blank entries indicate ERROR Harry H. Porter,

98 Predictive Parsing Algorithm Set input ptr to first symbol; Place $ after last input symbol Push $ Push S repeat X = stack top a = current input symbol if X is a terminal or X = $ then if X == a then Pop stack Advance input ptr else Error endif elseif Table[X,a] contains a rule then Pop stack Push Y K... Push Y 2 Push Y 1 Print ( X Y 1 Y 2... Y K ) else Syntax Error endif until X == $ // Table[X,a] is blank X A $ // call it X Y 1 Y 2... Y K Y 1 Y 2... Y K A $ Harry H. Porter,

99 Input: (id*id)+id Output: Example E' + T E' ε T' * F T' ε F ( E ) id ( id * id ) + id id + * ( ) $ E E TE' E TE' E' E' +TE' E' ε E' ε T T FT' T FT' T' T' ε T' *FT' T' ε T' ε F F id F (E) Harry H. Porter,

100 Input: (id*id)+id Output: Example E' + T E' ε T' * F T' ε F ( E ) id ( id * id ) + id Add $ to end of input Push $ Push E id + * ( ) $ E E TE' E TE' E' E' +TE' E' ε E' ε T T FT' T FT' T' T' ε T' *FT' T' ε T' ε F F id F (E) Harry H. Porter,

101 Input: (id*id)+id Output: Example E' + T E' ε T' * F T' ε F ( E ) id ( id * id ) + id $ E $ Add $ to end of input Push $ Push E id + * ( ) $ E E TE' E TE' E' E' +TE' E' ε E' ε T T FT' T FT' T' T' ε T' *FT' T' ε T' ε F F id F (E) Harry H. Porter,

102 Input: (id*id)+id Output: Example E' + T E' ε T' * F T' ε F ( E ) id ( id * id ) + id $ E $ Look at Table [ E, ( ] Use rule E TE' Pop E Push E' Push T Print E TE' id + * ( ) $ E E TE' E TE' E' E' +TE' E' ε E' ε T T FT' T FT' T' T' ε T' *FT' T' ε T' ε F F id F (E) Harry H. Porter,

103 Input: (id*id)+id Output: Example E' + T E' ε T' * F T' ε F ( E ) id ( id * id ) + id $ T E $ Look at Table [ E, ( ] Use rule E TE' Pop E Push E' Push T Print E TE' id + * ( ) $ E E TE' E TE' E' E' +TE' E' ε E' ε T T FT' T FT' T' T' ε T' *FT' T' ε T' ε F F id F (E) Harry H. Porter,

104 Input: (id*id)+id Output: Example E' + T E' ε T' * F T' ε F ( E ) id ( id * id ) + id $ T E $ id + * ( ) $ E E TE' E TE' E' E' +TE' E' ε E' ε T T FT' T FT' T' T' ε T' *FT' T' ε T' ε F F id F (E) Table [ T, ( ] = T FT' Pop T Push T Push F Print T FT' Harry H. Porter,

105 Input: (id*id)+id Output: Example E' + T E' ε T' * F T' ε F ( E ) id ( id * id ) + id $ F T E $ id + * ( ) $ E E TE' E TE' E' E' +TE' E' ε E' ε T T FT' T FT' T' T' ε T' *FT' T' ε T' ε F F id F (E) Table [ T, ( ] = T FT' Pop T Push T Push F Print T FT' Harry H. Porter,

106 Input: (id*id)+id Output: Example E' + T E' ε T' * F T' ε F ( E ) id ( id * id ) + id $ F T E $ Table [ F, ( ] = F (E) Pop F Push ) Push E Push ( Print F (E) id + * ( ) $ E E TE' E TE' E' E' +TE' E' ε E' ε T T FT' T FT' T' T' ε T' *FT' T' ε T' ε F F id F (E) Harry H. Porter,

107 Input: (id*id)+id Output: F ( E ) Example E' + T E' ε T' * F T' ε F ( E ) id ( id * id ) + id $ ( E ) T E $ Table [ F, ( ] = F (E) Pop F Push ) Push E Push ( Print F (E) id + * ( ) $ E E TE' E TE' E' E' +TE' E' ε E' ε T T FT' T FT' T' T' ε T' *FT' T' ε T' ε F F id F (E) Harry H. Porter,

108 Input: (id*id)+id Output: F ( E ) Example E' + T E' ε T' * F T' ε F ( E ) id ( id * id ) + id $ ( E ) T E $ Top of Stack matches next input Pop and Scan id + * ( ) $ E E TE' E TE' E' E' +TE' E' ε E' ε T T FT' T FT' T' T' ε T' *FT' T' ε T' ε F F id F (E) Harry H. Porter,

109 Input: (id*id)+id Output: F ( E ) Example E' + T E' ε T' * F T' ε F ( E ) id ( id * id ) + id $ E ) T E $ Top of Stack matches next input Pop and Scan id + * ( ) $ E E TE' E TE' E' E' +TE' E' ε E' ε T T FT' T FT' T' T' ε T' *FT' T' ε T' ε F F id F (E) Harry H. Porter,

110 Input: (id*id)+id Output: F ( E ) Example E' + T E' ε T' * F T' ε F ( E ) id ( id * id ) + id $ E ) T E $ Table [ E, id ] = E TE' Pop E Push E' Push T Print E TE' id + * ( ) $ E E TE' E TE' E' E' +TE' E' ε E' ε T T FT' T FT' T' T' ε T' *FT' T' ε T' ε F F id F (E) Harry H. Porter,

111 Input: (id*id)+id Output: F ( E ) T E ) T E $ Example E' + T E' ε T' * F T' ε F ( E ) id ( id * id ) + id $ Table [ E, id ] = E TE' Pop E Push E' Push T Print E TE' id + * ( ) $ E E TE' E TE' E' E' +TE' E' ε E' ε T T FT' T FT' T' T' ε T' *FT' T' ε T' ε F F id F (E) Harry H. Porter,

112 Input: (id*id)+id Output: F ( E ) T E ) T E $ Example E' + T E' ε T' * F T' ε F ( E ) id id + * ( ) $ E E TE' E TE' E' E' +TE' E' ε E' ε T T FT' T FT' T' T' ε T' *FT' T' ε T' ε F F id F (E) ( id * id ) + id $ Table [ T, id ] = T FT' Pop T Push T' Push F Print T FT' Harry H. Porter,

113 Input: (id*id)+id Output: F ( E ) F T E ) T E $ Example E' + T E' ε T' * F T' ε F ( E ) id id + * ( ) $ E E TE' E TE' E' E' +TE' E' ε E' ε T T FT' T FT' T' T' ε T' *FT' T' ε T' ε F F id F (E) ( id * id ) + id $ Table [ T, id ] = T FT' Pop T Push T' Push F Print T FT' Harry H. Porter,

114 Input: (id*id)+id Output: F ( E ) F T E ) T E $ Example E' + T E' ε T' * F T' ε F ( E ) id ( id * id ) + id $ Table [ F, id ] = F id Pop F Push id Print F id id + * ( ) $ E E TE' E TE' E' E' +TE' E' ε E' ε T T FT' T FT' T' T' ε T' *FT' T' ε T' ε F F id F (E) Harry H. Porter,

115 Input: (id*id)+id Output: F ( E ) F id id T E ) T E $ Example E' + T E' ε T' * F T' ε F ( E ) id ( id * id ) + id $ Table [ F, id ] = F id Pop F Push id Print F id id + * ( ) $ E E TE' E TE' E' E' +TE' E' ε E' ε T T FT' T FT' T' T' ε T' *FT' T' ε T' ε F F id F (E) Harry H. Porter,

116 Input: (id*id)+id Output: F ( E ) F id id T E ) T E $ Example E' + T E' ε T' * F T' ε F ( E ) id ( id * id ) + id $ Top of Stack matches next input Pop and Scan id + * ( ) $ E E TE' E TE' E' E' +TE' E' ε E' ε T T FT' T FT' T' T' ε T' *FT' T' ε T' ε F F id F (E) Harry H. Porter,

117 Input: (id*id)+id Output: F ( E ) F id T E ) T E $ Example E' + T E' ε T' * F T' ε F ( E ) id ( id * id ) + id $ Top of Stack matches next input Pop and Scan id + * ( ) $ E E TE' E TE' E' E' +TE' E' ε E' ε T T FT' T FT' T' T' ε T' *FT' T' ε T' ε F F id F (E) Harry H. Porter,

118 Input: (id*id)+id Output: F ( E ) F id T E ) T E $ Example E' + T E' ε T' * F T' ε F ( E ) id id + * ( ) $ E E TE' E TE' E' E' +TE' E' ε E' ε T T FT' T FT' T' T' ε T' *FT' T' ε T' ε F F id F (E) ( id * id ) + id $ Table [ T', * ] = T' *FT' Pop T' Push T' Push F Push * Print T' *FT' Harry H. Porter,

119 Input: (id*id)+id Output: F ( E ) F id T' * F T' * F T E ) T E $ Example E' + T E' ε T' * F T' ε F ( E ) id id + * ( ) $ E E TE' E TE' E' E' +TE' E' ε E' ε T T FT' T FT' T' T' ε T' *FT' T' ε T' ε F F id F (E) ( id * id ) + id $ Table [ T', * ] = T' *FT' Pop T' Push T' Push F Push * Print T' *FT' Harry H. Porter,

120 Input: (id*id)+id Output: F ( E ) F id T' * F T' * F T E ) T E $ Example E' + T E' ε T' * F T' ε F ( E ) id ( id * id ) + id $ Top of Stack matches next input Pop and Scan id + * ( ) $ E E TE' E TE' E' E' +TE' E' ε E' ε T T FT' T FT' T' T' ε T' *FT' T' ε T' ε F F id F (E) Harry H. Porter,

121 Input: (id*id)+id Output: F ( E ) F id T' * F T' F T E ) T E $ Example E' + T E' ε T' * F T' ε F ( E ) id ( id * id ) + id $ Top of Stack matches next input Pop and Scan id + * ( ) $ E E TE' E TE' E' E' +TE' E' ε E' ε T T FT' T FT' T' T' ε T' *FT' T' ε T' ε F F id F (E) Harry H. Porter,

122 Input: (id*id)+id Output: F ( E ) F id T' * F T' F T E ) T E $ Example E' + T E' ε T' * F T' ε F ( E ) id ( id * id ) + id $ Table [ F, id ] = F id Pop F Push id Print F id id + * ( ) $ E E TE' E TE' E' E' +TE' E' ε E' ε T T FT' T FT' T' T' ε T' *FT' T' ε T' ε F F id F (E) Harry H. Porter,

123 Input: (id*id)+id Output: F ( E ) F id T' * F T' F id id T E ) T E $ Example E' + T E' ε T' * F T' ε F ( E ) id ( id * id ) + id $ Table [ F, id ] = F id Pop F Push id Print F id id + * ( ) $ E E TE' E TE' E' E' +TE' E' ε E' ε T T FT' T FT' T' T' ε T' *FT' T' ε T' ε F F id F (E) Harry H. Porter,

124 Input: (id*id)+id Output: F ( E ) F id T' * F T' F id id T E ) T E $ Example E' + T E' ε T' * F T' ε F ( E ) id ( id * id ) + id $ Top of Stack matches next input Pop and Scan id + * ( ) $ E E TE' E TE' E' E' +TE' E' ε E' ε T T FT' T FT' T' T' ε T' *FT' T' ε T' ε F F id F (E) Harry H. Porter,

125 Input: (id*id)+id Output: F ( E ) F id T' * F T' F id T E ) T E $ Example E' + T E' ε T' * F T' ε F ( E ) id ( id * id ) + id $ Top of Stack matches next input Pop and Scan id + * ( ) $ E E TE' E TE' E' E' +TE' E' ε E' ε T T FT' T FT' T' T' ε T' *FT' T' ε T' ε F F id F (E) Harry H. Porter,

126 Input: (id*id)+id Output: F ( E ) F id T' * F T' F id T E ) T E $ Example E' + T E' ε T' * F T' ε F ( E ) id ( id * id ) + id $ Table [ T', ) ] = T' ε Pop T' Push <nothing> Print T' ε id + * ( ) $ E E TE' E TE' E' E' +TE' E' ε E' ε T T FT' T FT' T' T' ε T' *FT' T' ε T' ε F F id F (E) Harry H. Porter,

127 Input: (id*id)+id Output: F ( E ) F id T' * F T' F id T' ε Example E' + T E' ε T' * F T' ε F ( E ) id ( id * id ) + id $ E Table [ T', ) ] = T' ε ) Pop T' T E Push <nothing> $ Print T' ε id + * ( ) $ E E TE' E TE' E' E' +TE' E' ε E' ε T T FT' T FT' T' T' ε T' *FT' T' ε T' ε F F id F (E) Harry H. Porter,

128 Input: (id*id)+id Output: F ( E ) F id T' * F T' F id T' ε Example E' + T E' ε T' * F T' ε F ( E ) id ( id * id ) + id $ E Table [ E', ) ] = E' ε ) Pop E' T E Push <nothing> $ Print E' ε id + * ( ) $ E E TE' E TE' E' E' +TE' E' ε E' ε T T FT' T FT' T' T' ε T' *FT' T' ε T' ε F F id F (E) Harry H. Porter,

129 Input: (id*id)+id Output: F ( E ) F id T' * F T' F id T' ε E' ε ) T E $ Example E' + T E' ε T' * F T' ε F ( E ) id ( id * id ) + id $ Table [ E', ) ] = E' ε Pop E' Push <nothing> Print E' ε id + * ( ) $ E E TE' E TE' E' E' +TE' E' ε E' ε T T FT' T FT' T' T' ε T' *FT' T' ε T' ε F F id F (E) Harry H. Porter,

130 Input: (id*id)+id Output: F ( E ) F id T' * F T' F id T' ε E' ε ) T E $ Example E' + T E' ε T' * F T' ε F ( E ) id ( id * id ) + id $ Top of Stack matches next input Pop and Scan id + * ( ) $ E E TE' E TE' E' E' +TE' E' ε E' ε T T FT' T FT' T' T' ε T' *FT' T' ε T' ε F F id F (E) Harry H. Porter,

131 Input: (id*id)+id Output: F ( E ) F id T' * F T' F id T' ε E' ε T E $ Example E' + T E' ε T' * F T' ε F ( E ) id ( id * id ) + id $ Top of Stack matches next input Pop and Scan id + * ( ) $ E E TE' E TE' E' E' +TE' E' ε E' ε T T FT' T FT' T' T' ε T' *FT' T' ε T' ε F F id F (E) Harry H. Porter,

132 Input: (id*id)+id Output: F ( E ) F id T' * F T' F id T' ε E' ε T E $ Example E' + T E' ε T' * F T' ε F ( E ) id ( id * id ) + id $ Table [ T', + ] = T' ε Pop T' Push <nothing> Print T' ε id + * ( ) $ E E TE' E TE' E' E' +TE' E' ε E' ε T T FT' T FT' T' T' ε T' *FT' T' ε T' ε F F id F (E) Harry H. Porter,

133 Input: (id*id)+id Output: F ( E ) F id T' * F T' F id T' ε E' ε T' ε E $ Example E' + T E' ε T' * F T' ε F ( E ) id id + * ( ) $ E E TE' E TE' E' E' +TE' E' ε E' ε T T FT' T FT' T' T' ε T' *FT' T' ε T' ε F F id F (E) ( id * id ) + id $ Table [ T', + ] = T' ε Pop T' Push <nothing> Print T' ε Harry H. Porter,

134 Input: (id*id)+id Output: F ( E ) F id T' * F T' F id T' ε E' ε T' ε E $ Example E' + T E' ε T' * F T' ε F ( E ) id id + * ( ) $ E E TE' E TE' E' E' +TE' E' ε E' ε T T FT' T FT' T' T' ε T' *FT' T' ε T' ε F F id F (E) ( id * id ) + id $ Table [ E', + ] = E' +TE' Pop E' Push E' Push T Push + Print E' +TE' Harry H. Porter,

135 Input: (id*id)+id Output: F ( E ) F id T' * F T' F id T' ε E' ε T' ε E' + T E' + T E $ Example E' + T E' ε T' * F T' ε F ( E ) id id + * ( ) $ E E TE' E TE' E' E' +TE' E' ε E' ε T T FT' T FT' T' T' ε T' *FT' T' ε T' ε F F id F (E) ( id * id ) + id $ Table [ E', + ] = E' +TE' Pop E' Push E' Push T Push + Print E' +TE' Harry H. Porter,

136 Input: (id*id)+id Output: F ( E ) F id T' * F T' F id T' ε E' ε T' ε E' + T E' + T E $ Example E' + T E' ε T' * F T' ε F ( E ) id id + * ( ) $ E E TE' E TE' E' E' +TE' E' ε E' ε T T FT' T FT' T' T' ε T' *FT' T' ε T' ε F F id F (E) ( id * id ) + id $ Top of Stack matches next input Pop and Scan Harry H. Porter,

137 Input: (id*id)+id Output: F ( E ) F id T' * F T' F id T' ε E' ε T' ε E' + T E' T E $ Example E' + T E' ε T' * F T' ε F ( E ) id id + * ( ) $ E E TE' E TE' E' E' +TE' E' ε E' ε T T FT' T FT' T' T' ε T' *FT' T' ε T' ε F F id F (E) ( id * id ) + id $ Top of Stack matches next input Pop and Scan Harry H. Porter,

138 Input: (id*id)+id Output: F ( E ) F id T' * F T' F id T' ε E' ε T' ε E' + T E' T E $ Example E' + T E' ε T' * F T' ε F ( E ) id id + * ( ) $ E E TE' E TE' E' E' +TE' E' ε E' ε T T FT' T FT' T' T' ε T' *FT' T' ε T' ε F F id F (E) ( id * id ) + id $ Table [ T, id ] = T FT' Pop T Push T' Push F Print T FT' Harry H. Porter,

139 Input: (id*id)+id Output: F ( E ) F id T' * F T' F id T' ε E' ε T' ε E' + T E' F T E $ Example E' + T E' ε T' * F T' ε F ( E ) id id + * ( ) $ E E TE' E TE' E' E' +TE' E' ε E' ε T T FT' T FT' T' T' ε T' *FT' T' ε T' ε F F id F (E) ( id * id ) + id $ Table [ T, id ] = T FT' Pop T Push T' Push F Print T FT' Harry H. Porter,

140 Input: (id*id)+id Output: F ( E ) F id T' * F T' F id T' ε E' ε T' ε E' + T E' F T E $ Example E' + T E' ε T' * F T' ε F ( E ) id id + * ( ) $ E E TE' E TE' E' E' +TE' E' ε E' ε T T FT' T FT' T' T' ε T' *FT' T' ε T' ε F F id F (E) ( id * id ) + id $ Table [ F, id ] = F id Pop F Push id Print F id Harry H. Porter,

141 Input: (id*id)+id Output: F ( E ) F id T' * F T' F id T' ε E' ε T' ε E' + T E' F id id T E $ Example E' + T E' ε T' * F T' ε F ( E ) id id + * ( ) $ E E TE' E TE' E' E' +TE' E' ε E' ε T T FT' T FT' T' T' ε T' *FT' T' ε T' ε F F id F (E) ( id * id ) + id $ Table [ F, id ] = F id Pop F Push id Print F id Harry H. Porter,

142 Input: (id*id)+id Output: F ( E ) F id T' * F T' F id T' ε E' ε T' ε E' + T E' F id id T E $ Example E' + T E' ε T' * F T' ε F ( E ) id id + * ( ) $ E E TE' E TE' E' E' +TE' E' ε E' ε T T FT' T FT' T' T' ε T' *FT' T' ε T' ε F F id F (E) ( id * id ) + id $ Top of Stack matches next input Pop and Scan Harry H. Porter,

143 Input: (id*id)+id Output: F ( E ) F id T' * F T' F id T' ε E' ε T' ε E' + T E' F id T E $ Example E' + T E' ε T' * F T' ε F ( E ) id id + * ( ) $ E E TE' E TE' E' E' +TE' E' ε E' ε T T FT' T FT' T' T' ε T' *FT' T' ε T' ε F F id F (E) ( id * id ) + id $ Top of Stack matches next input Pop and Scan Harry H. Porter,

144 Input: (id*id)+id Output: F ( E ) F id T' * F T' F id T' ε E' ε T' ε E' + T E' F id T E $ Example E' + T E' ε T' * F T' ε F ( E ) id id + * ( ) $ E E TE' E TE' E' E' +TE' E' ε E' ε T T FT' T FT' T' T' ε T' *FT' T' ε T' ε F F id F (E) ( id * id ) + id $ Table [ T', $ ] = T' ε Pop T' Push <nothing> Print T' ε Harry H. Porter,

145 Input: (id*id)+id Output: F ( E ) F id T' * F T' F id T' ε E' ε T' ε E' + T E' F id T' ε E $ Example E' + T E' ε T' * F T' ε F ( E ) id id + * ( ) $ E E TE' E TE' E' E' +TE' E' ε E' ε T T FT' T FT' T' T' ε T' *FT' T' ε T' ε F F id F (E) ( id * id ) + id $ Table [ T', $ ] = T' ε Pop T' Push <nothing> Print T' ε Harry H. Porter,

146 Input: (id*id)+id Output: F ( E ) F id T' * F T' F id T' ε E' ε T' ε E' + T E' F id T' ε E $ Example E' + T E' ε T' * F T' ε F ( E ) id id + * ( ) $ E E TE' E TE' E' E' +TE' E' ε E' ε T T FT' T FT' T' T' ε T' *FT' T' ε T' ε F F id F (E) ( id * id ) + id $ Table [ E', $ ] = E' ε Pop E' Push <nothing> Print E' ε Harry H. Porter,

147 Input: (id*id)+id Output: F ( E ) F id T' * F T' F id T' ε E' ε T' ε E' + T E' F id T' ε E' ε $ Example E' + T E' ε T' * F T' ε F ( E ) id id + * ( ) $ E E TE' E TE' E' E' +TE' E' ε E' ε T T FT' T FT' T' T' ε T' *FT' T' ε T' ε F F id F (E) ( id * id ) + id $ Table [ E', $ ] = E' ε Pop E' Push <nothing> Print E' ε Harry H. Porter,

148 Input: (id*id)+id Output: F ( E ) F id T' * F T' F id T' ε E' ε T' ε E' + T E' F id T' ε E' ε $ Example E' + T E' ε T' * F T' ε F ( E ) id id + * ( ) $ E E TE' E TE' E' E' +TE' E' ε E' ε T T FT' T FT' T' T' ε T' *FT' T' ε T' ε F F id F (E) ( id * id ) + id $ Input symbol == $ Top of stack == $ Loop terminates with success Harry H. Porter,

149 Input: (id*id)+id Output: Reconstructing the Parse Tree E T E' E' + T E' ε T' * F T' ε F ( E ) id Harry H. Porter,

150 Input: (id*id)+id Output: Reconstructing the Parse Tree E T E' E' + T E' ε T' * F T' ε F ( E ) id F T' Harry H. Porter,

151 Input: (id*id)+id Output: F ( E ) Reconstructing the Parse Tree E T E' E' + T E' ε T' * F T' ε F ( E ) id F T' ( E ) Harry H. Porter,

152 Input: (id*id)+id Output: F ( E ) Reconstructing the Parse Tree E T E' F T' E' + T E' ε T' * F T' ε F ( E ) id ( E ) T E' Harry H. Porter,

153 Input: (id*id)+id Output: F ( E ) Reconstructing the Parse Tree E T E' F T' E' + T E' ε T' * F T' ε F ( E ) id ( E ) T E' F T' Harry H. Porter,

154 Input: (id*id)+id Output: F ( E ) F id Reconstructing the Parse Tree E T E' F T' ( E ) E' + T E' ε T' * F T' ε F ( E ) id T E' F T' id Harry H. Porter,

155 Input: (id*id)+id Output: F ( E ) F id T' * F T' Reconstructing the Parse Tree E T E' F T' ( E ) E' + T E' ε T' * F T' ε F ( E ) id T E' F T' id * F T' Harry H. Porter,

156 Input: (id*id)+id Output: F ( E ) F id T' * F T' F id Reconstructing the Parse Tree E T E' F T' ( E ) E' + T E' ε T' * F T' ε F ( E ) id T E' F T' id * F T' id Harry H. Porter,

157 Input: (id*id)+id Output: F ( E ) F id T' * F T' F id T' ε Reconstructing the Parse Tree T E' F T' ( E ) T E' E E' + T E' ε T' * F T' ε F ( E ) id F T' id * F T' id ε Harry H. Porter,

158 Input: (id*id)+id Output: F ( E ) F id T' * F T' F id T' ε E' ε Reconstructing the Parse Tree T E' F T' ( E ) T E' E E' + T E' ε T' * F T' ε F ( E ) id F T' ε id * F T' id ε Harry H. Porter,

159 Input: (id*id)+id Output: F ( E ) F id T' * F T' F id T' ε E' ε T' ε Reconstructing the Parse Tree T E' F T' ( E ) ε T E' F T' ε E E' + T E' ε T' * F T' ε F ( E ) id id * F T' id ε Harry H. Porter,

160 Input: (id*id)+id Output: F ( E ) F id T' * F T' F id T' ε E' ε T' ε E' + T E' Reconstructing the Parse Tree T E' F T' + T ( E ) ε T E' ε F T' E E' + T E' ε T' * F T' ε F ( E ) id E' id * F T' id ε Harry H. Porter,

161 Input: (id*id)+id Output: F ( E ) F id T' * F T' F id T' ε E' ε T' ε E' + T E' Reconstructing the Parse Tree F T T ( E ) ε T' F E' ε T' E + F E' T T' E' + T E' ε T' * F T' ε F ( E ) id E' id * F T' id ε Harry H. Porter,

162 Input: (id*id)+id Output: F ( E ) F id T' * F T' F id T' ε E' ε T' ε E' + T E' F id Reconstructing the Parse Tree F T T ( E ) ε T' F E' ε T' E + F id E' T T' E' + T E' ε T' * F T' ε F ( E ) id E' id * F T' id ε Harry H. Porter,

163 Input: (id*id)+id Output: F ( E ) F id T' * F T' F id T' ε E' ε T' ε E' + T E' F id T' ε Reconstructing the Parse Tree F id T * T ( E ) ε T' F F T' E' ε T' E + F id E' T T' ε E' + T E' ε T' * F T' ε F ( E ) id E' id ε Harry H. Porter,

164 Input: (id*id)+id Output: F ( E ) F id T' * F T' F id T' ε E' ε T' ε E' + T E' F id T' ε E' ε Reconstructing the Parse Tree F id T * T ( E ) ε F T' ε T' F F T' E' ε T' E + id E' T ε E' + T E' ε T' * F T' ε F ( E ) id E' id ε Harry H. Porter,

165 Input: (id*id)+id Output: F ( E ) F id T' * F T' F id T' ε E' ε T' ε E' + T E' F id T' ε E' ε Reconstructing the Parse Tree Leftmost Derivation: E T E' F T' E' ( E ) T' E' ( T E' ) T' E' ( F T' E' ) T' E' ( id T' E' ) T' E' ( id * F T' E' ) T' E' ( id * id T' E' ) T' E' ( id * id E' ) T' E' ( id * id ) T' E' ( id * id ) E' ( id * id ) + T E' ( id * id ) + F T' E' ( id * id ) + id T' E' ( id * id ) + id E' ( id * id ) + id E' + T E' ε T' * F T' ε F ( E ) id Harry H. Porter,

166 FIRST Function Let α be a string of symbols (terminals and nonterminals) Define: FIRST (α) = The set of terminals that could occur first in any string derivable from α = { a α * aw, plus ε if α * ε } Harry H. Porter,

167 FIRST Function Let α be a string of symbols (terminals and nonterminals) Define: FIRST (α) = The set of terminals that could occur first in any string derivable from α = { a α * aw, plus ε if α * ε } Example: E' + T E' ε T' * F T' ε F ( E ) id FIRST (F) =? Harry H. Porter,

168 FIRST Function Let α be a string of symbols (terminals and nonterminals) Define: FIRST (α) = The set of terminals that could occur first in any string derivable from α = { a α * aw, plus ε if α * ε } Example: E' + T E' ε T' * F T' ε F ( E ) id FIRST (F) = { (, id } FIRST (T') =? Harry H. Porter,

169 FIRST Function Let α be a string of symbols (terminals and nonterminals) Define: FIRST (α) = The set of terminals that could occur first in any string derivable from α = { a α * aw, plus ε if α * ε } Example: E' + T E' ε T' * F T' ε F ( E ) id FIRST (F) = { (, id } FIRST (T') = { *, ε} FIRST (T) =? Harry H. Porter,

170 FIRST Function Let α be a string of symbols (terminals and nonterminals) Define: FIRST (α) = The set of terminals that could occur first in any string derivable from α = { a α * aw, plus ε if α * ε } Example: E' + T E' ε T' * F T' ε F ( E ) id FIRST (F) = { (, id } FIRST (T') = { *, ε} FIRST (T) = { (, id } FIRST (E') =? Harry H. Porter,

171 FIRST Function Let α be a string of symbols (terminals and nonterminals) Define: FIRST (α) = The set of terminals that could occur first in any string derivable from α = { a α * aw, plus ε if α * ε } Example: E' + T E' ε T' * F T' ε F ( E ) id FIRST (F) = { (, id } FIRST (T') = { *, ε} FIRST (T) = { (, id } FIRST (E') = { +, ε} FIRST (E) =? Harry H. Porter,

172 FIRST Function Let α be a string of symbols (terminals and nonterminals) Define: FIRST (α) = The set of terminals that could occur first in any string derivable from α = { a α * aw, plus ε if α * ε } Example: E' + T E' ε T' * F T' ε F ( E ) id FIRST (F) = { (, id } FIRST (T') = { *, ε} FIRST (T) = { (, id } FIRST (E') = { +, ε} FIRST (E) = { (, id } Harry H. Porter,

173 To Compute the FIRST Function For all symbols X in the grammar... if X is a terminal then FIRST(X) = { X } if X ε is a rule then add ε to FIRST(X) if X Y 1 Y 2 Y 3... Y K is a rule then if a FIRST(Y 1 ) then add a to FIRST(X) if ε FIRST(Y 1 ) and a FIRST(Y 2 ) then add a to FIRST(X) if ε FIRST(Y 1 ) and ε FIRST(Y 2 ) and a FIRST(Y 3 ) then add a to FIRST(X)... if ε FIRST(Y i ) for all Y i then add ε to FIRST(X) Repeat until nothing more can be added to any sets. Harry H. Porter,

174 To Compute the FIRST(X 1 X 2 X 3...X N ) Result = {} Add everything in FIRST(X 1 ), except ε, to result Harry H. Porter,

175 To Compute the FIRST(X 1 X 2 X 3...X N ) Result = {} Add everything in FIRST(X 1 ), except ε, to result if ε FIRST(X 1 ) then Add everything in FIRST(X 2 ), except ε, to result endif Harry H. Porter,

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Ambiguity, Precedence, Associativity & Top-Down Parsing. Lecture 9-10 Ambiguity, Precedence, Associativity & Top-Down Parsing Lecture 9-10 (From slides by G. Necula & R. Bodik) 2/13/2008 Prof. Hilfinger CS164 Lecture 9 1 Administrivia Team assignments this evening for all

More 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

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

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

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

Bottom up parsing. General idea LR(0) SLR LR(1) LALR To best exploit JavaCUP, should understand the theoretical basis (LR parsing); Bottom up parsing General idea LR(0) SLR LR(1) LALR To best exploit JavaCUP, should understand the theoretical basis (LR parsing); 1 Top-down vs Bottom-up Bottom-up more powerful than top-down; Can process

More 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

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

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

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

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

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

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

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

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

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

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

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

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

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

Compiler Design 1. LR Parsing. Goutam Biswas. Lect 7

Compiler Design 1. LR Parsing. Goutam Biswas. Lect 7 Compiler Design 1 LR Parsing Compiler Design 2 LR(0) Parsing An LR(0) parser can take shift-reduce decisions entirely on the basis of the states of LR(0) automaton a of the grammar. Consider the following

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

INF5110 Compiler Construction

INF5110 Compiler Construction INF5110 Compiler Construction Parsing Spring 2016 1 / 131 Outline 1. Parsing Bottom-up parsing Bibs 2 / 131 Outline 1. Parsing Bottom-up parsing Bibs 3 / 131 Bottom-up parsing: intro LR(0) SLR(1) LALR(1)

More 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

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

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

ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών. Lecture 7a Syntax Analysis Elias Athanasopoulos

ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών. Lecture 7a Syntax Analysis Elias Athanasopoulos ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών Lecture 7a Syntax Analysis Elias Athanasopoulos eliasathan@cs.ucy.ac.cy Operator-precedence Parsing A class of shiq-reduce parsers that can be writen by hand

More information

CPS 220 Theory of Computation Pushdown Automata (PDA)

CPS 220 Theory of Computation Pushdown Automata (PDA) CPS 220 Theory of Computation Pushdown Automata (PDA) Nondeterministic Finite Automaton with some extra memory Memory is called the stack, accessed in a very restricted way: in a First-In First-Out fashion

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

Pushdown Automata (2015/11/23)

Pushdown Automata (2015/11/23) Chapter 6 Pushdown Automata (2015/11/23) Sagrada Familia, Barcelona, Spain Outline 6.0 Introduction 6.1 Definition of PDA 6.2 The Language of a PDA 6.3 Euivalence of PDA s and CFG s 6.4 Deterministic PDA

More information

Outline. CS21 Decidability and Tractability. Machine view of FA. Machine view of FA. Machine view of FA. Machine view of FA.

Outline. CS21 Decidability and Tractability. Machine view of FA. Machine view of FA. Machine view of FA. Machine view of FA. Outline CS21 Decidability and Tractability Lecture 5 January 16, 219 and Languages equivalence of NPDAs and CFGs non context-free languages January 16, 219 CS21 Lecture 5 1 January 16, 219 CS21 Lecture

More information

Syntax Analysis (Part 2)

Syntax Analysis (Part 2) Syntax Analysis (Part 2) Martin Sulzmann Martin Sulzmann Syntax Analysis (Part 2) 1 / 42 Bottom-Up Parsing Idea Build right-most derivation. Scan input and seek for matching right hand sides. Terminology

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

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

INF5110 Compiler Construction

INF5110 Compiler Construction INF5110 Compiler Construction Spring 2017 1 / 330 Outline 1. Parsing First and follow sets Top-down parsing Bottom-up parsing References 2 / 330 INF5110 Compiler Construction Parsing Spring 2017 3 / 330

More information

Pushdown Automata. Reading: Chapter 6

Pushdown Automata. Reading: Chapter 6 Pushdown Automata Reading: Chapter 6 1 Pushdown Automata (PDA) Informally: A PDA is an NFA-ε with a infinite stack. Transitions are modified to accommodate stack operations. Questions: What is a stack?

More information

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

(NB. Pages are intended for those who need repeated study in formal languages) Length of a string. Formal languages. Substrings: Prefix, suffix. (NB. Pages 22-40 are intended for those who need repeated study in formal languages) Length of a string Number of symbols in the string. Formal languages Basic concepts for symbols, strings and languages:

More information

Pushdown Automata: Introduction (2)

Pushdown Automata: Introduction (2) Pushdown Automata: Introduction Pushdown automaton (PDA) M = (K, Σ, Γ,, s, A) where K is a set of states Σ is an input alphabet Γ is a set of stack symbols s K is the start state A K is a set of accepting

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

UNIT-VI PUSHDOWN AUTOMATA

UNIT-VI PUSHDOWN AUTOMATA Syllabus R09 Regulation UNIT-VI PUSHDOWN AUTOMATA The context free languages have a type of automaton that defined them. This automaton, called a pushdown automaton, is an extension of the nondeterministic

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

Compiler Design. Spring Syntactic Analysis. Sample Exercises and Solutions. Prof. Pedro C. Diniz

Compiler Design. Spring Syntactic Analysis. Sample Exercises and Solutions. Prof. Pedro C. Diniz Compiler Design Spring 2015 Syntactic Analysis Sample Exercises and Solutions Prof. Pedro C. Diniz USC / Information Sciences Institute 4676 Admiralty Way, Suite 1001 Marina del Rey, California 90292 pedro@isi.edu

More 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

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

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

Chapter 4: Context-Free Grammars

Chapter 4: Context-Free Grammars Chapter 4: Context-Free Grammars 4.1 Basics of Context-Free Grammars Definition A context-free grammars, or CFG, G is specified by a quadruple (N, Σ, P, S), where N is the nonterminal or variable alphabet;

More 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

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

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

Compilers. Lexical analysis. Yannis Smaragdakis, U. Athens (original slides by Sam

Compilers. Lexical analysis. Yannis Smaragdakis, U. Athens (original slides by Sam Compilers Lecture 3 Lexical analysis Yannis Smaragdakis, U. Athens (original slides by Sam Guyer@Tufts) Big picture Source code Front End IR Back End Machine code Errors Front end responsibilities Check

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

Handout 8: Computation & Hierarchical parsing II. Compute initial state set S 0 Compute initial state set S 0

Handout 8: Computation & Hierarchical parsing II. Compute initial state set S 0 Compute initial state set S 0 Massachusetts Institute of Technology 6.863J/9.611J, Natural Language Processing, Spring, 2001 Department of Electrical Engineering and Computer Science Department of Brain and Cognitive Sciences Handout

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

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

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

Harvard CS 121 and CSCI E-207 Lecture 9: Regular Languages Wrap-Up, Context-Free Grammars Harvard CS 121 and CSCI E-207 Lecture 9: Regular Languages Wrap-Up, Context-Free Grammars Salil Vadhan October 2, 2012 Reading: Sipser, 2.1 (except Chomsky Normal Form). Algorithmic questions about regular

More information

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

Compiling Techniques

Compiling Techniques Lecture 3: Introduction to 22 September 2017 Reminder Action Create an account and subscribe to the course on piazza. Coursework Starts this afternoon (14.10-16.00) Coursework description is updated regularly;

More 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

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

Chapter 1. Formal Definition and View. Lecture Formal Pushdown Automata on the 28th April 2009

Chapter 1. Formal Definition and View. Lecture Formal Pushdown Automata on the 28th April 2009 Chapter 1 Formal and View Lecture on the 28th April 2009 Formal of PA Faculty of Information Technology Brno University of Technology 1.1 Aim of the Lecture 1 Define pushdown automaton in a formal way

More information

Foundations of Informatics: a Bridging Course

Foundations of Informatics: a Bridging Course Foundations of Informatics: a Bridging Course Week 3: Formal Languages and Semantics Thomas Noll Lehrstuhl für Informatik 2 RWTH Aachen University noll@cs.rwth-aachen.de http://www.b-it-center.de/wob/en/view/class211_id948.html

More information

UNIT-VIII COMPUTABILITY THEORY

UNIT-VIII COMPUTABILITY THEORY CONTEXT SENSITIVE LANGUAGE UNIT-VIII COMPUTABILITY THEORY A Context Sensitive Grammar is a 4-tuple, G = (N, Σ P, S) where: N Set of non terminal symbols Σ Set of terminal symbols S Start symbol of the

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

Pushdown Automata. Notes on Automata and Theory of Computation. Chia-Ping Chen

Pushdown Automata. Notes on Automata and Theory of Computation. Chia-Ping Chen Pushdown Automata Notes on Automata and Theory of Computation Chia-Ping Chen Department of Computer Science and Engineering National Sun Yat-Sen University Kaohsiung, Taiwan ROC Pushdown Automata p. 1

More information

Note: In any grammar here, the meaning and usage of P (productions) is equivalent to R (rules).

Note: In any grammar here, the meaning and usage of P (productions) is equivalent to R (rules). Note: In any grammar here, the meaning and usage of P (productions) is equivalent to R (rules). 1a) G = ({R, S, T}, {0,1}, P, S) where P is: S R0R R R0R1R R1R0R T T 0T ε (S generates the first 0. R generates

More information

Context-Free Grammar

Context-Free Grammar Context-Free Grammar CFGs are more powerful than regular expressions. They are more powerful in the sense that whatever can be expressed using regular expressions can be expressed using context-free grammars,

More information

Bottom-Up Syntax Analysis

Bottom-Up Syntax Analysis Bottom-Up Syntax Analysis Wilhelm/Maurer: Compiler Design, Chapter 8 Reinhard Wilhelm Universität des Saarlandes wilhelm@cs.uni-sb.de and Mooly Sagiv Tel Aviv University sagiv@math.tau.ac.il Subjects Functionality

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

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

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