1 Mariano Ceccato FBK Fondazione Bruno Kessler The TXL Programming Language (2)

Slides:



Advertisements
Similar presentations
Simplifications of Context-Free Grammars
Advertisements

JavaCUP JavaCUP (Construct Useful Parser) is a parser generator
Chapter 6 Writing a Program
Copyright © 2002 Pearson Education, Inc. Slide 1.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Structured Query Language (SQL)
1 The TXL Programming Language (3) Mariano Ceccato FBK Fondazione Bruno Kessler
DIVIDING INTEGERS 1. IF THE SIGNS ARE THE SAME THE ANSWER IS POSITIVE 2. IF THE SIGNS ARE DIFFERENT THE ANSWER IS NEGATIVE.
Programming Language Concepts
Chapter 2-2 A Simple One-Pass Compiler
Lesson 6 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Lilian Blot TO PROGRAMMING & PYTHON Introduction Autumn 2012 TPOP 1.
Modern Programming Languages, 2nd ed.
ABC Technology Project
Semantic Analysis and Symbol Tables
Lexical Analysis Arial Font Family.
CS 240 Computer Programming 1
25 seconds left…...
1 Week 9 Questions / Concerns Hand back Test#2 What’s due: Final Project due next Thursday June 5. Final Project check-off on Friday June 6 in class. Next.
1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson.
Chapter 2 Syntax A language that is simple to parse for the compiler is also simple to parse for the human programmer. N. Wirth.
1 Lecture 16:User-Definded function I Introduction to Computer Science Spring 2006.
Printf transformation in TXL Mariano Ceccato ITC-Irst Istituto per la ricerca Scientifica e Tecnologica
COMP-421 Compiler Design Presented by Dr Ioanna Dionysiou.
Chapter 2 Syntax. Syntax The syntax of a programming language specifies the structure of the language The lexical structure specifies how words can be.
176 Formal Languages and Applications: We know that Pascal programming language is defined in terms of a CFG. All the other programming languages are context-free.
1 Chapter 5 Compilers Source Code (with macro) Macro Processor Expanded Code Compiler or Assembler obj.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
The TXL Programming Language Filippo Ricca ITC-Irst Istituto per la ricerca Scientifica e Tecnologica
Yu-Chen Kuo1 Chapter 2 A Simple One-Pass Compiler.
The TXL Programming Language Mariano Ceccato ITC-Irst Istituto per la ricerca Scientifica e Tecnologica
Chapter 2 A Simple Compiler
(2.1) Grammars  Definitions  Grammars  Backus-Naur Form  Derivation – terminology – trees  Grammars and ambiguity  Simple example  Grammar hierarchies.
1 Syntax and Semantics The Purpose of Syntax Problem of Describing Syntax Formal Methods of Describing Syntax Derivations and Parse Trees Sebesta Chapter.
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
CPSC 388 – Compiler Design and Construction Parsers – Context Free Grammars.
CS 2104 Prog. Lang. Concepts Dr. Abhik Roychoudhury School of Computing Introduction.
Chapter 1 Introduction Dr. Frank Lee. 1.1 Why Study Compiler? To write more efficient code in a high-level language To provide solid foundation in parsing.
Compiler Phases: Source program Lexical analyzer Syntax analyzer Semantic analyzer Machine-independent code improvement Target code generation Machine-specific.
Lexical Analysis - An Introduction. The Front End The purpose of the front end is to deal with the input language Perform a membership test: code  source.
Lexical Analysis - An Introduction Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at.
CS 461 – Oct. 7 Applications of CFLs: Compiling Scanning vs. parsing Expression grammars –Associativity –Precedence Programming language (handout)
Context-Free Grammars and Parsing
Lesson 3 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
C H A P T E R TWO Syntax and Semantic.
Review: Compiler Phases: Source program Lexical analyzer Syntax analyzer Semantic analyzer Intermediate code generator Code optimizer Code generator Symbol.
CPS 506 Comparative Programming Languages Syntax Specification.
D Goforth COSC Translating High Level Languages Note error in assignment 1: #4 - refer to Example grammar 3.4, p. 126.
Syntax The Structure of a Language. Lexical Structure The structure of the tokens of a programming language The scanner takes a sequence of characters.
Syntax (2).
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
LECTURE 4 Syntax. SPECIFYING SYNTAX Programming languages must be very well defined – there’s no room for ambiguity. Language designers must use formal.
The TXL Programming Language Filippo Ricca ITC-Irst Istituto per la ricerca Scientifica e Tecnologica
The TXL Programming Language Filippo Ricca & Mariano Ceccato ITC-Irst Istituto per la ricerca Scientifica e Tecnologica
CC410: System Programming Dr. Manal Helal – Fall 2014 – Lecture 12–Compilers.
CSE 3302 Programming Languages
Chapter 3 – Describing Syntax
A Simple Syntax-Directed Translator
Simplifications of Context-Free Grammars
CS 363 Comparative Programming Languages
CS416 Compiler Design lec00-outline September 19, 2018
Syntax Analysis Sections :.
Programming Languages 2nd edition Tucker and Noonan
R.Rajkumar Asst.Professor CSE
Lecture 4: Lexical Analysis & Chomsky Hierarchy
CS416 Compiler Design lec00-outline February 23, 2019
High-Level Programming Language
Lec00-outline May 18, 2019 Compiler Design CS416 Compiler Design.
COMPILER CONSTRUCTION
Faculty of Computer Science and Information System
Presentation transcript:

