Lexical and Syntax Analysis


 Lorraine Little
 3 years ago
 Views:
Transcription
1 Lexical and Syntax Analysis (of Programming Languages) TopDown Parsing
2 Lexical and Syntax Analysis (of Programming Languages) TopDown Parsing
3 Easy for humans to write and understand String of characters Lexemes identified String of tokens Easy for programs to transform Data structure
4 Easy for humans to write and understand String of characters Lexemes identified String of tokens Easy for programs to transform Data structure
5 PART 1: SYNTAX OF LANGUAGES ContextFree Grammars Derivations Parse Trees Ambiguity Precedence and Associativity
6 PART 1: SYNTAX OF LANGUAGES ContextFree Grammars Derivations Parse Trees Ambiguity Precedence and Associativity
7 Syntax The syntax is a set of rules defining valid strings of a language, often specified by a contextfree grammar. For example, a grammar E for arithmetic expressions: e x y e + e e e e * e ( e )
8 Syntax The syntax is a set of rules defining valid strings of a language, often specified by a contextfree grammar. For example, a grammar E for arithmetic expressions: e x y e + e e e e * e ( e )
9 Contextfree grammars Have four components: 1. A set of terminal symbols. 2. A set of nonterminal symbols. 3. A set of productions (or rules) of the form: n X 1 X n where n is a nonterminal and X 1 X n is any sequence of terminals, nonterminals, and ε. 4. The start symbol (one of the nonterminals).
10 Contextfree grammars Have four components: 1. A set of terminal symbols. 2. A set of nonterminal symbols. 3. A set of productions (or rules) of the form: n X 1 X n where n is a nonterminal and X 1 X n is any sequence of terminals, nonterminals, and ε. 4. The start symbol (one of the nonterminals).
11 Notation Nonterminals are underlined. Rather than writing e x e e + e we may write: e x e + e (Also, symbols and ::= will be used interchangeably.)
12 Notation Nonterminals are underlined. Rather than writing e x e e + e we may write: e x e + e (Also, symbols and ::= will be used interchangeably.)
13 Why contextfree? Unrestricted Context Sensitive Context Free Regular Nice balance between expressive power and efficiency of parsing.
14 Why contextfree? Unrestricted Context Sensitive Context Free Regular Nice balance between expressive power and efficiency of parsing.
15 Derivations A derivation is a proof that the some string conforms to a grammar. For example: e e + e x + e x + ( e ) x + ( e * e ) x + ( y * e ) x + ( y * x )
16 Derivations A derivation is a proof that the some string conforms to a grammar. For example: e e + e x + e x + ( e ) x + ( e * e ) x + ( y * e ) x + ( y * x )
17 Derivations Leftmost derivation: always expand the leftmost nonterminal when applying the grammar rules. Rightmost derivation: always expand the rightmost nonterminal, e.g. e e + e e + ( e ) e + ( x ) x + ( x )
18 Derivations Leftmost derivation: always expand the leftmost nonterminal when applying the grammar rules. Rightmost derivation: always expand the rightmost nonterminal, e.g. e e + e e + ( e ) e + ( x ) x + ( x )
19 Parse tree: motivation Like a derivation: a proof that a given input is valid according to the grammar. But a parse tree: is more concise: we don t write out the sentence every time a nonterminal is expanded. abstracts over the order in which rules are applied.
20 Parse tree: motivation Like a derivation: a proof that a given input is valid according to the grammar. But a parse tree: is more concise: we don t write out the sentence every time a nonterminal is expanded. abstracts over the order in which rules are applied.
21 Parse tree: intuition If nonterminal n has a production n X Y Z where X, Y, and Z are terminals or nonterminals, then a parse tree may have an interior node labelled n with three children labelled X, Y, and Z. n X Y Z
22 Parse tree: intuition If nonterminal n has a production n X Y Z where X, Y, and Z are terminals or nonterminals, then a parse tree may have an interior node labelled n with three children labelled X, Y, and Z. n X Y Z
23 Parse tree: definition A parse tree is a tree in which: the root is labelled by the start symbol; each leaf is labelled by a terminal symbol, or ε; each interior node is labelled by a nonterminal; if n is a nonterminal labelling an interior node whose children are X 1, X 2,, X n then there must exist a production n X 1 X 2 X n.
24 Parse tree: definition A parse tree is a tree in which: the root is labelled by the start symbol; each leaf is labelled by a terminal symbol, or ε; each interior node is labelled by a nonterminal; if n is a nonterminal labelling an interior node whose children are X 1, X 2,, X n then there must exist a production n X 1 X 2 X n.
25 Example 1 Example input string: x + y * x Resulting parse tree according to grammar E: e e + e x e * e y x
26 Example 1 Example input string: x + y * x Resulting parse tree according to grammar E: e e + e x e * e y x
27 Example 2 The following is not a parse tree according to grammar E. e x + e e y * e x Why? Because e x + e is not a production in grammar E.
28 Example 2 The following is not a parse tree according to grammar E. e x + e e y * e x Why? Because e x + e is not a production in grammar E.
29 Syntax Analysis String of symbols Parse tree A parse tree is: 1. A proof that a given input is valid according to the grammar; 2. A structurerich representation of the input that can be stored in a data structure that is convenient to process. (Syntax analysis may also report that the input string is invalid.)
30 Syntax Analysis String of symbols Parse tree A parse tree is: 1. A proof that a given input is valid according to the grammar; 2. A structurerich representation of the input that can be stored in a data structure that is convenient to process. (Syntax analysis may also report that the input string is invalid.)
31 Ambiguity If there exists more than one parse tree for any string then the grammar is ambiguous. For example, the string x+y*x has two parse trees: e e e + e e * e x e * e e + e x y x x y
32 Ambiguity If there exists more than one parse tree for any string then the grammar is ambiguous. For example, the string x+y*x has two parse trees: e e e + e e * e x e * e e + e x y x x y
33 Operator precedence Different parse trees often have different meanings, so we usually want unambiguous grammars. Conventionally, * has a higher precedence (binds tighter) than +, so there is only one interpretation of x+y*x, namely x+(y*x).
34 Operator precedence Different parse trees often have different meanings, so we usually want unambiguous grammars. Conventionally, * has a higher precedence (binds tighter) than +, so there is only one interpretation of x+y*x, namely x+(y*x).
35 Operator associativity Even with operator precedence rules, ambiguity remains, e.g. xxx. Binary operators are either: leftassociative; rightassociative; nonassociative. Conventionally,  is leftassociative, so there is only one interpretation of xxx, namely (xx)x.
36 Operator associativity Even with operator precedence rules, ambiguity remains, e.g. xxx. Binary operators are either: leftassociative; rightassociative; nonassociative. Conventionally,  is leftassociative, so there is only one interpretation of xxx, namely (xx)x.
37 Exercise 1 Recall grammar E: e x y e + e e e e * e ( e ) Let all operators be left associative, and let * bind tighter than + and. Give an unambiguous grammar for expressions, using these rules of associativity and precedence.
38 Exercise 1 Recall grammar E: e x y e + e e e e * e ( e ) Let all operators be left associative, and let * bind tighter than + and. Give an unambiguous grammar for expressions, using these rules of associativity and precedence.
39 Answer: stepbystep Given a nonterminal e which involves operators at n levels of precedence: Step 1: introduce n+1 new nonterminals, e 0 e n.
40 Answer: stepbystep Given a nonterminal e which involves operators at n levels of precedence: Step 1: introduce n+1 new nonterminals, e 0 e n.
41 Let op denote an operator with precedence i. Step 2: replace each production with e e op e e i e i op e i+1 e i+1 if op is leftassociative, or e i e i+1 op e i e i+1 if op is rightassociative
42 Let op denote an operator with precedence i. Step 2: replace each production with e e op e e i e i op e i+1 e i+1 if op is leftassociative, or e i e i+1 op e i e i+1 if op is rightassociative
43 Construct the precedence table: Operator Precedence +,  0 * 1 Grammar E after step 2 becomes: e 0 e 0 + e 1 e 0 e 1 e 1 e 1 e 1 * e 2 e 2 e ( e ) x y
44 Construct the precedence table: Operator Precedence +,  0 * 1 Grammar E after step 2 becomes: e 0 e 0 + e 1 e 0 e 1 e 1 e 1 e 1 * e 2 e 2 e ( e ) x y
45 Step 3: replace each production with e e n After step 3: e 0 e 0 + e 1 e 0 e 1 e 1 e 1 e 1 * e 2 e 2 e 2 ( e ) x y
46 Step 3: replace each production with e e n After step 3: e 0 e 0 + e 1 e 0 e 1 e 1 e 1 e 1 * e 2 e 2 e 2 ( e ) x y
47 Step 4: replace all occurrences of e 0 with e. After step 4: e e + e 1 e e 1 e 1 e 1 e 1 * e 2 e 2 e 2 ( e ) x y
48 Step 4: replace all occurrences of e 0 with e. After step 4: e e + e 1 e e 1 e 1 e 1 e 1 * e 2 e 2 e 2 ( e ) x y
49 Exercise 2 Consider the following ambiguous grammar for logical propositions. p 0 (Zero) 1 (One) ~ p (Negation) p + p (Disjunction) p * p (Conjunction) Now let + and * be right associative and the operators in increasing order of binding strength be : +, *, ~. Give an unambiguous grammar for logical propositions.
50 Exercise 2 Consider the following ambiguous grammar for logical propositions. p 0 (Zero) 1 (One) ~ p (Negation) p + p (Disjunction) p * p (Conjunction) Now let + and * be right associative and the operators in increasing order of binding strength be : +, *, ~. Give an unambiguous grammar for logical propositions.
51 Exercise 3 Which of the following grammars are ambiguous? b 0 b e + e e e e x s if b then s if b then s else s skip
52 Exercise 3 Which of the following grammars are ambiguous? b 0 b e + e e e e x s if b then s if b then s else s skip
53 Summary of Part 1 Syntax of a language is often specified by a contextfree grammar Derivations and parse trees are proofs that a string is accepted by a grammar. Construction of unambiguous grammars using rules of precedence and associativity.
54 Summary of Part 1 Syntax of a language is often specified by a contextfree grammar Derivations and parse trees are proofs that a string is accepted by a grammar. Construction of unambiguous grammars using rules of precedence and associativity.
55 PART 2: TOPDOWN PARSING RecursiveDescent Backtracking LeftFactoring Predictive Parsing LeftRecursion Removal First and Follow Sets Parsing tables and LL(1)
56 PART 2: TOPDOWN PARSING RecursiveDescent Backtracking LeftFactoring Predictive Parsing LeftRecursion Removal First and Follow Sets Parsing tables and LL(1)
57 Topdown parsing Topdown: begin with the start symbol and expand nonterminals, succeeding when the input string is matched. A good strategy for writing parsers: 1. Implement a syntax checker to accept or refute input strings. 2. Modify the checker to construct a parse tree straightforward.
58 Topdown parsing Topdown: begin with the start symbol and expand nonterminals, succeeding when the input string is matched. A good strategy for writing parsers: 1. Implement a syntax checker to accept or refute input strings. 2. Modify the checker to construct a parse tree straightforward.
59 RECURSIVE DESCENT A popular topdown parsing technique.
60 RECURSIVE DESCENT A popular topdown parsing technique.
61 Recursive descent A recursive descent parser consists of a set of functions, one for each nonterminal. The function for nonterminal n returns true if some prefix of the input string can be derived from n, and false otherwise.
62 Recursive descent A recursive descent parser consists of a set of functions, one for each nonterminal. The function for nonterminal n returns true if some prefix of the input string can be derived from n, and false otherwise.
63 Consuming the input We assume a global variable next points to the input string. char* next; Consume c from input if possible. int eat(char c) { if (*next == c) { next++; return 1; } return 0; }
64 Consuming the input We assume a global variable next points to the input string. char* next; Consume c from input if possible. int eat(char c) { if (*next == c) { next++; return 1; } return 0; }
65 Recursive descent Let parser(x) denote X() if X is a nonterminal eat(x) if X is a terminal For each nonterminal N, introduce: int N() { char* save = next; } for each N X 1 X 2 X n if (parser(x 1 ) && parser(x 2 ) && && parser(x n )) return 1; else next = save; return 0; Backtrack
66 Recursive descent Let parser(x) denote X() if X is a nonterminal eat(x) if X is a terminal For each nonterminal N, introduce: int N() { char* save = next; } for each N X 1 X 2 X n if (parser(x 1 ) && parser(x 2 ) && && parser(x n )) return 1; else next = save; return 0; Backtrack
67 Exercise 4 Consider the following grammar G with start symbol e. e ( e + e ) ( e * e ) v v x y Using recursive descent, write a syntax checker for grammar G.
68 Exercise 4 Consider the following grammar G with start symbol e. e ( e + e ) ( e * e ) v v x y Using recursive descent, write a syntax checker for grammar G.
69 Answer (part 1) int e() { char* save = next; if (eat('(') && e() && eat('+') && e() && eat(')')) return 1; else next = save; if (eat('(') && e() && eat('*') && e() && eat(')')) return 1; else next = save; if (v()) return 1; else next = save; return 0; }
70 Answer (part 1) int e() { char* save = next; if (eat('(') && e() && eat('+') && e() && eat(')')) return 1; else next = save; if (eat('(') && e() && eat('*') && e() && eat(')')) return 1; else next = save; if (v()) return 1; else next = save; return 0; }
71 Answer (part 2) int v() { char* save = next; if (eat('x')) return 1; else next = save; if (eat('y')) return 1; else next = save; return 0; }
72 Answer (part 2) int v() { char* save = next; if (eat('x')) return 1; else next = save; if (eat('y')) return 1; else next = save; return 0; }
73 Exercise 5 How many function calls are made by the recursive descent parser to parse the following strings? (x*x) ((x*x)*x) (((x*x)*x)*x) (See animation of backtracking.)
74 Exercise 5 How many function calls are made by the recursive descent parser to parse the following strings? (x*x) ((x*x)*x) (((x*x)*x)*x) (See animation of backtracking.)
75 Answer Number of calls is quadratic in the length of the input string. Input string Length Calls (x*x) 5 21 ((x*x)*x) 9 53 (((x*x)*x)*x) Lesson: backtracking expensive!
76 Answer Number of calls is quadratic in the length of the input string. Input string Length Calls (x*x) 5 21 ((x*x)*x) 9 53 (((x*x)*x)*x) Lesson: backtracking expensive!
77 LEFT FACTORING Reducing backtracking!
78 LEFT FACTORING Reducing backtracking!
79 Left factoring When two productions for a nonterminal share a common prefix, expensive backtracking can be avoided by leftfactoring the grammar. Idea: Introduce a new nonterminal that accepts each of the different suffixes.
80 Left factoring When two productions for a nonterminal share a common prefix, expensive backtracking can be avoided by leftfactoring the grammar. Idea: Introduce a new nonterminal that accepts each of the different suffixes.
81 Example 3 Leftfactoring grammar G by introducing nonterminal r: e ( e r v r + e ) * e ) v x y Common prefix Different suffixes
82 Example 3 Leftfactoring grammar G by introducing nonterminal r: e ( e r v r + e ) * e ) v x y Common prefix Different suffixes
83 Exercise 6 How many function calls are made by the recursive descent parser (after leftfactoring) to parse the following strings? (x*x) ((x*x)*x) (((x*x)*x)*x)
84 Exercise 6 How many function calls are made by the recursive descent parser (after leftfactoring) to parse the following strings? (x*x) ((x*x)*x) (((x*x)*x)*x)
85 Answer Number of calls is now linear in the length of input string. Input string Length Calls (x*x) 5 13 ((x*x)*x) 9 22 (((x*x)*x)*x) Lesson: leftfactoring a grammar reduces backtracking.
86 Answer Number of calls is now linear in the length of input string. Input string Length Calls (x*x) 5 13 ((x*x)*x) 9 22 (((x*x)*x)*x) Lesson: leftfactoring a grammar reduces backtracking.
87 PREDICTIVE PARSING Eliminating backtracking!
88 PREDICTIVE PARSING Eliminating backtracking!
89 Predictive parsing Idea: know which production of a nonterminal to choose based solely on the next input symbol. Advantage: very efficient since it eliminates all backtracking. Disadvantage: not all grammars can be parsed in this way. (But many useful ones can.)
90 Predictive parsing Idea: know which production of a nonterminal to choose based solely on the next input symbol. Advantage: very efficient since it eliminates all backtracking. Disadvantage: not all grammars can be parsed in this way. (But many useful ones can.)
91 Running example The following grammar H will be used as a running example to demonstrate predictive parsing. e e + e e * e ( e ) x y Example: x+y*(y+x)
92 Running example The following grammar H will be used as a running example to demonstrate predictive parsing. e e + e e * e ( e ) x y Example: x+y*(y+x)
93 Removing ambiguity Since + and * are leftassociative and * binds tighter than +, we can derive an unambiguous variant of H. e e + t t t t * f f f ( e ) x y
94 Removing ambiguity Since + and * are leftassociative and * binds tighter than +, we can derive an unambiguous variant of H. e e + t t t t * f f f ( e ) x y
95 Left recursion Problem: leftrecursive grammars cause recursive descent parsers to loop forever. int e() { char* save = next; if (e() && eat('+') && t()) return 1; next = save; if (t()) return 1; next = save; Call to self without consuming any input } return 0;
96 Left recursion Problem: leftrecursive grammars cause recursive descent parsers to loop forever. int e() { char* save = next; if (e() && eat('+') && t()) return 1; next = save; if (t()) return 1; next = save; Call to self without consuming any input } return 0;
97 Eliminating left recursion Let α denote any sequence of grammar symbols. n n α Rule 1 n' α n' n α Rule 2 n α n' where α does not begin with n Introduce new production Rule 3 n' ε
98 Eliminating left recursion Let α denote any sequence of grammar symbols. n n α Rule 1 n' α n' n α Rule 2 n α n' where α does not begin with n Introduce new production Rule 3 n' ε
99 Example 4 Running example, after eliminating leftrecursion. e t e' e' + t e' ε t f t' t' * f t' ε f ( e ) x y
100 Example 4 Running example, after eliminating leftrecursion. e t e' e' + t e' ε t f t' t' * f t' ε f ( e ) x y
101 first and follow sets Predictive parsers are built using the first and follow sets of each nonterminal in a grammar. The first set of a nonterminal n is the set of symbols that can begin a string derived from n. The follow set of a nonterminal n is the set of symbols that can immediately follow n in any step of a derivation.
102 first and follow sets Predictive parsers are built using the first and follow sets of each nonterminal in a grammar. The first set of a nonterminal n is the set of symbols that can begin a string derived from n. The follow set of a nonterminal n is the set of symbols that can immediately follow n in any step of a derivation.
103 Definition of first sets Let α denote any sequence of grammar symbols. If α can derive a string beginning with terminal a then a first(α). If α can derive ε then ε first(α).
104 Definition of first sets Let α denote any sequence of grammar symbols. If α can derive a string beginning with terminal a then a first(α). If α can derive ε then ε first(α).
105 Computing first sets If a is a terminal then a first(a). If there exists a production n X 1 X 2 X n and i a first(x i ) and j < i ε first(x j ) then a first(n). If n ε then ε first(n).
106 Computing first sets If a is a terminal then a first(a). If there exists a production n X 1 X 2 X n and i a first(x i ) and j < i ε first(x j ) then a first(n). If n ε then ε first(n).
107 Exercise 7 What are the first sets for each nonterminal in the following grammar. e t e' e' + t e' ε t f t' t' * f t' ε f ( e ) x y
108 Exercise 7 What are the first sets for each nonterminal in the following grammar. e t e' e' + t e' ε t f t' t' * f t' ε f ( e ) x y
109 Answer first( f ) = { (, x, y } first( t' ) = { *, ε } first( t ) = { (, x, y } first( e' ) = { +, ε } first( e ) = { (, x, y }
110 Answer first( f ) = { (, x, y } first( t' ) = { *, ε } first( t ) = { (, x, y } first( e' ) = { +, ε } first( e ) = { (, x, y }
111 Definition of follow sets Let α and β denote any sequence of grammar symbols. Terminal a follow(n) if the start symbol of the grammar can derive a string of grammar symbols in which a immediately follows n. The set follow(n) never contains ε.
112 Definition of follow sets Let α and β denote any sequence of grammar symbols. Terminal a follow(n) if the start symbol of the grammar can derive a string of grammar symbols in which a immediately follows n. The set follow(n) never contains ε.
113 End markers In predictive parsing, it is useful to mark the end of the input string with a $ symbol. If the start symbol can derive a string of grammar symbols in which n is the rightmost symbol then $ is in follow(n).
114 End markers In predictive parsing, it is useful to mark the end of the input string with a $ symbol. If the start symbol can derive a string of grammar symbols in which n is the rightmost symbol then $ is in follow(n).
115 Computing follow sets If s is the start symbol of the grammar then $ follow(s). If n α x β then everything in first(β) except ε is in follow(x). If n α x or n α x β and ε first(β) then everything in follow(n) is in follow(x).
116 Computing follow sets If s is the start symbol of the grammar then $ follow(s). If n α x β then everything in first(β) except ε is in follow(x). If n α x or n α x β and ε first(β) then everything in follow(n) is in follow(x).
117 Exercise 8 What are the follow sets for each nonterminal in the following grammar. e t e' e' + t e' ε t f t' t' * f t' ε f ( e ) x y
118 Exercise 8 What are the follow sets for each nonterminal in the following grammar. e t e' e' + t e' ε t f t' t' * f t' ε f ( e ) x y
119 Answer follow( e' ) = { $, ) } follow( e ) = { $, ) } follow( t' ) = { +, $, ) } follow( t ) = { +, $, ) } follow( f ) = { *, +, ), $ }
120 Answer follow( e' ) = { $, ) } follow( e ) = { $, ) } follow( t' ) = { +, $, ) } follow( t ) = { +, $, ) } follow( f ) = { *, +, ), $ }
121 Predictive parsing table For each nonterminal n, a parse table T defines which production of n should be chosen, based on the next input symbol. for each production n α for each a first(α) add n α to T[n, a] if ε first(α) then for each b follow(n) add n α to T[n, a]
122 Predictive parsing table For each nonterminal n, a parse table T defines which production of n should be chosen, based on the next input symbol. for each production n α for each a first(α) add n α to T[n, a] if ε first(α) then for each b follow(n) add n α to T[n, a]
123 Exercise 9 Construct a predictive parsing table for the following grammar. e t e' e' + t e' ε t f t' t' * f t' ε f ( e ) x y
124 Exercise 9 Construct a predictive parsing table for the following grammar. e t e' e' + t e' ε t f t' t' * f t' ε f ( e ) x y
125 LL(1) grammars If each cell in the parse table contains at most one entry then the a nonbacktracking parser can be constructed and the grammar is said to be LL(1). First L: lefttoright scanning of the input. Second L: a leftmost derivation is constructed. The (1): using one input symbol of lookahead to decide which grammar production to choose.
126 LL(1) grammars If each cell in the parse table contains at most one entry then the a nonbacktracking parser can be constructed and the grammar is said to be LL(1). First L: lefttoright scanning of the input. Second L: a leftmost derivation is constructed. The (1): using one input symbol of lookahead to decide which grammar production to choose.
127 Exercise 10 Write a syntax checker for the grammar of Exercise 9, utilising the predictive parsing table. int e() {... } It should return a nonzero value if some prefix of the string pointed to by next conforms to the grammar, otherwise it should return zero.
128 Exercise 10 Write a syntax checker for the grammar of Exercise 9, utilising the predictive parsing table. int e() {... } It should return a nonzero value if some prefix of the string pointed to by next conforms to the grammar, otherwise it should return zero.
129 Answer (part 1) int e() { if (*next == 'x') return t() && e1(); if (*next == 'y') return t() && e1(); if (*next == '(') return t() && e1(); return 0; } int e1() { if (*next == '+') return eat('+') && t() && e1(); if (*next == ')') return 1; if (*next == '\0') return 1; return 0; }
130 Answer (part 1) int e() { if (*next == 'x') return t() && e1(); if (*next == 'y') return t() && e1(); if (*next == '(') return t() && e1(); return 0; } int e1() { if (*next == '+') return eat('+') && t() && e1(); if (*next == ')') return 1; if (*next == '\0') return 1; return 0; }
131 Answer (part 2) int t() { if (*next == 'x') return f() && t1(); if (*next == 'y') return f() && t1(); if (*next == '(') return f() && t1(); return 0; } int t1() { if (*next == '+') return 1; if (*next == '* ) return eat('*') && f() && t1(); if (*next == ')') return 1; if (*next == '\0') return 1; return 0; }
132 Answer (part 2) int t() { if (*next == 'x') return f() && t1(); if (*next == 'y') return f() && t1(); if (*next == '(') return f() && t1(); return 0; } int t1() { if (*next == '+') return 1; if (*next == '* ) return eat('*') && f() && t1(); if (*next == ')') return 1; if (*next == '\0') return 1; return 0; }
133 Answer (part 3) int f() { if (*next == 'x') return eat('x'); if (*next == 'y') return eat('y'); if (*next == '(') return eat('(') && e() && eat(')'); return 0; } (Notice how backtracking is not required.)
134 Answer (part 3) int f() { if (*next == 'x') return eat('x'); if (*next == 'y') return eat('y'); if (*next == '(') return eat('(') && e() && eat(')'); return 0; } (Notice how backtracking is not required.)
135 Predictive parsing algorithm Let s be a stack, initially containing the start symbol of the grammar, and let next point to the input string. while (top(s)!= $) if (top(s) is a terminal) { if (top(s) == *next) { pop(s); next++; } else error(); } else if (T[top(s), *next] == X Y 1 Y n ) { pop(s); push(s, Y n Y 1 ) /* Y 1 on top */ }
136 Predictive parsing algorithm Let s be a stack, initially containing the start symbol of the grammar, and let next point to the input string. while (top(s)!= $) if (top(s) is a terminal) { if (top(s) == *next) { pop(s); next++; } else error(); } else if (T[top(s), *next] == X Y 1 Y n ) { pop(s); push(s, Y n Y 1 ) /* Y 1 on top */ }
137 Exercise 11 Give the steps that a predictive parser takes to parse the following input. x + x * y For each step (loop iteration), show the input stream, the stack, and the parser action.
138 Exercise 11 Give the steps that a predictive parser takes to parse the following input. x + x * y For each step (loop iteration), show the input stream, the stack, and the parser action.
139 Acknowledgements Plus Stanford University lecture notes by Maggie Johnson and Julie Zelenski.
140 Acknowledgements Plus Stanford University lecture notes by Maggie Johnson and Julie Zelenski.
141 APPENDIX
142 APPENDIX
143 Chomsky hierarchy Let t range over terminals, x and z over nonterminals and, β and γ over sequences of terminals, nonterminals, and ε. Grammar Unrestricted Valid productions α β ContextSensitive α x γ α β γ ContextFree Regular x β x t x t z x ε
144 Chomsky hierarchy Let t range over terminals, x and z over nonterminals and, β and γ over sequences of terminals, nonterminals, and ε. Grammar Unrestricted Valid productions α β ContextSensitive α x γ α β γ ContextFree Regular x β x t x t z x ε
145 BackusNaur Form BNF is a standard ASCII notation for specification of contextfree grammars whose terminals are ASCII characters. For example: <exp> ::= <exp> "+" <exp> <exp> "" <exp> <var> <var> ::= "x" "y" The BNF notation can itself be specified in BNF.
Lexical and Syntax Analysis. TopDown Parsing
Lexical and Syntax Analysis TopDown Parsing Easy for humans to write and understand String of characters Lexemes identified String of tokens Easy for programs to transform Data structure Syntax A syntax
More informationA programming language requires two major definitions A simple one pass compiler
A programming language requires two major definitions A simple one pass compiler [Syntax: what the language looks like A contextfree grammar written in BNF (BackusNaur Form) usually suffices. [Semantics:
More informationCS1622. Today. A Recursive Descent Parser. Preliminaries. Lecture 9 Parsing (4)
CS1622 Lecture 9 Parsing (4) CS 1622 Lecture 9 1 Today Example of a recursive descent parser Predictive & LL(1) parsers Building parse tables CS 1622 Lecture 9 2 A Recursive Descent Parser. Preliminaries
More informationParsing. Roadmap. > Contextfree grammars > Derivations and precedence > Topdown parsing > Leftrecursion > Lookahead > Tabledriven parsing
Roadmap > Contextfree grammars > Derivations and precedence > Topdown parsing > Leftrecursion > Lookahead > Tabledriven parsing The role of the parser > performs contextfree syntax analysis > guides
More informationTopDown Parsing and Intro to BottomUp Parsing. Lecture 7
TopDown Parsing and Intro to BottomUp Parsing Lecture 7 1 Predictive Parsers Like recursivedescent but parser can predict which production to use Predictive parsers are never wrong Always able to guess
More informationWednesday, September 9, 15. Parsers
Parsers What is a parser A parser has two jobs: 1) Determine whether a string (program) is valid (think: grammatically correct) 2) Determine the structure of a program (think: diagramming a sentence) Agenda
More informationTopDown Parsing and Intro to BottomUp Parsing. Lecture 7
TopDown Parsing and Intro to BottomUp Parsing Lecture 7 1 Predictive Parsers Like recursivedescent but parser can predict which production to use Predictive parsers are never wrong Always able to guess
More informationParsers. What is a parser. Languages. Agenda. Terminology. Languages. A parser has two jobs:
What is a parser Parsers A parser has two jobs: 1) Determine whether a string (program) is valid (think: grammatically correct) 2) Determine the structure of a program (think: diagramming a sentence) Agenda
More informationSyntax Analysis Part I
Syntax Analysis Part I Chapter 4: ContextFree Grammars Slides adapted from : Robert van Engelen, Florida State University Position of a Parser in the Compiler Model Source Program Lexical Analyzer Token,
More information3. Parsing. Oscar Nierstrasz
3. Parsing Oscar Nierstrasz Thanks to Jens Palsberg and Tony Hosking for their kind permission to reuse and adapt the CS132 and CS502 lecture notes. http://www.cs.ucla.edu/~palsberg/ http://www.cs.purdue.edu/homes/hosking/
More informationTop down vs. bottom up parsing
Parsing A grammar describes the strings that are syntactically legal A recogniser simply accepts or rejects strings A generator produces sentences in the language described by the grammar A parser constructs
More informationAbstract Syntax Trees & TopDown Parsing
Review of Parsing Abstract Syntax Trees & TopDown Parsing Given a language L(G), a parser consumes a sequence of tokens s and produces a parse tree Issues: How do we recognize that s L(G)? A parse tree
More informationParsing III. CS434 Lecture 8 Spring 2005 Department of Computer Science University of Alabama Joel Jones
Parsing III (Topdown parsing: recursive descent & LL(1) ) (Bottomup parsing) CS434 Lecture 8 Spring 2005 Department of Computer Science University of Alabama Joel Jones Copyright 2003, Keith D. Cooper,
More informationBuilding Compilers with Phoenix
Building Compilers with Phoenix SyntaxDirected Translation Structure of a Compiler Character Stream Intermediate Representation Lexical Analyzer MachineIndependent Optimizer token stream Intermediate
More informationAbstract Syntax Trees & TopDown Parsing
Abstract Syntax Trees & TopDown Parsing Review of Parsing Given a language L(G), a parser consumes a sequence of tokens s and produces a parse tree Issues: How do we recognize that s L(G)? A parse tree
More informationAbstract Syntax Trees & TopDown Parsing
Review of Parsing Abstract Syntax Trees & TopDown Parsing Given a language L(G), a parser consumes a sequence of tokens s and produces a parse tree Issues: How do we recognize that s L(G)? A parse tree
More informationMonday, September 13, Parsers
Parsers Agenda Terminology LL(1) Parsers Overview of LR Parsing Terminology Grammar G = (Vt, Vn, S, P) Vt is the set of terminals Vn is the set of nonterminals S is the start symbol P is the set of productions
More informationTableDriven Parsing
TableDriven Parsing It is possible to build a nonrecursive predictive parser by maintaining a stack explicitly, rather than implicitly via recursive calls [1] The nonrecursive parser looks up the production
More informationCOP 3402 Systems Software Top Down Parsing (Recursive Descent)
COP 3402 Systems Software Top Down Parsing (Recursive Descent) Top Down Parsing 1 Outline 1. Top down parsing and LL(k) parsing 2. Recursive descent parsing 3. Example of recursive descent parsing of arithmetic
More informationParsers. Xiaokang Qiu Purdue University. August 31, 2018 ECE 468
Parsers Xiaokang Qiu Purdue University ECE 468 August 31, 2018 What is a parser A parser has two jobs: 1) Determine whether a string (program) is valid (think: grammatically correct) 2) Determine the structure
More informationAdministrativia. WA1 due on Thu PA2 in a week. Building a Parser III. Slides on the web site. CS164 3:305:00 TT 10 Evans.
Administrativia Building a Parser III CS164 3:305:00 10 vans WA1 due on hu PA2 in a week Slides on the web site I do my best to have slides ready and posted by the end of the preceding logical day yesterday,
More information1 Introduction. 2 Recursive descent parsing. Predicative parsing. Computer Language Implementation Lecture Note 3 February 4, 2004
CMSC 51086 Winter 2004 Computer Language Implementation Lecture Note 3 February 4, 2004 Predicative parsing 1 Introduction This note continues the discussion of parsing based on context free languages.
More informationCOP 3402 Systems Software Syntax Analysis (Parser)
COP 3402 Systems Software Syntax Analysis (Parser) Syntax Analysis 1 Outline 1. Definition of Parsing 2. Context Free Grammars 3. Ambiguous/Unambiguous Grammars Syntax Analysis 2 Lexical and Syntax Analysis
More informationWednesday, August 31, Parsers
Parsers How do we combine tokens? Combine tokens ( words in a language) to form programs ( sentences in a language) Not all combinations of tokens are correct programs (not all sentences are grammatically
More informationContextFree Grammar. Concepts Introduced in Chapter 2. Parse Trees. Example Grammar and Derivation
Concepts Introduced in Chapter 2 A more detailed overview of the compilation process. Parsing Scanning Semantic Analysis SyntaxDirected Translation Intermediate Code Generation ContextFree Grammar A
More informationPart 3. Syntax analysis. Syntax analysis 96
Part 3 Syntax analysis Syntax analysis 96 Outline 1. Introduction 2. Contextfree grammar 3. Topdown parsing 4. Bottomup parsing 5. Conclusion and some practical considerations Syntax analysis 97 Structure
More informationSyntax Analysis. COMP 524: Programming Language Concepts Björn B. Brandenburg. The University of North Carolina at Chapel Hill
Syntax Analysis Björn B. Brandenburg The University of North Carolina at Chapel Hill Based on slides and notes by S. Olivier, A. Block, N. Fisher, F. HernandezCampos, and D. Stotts. The Big Picture Character
More informationBuilding a Parser III. CS164 3:305:00 TT 10 Evans. Prof. Bodik CS 164 Lecture 6 1
Building a Parser III CS164 3:305:00 TT 10 Evans 1 Overview Finish recursive descent parser when it breaks down and how to fix it eliminating left recursion reordering productions Predictive parsers (aka
More informationSyntax Analysis. The Big Picture. The Big Picture. COMP 524: Programming Languages Srinivas Krishnan January 25, 2011
Syntax Analysis COMP 524: Programming Languages Srinivas Krishnan January 25, 2011 Based in part on slides and notes by Bjoern Brandenburg, S. Olivier and A. Block. 1 The Big Picture Character Stream Token
More informationCompilers. Yannis Smaragdakis, U. Athens (original slides by Sam
Compilers Parsing Yannis Smaragdakis, U. Athens (original slides by Sam Guyer@Tufts) Next step text chars Lexical analyzer tokens Parser IR Errors Parsing: Organize tokens into sentences Do tokens conform
More informationAmbiguity, Precedence, Associativity & TopDown Parsing. Lecture 910
Ambiguity, Precedence, Associativity & TopDown Parsing Lecture 910 (From slides by G. Necula & R. Bodik) 9/18/06 Prof. Hilfinger CS164 Lecture 9 1 Administrivia Please let me know if there are continued
More informationParsing III. (Topdown parsing: recursive descent & LL(1) )
Parsing III (Topdown parsing: recursive descent & LL(1) ) Roadmap (Where are we?) Previously We set out to study parsing Specifying syntax Contextfree grammars Ambiguity Topdown parsers Algorithm &
More informationLL(k) Parsing. Predictive Parsers. LL(k) Parser Structure. Sample Parse Table. LL(1) Parsing Algorithm. Push RHS in Reverse Order 10/17/2012
Predictive Parsers LL(k) Parsing Can we avoid backtracking? es, if for a given input symbol and given nonterminal, we can choose the alternative appropriately. his is possible if the first terminal of
More informationCompilers. Predictive Parsing. Alex Aiken
Compilers Like recursivedescent but parser can predict which production to use By looking at the next fewtokens No backtracking Predictive parsers accept LL(k) grammars L means lefttoright scan of input
More informationCS502: Compilers & Programming Systems
CS502: Compilers & Programming Systems Topdown Parsing Zhiyuan Li Department of Computer Science Purdue University, USA There exist two wellknown schemes to construct deterministic topdown parsers:
More informationSYNTAX ANALYSIS 1. Define parser. Hierarchical analysis is one in which the tokens are grouped hierarchically into nested collections with collective meaning. Also termed as Parsing. 2. Mention the basic
More informationSyntax Analysis/Parsing. Contextfree grammars (CFG s) Contextfree grammars vs. Regular Expressions. BNF description of PL/0 syntax
Susan Eggers 1 CSE 401 Syntax Analysis/Parsing Contextfree grammars (CFG s) Purpose: determine if tokens have the right form for the language (right syntactic structure) stream of tokens abstract syntax
More informationParsing #1. Leonidas Fegaras. CSE 5317/4305 L3: Parsing #1 1
Parsing #1 Leonidas Fegaras CSE 5317/4305 L3: Parsing #1 1 Parser source file get next character scanner get token parser AST token A parser recognizes sequences of tokens according to some grammar and
More information8 Parsing. Parsing. Top Down Parsing Methods. Parsing complexity. Top down vs. bottom up parsing. Top down vs. bottom up parsing
8 Parsing Parsing A grammar describes syntactically legal strings in a language A recogniser simply accepts or rejects strings A generator produces strings A parser constructs a parse tree for a string
More informationCOMP421 Compiler Design. Presented by Dr Ioanna Dionysiou
COMP421 Compiler Design Presented by Dr Ioanna Dionysiou Administrative! Any questions about the syllabus?! Course Material available at www.cs.unic.ac.cy/ioanna! Next time reading assignment [ALSU07]
More informationCompilerconstructie. najaar Rudy van Vliet kamer 140 Snellius, tel rvvliet(at)liacs(dot)nl. college 3, vrijdag 22 september 2017
Compilerconstructie najaar 2017 http://www.liacs.leidenuniv.nl/~vlietrvan1/coco/ Rudy van Vliet kamer 140 Snellius, tel. 071527 2876 rvvliet(at)liacs(dot)nl college 3, vrijdag 22 september 2017 + werkcollege
More informationCS 406/534 Compiler Construction Parsing Part I
CS 406/534 Compiler Construction Parsing Part I Prof. Li Xu Dept. of Computer Science UMass Lowell Fall 2004 Part of the course lecture notes are based on Prof. Keith Cooper, Prof. Ken Kennedy and Dr.
More informationChapter 3. Parsing #1
Chapter 3 Parsing #1 Parser source file get next character scanner get token parser AST token A parser recognizes sequences of tokens according to some grammar and generates Abstract Syntax Trees (ASTs)
More informationCompilation Lecture 3: Syntax Analysis: TopDown parsing. Noam Rinetzky
Compilation 03683133 Lecture 3: Syntax Analysis: TopDown parsing Noam Rinetzky 1 Recursive descent parsing Define a function for every nonterminal Every function work as follows Find applicable production
More informationSyntactic Analysis. TopDown Parsing
Syntactic Analysis TopDown Parsing Copyright 2017, Pedro C. Diniz, all rights reserved. Students enrolled in Compilers class at University of Southern California (USC) have explicit permission to make
More informationSometimes an ambiguous grammar can be rewritten to eliminate the ambiguity.
Eliminating Ambiguity Sometimes an ambiguous grammar can be rewritten to eliminate the ambiguity. Example: consider the following grammar stat if expr then stat if expr then stat else stat other One can
More informationDefining syntax using CFGs
Defining syntax using CFGs Roadmap Last time Defined contextfree grammar This time CFGs for specifying a language s syntax Language membership List grammars Resolving ambiguity CFG Review G = (N,Σ,P,S)
More informationContextfree grammars (CFG s)
Syntax Analysis/Parsing Purpose: determine if tokens have the right form for the language (right syntactic structure) stream of tokens abstract syntax tree (AST) AST: captures hierarchical structure of
More informationSyntax Analysis. Martin Sulzmann. Martin Sulzmann Syntax Analysis 1 / 38
Syntax Analysis Martin Sulzmann Martin Sulzmann Syntax Analysis 1 / 38 Syntax Analysis Objective Recognize individual tokens as sentences of a language (beyond regular languages). Example 1 (OK) Program
More informationAmbiguity. Grammar E E + E E * E ( E ) int. The string int * int + int has two parse trees. * int
Administrivia Ambiguity, Precedence, Associativity & opdown Parsing eam assignments this evening for all those not listed as having one. HW#3 is now available, due next uesday morning (Monday is a holiday).
More informationCS415 Compilers. Syntax Analysis. These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University
CS415 Compilers Syntax Analysis These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University Limits of Regular Languages Advantages of Regular Expressions
More informationA Simple SyntaxDirected Translator
Chapter 2 A Simple SyntaxDirected Translator 11 Introduction The analysis phase of a compiler breaks up a source program into constituent pieces and produces an internal representation for it, called
More informationSyntax Analysis Check syntax and construct abstract syntax tree
Syntax Analysis Check syntax and construct abstract syntax tree if == = ; b 0 a b Error reporting and recovery Model using context free grammars Recognize using Push down automata/table Driven Parsers
More informationCOP4020 Programming Languages. Syntax Prof. Robert van Engelen
COP4020 Programming Languages Syntax Prof. Robert van Engelen Overview Tokens and regular expressions Syntax and contextfree grammars Grammar derivations More about parse trees Topdown and bottomup
More informationCompiler Design 1. TopDown Parsing. Goutam Biswas. Lect 5
Compiler Design 1 TopDown Parsing Compiler Design 2 Nonterminal as a Function In a topdown parser a nonterminal may be viewed as a generator of a substring of the input. We may view a nonterminal
More informationPrelude COMP 181 Tufts University Computer Science Last time Grammar issues Key structure meaning Tufts University Computer Science
Prelude COMP Lecture Topdown Parsing September, 00 What is the Tufts mascot? Jumbo the elephant Why? P. T. Barnum was an original trustee of Tufts : donated $0,000 for a natural museum on campus Barnum
More informationCA Compiler Construction
CA4003  Compiler Construction David Sinclair A topdown parser starts with the root of the parse tree, labelled with the goal symbol of the grammar, and repeats the following steps until the fringe of
More informationA simple syntaxdirected
Syntaxdirected is a grammaroriented compiling technique Programming languages: Syntax: what its programs look like? Semantic: what its programs mean? 1 A simple syntaxdirected Lexical Syntax Character
More informationCOP4020 Programming Languages. Syntax Prof. Robert van Engelen
COP4020 Programming Languages Syntax Prof. Robert van Engelen Overview n Tokens and regular expressions n Syntax and contextfree grammars n Grammar derivations n More about parse trees n Topdown and
More informationSection A. A grammar that produces more than one parse tree for some sentences is said to be ambiguous.
Section A 1. What do you meant by parser and its types? A parser for grammar G is a program that takes as input a string w and produces as output either a parse tree for w, if w is a sentence of G, or
More informationCompilers: CS31003 Computer Sc & Engg: IIT Kharagpur 1. TopDown Parsing. Lect 5. Goutam Biswas
Compilers: CS31003 Computer Sc & Engg: IIT Kharagpur 1 TopDown Parsing Compilers: CS31003 Computer Sc & Engg: IIT Kharagpur 2 Nonterminal as a Function In a topdown parser a nonterminal may be viewed
More informationEDA180: Compiler Construc6on Context free grammars. Görel Hedin Revised:
EDA180: Compiler Construc6on Context free grammars Görel Hedin Revised: 201301 28 Compiler phases and program representa6ons source code Lexical analysis (scanning) Intermediate code genera6on tokens
More informationTypes of parsing. CMSC 430 Lecture 4, Page 1
Types of parsing Topdown parsers start at the root of derivation tree and fill in picks a production and tries to match the input may require backtracking some grammars are backtrackfree (predictive)
More informationCSCI312 Principles of Programming Languages
Copyright 2006 The McGrawHill Companies, Inc. CSCI312 Principles of Programming Languages! LL Parsing!! Xu Liu Derived from Keith Cooper s COMP 412 at Rice University Recap Copyright 2006 The McGrawHill
More informationIntroduction to Syntax Analysis
Compiler Design 1 Introduction to Syntax Analysis Compiler Design 2 Syntax Analysis The syntactic or the structural correctness of a program is checked during the syntax analysis phase of compilation.
More informationIntroduction to Syntax Analysis. The Second Phase of FrontEnd
Compiler Design IIIT Kalyani, WB 1 Introduction to Syntax Analysis The Second Phase of FrontEnd Compiler Design IIIT Kalyani, WB 2 Syntax Analysis The syntactic or the structural correctness of a program
More informationContextFree Languages and Parse Trees
ContextFree Languages and Parse Trees Mridul Aanjaneya Stanford University July 12, 2012 Mridul Aanjaneya Automata Theory 1/ 41 ContextFree Grammars A contextfree grammar is a notation for describing
More informationNote that for recursive descent to work, if A ::= B1 B2 is a grammar rule we need First k (B1) disjoint from First k (B2).
LL(k) Grammars We need a bunch of terminology. For any terminal string a we write First k (a) is the prefix of a of length k (or all of a if its length is less than k) For any string g of terminal and
More informationFall Compiler Principles Lecture 2: LL parsing. Roman Manevich BenGurion University of the Negev
Fall 20172018 Compiler Principles Lecture 2: LL parsing Roman Manevich BenGurion University of the Negev 1 Books Compilers Principles, Techniques, and Tools Alfred V. Aho, Ravi Sethi, Jeffrey D. Ullman
More informationEDAN65: Compilers, Lecture 04 Grammar transformations: Eliminating ambiguities, adapting to LL parsing. Görel Hedin Revised:
EDAN65: Compilers, Lecture 04 Grammar transformations: Eliminating ambiguities, adapting to LL parsing Görel Hedin Revised: 20170904 This lecture Regular expressions Contextfree grammar Attribute grammar
More informationPrinciples of Programming Languages COMP251: Syntax and Grammars
Principles of Programming Languages COMP251: Syntax and Grammars Prof. Dekai Wu Department of Computer Science and Engineering The Hong Kong University of Science and Technology Hong Kong, China Fall 2006
More informationChapter 3: Describing Syntax and Semantics. Introduction Formal methods of describing syntax (BNF)
Chapter 3: Describing Syntax and Semantics Introduction Formal methods of describing syntax (BNF) We can analyze syntax of a computer program on two levels: 1. Lexical level 2. Syntactic level Lexical
More informationIt parses an input string of tokens by tracing out the steps in a leftmost derivation.
It parses an input string of tokens by tracing out CS 4203 Compiler Theory the steps in a leftmost derivation. CHAPTER 4: TOPDOWN PARSING Part1 And the implied traversal of the parse tree is a preorder
More informationCompiler Design Concepts. Syntax Analysis
Compiler Design Concepts Syntax Analysis Introduction First task is to break up the text into meaningful words called tokens. newval=oldval+12 id = id + num Token Stream Lexical Analysis Source Code (High
More informationMore Assigned Reading and Exercises on Syntax (for Exam 2)
More Assigned Reading and Exercises on Syntax (for Exam 2) 1. Read sections 2.3 (Lexical Syntax) and 2.4 (ContextFree Grammars) on pp. 33 41 of Sethi. 2. Read section 2.6 (Variants of Grammars) on pp.
More informationHomework. Lecture 7: Parsers & Lambda Calculus. Rewrite Grammar. Problems
Homework Lecture 7: Parsers & Lambda Calculus CSC 131 Spring, 2019 Kim Bruce First line:  module Hmwk3 where  Next line should be name as comment  Name of program file should be Hmwk3.hs Problems How
More informationCSCI312 Principles of Programming Languages!
CSCI312 Principles of Programming Languages!! Chapter 3 Regular Expression and Lexer Xu Liu Recap! Copyright 2006 The McGrawHill Companies, Inc. Clite: Lexical Syntax! Input: a stream of characters from
More informationCSCI312 Principles of Programming Languages!
CSCI312 Principles of Programming Languages! Chapter 2 Syntax! Xu Liu Review! Principles of PL syntax, naming, types, semantics Paradigms of PL design imperative, OO, functional, logic What makes a successful
More informationLexical and Syntax Analysis. BottomUp Parsing
Lexical and Syntax Analysis BottomUp Parsing Parsing There are two ways to construct derivation of a grammar. TopDown: begin with start symbol; repeatedly replace an instance of a production s LHS with
More informationSyntax. In Text: Chapter 3
Syntax In Text: Chapter 3 1 Outline Syntax: Recognizer vs. generator BNF EBNF Chapter 3: Syntax and Semantics 2 Basic Definitions Syntax the form or structure of the expressions, statements, and program
More informationCIT Lecture 5 ContextFree Grammars and Parsing 4/2/2003 1
CIT3136  Lecture 5 ContextFree Grammars and Parsing 4/2/2003 1 Definition of a Contextfree Grammar: An alphabet or set of basic symbols (like regular expressions, only now the symbols are whole tokens,
More information4. Lexical and Syntax Analysis
4. Lexical and Syntax Analysis 4.1 Introduction Language implementation systems must analyze source code, regardless of the specific implementation approach Nearly all syntax analysis is based on a formal
More informationLECTURE 7. Lex and Intro to Parsing
LECTURE 7 Lex and Intro to Parsing LEX Last lecture, we learned a little bit about how we can take our regular expressions (which specify our valid tokens) and create real programs that can recognize them.
More informationDerivations vs Parses. Example. Parse Tree. Ambiguity. Different Parse Trees. Context Free Grammars 9/18/2012
Derivations vs Parses Grammar is used to derive string or construct parser Context ree Grammars A derivation is a sequence of applications of rules Starting from the start symbol S......... (sentence)
More information3. Syntax Analysis. Andrea Polini. Formal Languages and Compilers Master in Computer Science University of Camerino
3. Syntax Analysis Andrea Polini Formal Languages and Compilers Master in Computer Science University of Camerino (Formal Languages and Compilers) 3. Syntax Analysis CS@UNICAM 1 / 54 Syntax Analysis: the
More informationParsing. source code. while (k<=n) {sum = sum+k; k=k+1;}
Compiler Construction Grammars Parsing source code scanner tokens regular expressions lexical analysis Lennart Andersson parser context free grammar Revision 2012 01 23 2012 parse tree AST builder (implicit)
More informationCSE 3302 Programming Languages Lecture 2: Syntax
CSE 3302 Programming Languages Lecture 2: Syntax (based on slides by Chengkai Li) Leonidas Fegaras University of Texas at Arlington CSE 3302 L2 Spring 2011 1 How do we define a PL? Specifying a PL: Syntax:
More informationIntroduction to Lexing and Parsing
Introduction to Lexing and Parsing ECE 351: Compilers Jon Eyolfson University of Waterloo June 18, 2012 1 Riddle Me This, Riddle Me That What is a compiler? 1 Riddle Me This, Riddle Me That What is a compiler?
More information4 (c) parsing. Parsing. Top down vs. bo5om up parsing
4 (c) parsing Parsing A grammar describes syntac2cally legal strings in a language A recogniser simply accepts or rejects strings A generator produces strings A parser constructs a parse tree for a string
More informationIntroduction to Parsing. Lecture 8
Introduction to Parsing Lecture 8 Adapted from slides by G. Necula Outline Limitations of regular languages Parser overview Contextfree grammars (CFG s) Derivations Languages and Automata Formal languages
More informationLL parsing Nullable, FIRST, and FOLLOW
EDAN65: Compilers LL parsing Nullable, FIRST, and FOLLOW Görel Hedin Revised: 201409 22 Regular expressions Context free grammar ATribute grammar Lexical analyzer (scanner) SyntacKc analyzer (parser)
More information4. Lexical and Syntax Analysis
4. Lexical and Syntax Analysis 4.1 Introduction Language implementation systems must analyze source code, regardless of the specific implementation approach Nearly all syntax analysis is based on a formal
More informationPART 3  SYNTAX ANALYSIS. F. Wotawa TU Graz) Compiler Construction Summer term / 309
PART 3  SYNTAX ANALYSIS F. Wotawa (IST @ TU Graz) Compiler Construction Summer term 2016 64 / 309 Goals Definition of the syntax of a programming language using context free grammars Methods for parsing
More informationSyntaxDirected Translation. Lecture 14
SyntaxDirected Translation Lecture 14 (adapted from slides by R. Bodik) 9/27/2006 Prof. Hilfinger, Lecture 14 1 Motivation: parser as a translator syntaxdirected translation stream of tokens parser ASTs,
More informationCPS 506 Comparative Programming Languages. Syntax Specification
CPS 506 Comparative Programming Languages Syntax Specification Compiling Process Steps Program Lexical Analysis Convert characters into a stream of tokens Lexical Analysis Syntactic Analysis Send tokens
More informationCompila(on (Semester A, 2013/14)
Compila(on 03683133 (Semester A, 2013/14) Lecture 4: Syntax Analysis (Top Down Parsing) Modern Compiler Design: Chapter 2.2 Noam Rinetzky Slides credit: Roman Manevich, Mooly Sagiv, Jeff Ullman, Eran
More informationFall Compiler Principles Lecture 3: Parsing part 2. Roman Manevich BenGurion University
Fall 20142015 Compiler Principles Lecture 3: Parsing part 2 Roman Manevich BenGurion University Tentative syllabus Front End Intermediate Representation Optimizations Code Generation Scanning Lowering
More informationSyntax/semantics. Program <> program execution Compiler/interpreter Syntax Grammars Syntax diagrams Automata/State Machines Scanning/Parsing
Syntax/semantics Program program execution Compiler/interpreter Syntax Grammars Syntax diagrams Automata/State Machines Scanning/Parsing Metamodels 8/27/10 1 Program program execution Syntax Semantics
More informationPart III : Parsing. From Regular to ContextFree Grammars. Deriving a Parser from a ContextFree Grammar. Scanners and Parsers.
Part III : Parsing From Regular to ContextFree Grammars Deriving a Parser from a ContextFree Grammar Scanners and Parsers A Parser for EBNF LeftParsable Grammars Martin Odersky, LAMP/DI 1 From Regular
More informationEarlier edition Dragon book has been revised. Course Outline Contact Room 124, tel , rvvliet(at)liacs(dot)nl
Compilerconstructie najaar 2013 http://www.liacs.nl/home/rvvliet/coco/ Rudy van Vliet kamer 124 Snellius, tel. 071527 5777 rvvliet(at)liacs(dot)nl college 1, dinsdag 3 september 2013 Overview 1 Why this
More information