180 likes | 189 Views
This document contains exercises on scanning and top-down parsing principles from the Winter 2012-2013 compiler course. It includes regular expressions, parsing questions, and LL(1) parser construction.
E N D
Winter 2012-2013Compiler PrinciplesExercises on scanning& top-down parsing Mayer Goldberg and Roman Manevich Ben-Gurion University
Scanning question 1 • Write a regular expression for positive integers where every three consecutive digits, starting from the right, are separated by a _ • Examples: 23 1_000 2_784_153 • Negative examples: 1_0 1_5422
Scanning question 1 • Write a regular expression for positive integers where every three consecutive digits, starting from the right, are separated by a _ • Examples: 23 1_000 2_784_153 • Negative examples: 1_0 1_5422 • Answer:D = 0 | 1 | 2 … | 9Small = D | DD | DDDThree = DDDR = Small (_Three)*
Parsing question 1 • Consider the grammar belowS ( L ) | a L L , S | S • show a parse tree for each of the following • (a, a) • (a, (a, a))
Answer S S L L L S S S ( a , ,a ) ( a ,
Parsing question 2 • Consider the grammar GL L , a | a • What is the language generated by G? • Eliminate the left-recursion in G • Write a non-left-recursive version without ε productions • Construct an LL(1) parser and run it on a, a, a
Parsing question 2 • Consider the grammar GL L , a | a • What is the language generated by G? • Eliminate the left-recursion in G • Write a non-left-recursive version with ε productions • Is the resulting grammar LL(1)? • Answer • The language given by the regular expression (a ,)* a • L a M M , a M | ε • L a | a M M , a M | , a • The grammar contains FIRST-FIRST conflicts for both L and M and therefore it is not LL(1)
Parsing question 3 Consider the grammar G1 belowX P PP aP b Q: Is it ambiguous?
Parsing question 3 Consider the grammar G1 belowX P PP aP b Q: Is it ambiguous? A: since the only non-terminal with alternatives is P and the first of its two alternatives ({a} and {b}) the grammar is LL(1). All LL(1) grammars are unambiguous
Parsing question 4 • Consider the following grammarE a ( S ) w tE a ( S ) w bS S ; c | c • Construct an LL(1) parser, applying any transformations necessary if the grammar is not in LL(1) • Hint: move the ) down to help transformations • Apply the parser to the inputa (c; c) w b
Answer The grammarE a ( S ) w tE a ( S ) w bS S ; c | cwill have a FIRST-FIRST conflict for E, since the first two rules start with a. We therefore apply left-factoring and obtain E a ( S ) E’E’ w b | w tS S ; c | c
Answer The grammarE a ( S ) E’E’ w b | w tS S ; c | cIs left-recursive on S so we eliminate the left-recursion and obtain the grammar E a ( S ) E’E’ w b | w tS c | c ; S
Answer Let’s compute FIRST sets forE a ( S ) E’E’ w b | w tS c | c ; S FIRST(S c) = FIRST(c ; S) = {c}FIRST(w b)=FIRST(w t) = {w}so there are FIRST-FIRST conflict. We apply left-factoring to both E’ and S and obtainE a ( S ) E’E’ w E’’ E’’ b | tS c S’ S’ ε | ; S Now if we apply substitution for S’ we will just get back to a left-recursive grammar.Time to use the hint
Answer Starting fromE a ( S ) E’E’ w E’’ E’’ b | t S c | c ; SLet’s move ) from E to S E a ( S E’E’ w E’’ E’’ b | t S c ) | c ; S Now let’s apply left-factoring to S and obtain:E a ( S E’E’ w E’’ E’’ b | t S c S’S’ ) | ; S
Answer Let’s compute FIRST sets for (1) E a (SE’(2) E’ w E’’ (3) E’’ b(4) E’’ t (5) S c S’(6) S’ )(7) S’ ; S FIRST(E) = {a}FIRST(E’) = {w}FIRST(E’’) = {b, t}FIRST(S) = {c}FIRST(S’) = {), ;} There are no conflicts so the grammar is in LL(1)
Running on a ( c; c )w b Since the input has been read and the stack is empty – the parsing was successful and the input is accepted