1 Mariano Ceccato FBK Fondazione Bruno Kessler The TXL Programming Language (2)

2 The three phases of TXL Parse Transform Unparse Input textParse tree Transformed parse tree Output text blue fish [words] [word] [words] blue [word][empty] fish [words] [word] marlin [empty] marlin

3 Anatomy of a TXL program Base grammar Grammar overrides Transformation rules The base grammar defines the lexical forms (tokens or terminals) and the syntactic forms (non-terminals). The optional grammar overrides non-terminal of the base grammar. The ruleset defines the set of transformation rules and functions

4 Anatomy of a TXL program Base Grammar Grammar overrides Transformation rules Example: Expr grammar include Expr.Grammar redefine expr … | exp([number], [number])) include Expr-exp.Grammar rule main rule one rule two

5 Specifying Lexical Forms Lexical forms specify how the input is partitionated into tokens. Predefined defaults include identifiers [id] (e.g. ABC, rt789), integer and float [number] (e.g. 123, , 3e22), string [string] (e.g. hi there). The tokens statement gives regular expressions for each class of token in the input language. tokens hexnumber 0[xX][\dABCDEFabcdef]+ end tokens Example:

6 Specifying lexical Forms (contd) Any single char (not [, ]) not preceded by a \ or # simply represents itself. Single char patterns: ex. \d (digits), \a (alphabetic char). Regular expression operators: [PQR] (any one of), (PQR) (sequence of), P*, P+, P?. tokens name regular expression end tokens Regular expression:

7 Specifying lexical Forms (contd) The keys specifies that certain identifiers are to be treated as unique special symbols. The compounds specifies char seuqences to be treated as a single terminal. The comments specifies the commenting conventions of the input language. By default comments are ignored by TXL. keys procedure repeat program end keys compounds := >= <= end compounds comments /* */ // end comments

8 Specifying Syntactic Forms The general form of a non-terminal is: define name alternative1 | alternative2 … | alternativeN end define Where each alternative is any sequence of terminal and non terminal (enclosed in square brackets). The special type [program] describes the structure of the entire input.

9 Specifying Syntactic Forms (contd) Extended BNF-like sequence notation: [repeat x] sequence of zero or more (X*) [list X] comma-separated list [opt X] optional (zero or one) define statements [repeat statement] end define define statements [statement] | [statement] [statements] end define … are equivalent

10 Specifying Syntactic Forms (contd) define formalParameters ([list formalParameter+]) | [empty] end define define formalParameter [id] : [type] end define define type int | bool end define key procedure begin end int bool end key define proc procedure [id] [forrmalParameters] begin [body] end end define

11 Ambiguity TXL resolves ambiguities by choosing the first alternative of each non-terminal that can match the input. define T [number] | ([T]) | + [T] | + + [T] end define ++2 T T T T T ++ 2 Example: T-language

12 Transformation rules TXL has two kinds of transformation rules, rules and functions, which are distinguished by whether they should transform only one (for functions) or many (for rules) occurrences of their pattern. Rules search their scope for the first istance of their target type matching their pattern, transform it, and then reapply to the entire scope until no more matches are found. Functions do not search, but attempt to match only their entire scope to their pattern, transforming it if it matches.

13 Rules and function function 2To42 replace [number] 2 by 42 end function rule 2To42 replace [number] 2 by 42 end rule > > > Rules search the pattern!

14 Searching functions function 2To42 replace * [number] 2 by 42 end function Note: change only * > >

15 Syntax of rules and functions Simplified and given in TXL. rule [ruleid] [repeat formalArgument] [repeat construct_deconstruct_where] replace [type] [pattern] [repeat construct_deconstruct_where] by [replacement] end rule The same for functions! N.B. If the where-condition is false the rule can not be applied and the result is the input-AST.

16 Built-in functions rule resolveAdd replace [expr] N1 [number] + N2 [number] by N1 [add N2] end rule function add … end function rule resolveAdd replace [expr] N1 [number] + N2 [number] by N1 [+ N2] end rule … are equivalent!

17 Built-in functions (contd) rule sort replace [repeat number] N1 [number] N2 [number] Rest [repeat number] where N1 [> N2] by N2 N1 Rest end rule > … >

18 Recursive functions function fact replace [number] n [number] construct nMinusOne [number] n [- 1] where n [> 1] construct factMinusOne [number] nMinusOne [fact] by n [* factMinusOne] end function

19 Using rule parameters rule resolveConstants replace [repeat statement] const C [id] = V [expr] RestOfscope [repeat statement] by RestOfScope [replaceByValue C V] end rule rule replaceByValue ConstName [id] Value [expr] replace [primary] ConstName by (Value) end rule Example: Const Pi = 3.14; Area := r*r*Pi; Area := r*r*(3.14);

20 Exercises Implementing the T-language (page 11). Implementing the Calculator.txl. Adding to the expr-grammar the exponential i.e Exp(x, n). Computing the exponential: - in syntax way: ex. Exp(2, 3) ----> 2*2*2 - in semantic way: by means a recursive function that substitute at Exp(x, n) the correct value.

21 Homework Implementing a simple version of commands-language where commands can be: - assignments i.e. [id] := [expr]; - declarations i.e. const [id] = [number]; Implementing some transformation rules (page 19) that substitute in the assignments identifiers with related values. Example: Const Pi = 3.14; Area := r*r*Pi; Area := r*r*3.14;