The TXL Programming Language Filippo Ricca & Mariano Ceccato ITC-Irst Istituto per la ricerca Scientifica e Tecnologica

Slides:



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

Chapter 2 Syntax A language that is simple to parse for the compiler is also simple to parse for the human programmer. N. Wirth.
Printf transformation in TXL Mariano Ceccato ITC-Irst Istituto per la ricerca Scientifica e Tecnologica
Lex -- a Lexical Analyzer Generator (by M.E. Lesk and Eric. Schmidt) –Given tokens specified as regular expressions, Lex automatically generates a routine.
CS252: Systems Programming Ninghui Li Topic 4: Regular Expressions and Lexical Analysis.
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.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
PZ02A - Language translation
Context-Free Grammars Lecture 7
The TXL Programming Language Filippo Ricca ITC-Irst Istituto per la ricerca Scientifica e Tecnologica
The TXL Programming Language Mariano Ceccato ITC-Irst Istituto per la ricerca Scientifica e Tecnologica
Chapter 2 A Simple Compiler
Specifying Languages CS 480/680 – Comparative Languages.
(2.1) Grammars  Definitions  Grammars  Backus-Naur Form  Derivation – terminology – trees  Grammars and ambiguity  Simple example  Grammar hierarchies.
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 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.
1 Introduction to Parsing Lecture 5. 2 Outline Regular languages revisited Parser overview Context-free grammars (CFG’s) Derivations.
CS 2104 Prog. Lang. Concepts Dr. Abhik Roychoudhury School of Computing Introduction.
Compiler Phases: Source program Lexical analyzer Syntax analyzer Semantic analyzer Machine-independent code improvement Target code generation Machine-specific.
REGULAR EXPRESSIONS. Lexical Analysis Lexical analysers can be constructed by programs such as LEX These programs employ as input a description of the.
Syntax Specification and BNF © Allan C. Milne Abertay University v
Syntax and Backus Naur Form
1 Chapter 3 Describing Syntax and Semantics. 3.1 Introduction Providing a concise yet understandable description of a programming language is difficult.
CS Describing Syntax CS 3360 Spring 2012 Sec Adapted from Addison Wesley’s lecture notes (Copyright © 2004 Pearson Addison Wesley)
Context-Free Grammars and Parsing
PART I: overview material
Lesson 3 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
ISBN Chapter 3 Describing Syntax and Semantics.
Introduction to Parsing
CPS 506 Comparative Programming Languages Syntax Specification.
D Goforth COSC Translating High Level Languages.
D Goforth COSC Translating High Level Languages Note error in assignment 1: #4 - refer to Example grammar 3.4, p. 126.
Chapter 3 Describing Syntax and Semantics
Context Free Grammars CFGs –Add recursion to regular expressions Nested constructions –Notation expression  identifier | number | - expression | ( expression.
Syntax The Structure of a Language. Lexical Structure The structure of the tokens of a programming language The scanner takes a sequence of characters.
1 Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
Syntax (2).
Compiler Construction By: Muhammad Nadeem Edited By: M. Bilal Qureshi.
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
Syntax and Semantics Form and Meaning of Programming Languages Copyright © by Curt Hill.
Programming Languages and Design Lecture 2 Syntax Specifications of Programming Languages Instructor: Li Ma Department of Computer Science Texas Southern.
06/03/2007 The TXL Programming Language (4) 1 Mariano Ceccato ITC-Irst Istituto per la ricerca Scientifica e Tecnologica
LECTURE 4 Syntax. SPECIFYING SYNTAX Programming languages must be very well defined – there’s no room for ambiguity. Language designers must use formal.
©SoftMoore ConsultingSlide 1 Context-Free Grammars.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 1 Ahmed Ezzat.
The TXL Programming Language Filippo Ricca ITC-Irst Istituto per la ricerca Scientifica e Tecnologica
ICS611 Lex Set 3. Lex and Yacc Lex is a program that generates lexical analyzers Converting the source code into the symbols (tokens) is the work of the.
BNF A CFL Metalanguage Some Variations Particular View to SLK Copyright © 2015 – Curt Hill.
Chapter 3 – Describing Syntax CSCE 343. Syntax vs. Semantics Syntax: The form or structure of the expressions, statements, and program units. Semantics:
Syntax(1). 2 Syntax  The syntax of a programming language is a precise description of all its grammatically correct programs.  Levels of syntax Lexical.
Chapter 3 – Describing Syntax
Parsing 2 of 4: Scanner and Parsing
A Simple Syntax-Directed Translator
Introduction to Parsing
Introduction to Parsing (adapted from CS 164 at Berkeley)
Chapter 3 – Describing Syntax
Syntax (1).
PROGRAMMING LANGUAGES
Compiler Construction
CS 363 Comparative Programming Languages
Syntax Analysis Sections :.
Programming Languages 2nd edition Tucker and Noonan
R.Rajkumar Asst.Professor CSE
Lecture 4: Lexical Analysis & Chomsky Hierarchy
BNF 9-Apr-19.
High-Level Programming Language
Discrete Maths 13. Grammars Objectives
Faculty of Computer Science and Information System
Presentation transcript:

The TXL Programming Language Filippo Ricca & Mariano Ceccato ITC-Irst Istituto per la ricerca Scientifica e Tecnologica

2 The three phases of TXL txl “input file” “txl file” 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 (cont’d)  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 (cont’d)  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 (N.B: enclosed in square brackets).  The special type [program] describes the structure of the entire input.

9 Specifying Syntactic Forms (cont’d)  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 (cont’d) 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 functions 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 (cont’d) rule sort replace [repeat number] N1 [number] N2 [number] Rest [repeat number] where N1 [> N2] by N2 N1 Rest end rule > … >