Processadors de Llenguatge II. Rafael Ramirez
|
|
- Sheila Hardy
- 5 years ago
- Views:
Transcription
1 Processadors de Llenguatge II Rafael Ramirez
2 Temas Sintaxis Semántica Verificación formal Compilación de programas Programación Lógica Programación Funcional Programación Concurrente Si hay tiempo Programación Imperativa Programación Orientada a Objectos
3 Evaluación Examen teoría: 60% Practicas/seminarios: 40% Practicas = (practicas + examen)/2 Participación *Se han de aprobar tanto examen (teoria y practicas) como practicas.
4 Libros Programming Languages Design and Implementation Terrence Pratt and Marvin Zelkowitz Prentice Hall (2001) Concepts of Programming Languages Robert Sebesta Addison Wesley (2002) Modern compiler implementation in ML Andrew W. Appel Cambridge University Press (1997)
5 Contacto Clase Prácticas Horas oficina: martes pm Anuncios, documentacion,
6 Prácticas/seminarios Seminarios (3): grupos de 2-4 Prácticas (6): grupos de 2
7 Processadors de Llenguatge II Introducción Pratt 1.2, 1.3.1, 1.3.2
8 Programming Languages Main issues in language design, impl. and usage Syntax -- What a program looks like Semantics -- What a program means programming paradigm Implementation -- How a program executes Verification is the program 100% correct?
9 Programming Languages A programming paradigm is a fundamental style of computer programming for solving specific software engineering problems. Paradigms differ in the concepts and abstractions used to represent the elements of a program (such as objects, functions, variables, constraints...) and the steps that compose a computation (assignation, evaluation,...). A programming language can support multiple paradigms. For example programs written in C++ can be purely procedural, or purely object-oriented, or contain elements of both paradigms. P.e., in object-oriented programming, programmers can think of a program as a collection of interacting objects, while in functional programming a program can be thought of as a sequence of stateless function evaluations.
10 Programming Languages Which language for a specific problem? Pascal, C -- procedural, statement oriented C++, Java, Smalltalk -- Object oriented ML, Lisp -- Functional Prolog -- Logic-based
11 Language expressiveness Existe un programa que puede ser escrito en un lenguaje para el cual no hay realmente programa equivalente en alguno de los otros lenguajes? e.g. existe un programa que puede ser escrito en Prolog para el cual no haya equivalente en Pascal?
12 Language expressiveness Church s thesis Any computable function can be computed by a Turing machine. Why do we need all these different languages?
13 Language Goals 1950s--1960s Compile programs to execute efficiently. Programmers cheap; Machines expensive. Direct connection between language features and hardware - goto statements,... Today CPU power and memory very cheap; programmers expensive. Compile programs that are built efficiently Direct connection between language features and design concepts - encapsulation, inheritance,...
14 Attributes of a good language Clarity, simplicity a language should provide a clear, simple and unified set of concepts to be used as primitives to develop algorithms. Orthogonality -every combination of features is meaningful easier to remember if few exceptions Naturalness for the application - program structure reflects the logical structure of algorithm Support for abstraction - program data reflects problem being solved
15 Attributes of a good language (cont) Ease of program verification - verifying that program correctly performs its required function Programming environment - external support for the language Portability of programs - transportability of the resulting programs from the computer on which they are developed to other computer systems Cost of use - program execution, program translation, program creation, and program maintenance
16 Language paradigms Imperative programming Basic concept is machine state Program consists of a sequence of statements: Stat1; Stat2;... Execution of each statement to change state, i.e. change the value of one or more locations in memory. Functional programming Idea is to find a function that produces the answer, i.e. what is the function that we must apply to the initial state to get the answer. Function composition is major operation: f1(f2(f3(x))) Programming consists of building the function that computes the answer
17 Language paradigms (cont) Logic programming Based on predicate logic State what the problem is, as opposed to how to solve the problem. Execution is deduction. father(x,y), father(y,z) grandfather(x,z) Object-oriented programming Data Objects are built Limited set of functions operate on these data Complex objects are designed as extensions of simpler objects (inheritance) Syntax: Set of objects (classes) containing data and methods.
18 Language paradigms (cont) Concurrent programming Program = set of processes Each process execute sequentially Processes execute concurrently Processes communicate and synchronize their execution. Involve safety and progress properties.
19 Processadors de Llenguatge II Language Syntax Pratt 3.1, 3.3.1
20 Program structure Language = Syntax + Semantics The syntax of a language is concerned with the form of a program: how expressions, commands, declarations etc. are put together to result in the final program. The semantics of a language is concerned with the meaning of a program: how the programs behave when executed on computers.
21 Program structure Just like in natural language: Semantics (Meaning) You have in your mind, to express a conditional, when true do something, when false, do something else. Syntax (Form/Structure) C: if (condition) { part1 } else { part2 } Pascal: if condition begin part1 end else begin part2 end LISP: if (condition) (part1) (part2))
22 Program structure Syntax What a program looks like Useful notations for describing PL s syntax: BNF grammars (context free grammars) Regular grammars Semantics What the program means Static semantics - Semantics determined at compile time: var A: integer; Type and storage for A Dynamic semantics - Semantics determined during execution: X = ``ABC'' X a string; value of X Useful notations: Informal: English documents (e.g. ref manuals, etc.) Formal: operational semantics, axiomatic semantics, etc.
23 BNF grammars Definition: (Some preliminaries) 1. An alphabet is a set whose elements are called symbols. 2. A word (or string) w, over an alphabet A is a finite sequence of symbols from the alphabet. i.e. w A n where n N. When n=0, the word is called an empty word/string and is denoted as ε. 3. The set of words of length n over an alphabet A, is denoted as A n. 4. The set of words over an alphabet A, is denoted as A * where: A * = A n n N
24 BNF grammars Definition: 1. A context-free grammar G, is a quadruple (V,Σ,R,S) where: a. V is an alphabet b. Σ is a set of terminals, where Σ V (V Σ is known as the set of non-terminals) c. R is a set of production rules such that R (V Σ) x V* d. S is the start symbol, where S (V Σ) 2. The language generated by grammar G (denoted L(G)), is the set of words over Σ, which can be produced by R.
25 BNF grammars Nonterminal: A finite set of symbols: <sentence> <subject> <predicate> <verb> <article> <noun> Terminal: A finite set of symbols: the, boy, girl, ran, ate, cake Start symbol: One of the nonterminals: <sentence>
26 Backus-Naur form (BNF) grammars Rules (productions): A finite set of replacement rules: <sentence> ::= <subject> <predicate> <subject> ::= <article> <noun> <predicate>::= <verb> <article> <noun> <verb> ::= ran ate <article> ::= the <noun> ::= boy girl cake Replacement Operator: Replace any nonterminal by a right hand side value using any rule (written )
27 Example BNF sentences <sentence> <subject> <predicate> <article> <noun> <predicate> the <noun> <predicate>... the boy ate the cake First rule Second rule Fifth rule Also from <sentence> you can derive the cake ate the boy Syntax does not imply correct semantics Note: Rule <A> ::= <B><C> This BNF rule also written with equivalent (CFG) syntax: A BC
28 Languages Any string derived from the start symbol is a sentential form. A language generated by grammar G (written L (G) ) is the set of all strings over the terminal alphabet (i.e., sentences) derived from start symbol. That is, a language is the set of sentential forms containing only terminal symbols.
29 Derivations A derivation is a sequence of sentential forms starting from start symbol. Grammar: B 0B 1B 0 1 Derivation: B 0B 01B 010 Each derivation step is the application of a production rule.
30 Derivations Derivations may not be unique S SS (S) () S SS (S)S (())S (())() S SS S() (S)() (())() Different derivations but get the same parse tree
31 Parse tree A parse tree is a hierarchical synt. structure Internal node denote non-terminals Leaf nodes denote terminals. Grammar: B 0B 1B 0 1 Derivation: B 0B 01B 010 From derivation get parse tree as shown in the right.
32 DERIVATION Grammar V = { } Σ = { } R = { } S = { } Tree PARSING String 19a0c6duw Tree
33 Ambiguity But from some grammars you can get 2 different parse trees for the same string: ()()(). Each corresponds to a unique derivation: S SS SSS ()SS ()()S ()()() A grammar is ambiguous if some sentence has 2 distinct parse trees. We desire unambiguous grammars to understand semantics.
34 Why Ambiguity is a problem Most common cause is due to infix binary operations Grammar <expr> ::= <num> <expr> <expr> String <expr> <expr> <expr> Parse <expr> <expr> <expr> <expr> <expr> <num> <num> <expr> <expr> <num> <num> Which One? 1 <num> <num> 2 3
35 Why Ambiguity is a problem It may be true that (1-2)-3 = 1-(2-3) and different parse trees doesn t matter. Such cases, are not true in general, and we must assume the more conservative position: different parse trees have different meaning. Grammar <expr> ::= <num> <expr> <expr> String <expr> Parse <expr> <expr> <expr> <expr> <expr> <expr> <expr> <num> <num> <expr> <expr> <num> <num> 3 1 <num> <num> 1 2 Which One? Different Parse Trees, 2 3 (1-2)-3 Different Meaning! 1-(2-3)
36 Why Ambiguity is a problem <expr> ::= <num> <expr> <expr> ambiguous To make it unambiguous, I want the to be Left associative: <expr> ::= <num> <expr> <num> Right Assocative: <expr> ::= <num> <num> <expr>
37 Why Ambiguity is a problem Several programming languages allow 2 forms of conditional statements: 1. If boolean_exp then statement1 2. If boolean_exp then statement1 else statement2 If boolean_exp1 then if boolean_exp2 then S1 else S2
38 Exercise 1 Is the following grammar (of arithmetic expressions) an ambiguous grammar? Try to construct a derivation with two different parse trees. <E> ::= <E> + <E> <E> ::= <E> *<E> <E> ::= ( <E> ) <E> ::= I
39 Exercise 1 - Answer <E> ::= <E> + <E> <E> ::= <E> *<E> * 4 <E> ::= i E I E E + E E * E I I * I I + I 2 3 4
40 To make it unambiguous (Case 1) + to be of a higher precedence than * <expr> ::= <expr2> <expr2> * <expr> <expr2> ::= <num> <num> + <expr2> <expr> <expr2> * <expr> 1+2*3 (1+2)*3 <num> + <expr2> 1 <num> 2 <expr2> <num> 3
41 Design of Rules From semantics (what you want it to mean) eg. I want to express the idea of addition To abstract syntax (what components you need to capture the meaning) eg. I need an operator and two operands To concrete syntax <expr> ::= <num> <expr> + <expr> To unambiguous concrete syntax. <expr> ::= <num> <expr> + <num> Unambiguous concrete syntax essential for a parser (parser takes a string, returns a syntax tree)
42 Implementation of Rules Divide the BNF rules into 2 groups: Lexical rules Syntactic rules Lexical rules are rules which deal with grouping individual symbols into meaningful words. Syntactic rules are rules, which deal with the structure of the overall sentence.
43 Example <E> ::= <E> + <E> <E> - <T> <T> <T> ::= <T> * <F> <T> / <F> <F> <F> ::= <name> <num> (<E>) <name> ::= <alpha> <alpha><alphanum> <alphanum> ::= <alpha><alphanum> <digit><alphanum> <alpha> <digit> <num> ::= <digit> <digit><num> <alpha> ::= a b c A B C Z <digit> ::=
44 Example <E> ::= <E> + <E> <E> - <T> <T> <T> ::= <T> * <F> <T> / <F> <F> <F> ::= <name> <num> (<E>) <name> ::= <alpha> <alpha><alphanum> <alphanum> ::= <alpha><alphanum> <digit><alphanum> <alpha> <digit> <num> ::= <digit> <digit><num> <alpha> ::= a b c A B C Z <digit> ::= Syntactic Rules Lexical Rules
45 Implementation of Rules Lexical Rules get implemented as a Lexical Analyzer: Input : String of Characters Output : List of Tokens Syntactic Rules get implemented as a Syntactic Analyzer: Input : List of Tokens Output : Parse Tree Parser = Lexical Analyzer + Syntactic Analyzer. (Very often, parser is used in a loose sense to refer to the syntactic analyzer)
46 Extended BNF This is a shorthand notation for BNF rules. It adds no power to the syntax,only a shorthand way to write productions: [ ] Grouping from which one must be chosen Binary_E T [+ -] T { }* - Repetition - 0 or more E T {[+ -] T}*
47 Extended BNF { } + - Repetition - 1 or more Usage similar to { }* { } opt - Optional I if E then S if E then S else S Can be written in EBNF as I if E then S { else S} opt
48 Extended BNF Example: Identifier - a letter followed by 0 or more letters or digits: Extended BNF I L { L D }* L a b... D Regular BNF
49 Extended BNF Example: Identifier - a letter followed by 0 or more letters or digits: Extended BNF I L { L D }* L a b... D Regular BNF I L L M M CM C C L D L a b... D
50 Classes of grammars BNF: Backus-Naur Form - Context free - Type 2 - Already described Regular grammars: subclass of BNF - Type 3: BNF rules are restricted: A t N t where: N = nonterminal, t = terminal Examples: Binary numbers: B 0 B 1 B 0 1 Identifiers: I a L b L c L... z L a... y z L 1 L 2 L... 9 L 0 L a L b L c L... z L a... y z
51 Other classes of grammars The context free and regular grammars are important for programming language design. Other classes have theoretical importance, but not in this course: Context sensitive grammar: Type 1 Rules: α β where α β i.e., length of α length of β, α: string of non terminals, β: string of terminals & non terminals Unrestricted, recursively enumerable: Type 0 - Rules: α β. No restrictions on α and β.
52
53 Parsing BNF and extended BNF are notations for formally describing program syntax. Given the BNF grammar for the syntax of a programming language (say Java), how do we determine that a given Java program obeys all the grammar rules. This is achieved by parsing. Two main methods of parsing Top-down parsing (recursive-descent). Bottom-up parsing.
54 Implementation of Rules Grammar <num> ::= <int> <real> <int> ::= <digit> <digit><int> <real> ::= <int-part>. <fraction> <int-part> ::= <digit> <int-part> <digit> <fraction> ::= <digit> <digit> <fraction> <digit> ::= Is accepted by the grammar with <num> as the starting non-terminal? Top-Down Parse => <num> => <int> (Try first choice) => <digit> (Try first choice) => 1 (Backtrack : 2.34 not parsed)
55 Implementation of Rules Grammar <num> ::= <int> <real> <int> ::= <digit> <digit><int> <real> ::= <int-part>. <fraction> <int-part> ::= <digit> <int-part> <digit> <fraction> ::= <digit> <digit> <fraction> <digit> ::= Is accepted by the grammar with <num> as the starting non-terminal? Top-Down Parse => <num> => <int> (Try first choice) => <digit><int> (Try second choice) => 1<int> => 1<digit> (Try first choice) => 12 (Backtrack:.34 not parsed)
56 Implementation of Rules Grammar <num> ::= <int> <real> <int> ::= <digit> <digit><int> <real> ::= <int-part>. <fraction> <int-part> ::= <digit> <int-part> <digit> <fraction> ::= <digit> <digit> <fraction> <digit> ::= Is accepted by the grammar with <num> as the starting non-terminal? Top-Down Parse => <num> => <real> (Try second choice) => <int-part>.<fraction> => => (success!)
57 Implementation of Rules Grammar <num> ::= <int> <real> <int> ::= <digit> <digit><int> <real> ::= <int-part>. <fraction> <int-part> ::= <digit> <int-part> <digit> <fraction> ::= <digit> <digit> <fraction> <digit> ::= Is accepted by the grammar with <num> as the starting non-terminal? Bottom-Up Parse => => <digit>2.34 => <int>2.34 => <num>2.34 (Backtrack)
58 Implementation of Rules Grammar <num> ::= <int> <real> <int> ::= <digit> <digit><int> <real> ::= <int-part>. <fraction> <int-part> ::= <digit> <int-part> <digit> <fraction> ::= <digit> <digit> <fraction> <digit> ::= Is accepted by the grammar with <num> as the starting non-terminal? Bottom-Up Parse => => <digit>2.34 => <digit><digit>.34 => <digit><int>.34 => <int>.34 => <num>.34 (Backtrack)
59 Implementation of Rules Grammar <num> ::= <int> <real> <int> ::= <digit> <digit><int> <real> ::= <int-part>. <fraction> <int-part> ::= <digit> <int-part> <digit> <fraction> ::= <digit> <digit> <fraction> <digit> ::= Is accepted by the grammar with <num> as the starting non-terminal? Bottom-Up Parse => => => <int-part>.34 => => <int-part>.<fraction> => <real> => <num> (Success!)
60 Recursive descent parsing overview A simple parsing algorithm Shows the relationship between the formal description of a programming language and the ability to generate executable code. Each non-terminal becomes a procedure. Recursively call the procedure to check different parts. (+) Simple Translation from BNF ( ) Not Efficient
61 Recursive descent parsing Example of Top-down parsing. <expression> ::= <term>+<term> <term>-<term> (Some details are left out; just get the idea) procedure Expression; begin Term; /* Call Term to find first term */ while ((nextchar=`+') or (nextchar=`-')) do begin nextchar:=getchar; /* Skip operator */ Term end end procedure Term; begin... end First recognize a Term; as long as the next symbol is either a + or -, we recognize another Term.
62 Structure of a compiler
63 Attribute Grammars Productions E E+T T T T*P P P I (E)
64 Attribute Grammars Production E E+T E T T T*P T P P I P (E) Attribute value(e 1 ) = value(e 2 )+value(t) value(e) = value(t) value(t 1 ) = value(t 2 )* value(p) value(t) = value(p) value(p) = value(i) value(p) = value(e) E 1 is first E in production and E 2 is second E in production Technique often useful for passing data type information within a parse tree.
65 Example attributed tree
66 Use of attribute grammars First need to develop parse tree. Attribute grammars assume you already have the derivation; it is not a parsing algorithm. Functions for attributes can be arbitrary. Process to build attributes is mostly manual. Attribute grammars may be used along with parsing to generate intermediate code. As the parse tree is built, information from one nonterminal is passed to another nonterminal higher in the tree. When parse is completed, all attribute values have already been computed.
67 Summary We need a formalism for describing the set of all allowed programs in a programming language. BNF and EBNF grammars Given a program P in a programming language L and the BNF grammar for L, we can find out whether P is a syntactically correct program in language L. This activity is called parsing. The Recursive Descent Parsing technique is one such parsing technique.
CPS 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 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 informationSpecifying Syntax. An English Grammar. Components of a Grammar. Language Specification. Types of Grammars. 1. Terminal symbols or terminals, Σ
Specifying Syntax Language Specification Components of a Grammar 1. Terminal symbols or terminals, Σ Syntax Form of phrases Physical arrangement of symbols 2. Nonterminal symbols or syntactic categories,
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 context-free grammars n Grammar derivations n More about parse trees n Top-down and
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 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 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 informationCSCI312 Principles of Programming Languages!
CSCI312 Principles of Programming Languages! Chapter 2 Syntax! Xu Liu Review! Principles of PL syntax, naming, types, semantics Paradigms of PL design imperative, OO, functional, logic What makes a successful
More 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 informationChapter 3. Describing Syntax and Semantics ISBN
Chapter 3 Describing Syntax and Semantics ISBN 0-321-49362-1 Chapter 3 Topics Introduction The General Problem of Describing Syntax Formal Methods of Describing Syntax Copyright 2009 Addison-Wesley. All
More informationA programming language requires two major definitions A simple one pass compiler
A programming language requires two major definitions A simple one pass compiler [Syntax: what the language looks like A context-free grammar written in BNF (Backus-Naur Form) usually suffices. [Semantics:
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 informationChapter 3. Describing Syntax and Semantics ISBN
Chapter 3 Describing Syntax and Semantics ISBN 0-321-49362-1 Chapter 3 Topics Introduction The General Problem of Describing Syntax Formal Methods of Describing Syntax Attribute Grammars Describing the
More informationProgramming Language Specification and Translation. ICOM 4036 Fall Lecture 3
Programming Language Specification and Translation ICOM 4036 Fall 2009 Lecture 3 Some parts are Copyright 2004 Pearson Addison-Wesley. All rights reserved. 3-1 Language Specification and Translation Topics
More informationChapter 3. 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 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 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 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 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 informationDescribing Syntax and Semantics
Describing Syntax and Semantics Introduction Syntax: the form or structure of the expressions, statements, and program units Semantics: the meaning of the expressions, statements, and program units Syntax
More informationICOM 4036 Spring 2004
Language Specification and Translation ICOM 4036 Spring 2004 Lecture 3 Copyright 2004 Pearson Addison-Wesley. All rights reserved. 3-1 Language Specification and Translation Topics Structure of a Compiler
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 context-free grammars terminal and nonterminal symbols productions definition of EBNF by itself parse tree grammars
More informationLANGUAGE PROCESSORS. Presented By: Prof. S.J. Soni, SPCE Visnagar.
LANGUAGE PROCESSORS Presented By: Prof. S.J. Soni, SPCE Visnagar. Introduction Language Processing activities arise due to the differences between the manner in which a software designer describes the
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 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 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 informationWeek 2: Syntax Specification, Grammars
CS320 Principles of Programming Languages Week 2: Syntax Specification, Grammars Jingke Li Portland State University Fall 2017 PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 1/ 62 Words and Sentences
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 informationSyntax and Semantics
Syntax and Semantics Syntax - The form or structure of the expressions, statements, and program units Semantics - The meaning of the expressions, statements, and program units Syntax Example: simple C
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 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 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 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 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 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 informationA simple syntax-directed
Syntax-directed is a grammaroriented compiling technique Programming languages: Syntax: what its programs look like? Semantic: what its programs mean? 1 A simple syntax-directed Lexical Syntax Character
More informationCMPT 755 Compilers. Anoop Sarkar.
CMPT 755 Compilers Anoop Sarkar http://www.cs.sfu.ca/~anoop Parsing source program Lexical Analyzer token next() Parser parse tree Later Stages Lexical Errors Syntax Errors Context-free Grammars Set of
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 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 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 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 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 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 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 information3. Context-free grammars & parsing
3. Context-free grammars & parsing The parsing process sequences of tokens parse tree or syntax tree a / [ / index / ]/= / 4 / + / 2 The parsing process sequences of tokens parse tree or syntax tree a
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 informationChapter 3: CONTEXT-FREE GRAMMARS AND PARSING Part 1
Chapter 3: CONTEXT-FREE 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 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 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 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 informationGrammar Rules in Prolog!!
Grammar Rules in Prolog GR-1 Backus-Naur Form (BNF) BNF is a common grammar used to define programming languages» Developed in the late 1950 s Because grammars are used to describe a language they are
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 informationUNIT I Programming Language Syntax and semantics. Kainjan Sanghavi
UNIT I Programming Language Syntax and semantics B y Kainjan Sanghavi Contents Language Definition Syntax Abstract and Concrete Syntax Concept of binding Language Definition Should enable a person or computer
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 informationPart 5 Program Analysis Principles and Techniques
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
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 informationGrammar Rules in Prolog
Grammar Rules in Prolog Based on Clocksin & Mellish Chapter 9 GR-1 Backus-Naur Form (BNF) BNF is a common grammar used to define programming languages» Developed in the late 1950's early 60's Grammars
More informationIntroduction to Syntax Analysis
Compiler Design 1 Introduction to Syntax Analysis Compiler Design 2 Syntax Analysis The syntactic or the structural correctness of a program is checked during the syntax analysis phase of compilation.
More informationParsing. Roadmap. > Context-free grammars > Derivations and precedence > Top-down parsing > Left-recursion > Look-ahead > Table-driven parsing
Roadmap > Context-free grammars > Derivations and precedence > Top-down parsing > Left-recursion > Look-ahead > Table-driven parsing The role of the parser > performs context-free syntax analysis > guides
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 informationContext-Free Grammar (CFG)
Context-Free Grammar (CFG) context-free grammar looks like this bunch of rules: ain idea: + 1 (),, are non-terminal symbols aka variables. When you see them, you apply rules to expand. One of them is designated
More informationIntroduction to Syntax Analysis. The Second Phase of Front-End
Compiler Design IIIT Kalyani, WB 1 Introduction to Syntax Analysis The Second Phase of Front-End Compiler Design IIIT Kalyani, WB 2 Syntax Analysis The syntactic or the structural correctness of a program
More informationEDA180: Compiler Construc6on Context- free grammars. Görel Hedin Revised:
EDA180: Compiler Construc6on Context- free grammars Görel Hedin Revised: 2013-01- 28 Compiler phases and program representa6ons source code Lexical analysis (scanning) Intermediate code genera6on tokens
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 informationProgramming Lecture 3
Programming Lecture 3 Expressions (Chapter 3) Primitive types Aside: Context Free Grammars Constants, variables Identifiers Variable declarations Arithmetic expressions Operator precedence Assignment statements
More informationannouncements CSE 311: Foundations of Computing review: regular expressions review: languages---sets of strings
CSE 311: Foundations of Computing Fall 2013 Lecture 19: Regular expressions & context-free grammars announcements Reading assignments 7 th Edition, pp. 878-880 and pp. 851-855 6 th Edition, pp. 817-819
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 informationDefining Program Syntax. Chapter Two Modern Programming Languages, 2nd ed. 1
Defining Program Syntax Chapter Two Modern Programming Languages, 2nd ed. 1 Syntax And Semantics Programming language syntax: how programs look, their form and structure Syntax is defined using a kind
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 informationHomework & Announcements
Homework & nnouncements New schedule on line. Reading: Chapter 18 Homework: Exercises at end Due: 11/1 Copyright c 2002 2017 UMaine School of Computing and Information S 1 / 25 COS 140: Foundations of
More informationLanguages and Compilers
Principles of Software Engineering and Operational Systems Languages and Compilers SDAGE: Level I 2012-13 3. Formal Languages, Grammars and Automata Dr Valery Adzhiev vadzhiev@bournemouth.ac.uk Office:
More informationSyntax and Grammars 1 / 21
Syntax and Grammars 1 / 21 Outline What is a language? Abstract syntax and grammars Abstract syntax vs. concrete syntax Encoding grammars as Haskell data types What is a language? 2 / 21 What is a language?
More informationSpecial Topics: Programming Languages
Lecture #2 0 V22.0490.001 Special Topics: Programming Languages B. Mishra New York University. Lecture #2 Lecture #2 1 Slide 1 What Constitutes a Programming Language? Desiderata 1. Every computable function
More informationMIT Specifying Languages with Regular Expressions and Context-Free Grammars. Martin Rinard Massachusetts Institute of Technology
MIT 6.035 Specifying Languages with Regular essions and Context-Free Grammars Martin Rinard Massachusetts Institute of Technology Language Definition Problem How to precisely define language Layered structure
More informationCS152 Programming Language Paradigms Prof. Tom Austin, Fall Syntax & Semantics, and Language Design Criteria
CS152 Programming Language Paradigms Prof. Tom Austin, Fall 2014 Syntax & Semantics, and Language Design Criteria Lab 1 solution (in class) Formally defining a language When we define a language, we need
More information1. true / false By a compiler we mean a program that translates to code that will run natively on some machine.
1. true / false By a compiler we mean a program that translates to code that will run natively on some machine. 2. true / false ML can be compiled. 3. true / false FORTRAN can reasonably be considered
More informationChapter 3 (part 3) Describing Syntax and Semantics
Chapter 3 (part 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
More informationCourse Overview. Introduction (Chapter 1) Compiler Frontend: Today. Compiler Backend:
Course Overview Introduction (Chapter 1) Compiler Frontend: Today Lexical Analysis & Parsing (Chapter 2,3,4) Semantic Analysis (Chapter 5) Activation Records (Chapter 6) Translation to Intermediate Code
More informationSection A. A grammar that produces more than one parse tree for some sentences is said to be ambiguous.
Section A 1. What do you meant by parser and its types? A parser for grammar G is a program that takes as input a string w and produces as output either a parse tree for w, if w is a sentence of G, or
More 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 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/semantics. Program <> program execution Compiler/interpreter Syntax Grammars Syntax diagrams Automata/State Machines Scanning/Parsing
Syntax/semantics Program program execution Compiler/interpreter Syntax Grammars Syntax diagrams Automata/State Machines Scanning/Parsing Meta-models 8/27/10 1 Program program execution Syntax Semantics
More informationChapter 4. Lexical and Syntax Analysis
Chapter 4 Lexical and Syntax Analysis Chapter 4 Topics Introduction Lexical Analysis The Parsing Problem Recursive-Descent Parsing Bottom-Up Parsing Copyright 2012 Addison-Wesley. All rights reserved.
More informationEarlier edition Dragon book has been revised. Course Outline Contact Room 124, tel , rvvliet(at)liacs(dot)nl
Compilerconstructie najaar 2013 http://www.liacs.nl/home/rvvliet/coco/ Rudy van Vliet kamer 124 Snellius, tel. 071-527 5777 rvvliet(at)liacs(dot)nl college 1, dinsdag 3 september 2013 Overview 1 Why this
More informationLecture 8: Context Free Grammars
Lecture 8: Context Free s Dr Kieran T. Herley Department of Computer Science University College Cork 2017-2018 KH (12/10/17) Lecture 8: Context Free s 2017-2018 1 / 1 Specifying Non-Regular Languages Recall
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 informationChapter 3: Syntax and Semantics. Syntax and Semantics. Syntax Definitions. Matt Evett Dept. Computer Science Eastern Michigan University 1999
Chapter 3: Syntax and Semantics Matt Evett Dept. Computer Science Eastern Michigan University 1999 Syntax and Semantics Syntax - the form or structure of the expressions, statements, and program units
More informationCompiler Design Overview. Compiler Design 1
Compiler Design Overview Compiler Design 1 Preliminaries Required Basic knowledge of programming languages. Basic knowledge of FSA and CFG. Knowledge of a high programming language for the programming
More information2.2 Syntax Definition
42 CHAPTER 2. A SIMPLE SYNTAX-DIRECTED TRANSLATOR sequence of "three-address" instructions; a more complete example appears in Fig. 2.2. This form of intermediate code takes its name from instructions
More informationLexical and Syntax Analysis
Lexical and Syntax Analysis (of Programming Languages) Top-Down Parsing Lexical and Syntax Analysis (of Programming Languages) Top-Down Parsing Easy for humans to write and understand String of characters
More informationContext-Free Grammar. Concepts Introduced in Chapter 2. Parse Trees. Example Grammar and Derivation
Concepts Introduced in Chapter 2 A more detailed overview of the compilation process. Parsing Scanning Semantic Analysis Syntax-Directed Translation Intermediate Code Generation Context-Free Grammar A
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: 2017-09-04 This lecture Regular expressions Context-free grammar Attribute grammar
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 informationProgramming Languages Third Edition
Programming Languages Third Edition Chapter 12 Formal Semantics Objectives Become familiar with a sample small language for the purpose of semantic specification Understand operational semantics Understand
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 informationA language is a subset of the set of all strings over some alphabet. string: a sequence of symbols alphabet: a set of symbols
The current topic:! Introduction! Object-oriented programming: Python! Functional programming: Scheme! Python GUI programming (Tkinter)! Types and values! Logic programming: Prolog! Introduction! Rules,
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 informationR13 SET Discuss how producer-consumer problem and Dining philosopher s problem are solved using concurrency in ADA.
R13 SET - 1 III B. Tech I Semester Regular Examinations, November - 2015 1 a) What constitutes a programming environment? [3M] b) What mixed-mode assignments are allowed in C and Java? [4M] c) What is
More information