Download presentation
Presentation is loading. Please wait.
Published byFelix Stanley Modified over 9 years ago
1
1 October 2, 2015 1 October 2, 2015October 2, 2015October 2, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/http://www.apu.edu/clas/computerscience/ CS400 Compiler Construction
2
2 Syntax Analysis Part I Chapter 4 October 2, 2015 2 Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/http://www.apu.edu/clas/computerscience/ CS@APU: CS400 Compiler Construction
3
3 Position of a Parser in the Compiler Model Lexical Analyzer Parser and rest of front-end Source Program Token, tokenval Symbol Table Get next token Lexical errorSyntax error Semantic error Intermediate representation October 2, 2015 3 Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/http://www.apu.edu/clas/computerscience/ CS@APU: CS400 Compiler Construction
4
4 October 2, 2015 4 Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/http://www.apu.edu/clas/computerscience/ CS@APU: CS400 Compiler Construction Keep in mind following questions Parser –T–Twofold: –C–Checking syntax –I–Invoking semantic action Error Handling –T–Two abilities: –I–Identifying errors –L–Locating errors Chomsky Hierarchy –F–From Turing machine –t–to Finite Automaton –F–From non to more restriction
5
5 The Parser A parser implements a C-F grammar The role of the parser is twofold: 1.To check syntax (= string recognizer) –And to report syntax errors accurately 2.To invoke semantic actions –For static semantics checking, e.g. type checking of expressions, functions, etc. –For syntax-directed translation of the source code to an intermediate representation October 2, 2015 5 Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/http://www.apu.edu/clas/computerscience/ CS@APU: CS400 Compiler Construction
6
6 Syntax-Directed Translation One of the major roles of the parser is to produce an intermediate representation (IR) of the source program using syntax-directed translation methods Possible IR output: –Abstract syntax trees (ASTs) –Control-flow graphs (CFGs) with triples, three-address code, or register transfer list notation –WHIRL (SGI Pro64 compiler) has 5 IR levels! October 2, 2015 6 Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/http://www.apu.edu/clas/computerscience/ CS@APU: CS400 Compiler Construction
7
7 WHIRL Winning Hierarchical Intermediate Representation 5 Levels: –VH, H, M L, VL –Lowering happens when needed –Each optimization performed at the right level October 2, 2015 7 Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/http://www.apu.edu/clas/computerscience/ CS@APU: CS400 Compiler Construction
8
8 Error Handling A good compiler should assist in identifying and locating errors –Lexical errors: important, compiler can easily recover and continue –Syntax errors: most important for compiler, can almost always recover –Static semantic errors: important, can sometimes recover –Dynamic semantic errors: hard or impossible to detect at compile time, runtime checks are required –Logical errors: hard or impossible to detect October 2, 2015 8 Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/http://www.apu.edu/clas/computerscience/ CS@APU: CS400 Compiler Construction
9
9 Viable-Prefix Property The viable-prefix property of LL/LR parsers allows early detection of syntax errors –Goal: detection of an error as soon as possible without further consuming unnecessary input –How: detect an error as soon as the prefix of the input does not match a prefix of any string in the language … for (;) … … DO 10 I = 1;0 … Error is detected here Prefix October 2, 2015 9 Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/http://www.apu.edu/clas/computerscience/ CS@APU: CS400 Compiler Construction
10
10 Error Recovery Strategies Panic mode –Discard input until a token in a set of designated synchronizing tokens is found Phrase-level recovery –Perform local correction on the input to repair the error Error productions –Augment grammar with productions for erroneous constructs Global correction –Choose a minimal sequence of changes to obtain a global least-cost correction October 2, 2015 10 Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/http://www.apu.edu/clas/computerscience/ CS@APU: CS400 Compiler Construction
11
11 Grammars (Recap) Context-free grammar is a 4-tuple G = (N, T, P, S) where –T is a finite set of tokens (terminal symbols) –N is a finite set of nonterminals –P is a finite set of productions of the form where (N T)* N (N T)* and (N T)* –S N is a designated start symbol October 2, 2015 11 Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/http://www.apu.edu/clas/computerscience/ CS@APU: CS400 Compiler Construction
12
12 Notational Conventions Used Terminals a,b,c,… T specific terminals: 0, 1, id, + Nonterminals A,B,C,… N specific nonterminals: expr, term, stmt Grammar symbols X,Y,Z (N T) Strings of terminals u,v,w,x,y,z T* Strings of grammar symbols , , (N T)* October 2, 2015 12 Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/http://www.apu.edu/clas/computerscience/ CS@APU: CS400 Compiler Construction
13
13 Derivations (Recap) The one-step derivation is defined by A where A is a production in the grammar In addition, we define – is leftmost lm if does not contain a nonterminal – is rightmost rm if does not contain a nonterminal –Transitive closure * (zero or more steps) –Positive closure + (one or more steps) The language generated by G is defined by L(G) = {w T* | S + w} October 2, 2015 13 Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/http://www.apu.edu/clas/computerscience/ CS@APU: CS400 Compiler Construction
14
14 Derivation (Example) Grammar G = ({E}, {+,*,(,),-,id}, P, E) with productions P =E E + E E E * E E ( E ) E - E E id E - E - id E * EE * E E + id * id + id E rm E + E rm E + id rm id + id Example derivations: E * id + id October 2, 2015 14 Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/http://www.apu.edu/clas/computerscience/ CS@APU: CS400 Compiler Construction
15
15 Chomsky Hierarchy: Language Classification October 2, 2015 15 Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/http://www.apu.edu/clas/computerscience/ CS@APU: CS400 Compiler Construction Avram Noam Chomsky is an American linguist, philosopher,, and lecturer. He is a professor emeritus of linguistics at the MIT. Chomsky is well known in the academic and scientific community as the father of modern linguistics. In the 1950s, Chomsky began developing his theory of generative grammar, which has had a profound influence on linguistics. He established the Chomsky hierarchy, a classification of formal languages in terms of their generative power. His naturalistic approach to the study of language has affected the philosophy of language and mind.
16
16 October 2, 2015 16 Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/http://www.apu.edu/clas/computerscience/ CS@APU: CS400 Compiler Construction A grammar G is said to be –Regular if it is right linear where each production is of the form A w Bor A w or left linear where each production is of the form A B wor A w –Context free if each production is of the form A where A N and (N T)* –Context sensitive if each production is of the form A where A N, , , (N T)*, | | > 0 –Unrestricted Chomsky Hierarchy: Language Classification
17
17 Chomsky Hierarchy L (regular) L (context free) L (context sensitive) L (unrestricted) October 2, 2015 17 Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/http://www.apu.edu/clas/computerscience/ CS@APU: CS400 Compiler Construction
18
18 Chomsky Hierarchy L (regular) L (context free) L (context sensitive) L (unrestricted) Where L (T) = { L(G) | G is of type T } That is: the set of all languages generated by grammars G of type T L 1 = { a n b n | n 1 } is context free L 2 = { a n b n c n | n 1 } is context sensitive Every finite language is regular! (construct a FSA for strings in L(G)) Examples: October 2, 2015 18 Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/http://www.apu.edu/clas/computerscience/ CS@APU: CS400 Compiler Construction unrestricted context sensitive context free regular
19
19 Parsing Universal (any C-F grammar) –Cocke-Younger-Kasimi –Earley Top-down (C-F grammar with restrictions) –Recursive descent (predictive parsing) –LL (Left-to-right, Leftmost derivation) methods Bottom-up (C-F grammar with restrictions) –Operator precedence parsing –LR (Left-to-right, Rightmost derivation) methods SLR, canonical LR, LALR October 2, 2015 19 Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/http://www.apu.edu/clas/computerscience/ CS@APU: CS400 Compiler Construction
20
20 October 2, 2015 20 Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/http://www.apu.edu/clas/computerscience/ CS@APU: CS400 Compiler Construction Got it with following questions Parser –T–Twofold: –C–Checking syntax –I–Invoking semantic action Error Handling –T–Two abilities: –I–Identifying errors –L–Locating errors Chomsky Hierarchy –F–From Turing machine –t–to Finite Automaton –F–From non to more restriction
21
21 Thank you very much! Questions? October 2, 2015 21 Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/http://www.apu.edu/clas/computerscience/ CS@APU: CS400 Compiler Construction
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.