Outline. Limitations of regular languages Parser overview Context-free grammars (CFG s) Derivations Syntax-Directed Translation
|
|
- Scot Gordon
- 6 years ago
- Views:
Transcription
1 Outline Introduction to Parsing Lecture 8 Adapted from slides by G. Necula and R. Bodik Limitations of regular languages Parser overview Context-free grammars (CG s) Derivations Syntax-Directed ranslation 2/8/2008 Prof. Hilfinger CS164 Lecture 8 1 2/8/2008 Prof. Hilfinger CS164 Lecture 8 2 Languages and Automata ormal languages are very important in CS specially in programming languages Regular languages he weakest formal languages widely used Many applications We will also study context-free languages Limitations of Regular Languages Intuition: A finite automaton that runs long enough must repeat states inite automaton can t remember # of times it has visited a particular state inite automaton has finite memory Only enough to store in which state it is Cannot count, except up to a finite limit.g., language of balanced parentheses is not regular: { ( i ) i i 0} 2/8/2008 Prof. Hilfinger CS164 Lecture 8 3 2/8/2008 Prof. Hilfinger CS164 Lecture 8 4 he Structure of a Compiler he unctionality of the Parser Source Lexical analysis okens Input: sequence of tokens from lexer Output: abstract syntax tree of the program oday we start Parsing Optimization Interm. Language Code Gen. Machine Code 2/8/2008 Prof. Hilfinger CS164 Lecture 8 5 2/8/2008 Prof. Hilfinger CS164 Lecture 8 6 1
2 xample Pyth: if x == y: z =1 else: z = 2 Parser input: I ID == ID : ID = IN LS : ID = IN Parser output (abstract syntax tree): I-HN-LS == = = ID ID ID IN ID IN 2/8/2008 Prof. Hilfinger CS164 Lecture 8 7 Why A ree? ach stage of the compiler has two purposes: Detect and filter out some class of errors Compute some new information or translate the representation of the program to make things easier for later stages Recursive structure of tree suits recursive structure of language definition With tree, later stages can easily find the else clause, e.g., rather than having to scan through tokens to find it. 2/8/2008 Prof. Hilfinger CS164 Lecture 8 8 Comparison with Lexical Analysis he Role of the Parser Phase Lexer Parser Input Sequence of characters Sequence of tokens Output Sequence of tokens Syntax tree Not all sequences of tokens are programs Parser must distinguish between valid and invalid sequences of tokens We need A language for describing valid sequences of tokens A method for distinguishing valid from invalid sequences of tokens 2/8/2008 Prof. Hilfinger CS164 Lecture 8 9 2/8/2008 Prof. Hilfinger CS164 Lecture 8 10 Programming Language Structure Programming languages have recursive structure Consider the language of arithmetic expressions with egers,,, and ( ) An expression is either: an eger an expression followed by followed by expression an expression followed by followed by expression a ( followed by an expression followed by ),, ( ) are expressions Notation for Programming Languages An alternative notation: ( ) We can view these rules as rewrite rules We start with and replace occurrences of with some right-hand side ( ) ( ) ( ) 2/8/2008 Prof. Hilfinger CS164 Lecture /8/2008 Prof. Hilfinger CS164 Lecture
3 Observation All arithmetic expressions can be obtained by a sequence of replacements Any sequence of replacements forms a valid arithmetic expression his means that we cannot obtain ( ) ) by any sequence of replacements. Why? his set of rules is a context-free grammar 2/8/2008 Prof. Hilfinger CS164 Lecture 8 13 Context-ree Grammars A CG consists of A set of non-terminals N By convention, written with capital letter in these notes A set of terminals By convention, either lower case names or punctuation A start symbol S (a non-terminal) A set of productions Assuming N ε Y 1 Y 2... Y n, or where Y i N 2/8/2008 Prof. Hilfinger CS164 Lecture 8 14 xamples of CGs Simple arithmetic expressions: ( ) One non-terminal: Several terminals:,,, (, ) Called terminals because they are never replaced By convention the non-terminal for the first production is the start one he Language of a CG Read productions as replacement rules: X Y 1... Y n Means X can be replaced by Y 1... Y n X ε Means X can be erased (replaced with empty string) 2/8/2008 Prof. Hilfinger CS164 Lecture /8/2008 Prof. Hilfinger CS164 Lecture 8 16 Key Idea 1. Begin with a string consisting of the start symbol S 2. Replace any non-terminal X in the string by a right-hand side of some production X Y 1 Y n 3. Repeat (2) until there are only terminals in the string 4. he successive strings created in this way are called sentential forms. he Language of a CG (Cont.) More formally, may write X 1 X i-1 X i X i1 X n X 1 X i-1 Y 1 Y m X i1 X n if there is a production X i Y 1 Y m 2/8/2008 Prof. Hilfinger CS164 Lecture /8/2008 Prof. Hilfinger CS164 Lecture
4 he Language of a CG (Cont.) Write X 1 X n Y 1 Y m if X 1 X n Y 1 Y m in 0 or more steps he Language of a CG Let G be a context-free grammar with start symbol S. hen the language of G is: L(G) = { a 1 a n S a 1 a n and every a i is a terminal } 2/8/2008 Prof. Hilfinger CS164 Lecture /8/2008 Prof. Hilfinger CS164 Lecture 8 20 xamples: S 0 also written as S 0 1 S 1 Generates the language { 0, 1 } What about S 1 A A 0 1 What about S 1 A A 0 1 A What about S ε ( S ) Pyth xample A fragment of Pyth: Compound while xpr: Block if xpr: Block lses lses ε else: Block elif xpr: Block lses Block Stmt_List Suite (ormal language papers use one-character nonterminals, but we don t have to!) 2/8/2008 Prof. Hilfinger CS164 Lecture /8/2008 Prof. Hilfinger CS164 Lecture 8 22 Derivations and Parse rees Derivation xample A derivation is a sequence of sentential forms resulting from the application of a sequence of productions S A derivation can be represented as a parse tree Start symbol is the tree s root or a production X Y 1 Y n add children Y 1,, Y n to node X Grammar () String 2/8/2008 Prof. Hilfinger CS164 Lecture /8/2008 Prof. Hilfinger CS164 Lecture
5 Derivation xample (Cont.) Derivation in Detail (1) 2/8/2008 Prof. Hilfinger CS164 Lecture /8/2008 Prof. Hilfinger CS164 Lecture 8 26 Derivation in Detail (2) Derivation in Detail (3) 2/8/2008 Prof. Hilfinger CS164 Lecture /8/2008 Prof. Hilfinger CS164 Lecture 8 28 Derivation in Detail (4) Derivation in Detail (5) 2/8/2008 Prof. Hilfinger CS164 Lecture /8/2008 Prof. Hilfinger CS164 Lecture
6 Derivation in Detail (6) Notes on Derivations A parse tree has erminals at the leaves Non-terminals at the erior nodes A left-right traversal of the leaves is the original input he parse tree shows the association of operations, the input string does not! here may be multiple ways to match the input Derivations (and parse trees) choose one 2/8/2008 Prof. Hilfinger CS164 Lecture /8/2008 Prof. Hilfinger CS164 Lecture 8 32 he Payoff: parser as a translator syntax-directed translation Mechanism of syntax-directed translation syntax-directed translation is done by extending the CG a translation rule is defined for each production stream of tokens parser syntax translation rules (typically hardcoded in the parser) ASs, or assembly code given X d A B c the translation of X is defined recursively using translation of nonterminals A, B values of attributes of terminals d, c constants 2/8/2008 Prof. Hilfinger CS164 Lecture /8/2008 Prof. Hilfinger CS164 Lecture 8 34 o translate an input string: xample 1: Arithmetic expression to value 1. Build the parse tree. 2. Working bottom-up Use the translation rules to compute the translation of each nonterminal in the tree Result: the translation of the string is the translation of the parse tree's root nonterminal. Why bottom up? a nonterminal's value may depend on the value of the symbols on the right-hand side, so translate a non-terminal node only after children translations are available. Syntax-directed translation rules: 1.trans = 2.trans.trans.trans =.trans 1.trans = 2.trans.trans.trans =.trans.trans =.value ( ).trans =.trans 2/8/2008 Prof. Hilfinger CS164 Lecture /8/2008 Prof. Hilfinger CS164 Lecture
7 xample 1: Bison/Yacc Notation xample 1 (cont): Annotated Parse ree : { $$ = $1 $3; } : { $$ = $1 $3; } : { $$ = $1; } : ( ) { $$ = $2; } KY: $$ : Semantic value of left-hand side $n : Semantic value of n th symbol on right-hand side 2/8/2008 Prof. Hilfinger CS164 Lecture 8 37 Input: 2 (4 5) (18) (18) (2) (9) ( (9) (2) (2) (4) (5) (4) (5) (4) (5) 2/8/2008 Prof. Hilfinger CS164 Lecture 8 (4) 38 ) xample 2: Compute the type of an expression xample 2 (cont) -> if $1 == IN and $3 == IN: $$ = IN else: $$ = RROR -> and if $1 == BOOL and $3 == BOOL: $$ = BOOL else: $$ = RROR -> == if $1 == $3 and $2!= RROR: $$ = BOOL else: $$ = RROR -> true $$ = BOOL -> false $$ = BOOL -> $$ = IN -> ( ) $$ = $2 Input: (2 2) == 4 (IN) (IN) == ( (IN) ) (IN) (BOOL) (IN) (IN) (IN) (IN) 2/8/2008 Prof. Hilfinger CS164 Lecture /8/2008 Prof. Hilfinger CS164 Lecture 8 40 Building Abstract Syntax rees xamples so far, streams of tokens translated o eger values, or types ranslating o ASs is not very different 2/8/2008 Prof. Hilfinger CS164 Lecture 8 41 AS vs. Parse ree AS is condensed form of a parse tree operators appear at ernal nodes, not at leaves. "Chains" of single productions are collapsed. Lists are "flattened". Syntactic details are omitted e.g., parentheses, commas, semi-colons AS is a better structure for later compiler stages omits details having to do with the source language, only contains information about the essential structure of the program. 2/8/2008 Prof. Hilfinger CS164 Lecture
8 xample: 2 (4 5) Parse tree vs. AS AS-building translation rules (2) ( (4) ) (5) 2/8/2008 Prof. Hilfinger CS164 Lecture $$ = new PlusNode($1, $3) $$ = $1 $$ = new imesnode($1, $3) $$ = $1 $$ = new IntLitNode($1) ( ) $$ = $2 2/8/2008 Prof. Hilfinger CS164 Lecture 8 44 xample: 2 (4 5): Steps in Creating AS (2) 2 (Only some of the 4 5 semantic values are shown) ( ) (4) (5) 2/8/2008 Prof. Hilfinger CS164 Lecture 8 45 Leftmost and Rightmost Derivations Leftmost derivation: always act on leftmost non-terminal Rightmost derivation: always act on rightmost non-terminal 2/8/2008 Prof. Hilfinger CS164 Lecture 8 46 rightmost Derivation in Detail (1) rightmost Derivation in Detail (2) 2/8/2008 Prof. Hilfinger CS164 Lecture /8/2008 Prof. Hilfinger CS164 Lecture
9 rightmost Derivation in Detail (3) rightmost Derivation in Detail (4) 2/8/2008 Prof. Hilfinger CS164 Lecture /8/2008 Prof. Hilfinger CS164 Lecture 8 50 rightmost Derivation in Detail (5) rightmost Derivation in Detail (6) 2/8/2008 Prof. Hilfinger CS164 Lecture /8/2008 Prof. Hilfinger CS164 Lecture 8 52 Aside: Canonical Derivations ake a look at that last derivation in reverse. he active part (red) tends to move left to right. We call this a reverse rightmost or canonical derivation. Comes up in bottom-up parsing. We ll return to it in a couple of lectures. Derivations and Parse rees or each parse tree there is exactly one leftmost and one rightmost derivation he difference is the order in which branches are added, not the structure of the tree. 2/8/2008 Prof. Hilfinger CS164 Lecture /8/2008 Prof. Hilfinger CS164 Lecture
10 Summary of Derivations We are not just erested in whether s L(G) Also need derivation (or parse tree) and AS. Parse trees slavishly reflect the grammar. Abstract syntax trees abstract from the grammar, cutting out detail that erferes with later stages. A derivation defines a parse tree But one parse tree may have many derivations Derivations drive translation (to ASs, etc.) Leftmost and rightmost derivations most important in parser implementation 2/8/2008 Prof. Hilfinger CS164 Lecture
Outline. Limitations of regular languages. Introduction to Parsing. Parser overview. Context-free grammars (CFG s)
Outline Limitations of regular languages Introduction to Parsing Parser overview Lecture 8 Adapted from slides by G. Necula Context-free grammars (CFG s) Derivations Languages and Automata Formal languages
More informationIntroduction to Parsing. Lecture 8
Introduction to Parsing Lecture 8 Adapted from slides by G. Necula Outline Limitations of regular languages Parser overview Context-free grammars (CFG s) Derivations Languages and Automata Formal languages
More informationOutline. Parser overview Context-free grammars (CFG s) Derivations Syntax-Directed Translation
Outline Introduction to Parsing (adapted from CS 164 at Berkeley) Parser overview Context-free grammars (CFG s) Derivations Syntax-Directed ranslation he Functionality of the Parser Input: sequence of
More informationSyntax-Directed Translation. Lecture 14
Syntax-Directed Translation Lecture 14 (adapted from slides by R. Bodik) 9/27/2006 Prof. Hilfinger, Lecture 14 1 Motivation: parser as a translator syntax-directed translation stream of tokens parser ASTs,
More informationIntroduction to Parsing. Lecture 5
Introduction to Parsing Lecture 5 1 Outline Regular languages revisited Parser overview Context-free grammars (CFG s) Derivations Ambiguity 2 Languages and Automata Formal languages are very important
More informationOutline. Regular languages revisited. Introduction to Parsing. Parser overview. Context-free grammars (CFG s) Lecture 5. Derivations.
Outline Regular languages revisited Introduction to Parsing Lecture 5 Parser overview Context-free grammars (CFG s) Derivations Prof. Aiken CS 143 Lecture 5 1 Ambiguity Prof. Aiken CS 143 Lecture 5 2 Languages
More informationIntroduction to Parsing Ambiguity and Syntax Errors
Introduction to Parsing Ambiguity and Syntax rrors Outline Regular languages revisited Parser overview Context-free grammars (CFG s) Derivations Ambiguity Syntax errors Compiler Design 1 (2011) 2 Languages
More informationIntroduction to Parsing. Lecture 5
Introduction to Parsing Lecture 5 1 Outline Regular languages revisited Parser overview Context-free grammars (CFG s) Derivations Ambiguity 2 Languages and Automata Formal languages are very important
More informationIntroduction to Parsing Ambiguity and Syntax Errors
Introduction to Parsing Ambiguity and Syntax rrors Outline Regular languages revisited Parser overview Context-free grammars (CFG s) Derivations Ambiguity Syntax errors 2 Languages and Automata Formal
More information( ) i 0. Outline. Regular languages revisited. Introduction to Parsing. Parser overview. Context-free grammars (CFG s) Lecture 5.
Outline Regular languages revisited Introduction to Parsing Lecture 5 Parser overview Context-free grammars (CFG s) Derivations Prof. Aiken CS 143 Lecture 5 1 Ambiguity Prof. Aiken CS 143 Lecture 5 2 Languages
More informationIntroduction to Parsing. Lecture 5. Professor Alex Aiken Lecture #5 (Modified by Professor Vijay Ganesh)
Introduction to Parsing Lecture 5 (Modified by Professor Vijay Ganesh) 1 Outline Regular languages revisited Parser overview Context-free grammars (CFG s) Derivations Ambiguity 2 Languages and Automata
More informationContext-Free Grammars
Context-Free Grammars Lecture 7 http://webwitch.dreamhost.com/grammar.girl/ Outline Scanner vs. parser Why regular expressions are not enough Grammars (context-free grammars) grammar rules derivations
More informationIntro To Parsing. Step By Step
#1 Intro To Parsing Step By Step #2 Self-Test from Last Time Are practical parsers and scanners based on deterministic or non-deterministic automata? How can regular expressions be used to specify nested
More informationProgramming Languages & Translators PARSING. Baishakhi Ray. Fall These slides are motivated from Prof. Alex Aiken: Compilers (Stanford)
Programming Languages & Translators PARSING Baishakhi Ray Fall 2018 These slides are motivated from Prof. Alex Aiken: Compilers (Stanford) Languages and Automata Formal languages are very important in
More informationAmbiguity. Grammar E E + E E * E ( E ) int. The string int * int + int has two parse trees. * int
Administrivia Ambiguity, Precedence, Associativity & op-down 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 informationAmbiguity, Precedence, Associativity & Top-Down Parsing. Lecture 9-10
Ambiguity, Precedence, Associativity & Top-Down Parsing Lecture 9-10 (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 informationBottom-Up Parsing. Lecture 11-12
Bottom-Up Parsing Lecture 11-12 (From slides by G. Necula & R. Bodik) 2/20/08 Prof. Hilfinger CS164 Lecture 11 1 Administrivia Test I during class on 10 March. 2/20/08 Prof. Hilfinger CS164 Lecture 11
More informationSome Basic Definitions. Some Basic Definitions. Some Basic Definitions. Language Processing Systems. Syntax Analysis (Parsing) Prof.
Language Processing Systems Prof. Mohamed Hamada Software ngineering Lab. he University of Aizu Japan Syntax Analysis (Parsing) Some Basic Definitions Some Basic Definitions syntax: the way in which words
More informationBottom-Up Parsing. Lecture 11-12
Bottom-Up Parsing Lecture 11-12 (From slides by G. Necula & R. Bodik) 9/22/06 Prof. Hilfinger CS164 Lecture 11 1 Bottom-Up Parsing Bottom-up parsing is more general than topdown parsing And just as efficient
More informationError Handling Syntax-Directed Translation Recursive Descent Parsing
Announcements rror Handling Syntax-Directed ranslation Lecture 6 PA1 & WA1 Due today at midnight PA2 & WA2 Assigned today Prof. Aiken CS 143 Lecture 6 1 Prof. Aiken CS 143 Lecture 6 2 Outline xtensions
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 informationError Handling Syntax-Directed Translation Recursive Descent Parsing
Announcements rror Handling Syntax-Directed ranslation Lecture 6 PA1 & WA1 Due today at midnight PA2 & WA2 Assigned today Prof. Aiken CS 14 Lecture 6 1 Prof. Aiken CS 14 Lecture 6 2 Outline xtensions of
More informationCOMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou
COMP-421 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 informationsyntax tree - * * * - * * * * * 2 1 * * 2 * (2 * 1) - (1 + 0)
0//7 xpression rees rom last time: we can draw a syntax tree for the Java expression ( 0). 0 ASS, GRAMMARS, PARSING, R RAVRSALS Lecture 3 CS0 all 07 Preorder, Postorder, and Inorder Preorder, Postorder,
More informationGrammars and ambiguity. CS164 3:30-5:00 TT 10 Evans. Prof. Bodik CS 164 Lecture 8 1
Grammars and ambiguity CS164 3:30-5:00 TT 10 vans 1 Overview derivations and parse trees different derivations produce may produce same parse tree ambiguous grammars what they are and how to fix them 2
More informationCSE 401 Midterm Exam Sample Solution 2/11/15
Question 1. (10 points) Regular expression warmup. For regular expression questions, you must restrict yourself to the basic regular expression operations covered in class and on homework assignments:
More informationLexical Analysis. COMP 524, Spring 2014 Bryan Ward
Lexical Analysis COMP 524, Spring 2014 Bryan Ward Based in part on slides and notes by J. Erickson, S. Krishnan, B. Brandenburg, S. Olivier, A. Block and others The Big Picture Character Stream Scanner
More informationLexical Analysis. Lecture 2-4
Lexical Analysis Lecture 2-4 Notes by G. Necula, with additions by P. Hilfinger Prof. Hilfinger CS 164 Lecture 2 1 Administrivia Moving to 60 Evans on Wednesday HW1 available Pyth manual available on line.
More informationWhere We Are. CMSC 330: Organization of Programming Languages. This Lecture. Programming Languages. Motivation for Grammars
CMSC 330: Organization of Programming Languages Context Free Grammars Where We Are Programming languages Ruby OCaml Implementing programming languages Scanner Uses regular expressions Finite automata Parser
More informationCS 314 Principles of Programming Languages
CS 314 Principles of Programming Languages Lecture 5: Syntax Analysis (Parsing) Zheng (Eddy) Zhang Rutgers University January 31, 2018 Class Information Homework 1 is being graded now. The sample solution
More informationA Simple Syntax-Directed Translator
Chapter 2 A Simple Syntax-Directed Translator 1-1 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. Prof. James L. Frankel Harvard University. Version of 6:43 PM 6-Feb-2018 Copyright 2018, 2015 James L. Frankel. All rights reserved.
Syntax Analysis Prof. James L. Frankel Harvard University Version of 6:43 PM 6-Feb-2018 Copyright 2018, 2015 James L. Frankel. All rights reserved. Context-Free Grammar (CFG) terminals non-terminals start
More informationCSE302: Compiler Design
CSE302: Compiler Design Instructor: Dr. Liang Cheng Department of Computer Science and Engineering P.C. Rossin College of Engineering & Applied Science Lehigh University February 20, 2007 Outline Recap
More informationCSCI312 Principles of Programming Languages!
CSCI312 Principles of Programming Languages!! Chapter 3 Regular Expression and Lexer Xu Liu Recap! Copyright 2006 The McGraw-Hill Companies, Inc. Clite: Lexical Syntax! Input: a stream of characters from
More informationE E+E E E (E) id. id + id E E+E. id E + E id id + E id id + id. Overview. derivations and parse trees. Grammars and ambiguity. ambiguous grammars
Overview Grammars and ambiguity CS164 3:30-5:00 TT 10 vans derivations and parse trees dferent derivations produce may produce same parse tree ambiguous grammars what they are and how to fix them 1 2 Recall:
More informationParsing: Derivations, Ambiguity, Precedence, Associativity. Lecture 8. Professor Alex Aiken Lecture #5 (Modified by Professor Vijay Ganesh)
Parsing: Derivations, Ambiguity, Precedence, Associativity Lecture 8 (Modified by Professor Vijay Ganesh) 1 Topics covered so far Regular languages and Finite automaton Parser overview Context-free grammars
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 informationReview of CFGs and Parsing II Bottom-up Parsers. Lecture 5. Review slides 1
Review of CFGs and Parsing II Bottom-up Parsers Lecture 5 1 Outline Parser Overview op-down Parsers (Covered largely through labs) Bottom-up Parsers 2 he Functionality of the Parser Input: sequence of
More informationCMSC 330: Organization of Programming Languages. Architecture of Compilers, Interpreters
: Organization of Programming Languages Context Free Grammars 1 Architecture of Compilers, Interpreters Source Scanner Parser Static Analyzer Intermediate Representation Front End Back End Compiler / Interpreter
More informationCSE P 501 Compilers. Parsing & Context-Free Grammars Hal Perkins Winter /15/ Hal Perkins & UW CSE C-1
CSE P 501 Compilers Parsing & Context-Free Grammars Hal Perkins Winter 2008 1/15/2008 2002-08 Hal Perkins & UW CSE C-1 Agenda for Today Parsing overview Context free grammars Ambiguous grammars Reading:
More informationRecursive Descent Parsers
Recursive Descent Parsers Lecture 7 Robb T. Koether Hampden-Sydney College Wed, Jan 28, 2015 Robb T. Koether (Hampden-Sydney College) Recursive Descent Parsers Wed, Jan 28, 2015 1 / 18 1 Parsing 2 LL Parsers
More informationTHE COMPILATION PROCESS EXAMPLE OF TOKENS AND ATTRIBUTES
THE COMPILATION PROCESS Character stream CS 403: Scanning and Parsing Stefan D. Bruda Fall 207 Token stream Parse tree Abstract syntax tree Modified intermediate form Target language Modified target language
More informationProperties of Regular Expressions and Finite Automata
Properties of Regular Expressions and Finite Automata Some token patterns can t be defined as regular expressions or finite automata. Consider the set of balanced brackets of the form [[[ ]]]. This set
More informationCMSC 330: Organization of Programming Languages
CMSC 330: Organization of Programming Languages Context Free Grammars 1 Architecture of Compilers, Interpreters Source Analyzer Optimizer Code Generator Abstract Syntax Tree Front End Back End Compiler
More informationCSE450 Translation of Programming Languages. Lecture 4: Syntax Analysis
CSE450 Translation of Programming Languages Lecture 4: Syntax Analysis http://xkcd.com/859 Structure of a Today! Compiler Source Language Lexical Analyzer Syntax Analyzer Semantic Analyzer Int. Code Generator
More informationCS 403: Scanning and Parsing
CS 403: Scanning and Parsing Stefan D. Bruda Fall 2017 THE COMPILATION PROCESS Character stream Scanner (lexical analysis) Token stream Parser (syntax analysis) Parse tree Semantic analysis Abstract syntax
More informationCSCE 314 Programming Languages
CSCE 314 Programming Languages Syntactic Analysis Dr. Hyunyoung Lee 1 What Is a Programming Language? Language = syntax + semantics The syntax of a language is concerned with the form of a program: how
More informationAdministrivia. Lexical Analysis. Lecture 2-4. Outline. The Structure of a Compiler. Informal sketch of lexical analysis. Issues in lexical analysis
dministrivia Lexical nalysis Lecture 2-4 Notes by G. Necula, with additions by P. Hilfinger Moving to 6 Evans on Wednesday HW available Pyth manual available on line. Please log into your account and electronically
More informationLecture 8: Deterministic Bottom-Up Parsing
Lecture 8: Deterministic Bottom-Up Parsing (From slides by G. Necula & R. Bodik) Last modified: Fri Feb 12 13:02:57 2010 CS164: Lecture #8 1 Avoiding nondeterministic choice: LR We ve been looking at general
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 informationArchitecture of Compilers, Interpreters. CMSC 330: Organization of Programming Languages. Front End Scanner and Parser. Implementing the Front End
Architecture of Compilers, Interpreters : Organization of Programming Languages ource Analyzer Optimizer Code Generator Context Free Grammars Intermediate Representation Front End Back End Compiler / Interpreter
More informationBuilding Compilers with Phoenix
Building Compilers with Phoenix Syntax-Directed Translation Structure of a Compiler Character Stream Intermediate Representation Lexical Analyzer Machine-Independent Optimizer token stream Intermediate
More informationAbstract Syntax Trees & Top-Down Parsing
Review of Parsing Abstract Syntax Trees & Top-Down 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 informationBottom-Up Parsing II (Different types of Shift-Reduce Conflicts) Lecture 10. Prof. Aiken (Modified by Professor Vijay Ganesh.
Bottom-Up Parsing II Different types of Shift-Reduce Conflicts) Lecture 10 Ganesh. Lecture 10) 1 Review: Bottom-Up Parsing Bottom-up parsing is more general than topdown parsing And just as efficient Doesn
More informationCMSC 330: Organization of Programming Languages
CMSC 330: Organization of Programming Languages Context Free Grammars 1 Architecture of Compilers, Interpreters Source Analyzer Optimizer Code Generator Abstract Syntax Tree Front End Back End Compiler
More informationCompilers and computer architecture From strings to ASTs (2): context free grammars
1 / 1 Compilers and computer architecture From strings to ASTs (2): context free grammars Martin Berger October 2018 Recall the function of compilers 2 / 1 3 / 1 Recall we are discussing parsing Source
More informationLecture 7: Deterministic Bottom-Up Parsing
Lecture 7: Deterministic Bottom-Up Parsing (From slides by G. Necula & R. Bodik) Last modified: Tue Sep 20 12:50:42 2011 CS164: Lecture #7 1 Avoiding nondeterministic choice: LR We ve been looking at general
More informationReview: Shift-Reduce Parsing. Bottom-up parsing uses two actions: Bottom-Up Parsing II. Shift ABC xyz ABCx yz. Lecture 8. Reduce Cbxy ijk CbA ijk
Review: Shift-Reduce Parsing Bottom-up parsing uses two actions: Bottom-Up Parsing II Lecture 8 Shift ABC xyz ABCx yz Reduce Cbxy ijk CbA ijk Prof. Aiken CS 13 Lecture 8 1 Prof. Aiken CS 13 Lecture 8 2
More informationSyntax Analysis Part I
Syntax Analysis Part I Chapter 4: Context-Free Grammars Slides adapted from : Robert van Engelen, Florida State University Position of a Parser in the Compiler Model Source Program Lexical Analyzer Token,
More informationAbstract Syntax Trees & Top-Down Parsing
Abstract Syntax Trees & Top-Down 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 & Top-Down Parsing
Review of Parsing Abstract Syntax Trees & Top-Down 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 information([1-9] 1[0-2]):[0-5][0-9](AM PM)? What does the above match? Matches clock time, may or may not be told if it is AM or PM.
What is the corresponding regex? [2-9]: ([1-9] 1[0-2]):[0-5][0-9](AM PM)? What does the above match? Matches clock time, may or may not be told if it is AM or PM. CS 230 - Spring 2018 4-1 More CFG Notation
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 informationBottom Up Parsing. Shift and Reduce. Sentential Form. Handle. Parse Tree. Bottom Up Parsing 9/26/2012. Also known as Shift-Reduce parsing
Also known as Shift-Reduce parsing More powerful than top down Don t need left factored grammars Can handle left recursion Attempt to construct parse tree from an input string eginning at leaves and working
More informationLexical Analysis. Lecture 3-4
Lexical Analysis Lecture 3-4 Notes by G. Necula, with additions by P. Hilfinger Prof. Hilfinger CS 164 Lecture 3-4 1 Administrivia I suggest you start looking at Python (see link on class home page). Please
More informationParsing. Note by Baris Aktemur: Our slides are adapted from Cooper and Torczon s slides that they prepared for COMP 412 at Rice.
Parsing Note by Baris Aktemur: Our slides are adapted from Cooper and Torczon s slides that they prepared for COMP 412 at Rice. Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students
More informationΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών
ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών Lecture 5a Syntax Analysis lias Athanasopoulos eliasathan@cs.ucy.ac.cy Syntax Analysis Συντακτική Ανάλυση Context-free Grammars (CFGs) Derivations Parse trees
More informationSyntax Analysis. Amitabha Sanyal. (www.cse.iitb.ac.in/ as) Department of Computer Science and Engineering, Indian Institute of Technology, Bombay
Syntax Analysis (www.cse.iitb.ac.in/ as) Department of Computer Science and Engineering, Indian Institute of Technology, Bombay September 2007 College of Engineering, Pune Syntax Analysis: 2/124 Syntax
More informationContext-Free Grammars
Context-Free Grammars 1 Informal Comments A context-free grammar is a notation for describing languages. It is more powerful than finite automata or RE s, but still cannot define all possible languages.
More informationCMSC 330: Organization of Programming Languages. Context Free Grammars
CMSC 330: Organization of Programming Languages Context Free Grammars 1 Architecture of Compilers, Interpreters Source Analyzer Optimizer Code Generator Abstract Syntax Tree Front End Back End Compiler
More informationCMSC 330: Organization of Programming Languages
CMSC 330: Organization of Programming Languages Context Free Grammars and Parsing 1 Recall: Architecture of Compilers, Interpreters Source Parser Static Analyzer Intermediate Representation Front End Back
More informationParsing Part II. (Ambiguity, Top-down parsing, Left-recursion Removal)
Parsing Part II (Ambiguity, Top-down parsing, Left-recursion Removal) Ambiguous Grammars Definitions If a grammar has more than one leftmost derivation for a single sentential form, the grammar is ambiguous
More informationAdministrativia. WA1 due on Thu PA2 in a week. Building a Parser III. Slides on the web site. CS164 3:30-5:00 TT 10 Evans.
Administrativia Building a Parser III CS164 3:30-5: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 informationADTS, GRAMMARS, PARSING, TREE TRAVERSALS
1 Pointers to material ADS, GRAMMARS, PARSING, R RAVRSALS Lecture 13 CS110 all 016 Parse trees: text, section 3.36 Definition of Java Language, sometimes useful: docs.oracle.com/javase/specs/jls/se8/html/index.html
More informationFront End. Hwansoo Han
Front nd Hwansoo Han Traditional Two-pass Compiler Source code Front nd IR Back nd Machine code rrors High level functions Recognize legal program, generate correct code (OS & linker can accept) Manage
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 informationChapter 3: CONTEXT-FREE GRAMMARS AND PARSING Part2 3.3 Parse Trees and Abstract Syntax Trees
Chapter 3: CONTEXT-FREE GRAMMARS AND PARSING Part2 3.3 Parse Trees and Abstract Syntax Trees 3.3.1 Parse trees 1. Derivation V.S. Structure Derivations do not uniquely represent the structure of the strings
More informationCMSC 330: Organization of Programming Languages. Context Free Grammars
CMSC 330: Organization of Programming Languages Context Free Grammars 1 Architecture of Compilers, Interpreters Source Analyzer Optimizer Code Generator Abstract Syntax Tree Front End Back End Compiler
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 informationOptimizing Finite Automata
Optimizing Finite Automata We can improve the DFA created by MakeDeterministic. Sometimes a DFA will have more states than necessary. For every DFA there is a unique smallest equivalent DFA (fewest states
More informationFall Compiler Principles Context-free Grammars Refresher. Roman Manevich Ben-Gurion University of the Negev
Fall 2016-2017 Compiler Principles Context-free Grammars Refresher Roman Manevich Ben-Gurion University of the Negev 1 xample grammar S S ; S S id := S print (L) id num + L L L, shorthand for Statement
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 informationCS5363 Final Review. cs5363 1
CS5363 Final Review cs5363 1 Programming language implementation Programming languages Tools for describing data and algorithms Instructing machines what to do Communicate between computers and programmers
More informationLECTURE 3. Compiler Phases
LECTURE 3 Compiler Phases COMPILER PHASES Compilation of a program proceeds through a fixed series of phases. Each phase uses an (intermediate) form of the program produced by an earlier phase. Subsequent
More informationMIT Specifying Languages with Regular Expressions and Context-Free Grammars
MIT 6.035 Specifying Languages with Regular essions and Context-Free Grammars Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology Language Definition Problem How to precisely
More informationCOP4020 Programming Languages. Syntax Prof. Robert van Engelen
COP4020 Programming Languages Syntax Prof. Robert van Engelen Overview Tokens and regular expressions Syntax and context-free grammars Grammar derivations More about parse trees Top-down and bottom-up
More informationCSE 130 Programming Language Principles & Paradigms Lecture # 5. Chapter 4 Lexical and Syntax Analysis
Chapter 4 Lexical and Syntax Analysis Introduction - Language implementation systems must analyze source code, regardless of the specific implementation approach - Nearly all syntax analysis is based on
More informationLexical and Syntax Analysis. Top-Down Parsing
Lexical and Syntax Analysis Top-Down 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 informationADTS, GRAMMARS, PARSING, TREE TRAVERSALS
1 Prelim 1 2 Where: Kennedy Auditorium When: A-Lib: 5:30-7 Lie-Z: 7:30-9 (unless we explicitly notified you otherwise) ADS, GRAMMARS, PARSING, R RAVRSALS Lecture 13 CS2110 Spring 2016 Pointers to material
More informationFormal Languages and Compilers Lecture VI: Lexical Analysis
Formal Languages and Compilers Lecture VI: Lexical Analysis Free University of Bozen-Bolzano Faculty of Computer Science POS Building, Room: 2.03 artale@inf.unibz.it http://www.inf.unibz.it/ artale/ Formal
More informationLexical and Syntax Analysis
Lexical and Syntax Analysis In Text: Chapter 4 N. Meng, F. Poursardar Lexical and Syntactic Analysis Two steps to discover the syntactic structure of a program Lexical analysis (Scanner): to read the input
More informationDefining syntax using CFGs
Defining syntax using CFGs Roadmap Last time Defined context-free grammar This time CFGs for specifying a language s syntax Language membership List grammars Resolving ambiguity CFG Review G = (N,Σ,P,S)
More informationCSE P 501 Compilers. Parsing & Context-Free Grammars Hal Perkins Spring UW CSE P 501 Spring 2018 C-1
CSE P 501 Compilers Parsing & Context-Free Grammars Hal Perkins Spring 2018 UW CSE P 501 Spring 2018 C-1 Administrivia Project partner signup: please find a partner and fill out the signup form by noon
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 informationCS164: Midterm I. Fall 2003
CS164: Midterm I Fall 2003 Please read all instructions (including these) carefully. Write your name, login, and circle the time of your section. Read each question carefully and think about what s being
More informationSyntax. Syntax. We will study three levels of syntax Lexical Defines the rules for tokens: literals, identifiers, etc.
Syntax Syntax Syntax defines what is grammatically valid in a programming language Set of grammatical rules E.g. in English, a sentence cannot begin with a period Must be formal and exact or there will
More informationMA513: Formal Languages and Automata Theory Topic: Context-free Grammars (CFG) Lecture Number 18 Date: September 12, 2011
MA53: Formal Languages and Automata Theory Topic: Context-free Grammars (CFG) Lecture Number 8 Date: September 2, 20 xercise: Define a context-free grammar that represents (a simplification of) expressions
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 informationprogramming languages need to be precise a regular expression is one of the following: tokens are the building blocks of programs
Chapter 2 :: Programming Language Syntax Programming Language Pragmatics Michael L. Scott Introduction programming languages need to be precise natural languages less so both form (syntax) and meaning
More informationADTS, GRAMMARS, PARSING, TREE TRAVERSALS
3//15 1 AD: Abstract Data ype 2 Just like a type: Bunch of values together with operations on them. Used often in discussing data structures Important: he definition says ntthing about the implementation,
More information