Com S 342 meeting -*- Outline -*- * XML and the Design of Scheme Goals: help students understand Scheme's syntax orientation for rest of course overview of PL design, parts of language ** language design goals ------------------------------------------ LANGUAGE DESIGN GOALS FOR SCHEME simplicity: especially ease parsing the input and implementation ==> regularity generality: since it aids simplicity ability to manipulate programs as data: to ease construction of interpreters and compilers to allow "meta programming" ------------------------------------------ want to be able to manipulate programs and data easily (meta-programming, genetic programming) ** XML ------------------------------------------ WHY XML? A program is a document - mostly formal, - structured - follows some standard grammar (schema) Processing tools: Compiler = lexer + parser + code gen. Interpreter = lexer + parser + evaluator Lexer: Character* -> Token* Parser: Token* -> Abstract Syntax Trees Code Gen.: Abstract Syntax Trees -> object code Interpreter: Abstract Syntax Trees -> effects How to focus on interpreter and ignore lexing and parsing techniques? Solution: XML ------------------------------------------ XML is the current standard for structured documents ------------------------------------------ XML Extensible Markup Language (XML) - international standard - structured - text-based Many tools for parsing XML, XML documents -> internal tree-objects XML used as syntax for many languages: - ant (building systems) - o:XML (an OO language) - MetaL ... ------------------------------------------ *** XML syntax ------------------------------------------ EXAMPLE: HELLO WORLD IN o:XML ------------------------------------------ Q: What's the advantage of this syntax? From Introducing o:XML by Martin Klang, July 21, 2004 at http://www.xml.com/pub/a/2004/07/21/oxml.html: "Using the same format for code as we use for data allows us to think slightly differently about the code. The application is not only a runtime executable, magically incantated by the source code -- it is structured data, information, a document!" ------------------------------------------ XML SYNTAX BASICS (simplified) Document ::= Prolog Element Prolog ::= Element ::= < Name {Attribute}* /> | < Name {Attribute}* > {Content}* Attribute ::= Name = CharData Content ::= CharData | Element ------------------------------------------ Note the two kinds of Element, the example above uses both. (And the recursion is remarkably like S-expressions!) Q: What's the advantage of using the ... syntax? Better for human readers to match Q: What's the problem with this kind of syntax? painful to type ... It's verbose. Q: What's the advantage of the syntax? It's more succinct. But look at the 2nd and 3rd lines from the bottom: these use < as opening marker, have a keyword, attributes, and then /> as closing marker. *** another example, Water In water you get to choose which style you like, either one works: ------------------------------------------ WATER LANGUAGE From the Water home page (on Jan 9, 2004) http://waterlanguage.org/index.html: "Water(TM) is a new language optimized for rapidly prototyping XML Web services. It delivers both power and simplicity. The language is as easy as BASIC and as powerful as LISP." x. x. n. 1 else n. /> /> ==> 6 ==> 24 ------------------------------------------ (Well... it look like it *is* Lisp in different syntax. :-) ------------------------------------------ WATER SYNTAX (simplified) Document ::= Element Element ::= < Name {Content}* /> | < Name {Attribute}* > {Content}* Attribute ::= Name = CharData Content ::= CharData | Element | Attribute ------------------------------------------ ** Scheme's Syntax ------------------------------------------ SCHEME SYNTAX (S-EXPRESSIONS) (define (double x) (+ x x)) (define (factorial n) (if (= n 0) 1 (* n (factorial (- n 1))))) (double 3) ==> 6 (factorial 4) ==> 24 ------------------------------------------ ------------------------------------------ SCHEME SYNTAX (simplified) Vs. XML: - use ( and ) instead of < and /> - no long form, , for closing - the initial Name can be omitted The initial name is either - a keyword (special form) - a procedure name (e.g., +, *) Program ::= {S-Expression}* S-List ::= ( {S-Expression}* ) S-Expression ::= Atom | S-Expression Atom ::= Keyword | Proc-Name | Literal Literal ::= Number | ... ------------------------------------------ *** comparison to XML-based syntax Compare to the Water syntax... ------------------------------------------ COMPARISON TO XML SYNTAX XML SCHEME Document Program ... (define ...) ... Element S-List fact 3 (fact 3) Content S-Expression fact fact ------------------------------------------ *** regularity ------------------------------------------ SCHEME SYNTAX IS REGULAR TO AID PROCESSING BY COMPUTERS Mathematics Scheme 3 + 4*2 (+ 3 (* 4 2)) (3+4) * 2 (* (+ 3 4) 2) f(x,y) (f x y) ------------------------------------------ draw trees ** conclusion ------------------------------------------ SCHEME SYNTAX SUMMARY Scheme's syntax, like XML, is designed to: - be very simple and regular - ease parsing by computer - allow programs to be represented as data Scheme syntax is a textual representation of abstract syntax trees, like XML. It's not designed to be good for humans. ------------------------------------------