國立台灣大學 資訊工程學系 薛智文 98 spring syntax analysis parser 1/2 (textbook...

Download 國立台灣大學 資訊工程學系 薛智文  98 Spring Syntax Analysis  Parser 1/2 (textbook ch#4.14.7)

If you can't read please download the document

Upload: joella-mitchell

Post on 18-Jan-2018

252 views

Category:

Documents


0 download

DESCRIPTION

資工系網媒所 NEWS 實驗室 01:54 /522 Syntax Analysis Introduction Context-Free Grammars Writing a Grammar Top-Down Parsing Bottom-Up Parsing Introduction to LR Parsing: Simple LR More Powerful LR Parsers Using Ambiguous Grammars Parser Generators

TRANSCRIPT

98 Spring Syntax Analysis Parser 1/2 (textbook ch#4.14.7) NEWS 01:54 /521 The Role of Parser Lexical Analyzer Rest of Front End Symbol Table token get Next Token source program intermediate representation Parser parse tree NEWS 01:54 /522 Syntax Analysis Introduction Context-Free Grammars Writing a Grammar Top-Down Parsing Bottom-Up Parsing Introduction to LR Parsing: Simple LR More Powerful LR Parsers Using Ambiguous Grammars Parser Generators NEWS 01:54 /523 Main Tasks Abstract representations of the input program: abstract-syntax tree + symbol table intermediate code object code Context free grammar (CFG) or Backus-Naur Form (BNF) notation is used to specify the structure of legal programs. BNF is a metasyntax used to express CFGmetasyntax a formal way to describe formal languages. A metasyntax is a syntax used to describe the syntax of languages,syntaxlanguages A formal language : defined by precise mathematical or machine processable formulas. Error Handling. parser if it is a legal program, then output some abstract representation of the program a program represented by a sequence of tokens NEWS 01:54 /524 BNF (Backus-Naur Form) ::= "::=" " " ::= | ::= " ">" "::=" ::= " " | "" ::= | "|" ::= | ::= | " ">" ::= '"' '"' | "'" "'" ::= ::= | ::= | "." ::= ::= "," NEWS 01:54 /525 Error handling Types of errors: Syntactic errors. Static semantic errors. Example: a variable is not declared or declared twice in a language where a variable must be declared before its usage. Goals: Reports errors clearly and accurately. Recover from errors quickly enough to detect subsequent errors. Spent minimal overhead. Strategies: synchronizing tokens Panic-mode recovery: skip until synchronizing tokens are found. ; marks the end of a C-sentence; } closes a C-scope. Phrase-level recovery: perform local correction and then continue. assume a un-declared variable is declared with int. Error productions: anticipating common errors using grammars. missing ; between two var-declarations; Global correction: choose a minimal sequence of changes to obtain a globally least-cost correction. A very difficult task. May have more than one interpretations. Example in C: Is y = x; missing an operand in multiplication or the type of x should be pointer? NEWS 01:54 /526 Context Free Grammar (CFG) Definitions: G = ( T, N, P, S ), where terminals T : a set of terminals (in lower case letters); nonterminals N : a set of nonterminals (in upper case letters); productions P : productions of the form A 1, 2,, m, where A N and i T N ; S : the starting nonterminal, S N. Notations: terminals : strings with lower-cased English letters and printable characters. Examples: a, b, c, int and int_1. nonterminals: strings started with an upper-cased English letter. Examples: A, B, C, and Procedure. ,,, ( T N )* ,, and : alpha, beta, gamma and epsilon. A 1 A 2 } A 1 | 2 NEWS 01:54 /527 How does a CFG define a language? The language defined by the grammar is the set of strings (sequence of terminals) that can be derived from the starting nonterminal. How to derive something? Start with: current sequence = the starting nonterminal. Repeat find a nonterminal X in the current sequence find a production in the grammar with X on the left of the form X , where is or a sequence of terminals and/or nonterminals. create a new current sequence in which replaces X Until current sequence contains no nonterminals. We derive either or a string of terminals. NEWS 01:54 /528 Example Grammar E E int E E E E E 1 E E E / E 1 E / E E ( E ) 1 4 / 2 Details: The first step was done by choosing the second production. The second step was done by choosing the first production. Conventions: : means derives in one step; : means derives in one or more steps; : means derives in zero or more steps; In the above example, we can write E 1 4 / 2 NEWS 01:54 /529 Language language The language defined by a grammar G is L ( G ) = {w | S w}, where S is the starting symbol and w is a sequence of terminals or . element An element in a language is or a sequence of terminals in the set defined by the language. More terminology : derivation E 1 4 / 2 is a derivation of 1 4 / 2 from E. There are several kinds of derivations that are important: leftmost The derivation is a leftmost one if the leftmost nonterminal always gets to be chosen (if we have a choice) to be replaced. rightmost It is a rightmost one if the rightmost nonterminal is replaced all the times. A language that can be generated by a grammar is a context-free language. What is a sentence or a sentential form? NEWS 01:54 /5210 A Way to Describe Derivations derivationparse tree Construct a derivation or parse tree as follows: start with the starting nonterminal as a single-node tree REPEAT choose a leaf nonterminal X choose a production X symbols in become the children of X UNTIL no more leaf nonterminal left top-down This is called top-down parsing or expanding of the parse tree. Construct the parse tree starting from the root. bottom-up Other parsing methods, such as bottom-up, are known. NEWS 01:54 /5211 Top-down parsing Need to annotate the order of derivation on the nodes. It is better to keep a systematic order in parsing for the sake of performance or ease-to-understand. leftmost rightmost E E 1 E 1 E / E 1 E / 2 1 4 / 2 E E E E 1 E 4 E 2 / (4) (1) (2) (3) (5) NEWS 01:54 /5212 Parse Tree Examples Example: Using 1 4/2 as the input, the left parse tree is derived. A string is formed by reading the leaf nodes from left to right, which gives 1 4/2. The string 1 4/2 has another parse tree on the right. Grammar : E int E E E E E / E E (E) Some standard notations: Given a parse tree and a fixed order (for example leftmost or rightmost), we can derive the order of derivation. For the semantic of the parse tree, we normally interpret the meaning in a bottom-up fashion. That is, the one that is derived last will be serviced first. yieldfrontier The leaves of a parse tree are labeled by nonterminals or terminals, and read from left to right, constitute a sentential form, called the yield of frontier of the tree. E E E 1 E 4 E 2 / leftmost derivation E E E / 2 E 1 E 4 rightmost derivation NEWS 01:54 /5213 Ambiguous Grammar If for grammar G and string , there are more than one leftmost derivation for , or more than one rightmost derivation for , or more than one parse tree for , ambiguous then G is called ambiguous. Note: the above three conditions are equivalent in that if one is true, then all three are true. Q: How to prove this? Hint: Any unannotated tree can be annotated with a leftmost numbering. Problems with an ambiguous grammar: Ambiguity can make parsing difficult. Underlying structure is ill-defined: in the example, the precedence is not uniquely defined, e.g., the leftmost parse tree groups 4/2 while the rightmost parse tree groups 1 4, resulting in two different semantics. NEWS 01:54 /5214 How to use CFG ? Breaks down the problem into pieces. Think about a C program: Declarations: typedef, struct, variables,... Procedures: type-specifier, function name, parameters, function body. function body: various statements. Example: Procedure TypeDef id OptParams OptDecl {OptStatements} TypeDef integer | char | float | OptParams ( ListParams ) ListParams | NonEmptyParList NonEmptyParList NonEmptyParList, id | id One of purposes to write a grammar for a language is for others to understand. It will be nice to break things up into different levels in a top-down easily understandable fashion. NEWS 01:54 /5215 Non-context Free Grammars Some grammar is not CFG, that is, it may be context sensitive. Expressive power of grammars (in the order of small to large): Regular expression FA. Context-free grammar Context-sensitive grammar { wcw | w is a string of a and b s } cannot be expressed by CFG. NEWS 01:54 /5216 Common Grammar Problems (CGP) A grammar may have some bad styles or ambiguity. Some common grammar problems (CGPs) are: Useless terms; Ambiguity; Left factor; Left recursion. Top-down parsing can not handle left recursion. How to rewrite a grammar G 1 into another grammar G 2 so that G 2 has no CGPs and the two grammars are equivalent? G 1 and G 2 must accept the same set of strings, that is, L(G 1 ) = L(G 2 ). The semantic of a given string must stay the same using G 2. The main structure of the parse tree may need to stay unchanged. NEWS 01:54 /5217 CGP: useless terms useless A non-terminal X is useless if either a sequence includes X cannot be derived from the starting nonterminal, or no string can be derived starting from X, where a string means or a sequence of terminals. Example 1: S A B A + | | B digit | B digit C . B In Example 1: C is useless and so is the last production. Any nonterminal not in the right-hand side of any production is useless! NEWS 01:54 /5218 More Examples for Useless Terms Example 2: S X | Y X ( ) Y ( Y Y ) Y derives more and more nonterminals and is useless. Any recursively defined nonterminal without a production of deriving or a string of all terminals is useless! Direct useless. Indirect useless: one can only derive direct useless terms. From now on, we assume a grammar contains no useless nonterminals. NEWS 01:54 /5219 CGP: ambiguity (1/2) Sometimes an ambiguous grammar can be rewritten to eliminate the ambiguity. Example: G 1 S if E then S S if E then S else S S Others Input: if E 1 then if E 2 then S 1 else S 2 G 1 is ambiguous given the above input. Has two parse trees. dangling-else dangling-else ambiguity. S S ifE1E1 then S2S2 S1S1 elseifE2E2 then S S2S2 SelseifE1E1 then S1S1 ifE2E2 then NEWS 01:54 /5220 CGP: ambiguity (2/2) Rewrite G 1 into the following: G 2 S M atched | O pen M if E then M else M | Others O if E then S O if E then M else O Only one parse tree for the input if E 1 then if E 2 then S 1 else S 2 using grammar G 2. Intuition: else is matched with the nearest then. Others stands for any other statement. S S S2S2 S1S1 O if else E1E1 then M ifE2E2 then NEWS 01:54 /5221 CGP: left factor Left factor: a grammar G has two productions whose right-hand-sides have a common prefix. left-factors Have left-factors. Example: S (S) | () In this example, the common prefix is (. left-factoring This problem can be solved by using the left-factoring trick. A 1 | 2 Transform to: A A' A' 1 | 2 Example: S (S) | () Transform to S (S' S' S) | ) NEWS 01:54 /5222 Algorithm for left-factoring Input: context free grammar G left-factored Output: equivalent left-factored context-free grammar G for each nonterminal A do find the longest non- prefix that is common to right-hand sides of two or more productions; Replace A 1 | | n | 1 | | m with A A' | 1 | | m A' 1 | | n repeat the above process until A has no two productions with a common prefix; Example: S aaWaa | aaaa | aaTcc | bb Transform to S aaS ' | bb S ' Waa | aa | Tcc NEWS 01:54 /5223 CGP: left recursion Definitions: recursive recursive grammar: a grammar is recursive if this grammar contains a nonterminal X such that X X left-recursive G is left-recursive if X X immediately left-recursive G is immediately left-recursive if X X Why left recursion is bad? Potentially difficult to parse if you read input from left to right. Difficult to know when recursion should be stopped. NEWS 01:54 /5224 Example of Removing Immediate Left-recursion Need to remove left-recursion to come out an LL(1) grammar. Example: Grammar G : A A | , where does not start with A Revised grammar G' : A A' A' A' | The above two grammars are equivalent. That is L(G) L(G ' ). Example: A A a A a b leftmost derivation original grammar G A b A' a a leftmost derivation revised grammar G' input baa b a NEWS 01:54 /5225 Rules for Removing Immediate Left-recursion Both grammars recognize the same string, but G ' is not left-recursive. However, G is clear and intuitive. General rule for removing immediately left-recursion: Replace A A 1 | | A m | 1 | | n With A 1 A' | | n A' A' 1 A' | | m A' | This rule does not work if i = for some i. This is called a direct cycle in a grammar. May need to worry about whether the semantics are equivalent between the original grammar and the transformed grammar. NEWS 01:54 /5226 Algorithm 4.19 Algorithm 4.19 systematically eliminates left recursion and works only if the input grammar has no cycles or -productions. Cycle: A A -production: A Can remove cycles and all but one -production using other algorithms. Input: grammar G without cycles and -productions. Output: An equivalent grammar without left recursion. Number the nonterminals in some order A 1, A 2,..., A n for i = 1 to n do for j = 1 to i 1 do replace A i A j with A i 1 | | k where A j 1 | | k are all the current A j -productions Eliminate immediate left-recursion for A i New nonterminals generated above are numbered A i+n NEWS 01:54 /5227 Algorithm 4.19 Discussions Intuition: Consider only the productions where the leftmost item on the right hand side are nonterminals. If it is always the case that A i A j implies i < j, then it is not possible to have left-recursion. Why cycles are not allowed? For the procedure of removing immediate left-recursion. Why -productions are not allowed? Inside the loop, when A j , that is some g = , and the prefix of is some A k where k < i, it generates A i A k, k < i. Time and space complexities: Size of the resulting grammar can be O(w 3 ), where w is the original size. O(n 2 w 3 ) time, where n is the number of nonterminals in the input grammar. NEWS 01:54 /5228 Trace an instance of Algorithm 4.19 After each i -loop, only productions of the form A i A k , i < k remain. i = 1 allow A 1 A k , k before removing immediate left-recursion remove immediate left-recursion for A 1 i = 2 j = 1 : replace A 2 A 1 by A 2 ( A k 1 1 | | A k p p ) , where A 1 ( A k 1 1 | | A k p p ) and k j > 1, k j remove immediate left-recursion for A 2 i = 3 j = 1 : replace A 3 A 1 1 j = 2 : replace A 3 A 2 2 remove immediate left-recursion for A 3 NEWS 01:54 /5229 Example Original Grammar: (1) S Aa | b (2) A Ac | Sd | e Ordering of nonterminals: S A 1 and A A 2. i = 1 do nothing as there is no immediate left-recursion for S i = 2 replace A Sd by A Aad | bd hence (2) becomes A Ac | Aad | bd | e after removing immediate left-recursion: A bdA' | eA' A' cA' | adA' | Resulting grammar: S Aa | b A bdA' | eA' A' cA' | adA' | NEWS 01:54 /5230 Left-factoring and Left-recursion Removal Original grammar: S (S) | SS | () To remove immediate left-recursion, we have S (S)S' | ()S' S' SS' | To do left-factoring, we have S (S'' S'' S)S' | )S' S' SS' | NEWS 01:54 /5231 Top-down Parsing There are O ( n 3 )-time algorithms to parse a language defined by CFG, where n is the number of input tokens. For practical purpose, we need faster algorithms. Here we make restrictions to CFG so that we can design O ( n )-time algorithms. Recursive-descent parsing Recursive-descent parsing : top-down parsing that allows backtracking. Attempt to find a leftmost derivation for an input string. Try out all possibilities, that is, do an exhaustive search to find a parse tree that parses the input. NEWS 01:54 /5232 Example for Recursive-descent Parsing Input: cad Problems with the above approach: still too slow! want to select a derivation without ever causing backtracking! Predictive parser : a recursive-descent parser needing no backtracking. S cAd A bc | a S c A d S c A d b c error!! backtrack S c A d a NEWS 01:54 /5233 Predictive Parser (1/2) Find a rich class of grammars that can be parsed using predictive parsers. The class of LL(1) grammars [Lewis & Stearns 1968] can be parsed by a predictive parser in O(n) time. first L : scan the input from left-to-right second L : find a leftmost derivation (1): allow one lookahead token! Based on the current lookahead symbol, pick a derivation when there are multiple choices. Using a STACK during implementation to avoid recursion. Build a PARSING TABLE T, using the symbol X on the top of STACK and the lookahead symbol s as indexes, to decide the production to be used. If X is a terminal, then X = s. Input s is matched. If X is a nonterminal, then T(X, s) tells you the production to be used in the next derivation. NEWS 01:54 /5234 Predictive Parser (2/2) How a predictive parser works: start by pushing the starting nonterminal into the STACK and calling the scanner to get the first token. LOOP: if top-of-STACK is a nonterminal, then use the current token and the PARSING TABLE to choose a production pop the nonterminal from the STACK push the above productions right-hand-side to the STACK from right to left GOTO LOOP. if top-of-STACK is a terminal and matches the current token, then pop STACK and ask scanner to provide the next token GOTO LOOP. if STACK is empty and there is no more input, then ACCEPT! If none of the above succeed, then FAIL! STACK is empty and there is input left. top-of-STACK is a terminal, but does not match the current token top-of-STACK is a nonterminal, but the corresponding PARSING TABLE entry is ERROR! NEWS 01:54 /5235 Example for Parsing an LL(1) Grammar grammar: S a | ( S ) | [ S ]input: ([ a ]) Use the current input token to decide which production to derive from the top-of-STACK nonterminal. STACKINPUTACTION S )S( )S )]S[ )]S )]a )] ) ([a]) [a]) a]) ]) ) pop, push (S) pop, match with input pop, push [S] pop, match with input pop, push a pop, match with input accept S ( ) S S [] a leftmost derivation NEWS 01:54 /5236 About LL(1) (1/2) It is not always possible to build a predictive parser given a CFG; It works only if the CFG is LL(1) ! LL(1) is a subset of CFG. For example, the following grammar is not LL(1), but is LL(2). Grammar: S (S) | [S] | () | [ ] Try to parse the input (). In this example, we need 2-token look-ahead. If the next token is ), push () from right to left. If the next token is (, push ( S ) from right to left. STACKINPUTACTION S() pop, but use which production? NEWS 01:54 /5237 About LL(1) (2/2) A grammar is not LL (1) if it is left-recursive or has left-factors. However, grammars that are not left-recursive and have no left-factors may still not be LL (1). Q: Any examples? Two questions: How to tell whether a grammar G is LL(1) ? How to build the PARSING TABLE? NEWS 01:54 /5238 Definition of LL(1) Grammars To see if a grammar is LL (1), we need to compute its FIRST and FOLLOW sets, which are used to build its parsing table. FIRST sets: Definition: let be a sequence of terminals and/or nonterminals or FIRST() is the set of terminals that begin the strings derivable from if can derive , then FIRST() FIRST() = { t | (t is a terminal and t) or ( t = and ) } NEWS 01:54 /5239 How to compute FIRST(X)? (1/2) X is a terminal: FIRST(X) = {X} X is : FIRST(X) = {} X is a nonterminal: must check all productions with X on theleft-hand side. That is, for all X Y 1 Y 2 Y k perform the following steps: put FIRST(Y 1 ) { } into FIRST(X) if FIRST(Y 1 ), then put FIRST(Y 2 ) {} into FIRST(X) else continue ; if FIRST(Y k-1 ), then put FIRST(Y k ) {} into FIRST(X) else continue ; if FIRST(Y i ) for each 1 i k, then put into FIRST(X) NEWS 01:54 /5240 How to compute FIRST(X)? (2/2) Algorithm to compute FIRSTs for all non-terminals. compute FIRSTs for and all terminals; initialize FIRSTs for all non-terminals to ; Repeat for all nonterminals X do apply the steps to compute FIRST( X ) Until no items can be added to any FIRST set; What to do when recursive calls are encountered? direct recursive calls indirect recursive calls actions: do not go further Why? The time complexity of this algorithm. at least one item, terminal or , is added to some FIRST set in an iteration; maximum number of items in all FIRST sets are (|T| + 1) |N|, where T is the set of terminals and N is the set of nonterminals. Each iteration takes O(|N| + |T|) time. O(|N| |T|(|N| + |T|)). NEWS 01:54 /5241 Example for Computing FIRST(X) Start with computing FIRST for the last production and walk your way up. Grammar E E'T E' TE' | T FT' T' / FT' | F int | (E) H E' T FIRST(F) = {int, (} FIRST(T' ) = {/, } FIRST(T) = {int, (}, since FIRST(F), thats all. FIRST(E' ) = {, } FIRST(H) = {, int, (} FIRST(E) = {, int, (}, since FIRST(E' ). NEWS 01:54 /5242 How to compute FIRST()? To build a parsing table, we need FIRST () for all such that X is a production in the grammar. Need to compute FIRST( X ) for each nonterminal X. Let = X 1 X 2 X n. Perform the following steps in sequence: put FIRST( X 1 ) {} into FIRST( ) if FIRST( X 1 ), then put FIRST( X 2 ) {} into FIRST( ) else continue ; if FIRST(X n1 ), then put FIRST( X n ) {} into FIRST( ) else continue ; if FIRST(X i ) for each 1 i n, then put {} into FIRST( ) What to do when recursive calls are encountered? What are the time and space complexities? NEWS 01:54 /5243 Example for Computing FIRST() FIRST( T ' E ') = (FIRST( T ' ) {}) (FIRST( E ' ) {}) {} Grammar E E'T E' TE'| T FT' T' / FT' | F int | (E) FIRST(F) = {int, (} FIRST(T') = {/,} FIRST(T) = {int, (} FIRST(E') = {,} FIRST(E) = {, int, (} FIRST(E'T) = {, int, (} FIRST(TE') = {} FIRST() = {} FIRST(FT') = {int, (} FIRST(/FT') = {/} FIRST() = {} FIRST(int) = {int} FIRST((E)) = {(} NEWS 01:54 /5244 Why do we need FIRST()? During parsing, suppose top-of-STACK is a nonterminal A and there are several choices A 1 A 2 A k for derivation, and the current lookahead token is a If a FIRST( i ), then pick A i for derivation, pop, and then push i. If a is in several FIRST( i )s, then the grammar is not LL (1). Question: if a is not in any FIRST( i ), does this mean the input stream cannot be accepted? Maybe not! What happen if is in some FIRST( i )? NEWS 01:54 /5245 FOLLOW sets Assume there is a special EOF symbol $ ends every input. Add a new terminal $ . Definition: for a nonterminal X, FOLLOW( X ) is the set of terminals that can appear immediately to the right of X in some partial derivation. That is, S 1 Xt 2, where t is a terminal. If X can be the rightmost symbol in a derivation, then $ is in FOLLOW( X ). FOLLOW( X ) = {t | (t is a terminal and S 1 Xt 2 ) or ( t is $ and S X)}. NEWS 01:54 /5246 How to compute FOLLOW(X)? If X is the starting nonterminal, put $ into FOLLOW( X ). Find the productions with X on the right-hand-side. for each production of the form Y X, put FIRST( ) { } into FOLLOW( X ). if FIRST( ), then put FOLLOW( Y ) into FOLLOW( X ). for each production of the form Y X, put FOLLOW( Y ) into FOLLOW( X ). Repeat the above process for all nonterminals until nothing can be added to any FOLLOW set. What to do when recursive calls are encountered? Q: time and space complexities To see if a given grammar is LL(1), or to build its parsing table: compute FIRST( ) for every such that X is a production compute FOLLOW( X ) for all nonterminals X need to compute FIRST( ) for every such that Y X is a production NEWS 01:54 /5247 A Complete Example Grammar S Bc | DB B ab | cS D d | FIRST()FOLLOW() D B S Bc DB ab cS d {d,} {a, c} {a, c, d} {a, c} {d, a, c} {a} {c} {d} {} {a, c} {c, $} NEWS 01:54 /5248 Why do we need FOLLOW sets? Note FOLLOW( S ) always includes $. Situation: During parsing, the top-of-STACK is a nonterminal X and the lookahead symbol is a. Assume there are several choices for the nest derivation: X 1 X k If a FIRST( i ) for exactly one i, then we use that derivation. If a FIRST( i ), a FIRST( j ), and i j, then this grammar is not LL (1). If a FIRST( i ), for all i, then this grammar can still be LL(1) ! If there exists some i such that i and a FOLLOW( X ), then we can use the derivation X i. i if and only if FIRST( i ). NEWS 01:54 /5249 Grammars that are not LL(1) A grammar is not LL (1) if there exists productions X | and any one of the followings is true: FIRST( ) FIRST( ) It may be the case that FIRST( ) and FIRST( ) FIRST( ), and FIRST( ) FOLLOW( X) If a grammar is not LL (1), then you cannot write a linear-time predictive parser as described above. If a grammar is not LL (1), then we do not know to use the production X or the production X when the lookahead symbol is a in any of the following cases a FIRST( ) FIRST( ) ; FIRST( ) and FIRST( ) ; FIRST( ), and a FIRST( ) FOLLOW( X). Note that FIRST and FOLLOW sets are always sets of terminals, plus, perhaps, for some FIRST sets. NEWS 01:54 /5250 A Complete Example (1/2) Grammar: ProgHead prog id Parameter semicolon Parameter | id | l_paren Parameter r_paren FIRST and FOLLOW sets: FIRST()FOLLOW(X) ProgHead Parameter prog id Parameter semicolon l_paren Parameter r_paren {prog} {, id, l_paren} {prog} {l_paren} {$} {semicolon, r_paren} NEWS 01:54 /5251 A Complete Example (2/2) Input: prog id semicolon Last actions: Three choices: Parameter | id | l_paren Parameter r_paren semicolon FIRST() and semicolon FIRST( id ) and semicolon FIRST( l_paren Parameter r_paren ) Parameter and semicolon FOLLOW(Parameter) Hence we use the derivation Parameter STACKINPUTACTION $ ProgHead $ semicolon Parameter id prog $ semicolon Parameter id $ semicolon Parameter prog id semicolon $ id semicolon $ semicolon $ pop, push match with input WHAT TO DO? NEWS 01:54 /5252 LL(1) parsing table (1/2) Check for possible conflicts in X a |. FIRST( a) FIRST( ) = FIRST( ) and FOLLOW( X ) FIRST( a) = { a } Conflict!! FIRST( a) Check for possible conflicts in C a |. FIRST( a) FIRST( ) = FIRST( ) and FOLLOW( C ) FIRST( a) = FIRST( a) Grammar S XC X a| C a| FIRST()FOLLOW(X) S X C A XC {a, } {} {a} {a, } {$} {a, $} {$} NEWS 01:54 /5253 LL(1) Parsing Table (2/2) Parsing table: a$ SXCSXC S XC conflict C a S XC X C