Part 5 Program Analysis Principles and Techniques


 Ann Craig
 5 years ago
 Views:
Transcription
1 1 Part 5 Program Analysis Principles and Techniques Front end 2 source code scanner tokens parser il errors Responsibilities: Recognize legal programs Report errors Produce il Preliminary storage map Shape the code for the back end Much of front end construction can be automated
2 3 Scanner source code scanner tokens parser il Scanner errors Maps characters into tokens the basic unit of syntax x = x + y; Becomes <id,x> = <id,x> + <id,y> ; character string for token is a lexeme Typical tokens: number, id, +, , *, /, do, end Eliminates white space ( tables, blanks, comments) A key issue is speed use specialized recognizer (lex) 4 Specifying patterns A scanner must recognize various parts of the language s syntax. Some parts are easy: White space some combination of <b> and tab Keywords and operators specified as literal patterns do, end Comments opening and closing delimiters  /* */
3 Specifying patterns 5 Other parts are much harder: Identifiers alphabetic followed by k alphanumerics Special symbols (, $, &, ) Numbers interger 0 or digit from 19 followed by digitals from 09 decimals integer. digits from 09 reals (integer or decimal) E (+ or ) digits from 09 complex ( real, real ) We need a powerful notation to specify these patterns. Definitions 6 Operation Definition Union of L and M written L M Concatenation of L and M written LM Kleene closure of L written L* Positive closure of L written L+ L M = { s s L or s M } LM = {st s L and t M} L* = i=0 L i L+ = i=1 L i
4 Regular expressions 7 Patterns are often specified as regular languages. Notations used to describe a regular language (or a regular set) include both regular expressions and regular grammars. Regular expressions (over an alphabet Σ): 1. is a RE denoting the set { } 2. If a Є Σ, then a is RE denoting {a} Regular expressions 8 Regular expressions (over an alphabet Σ): 3. If r and s are REs, denoting L(r) and L(s), then: (r) is a RE denoting L(r) (r) (s) is a RE denoting L(r) L(s) (r) (s) is a RE denoting L(r) L(s) (r)* is a RE denoting L(r)* (r)+ is a RE denoting L(r)+ If we adopt a precedence for operators, the extra parentheses can go away. We assume closure, then concatenation, the alternation as the order of precedence.
5 9 RE examples identifier letter (a b c z A B C Z) digit ( ) id letter (letter digit)* numbers integer ( +  ) ( ) (digit)* decimal integer. (digit)* real (integer decimal) E (+ ) (digit)* Complex ( real. real ) 10 RE examples Most programming language tokens can be described with regular expressions. We can use regular expressions to automatically build scanners.
6 21 Regular expressions Regular expressions represent languages Languages are sets of strings. Operations include Kleene closure, concatenation, and union. Regular expression (a) (a) (b) (a)(b) (a)* (a)+ Language { a } { a, b } { ab } {, a, aa, } { a, aa, } 22 Regular expressions We assume that closure, concatenation, union as the order of precedence. ab cd* = (ab) (c(d*)) = { ab, c, cd, cdd, } a (bc)* = { a, abc, abcbc, }
7 Recognizers 23 From a regular expression, we can construct a deterministic finite automan (dfa). Recognizer for the identifier: letter digit S0 letter S1 other S2 digit other accept S3 error Recognizers 24 Identifier: letter (a b c z A B C Z) digit ( ) id letter (letter digit)*
8 So what is hard? 25 Language features that can cause problems: Reserved words PL/I had no reserved words if then then then = else; else else = then; Significant blanks FORTRAN and Algol68 ignore blanks do 10 I = 1,25 do 10 I = 1.25 So what is hard? 26 String constants special charaters in strings newline, tab, quote, comment, Finite closures some languages limit identifier lengths adds states to count length FORTRAN 66 6 characters These problems can be swept under the rug (avoided) by intelligent language design.
9 27 Methods and parameters What is a lexical error? 1234G6 Illegal character What should the scanner do? Report the error Try to connect it? Error connection techniques Minimum distance corrections Hard token recovery Skip until match 28 Scanners source code scanner tokens parser IL errors A scanner separates input into tokens based on lexical analysis. Legal tokens are usually specified by regular expressions (REs). Regular expressions specify regular languages.
10 Limits of regular languages 31 Not all languages are regular. You cannot construct dfa s to recognize these languages: L = { p k q k } L = { wcw τ w Σ* } Note: neither of these is a regular expression! (dfa s cannot count!) Limits of regular languages 32 But, this is a little subtle. You can construct dfa s for: Alternating 0 s and 1 s ( 1) (01)* ( 0) Sets of pairs of 0 s and 1 s (01 10)+
11 More regular languages 33 Let s look at another regular language the set of strings containing an even number of zeros and an even number of ones. The regular expression is: Start S2 1 1 S (00 11)* ((01 10)(00 11)*(01 10)(00 11)*)* 1 1 S3 Summary 34 Scanners Break up input into tokens Catch lexical errors Difficulty affected by language design Scanner generators Tokens specified by regular expressions Construct dfa to recognize language Highly efficient in practise
12 The role of the parser 35 source code scanner tokens parser il errors Parser: Perform contextfree syntax anaylsis Guide the contextsensitive anaylsis Construct an intermediate representation Produce meaningful error messages Attempt error correction Syntax analysis Contextfree syntax is specified with a grammar. 36 Formally, a contextfree grammar G is a fourtuple (T, NT, S, P) T is the set of terminal symbols in the grammar. For our purposes, the set of terminals is equivalent to the set of tokens returned by the lexical analyzer.
13 37 Syntax analysis NT is a set of syntactic variables that denote sets of (sub)strings occurring in the language. These are used to impose a structure on the grammar. S is a distinguished nonterminal (S NT) that denotes the entire set of strings in L(G). This is sometimes called a goal symbol. S cannot appear on the right hand side of some production. P is a set of productions that specify the way that terminals and nonterminals can be combined to from strings in the language. Each production must have a single nonterminal on its left hand side. 38 Syntax analysis Grammars are often written in BNF, or BackusNaur Form 1. <goal> ::= <expr> 2. <expr> ::= <expr> <op> <expr> 3. number 4. id 5. <op> ::= * 8. /
14 Syntax analysis 39 This grammar gives simple expressions over numbers and identifiers. In a BNF for a grammar, we represent Nonterminal with brackets or capital letters, Terminals with typewriter font or underline, Productions as in example Why use contextfree grammars? 40 Many advantages: Precise syntactic specification of a programming language Easy to understand, avoids ad hoc definition Easier to maintain, add new language features Can automatically construct efficient parser Parser construction reveals ambiguity, other difficulties Imparts structure to language Supports syntaxdirected translation
15 Grammars for regular languages 41 Can we place a restriction on the form of grammar to ensure that it describes a regular language? Provable fact: For any RE r, there is a grammar g such, that L(r) = L(g) The grammar that generate regular sets are called regular grammar Grammars for regular languages 42 Defintion: In a regular grammar, all productions have one of two forms: 1. A aa 2. A a Where A is a nonterminal and a is a terminal symbol. These are also called type 3 grammars (Chlomsky).
16 Scanning vs. parsing 43 Where do we draw the line? term [azaz] ( [ azaz ] [09 ] )* 0 [19 ] [09 ] * op +  * / expr (term op)* term Regular expressions are use to classify. identifiers, numbers, keywords Scanning vs. parsing 44 Contextfree grammars are used to count. Brackets ( ), begin end, if then else, imposing structure  expressions Grammar for cc has 188 productions.
17 47 Derivations We can view the productions of a cfg as rewriting rules. Using a example: <goal> <expr> <expr> <op> <expr> <expr> <op> <expr> <op> <expr> <id,x> <op> <expr> <op> <expr> <id,x> + <expr> <op> <expr> <id,x> + <num,2> <op> <expr> <id,x> + <num,2> * <expr> <id,x> + <num,2> * <id,y> 48 Derivations We have derived the sentence x + 2 * y We denote this <goal> id + num * id. Such a sequence of rewrites is a derivation or a parse. The process of discovering a derivation is called parsing.
18 49 Derivations At each step, we chose a nonterminal to replace. This choice can lead to different derivations. Two are of particular interest Leftmost derivation the leftmost nonterminal is replaced at each step Rightmost derivation the rightmost nonterminal is replaced at each step. The example was a leftmost derivation. 50 For the string x y: Rightmost Derivation <goal> <expr> <expr> <op> <expr> <expr> <op> <id,y> <expr> * <id,y> <expr> <op> <expr> * <id,y> <expr> <op> <num,2> * <id,y> <expr> + <num,2> * <id,y> <term> + <num,2> * <id,y> <id,x> + <num,2> * <id,y> Again, <goal>... id + num * id.
19 Parse tree 51 Let s look at the parse tree goal Treewalk evaluation would give the wrong answer. (x + 2) * y instead of x + (2 * y) expr <id,x> expr op term + <num,2> expr op * expr <id,y> Precedence 52 These two derivations point out a problem with the grammar. The grammar has no notion of precedence, or implied order of evaluation.
20 53 Precedence To add precedence takes additional machinery 1. <goal> ::= <expr> 2. <expr> ::= <expr> + <term> 3. <expr>  <term> 4. <term> 5. <term> ::= <term> * <factor> 6. <term> / < factor> 7. <factor> 8. <factor> ::= number 9. id 54 Precedence This grammar enforces a precedence on the derivation termsmust be derived from expressions forces the "correct" tree
21 Precedence 55 Now, for the string x + 2 * y: <goal> <expr> <expr> + <term> <expr> + <term> * <factor> <expr> + <term> * <id,y> <expr> + <factor> * <id,y> <expr> + <num,2> * <id,y> <term> + <num,2> * <id,y> <factor> + <num,2> * <id,y> <id,x> + <num,2> * <id,y> Again, <goal>... id + num * id, but this time, we build the desired tree. Precedence 56 This time, we get the desired parse tree. Treewalk evaluation computes x + (2 * y). <id,x> <num,2>
22 57 Ambiguity If a grammar has multiple leftmost derivations for a single (terminal) word of the language, the grammar is ambiguous. Similarly, a grammar with multiple rightmost derivations for a single (terminal) word of the language is ambiguous. Example <stmt> ::= if <expr> then <stmt> if <expr> then <stmt> else <stmt> other stmts 58 Ambiguity Consider deriving the sentential form: ife 1 then if E 2 then S 1 else S 2 It has two derivations. This ambiguity is purely grammatical. It is a contextfree ambiguity.
23 Ambiguity 59 We may be able to eliminate ambiguities by rearranging the grammar. <stmt> ::= <ms> <us> <ms> ::= if <expr> then <ms> else <ms> other stmts <us> ::= if <expr> then <stmt> if <expr> then <ms> else <us> Ambiguity 60 This grammar generates the same language as the ambiguous grammar, but applies the common sense rule match each else with the closest unmatched then This is pretty clearly the language designer's intent.
24 Ambiguity 61 Ambiguity generally refers to a confusion in the contextfree specification. Contextsensitive confusions can arise from overloading. a = f(17) In many Algollike languages, f could be either a function or a subscripted variable. Ambiguity 62 Disambiguating this Statement requires context. need values of declarations not context free really an issue of type Rather than complicate parsing, we will handle this separately.
25 66 Summary: Parser Parser performs contextfree syntax analysis Recognizes structure over set of tokens Deals with ambiguity and precedences
Parsing. 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 informationIntroduction to Parsing
Introduction to Parsing The Front End Source code Scanner tokens Parser IR Errors Parser Checks the stream of words and their parts of speech (produced by the scanner) for grammatical correctness Determines
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 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 informationEECS 6083 Intro to Parsing Context Free Grammars
EECS 6083 Intro to Parsing Context Free Grammars Based on slides from text web site: Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. 1 Parsing sequence of tokens parser
More informationCPSC 434 Lecture 3, Page 1
Front end source code tokens scanner parser il errors Responsibilities: recognize legal procedure report errors produce il preliminary storage map shape the code for the back end Much of front end construction
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 informationLexical Analysis. Introduction
Lexical Analysis Introduction Copyright 2015, Pedro C. Diniz, all rights reserved. Students enrolled in the Compilers class at the University of Southern California have explicit permission to make copies
More informationRegular Expressions. Agenda for Today. Grammar for a Tiny Language. Programming Language Specifications
Agenda for Today Regular Expressions CSE 413, Autumn 2005 Programming Languages Basic concepts of formal grammars Regular expressions Lexical specification of programming languages Using finite automata
More informationFormal Languages and Grammars. Chapter 2: Sections 2.1 and 2.2
Formal Languages and Grammars Chapter 2: Sections 2.1 and 2.2 Formal Languages Basis for the design and implementation of programming languages Alphabet: finite set Σ of symbols String: finite sequence
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 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 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 informationThe Front End. The purpose of the front end is to deal with the input language. Perform a membership test: code source language?
The Front End Source code Front End IR Back End Machine code Errors The purpose of the front end is to deal with the input language Perform a membership test: code source language? Is the program wellformed
More informationParsing II Topdown parsing. Comp 412
COMP 412 FALL 2018 Parsing II Topdown parsing Comp 412 source code IR Front End Optimizer Back End IR target code Copyright 2018, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled
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 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 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 informationLexical Analysis. Dragon Book Chapter 3 Formal Languages Regular Expressions Finite Automata Theory Lexical Analysis using Automata
Lexical Analysis Dragon Book Chapter 3 Formal Languages Regular Expressions Finite Automata Theory Lexical Analysis using Automata Phase Ordering of FrontEnds Lexical analysis (lexer) Break input string
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 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 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 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 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 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 informationCSE 413 Programming Languages & Implementation. Hal Perkins Autumn 2012 Grammars, Scanners & Regular Expressions
CSE 413 Programming Languages & Implementation Hal Perkins Autumn 2012 Grammars, Scanners & Regular Expressions 1 Agenda Overview of language recognizers Basic concepts of formal grammars Scanner Theory
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 informationCS 314 Principles of Programming Languages. Lecture 3
CS 314 Principles of Programming Languages Lecture 3 Zheng Zhang Department of Computer Science Rutgers University Wednesday 14 th September, 2016 Zheng Zhang 1 CS@Rutgers University Class Information
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 informationChapter 3: CONTEXTFREE GRAMMARS AND PARSING Part 1
Chapter 3: CONTEXTFREE GRAMMARS AND PARSING Part 1 1. Introduction Parsing is the task of Syntax Analysis Determining the syntax, or structure, of a program. The syntax is defined by the grammar rules
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 information2. Lexical Analysis! Prof. O. Nierstrasz!
2. Lexical Analysis! Prof. O. 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 informationCS Lecture 2. The Front End. Lecture 2 Lexical Analysis
CS 1622 Lecture 2 Lexical Analysis CS 1622 Lecture 2 1 Lecture 2 Review of last lecture and finish up overview The first compiler phase: lexical analysis Reading: Chapter 2 in text (by 1/18) CS 1622 Lecture
More informationCS 315 Programming Languages Syntax. Parser. (Alternatively handbuilt) (Alternatively handbuilt)
Programming languages must be precise Remember instructions This is unlike natural languages CS 315 Programming Languages Syntax Precision is required for syntax think of this as the format of the language
More informationCSEP 501 Compilers. Languages, Automata, Regular Expressions & Scanners Hal Perkins Winter /8/ Hal Perkins & UW CSE B1
CSEP 501 Compilers Languages, Automata, Regular Expressions & Scanners Hal Perkins Winter 2008 1/8/2008 200208 Hal Perkins & UW CSE B1 Agenda Basic concepts of formal grammars (review) Regular expressions
More informationLexical Analysis (ASU Ch 3, Fig 3.1)
Lexical Analysis (ASU Ch 3, Fig 3.1) Implementation by hand automatically ((F)Lex) Lex generates a finite automaton recogniser uses regular expressions Tasks remove white space (ws) display source program
More informationLanguages and Compilers
Principles of Software Engineering and Operational Systems Languages and Compilers SDAGE: Level I 201213 3. Formal Languages, Grammars and Automata Dr Valery Adzhiev vadzhiev@bournemouth.ac.uk Office:
More informationCSE 413 Programming Languages & Implementation. Hal Perkins Winter 2019 Grammars, Scanners & Regular Expressions
CSE 413 Programming Languages & Implementation Hal Perkins Winter 2019 Grammars, Scanners & Regular Expressions 1 Agenda Overview of language recognizers Basic concepts of formal grammars Scanner Theory
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 informationMIT Specifying Languages with Regular Expressions and ContextFree Grammars. Martin Rinard Massachusetts Institute of Technology
MIT 6.035 Specifying Languages with Regular essions and ContextFree Grammars Martin Rinard Massachusetts Institute of Technology Language Definition Problem How to precisely define language Layered structure
More informationMIT Specifying Languages with Regular Expressions and ContextFree Grammars
MIT 6.035 Specifying Languages with Regular essions and ContextFree Grammars Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology Language Definition Problem How to precisely
More informationDr. D.M. Akbar Hussain
Syntax Analysis Parsing Syntax Or Structure Given By Determines Grammar Rules Context Free Grammar 1 Context Free Grammars (CFG) Provides the syntactic structure: A grammar is quadruple (V T, V N, S, R)
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 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 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 informationFormal Languages and Compilers Lecture V: Parse Trees and Ambiguous Gr
Formal Languages and Compilers Lecture V: Parse Trees and Ambiguous Grammars Free University of BozenBolzano Faculty of Computer Science POS Building, Room: 2.03 artale@inf.unibz.it http://www.inf.unibz.it/
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 informationFront End. Hwansoo Han
Front nd Hwansoo Han Traditional Twopass 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 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 informationAcademic Formalities. CS Language Translators. Compilers A Sangam. What, When and Why of Compilers
Academic Formalities CS3300  Language Translators Introduction V. Krishna Nandivada IIT Madras Written assignments = 20 marks. Midterm = 30 marks, Final = 50 marks. Extra marks During the lecture time
More informationSyntactic Analysis. CS345H: Programming Languages. Lecture 3: Lexical Analysis. Outline. Lexical Analysis. What is a Token? Tokens
Syntactic Analysis CS45H: Programming Languages Lecture : Lexical Analysis Thomas Dillig Main Question: How to give structure to strings Analogy: Understanding an English sentence First, we separate a
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 informationCS 314 Principles of Programming Languages
CS 314 Principles of Programming Languages Lecture 2: Syntax Analysis Zheng (Eddy) Zhang Rutgers University January 22, 2018 Announcement First recitation starts this Wednesday Homework 1 will be release
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 informationCMPS Programming Languages. Dr. Chengwei Lei CEECS California State University, Bakersfield
CMPS 3500 Programming Languages Dr. Chengwei Lei CEECS California State University, Bakersfield Chapter 3 Describing Syntax and Semantics Chapter 3 Topics Introduction The General Problem of Describing
More informationIntroduction to Compiler
Formal Languages and Compiler (CSE322) Introduction to Compiler Jungsik Choi chjs@khu.ac.kr 2018. 3. 8 Traditional Twopass Compiler Source Front End Back End Compiler Target High level functions Recognize
More informationFormal Languages. Formal Languages
Regular expressions Formal Languages Finite state automata Deterministic Nondeterministic Review of BNF Introduction to Grammars Regular grammars Formal Languages, CS34 Fall2 BGRyder Formal Languages
More informationParsing Part II. (Ambiguity, Topdown parsing, Leftrecursion Removal)
Parsing Part II (Ambiguity, Topdown parsing, Leftrecursion Removal) Ambiguous Grammars Definitions If a grammar has more than one leftmost derivation for a single sentential form, the grammar is ambiguous
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 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 informationTheoretical Part. Chapter one:  What are the Phases of compiler? Answer:
Theoretical Part Chapter one:  What are the Phases of compiler? Six phases Scanner Parser Semantic Analyzer Source code optimizer Code generator Target Code Optimizer Three auxiliary components Literal
More informationConcepts Introduced in Chapter 3. Lexical Analysis. Lexical Analysis Terms. Attributes for Tokens
Concepts Introduced in Chapter 3 Lexical Analysis Regular Expressions (REs) Nondeterministic Finite Automata (NFA) Converting an RE to an NFA Deterministic Finite Automatic (DFA) Lexical Analysis Why separate
More informationGoals for course What is a compiler and briefly how does it work? Review everything you should know from 330 and before
CMSC 430 Today Goals for course What is a compiler and briefly how does it work? Review everything you should know from 330 and before > This looks like a lot of material (and it is), but most of this
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 2007
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 informationLexical Analysis  An Introduction. Lecture 4 Spring 2005 Department of Computer Science University of Alabama Joel Jones
Lexical Analysis  An Introduction Lecture 4 Spring 2005 Department of Computer Science University of Alabama Joel Jones Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved.
More informationIntroduction to Lexical Analysis
Introduction to Lexical Analysis Outline Informal sketch of lexical analysis Identifies tokens in input string Issues in lexical analysis Lookahead Ambiguities Specifying lexers Regular expressions Examples
More informationLecture 4: Syntax Specification
The University of North Carolina at Chapel Hill Spring 2002 Lecture 4: Syntax Specification Jan 16 1 Phases of Compilation 2 1 Syntax Analysis Syntax: Webster s definition: 1 a : the way in which linguistic
More informationLexical Analysis. Lexical analysis is the first phase of compilation: The file is converted from ASCII to tokens. It must be fast!
Lexical Analysis Lexical analysis is the first phase of compilation: The file is converted from ASCII to tokens. It must be fast! Compiler Passes Analysis of input program (frontend) character stream
More informationChapter 2 :: Programming Language Syntax
Chapter 2 :: Programming Language Syntax Michael L. Scott kkman@sangji.ac.kr, 2015 1 Regular Expressions A regular expression is one of the following: A character The empty string, denoted by Two regular
More informationFormal Languages and Compilers Lecture VI: Lexical Analysis
Formal Languages and Compilers Lecture VI: Lexical Analysis Free University of BozenBolzano Faculty of Computer Science POS Building, Room: 2.03 artale@inf.unibz.it http://www.inf.unibz.it/ artale/ Formal
More informationCS321 Languages and Compiler Design I. Winter 2012 Lecture 4
CS321 Languages and Compiler Design I Winter 2012 Lecture 4 1 LEXICAL ANALYSIS Convert source file characters into token stream. Remove contentfree characters (comments, whitespace,...) Detect lexical
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 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 informationUNIT 2 LEXICAL ANALYSIS
OVER VIEW OF LEXICAL ANALYSIS UNIT 2 LEXICAL ANALYSIS o To identify the tokens we need some method of describing the possible tokens that can appear in the input stream. For this purpose we introduce
More informationChapter 2  Programming Language Syntax. September 20, 2017
Chapter 2  Programming Language Syntax September 20, 2017 Specifying Syntax: Regular expressions and contextfree grammars Regular expressions are formed by the use of three mechanisms Concatenation Alternation
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 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 informationHabanero Extreme Scale Software Research Project
Habanero Extreme Scale Software Research Project Comp215: Grammars Zoran Budimlić (Rice University) Grammar, which knows how to control even kings  Moliere So you know everything about regular expressions
More information2.2 Syntax Definition
42 CHAPTER 2. A SIMPLE SYNTAXDIRECTED TRANSLATOR sequence of "threeaddress" instructions; a more complete example appears in Fig. 2.2. This form of intermediate code takes its name from instructions
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 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 informationIntroduction to Lexical Analysis
Introduction to Lexical Analysis Outline Informal sketch of lexical analysis Identifies tokens in input string Issues in lexical analysis Lookahead Ambiguities Specifying lexical analyzers (lexers) Regular
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 informationConcepts. Lexical scanning Regular expressions DFAs and FSAs Lex. Lexical analysis in perspective
Concepts Lexical scanning Regular expressions DFAs and FSAs Lex CMSC 331, Some material 1998 by Addison Wesley Longman, Inc. 1 CMSC 331, Some material 1998 by Addison Wesley Longman, Inc. 2 Lexical analysis
More informationCS415 Compilers. Lexical Analysis
CS415 Compilers Lexical Analysis These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University Lecture 7 1 Announcements First project and second homework
More informationChapter 4. Syntax  the form or structure of the expressions, statements, and program units
Syntax  the form or structure of the expressions, statements, and program units Semantics  the meaning of the expressions, statements, and program units Who must use language definitions? 1. Other language
More information1 Lexical Considerations
Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science 6.035, Spring 2013 Handout Decaf Language Thursday, Feb 7 The project for the course is to write a compiler
More informationChapter 3. Describing Syntax and Semantics ISBN
Chapter 3 Describing Syntax and Semantics ISBN 0321493621 Chapter 3 Topics Introduction The General Problem of Describing Syntax Formal Methods of Describing Syntax Copyright 2009 AddisonWesley. All
More informationSyntax. A. Bellaachia Page: 1
Syntax 1. Objectives & Definitions... 2 2. Definitions... 3 3. Lexical Rules... 4 4. BNF: Formal Syntactic rules... 6 5. Syntax Diagrams... 9 6. EBNF: Extended BNF... 10 7. Example:... 11 8. BNF Statement
More informationChapter 3. Describing Syntax and Semantics
Chapter 3 Describing Syntax and Semantics Chapter 3 Topics Introduction The General Problem of Describing Syntax Formal Methods of Describing Syntax Attribute Grammars Describing the Meanings of Programs:
More informationLanguages, Automata, Regular Expressions & Scanners. Winter /8/ Hal Perkins & UW CSE B1
CSE 401 Compilers Languages, Automata, Regular Expressions & Scanners Hal Perkins Winter 2010 1/8/2010 200210 Hal Perkins & UW CSE B1 Agenda Quick review of basic concepts of formal grammars Regular
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 informationAcademic Formalities. CS Modern Compilers: Theory and Practise. Images of the day. What, When and Why of Compilers
Academic Formalities CS6013  Modern Compilers: Theory and Practise Introduction V. Krishna Nandivada IIT Madras Written assignment = 5 marks. Programming assignments = 40 marks. Midterm = 25 marks, Final
More informationCSE 401 Midterm Exam 11/5/10
Name There are 5 questions worth a total of 100 points. Please budget your time so you get to all of the questions. Keep your answers brief and to the point. The exam is closed books, closed notes, closed
More informationThis book is licensed under a Creative Commons Attribution 3.0 License
6. Syntax Learning objectives: syntax and semantics syntax diagrams and EBNF describe contextfree grammars terminal and nonterminal symbols productions definition of EBNF by itself parse tree grammars
More informationCSE P 501 Compilers. Parsing & ContextFree Grammars Hal Perkins Winter /15/ Hal Perkins & UW CSE C1
CSE P 501 Compilers Parsing & ContextFree Grammars Hal Perkins Winter 2008 1/15/2008 200208 Hal Perkins & UW CSE C1 Agenda for Today Parsing overview Context free grammars Ambiguous grammars Reading:
More informationCOMP421 Compiler Design. Presented by Dr Ioanna Dionysiou
COMP421 Compiler Design Presented by Dr Ioanna Dionysiou Administrative! [ALSU03] Chapter 3  Lexical Analysis Sections 3.13.4, 3.63.7! Reading for next time [ALSU03] Chapter 3 Copyright (c) 2010 Ioanna
More informationCSE 311 Lecture 21: ContextFree Grammars. Emina Torlak and Kevin Zatloukal
CSE 311 Lecture 21: ContextFree Grammars Emina Torlak and Kevin Zatloukal 1 Topics Regular expressions A brief review of Lecture 20. Contextfree grammars Syntax, semantics, and examples. 2 Regular expressions
More informationChapter 4. Lexical analysis. Concepts. Lexical scanning Regular expressions DFAs and FSAs Lex. Lexical analysis in perspective
Chapter 4 Lexical analysis Lexical scanning Regular expressions DFAs and FSAs Lex Concepts CMSC 331, Some material 1998 by Addison Wesley Longman, Inc. 1 CMSC 331, Some material 1998 by Addison Wesley
More information