1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 5: Syntax Analysis COMP 144 Programming Language Concepts Spring 2002 Felix Hernandez-Campos

Download 1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 5: Syntax Analysis COMP 144 Programming Language Concepts Spring 2002 Felix Hernandez-Campos

Post on 21-Dec-2015

212 views

Category:

Documents

0 download

Embed Size (px)

TRANSCRIPT

<ul><li> Slide 1 </li> <li> 1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 5: Syntax Analysis COMP 144 Programming Language Concepts Spring 2002 Felix Hernandez-Campos Jan 18 The University of North Carolina at Chapel Hill </li> <li> Slide 2 </li> <li> 2 COMP 144 Programming Language Concepts Felix Hernandez-Campos Review: Compilation/Interpretation Compiler or Interpreter Translation Execution Source Code Target Code Interpre-tation </li> <li> Slide 3 </li> <li> 3 COMP 144 Programming Language Concepts Felix Hernandez-Campos Review: Syntax Analysis Compiler or Interpreter Translation Execution Source Code Specifying the formSpecifying the form of a programming language Tokens Regular Expression Syntax Context-Free Grammars Target Code </li> <li> Slide 4 </li> <li> 4 COMP 144 Programming Language Concepts Felix Hernandez-Campos Phases of Compilation </li> <li> Slide 5 </li> <li> 5 COMP 144 Programming Language Concepts Felix Hernandez-Campos Syntax Analysis Syntax:Syntax: Websters definition: 1 a : the way in which linguistic elements (as words) are put together to form constituents (as phrases or clauses) The syntax of a programming languageThe syntax of a programming language Describes its form Organization of tokens (elements) Context Free Grammars (CFGs) Must be recognizable by compilers and interpreters Parsing LL and LR parsers </li> <li> Slide 6 </li> <li> 6 COMP 144 Programming Language Concepts Felix Hernandez-Campos Context Free Grammars CFGsCFGs Add recursion to regular expressions Nested constructions Notation expression identifier | number | - expression | ( expression ) | ( expression ) | expression operator expression | expression operator expression operator + | - | * | / Terminal symbols Non-terminal symbols Production rule (i.e. substitution rule) terminal symbol terminal and non-terminal symbols </li> <li> Slide 7 </li> <li> 7 COMP 144 Programming Language Concepts Felix Hernandez-Campos Parsers ScannersScanners Task: recognize language tokens Implementation: Deterministic Finite Automaton Transition based on the next character ParsersParsers Task: recognize language syntax (organization of tokens) Implementation: Top-down parsing Bottom-up parsing </li> <li> Slide 8 </li> <li> 8 COMP 144 Programming Language Concepts Felix Hernandez-Campos Parse Trees A parse is graphical representation of a derivationA parse is graphical representation of a derivation ExampleExample </li> <li> Slide 9 </li> <li> 9 COMP 144 Programming Language Concepts Felix Hernandez-Campos Parsing example Example: comma-separated list of identifierExample: comma-separated list of identifier CFG id_list id id_list_tail id_list_tail , id_list_tail id_list_tail ; Parsing A, B, C; </li> <li> Slide 10 </li> <li> 10 COMP 144 Programming Language Concepts Felix Hernandez-Campos Top-down derivation of A, B, C; CFG Left-to-right, Left-most derivation LL(1) parsing </li> <li> Slide 11 </li> <li> 11 COMP 144 Programming Language Concepts Felix Hernandez-Campos Top-down derivation of A, B, C; CFG </li> <li> Slide 12 </li> <li> 12 COMP 144 Programming Language Concepts Felix Hernandez-Campos Bottom-up parsing of A, B, C; CFG Left-to-right, Right-most derivation LR(1) parsing </li> <li> Slide 13 </li> <li> 13 COMP 144 Programming Language Concepts Felix Hernandez-Campos Bottom-up parsing of A, B, C; CFG </li> <li> Slide 14 </li> <li> 14 COMP 144 Programming Language Concepts Felix Hernandez-Campos Bottom-up parsing of A, B, C; CFG </li> <li> Slide 15 </li> <li> 15 COMP 144 Programming Language Concepts Felix Hernandez-Campos Parsing Parsing an arbitrary Context Free GrammarParsing an arbitrary Context Free Grammar O(n 3 ) Too slow for large programs Linear-time parsingLinear-time parsing LL parsers Recognize LL grammar Use a top-down strategy LR parsers Recognize LR grammar Use a bottom-up strategy </li> <li> Slide 16 </li> <li> 16 COMP 144 Programming Language Concepts Felix Hernandez-Campos Hierarchy of Linear Parsers Basic containment relationshipBasic containment relationship All CFGs can be recognized by LR parser Only a subset of all the CFGs can be recognized by LL parsers LL parsing CFGs LR parsing </li> <li> Slide 17 </li> <li> 17 COMP 144 Programming Language Concepts Felix Hernandez-Campos Recursive Descent Parser Example LL(1) grammarLL(1) grammar </li> <li> Slide 18 </li> <li> 18 COMP 144 Programming Language Concepts Felix Hernandez-Campos Recursive Descent Parser Example Outline ofOutline of recursive parser This parser only verifies syntax match is the scanner </li> <li> Slide 19 </li> <li> 19 COMP 144 Programming Language Concepts Felix Hernandez-Campos Recursive Descent Parser Example </li> <li> Slide 20 </li> <li> 20 COMP 144 Programming Language Concepts Felix Hernandez-Campos Recursive Descent Parser Example </li> <li> Slide 21 </li> <li> 21 COMP 144 Programming Language Concepts Felix Hernandez-Campos Recursive Descent Parser Example </li> <li> Slide 22 </li> <li> 22 COMP 144 Programming Language Concepts Felix Hernandez-Campos Semantic Analysis Compiler or Interpreter Translation Execution Source Code Specifying the meaningSpecifying the meaning of a programming language Attribute Grammars Target Code </li> <li> Slide 23 </li> <li> 23 COMP 144 Programming Language Concepts Felix Hernandez-Campos Reading Assignment Scotts Chapter 2Scotts Chapter 2 Section 2.2.2 Section 2.2.3 </li> </ul>