From 3036881d206332a8ca9ed4d97715bf24142227a4 Mon Sep 17 00:00:00 2001 From: aarne Date: Thu, 23 Dec 2010 09:33:11 +0000 Subject: updated tutorial and quickstart for 3.2 --- doc/tutorial/gf-tutorial.html | 520 +++++++++++++++++++++++++++++++++++++----- 1 file changed, 468 insertions(+), 52 deletions(-) (limited to 'doc/tutorial/gf-tutorial.html') diff --git a/doc/tutorial/gf-tutorial.html b/doc/tutorial/gf-tutorial.html index 46b17b96b..3652df3a1 100644 --- a/doc/tutorial/gf-tutorial.html +++ b/doc/tutorial/gf-tutorial.html @@ -8,12 +8,264 @@

Grammatical Framework Tutorial

Aarne Ranta
-December 2010 (November 2008) +December 2010 for GF 3.2
+

+
+

+ + +

+
+

+

Overview

This is a hands-on introduction to grammar writing in GF. @@ -40,6 +292,7 @@ Prerequisites:

+

Outline

Lesson 1: a multilingual "Hello World" grammar. English, Finnish, Italian. @@ -66,6 +319,7 @@ and semantic definitions.

+

Slides

You can chop this tutorial into a set of slides by the command @@ -89,6 +343,7 @@ upper left corner of each slide, and the links behind the "Contents" link.

+

Lesson 1: Getting Started with GF

@@ -105,6 +360,7 @@ Goals:

+

What GF is

We use the term GF for three different things: @@ -133,6 +389,7 @@ using the GF system.

+

GF grammars and language processing tasks

A GF program is called a grammar. @@ -160,6 +417,7 @@ In general, a GF grammar is multilingual:

+

Getting the GF system

Open-source free software, downloaded via the GF Homepage: @@ -188,6 +446,7 @@ instructions in the Developers Guide.

+

Running the GF system

Type gf in the Unix (or Cygwin) shell: @@ -220,6 +479,7 @@ follow them.

+

A "Hello World" grammar

Like most programming language tutorials, we start with a @@ -237,6 +497,7 @@ Extra features:

+

The program: abstract syntax and concrete syntaxes

A GF program, in general, is a multilingual grammar. Its main parts @@ -356,6 +617,7 @@ Finnish and an Italian concrete syntaxes:

+

Using grammars in the GF system

In order to compile the grammar in GF, @@ -462,6 +724,7 @@ Linearization is by default to all available languages.

+

Exercises on the Hello World grammar

  1. Test the parsing and translation examples shown above, as well as @@ -491,6 +754,7 @@ of a variable. Inspect the error messages generated by GF.

    +

    Using grammars from outside GF

    You can use the gf program in a Unix pipe. @@ -516,6 +780,7 @@ You can also write a script, a file containing the lines

    +

    GF scripts

    If we name this script hello.gfs, we can do @@ -541,6 +806,7 @@ translation to the output.

    +

    What else can be done with the grammar

    Some more functions that will be covered: @@ -559,6 +825,7 @@ Some more functions that will be covered:

    +

    Embedded grammar applications

    Application programs, using techniques from Lesson 7: @@ -580,6 +847,7 @@ Application programs, using techniques from Lesson 7:

    +

    Lesson 2: Designing a grammar for complex phrases

    @@ -596,6 +864,7 @@ Goals:

    +

    The abstract syntax Food

    Phrases usable for speaking about food: @@ -643,6 +912,7 @@ Example Phrase

    +

    The concrete syntax FoodEng

         concrete FoodEng of Food = {
    @@ -690,6 +960,7 @@ Parse in other categories setting the cat flag:
     

    +

    Exercises on the Food grammar

    1. Extend the Food grammar by ten new food kinds and @@ -706,7 +977,9 @@ the prefix can occur at most once.

      +

      Commands for testing grammars

      +

      Generating trees and strings

      Random generation (generate_random = gr): build @@ -768,6 +1041,7 @@ What options a command has can be seen by the help = h command:

      +

      Exercises on generation

      1. If the command gt generated all @@ -781,6 +1055,7 @@ use the Unix word count command wc to count lines.

        +

        More on pipes: tracing

        Put the tracing option -tr to each command whose output you @@ -805,6 +1080,7 @@ strings, and try out the ambiguity test.

        +

        Writing and reading files

        To save the outputs into a file, pipe it to the write_file = wf command, @@ -829,6 +1105,7 @@ of grammars - the most systematic way to do this is by

        +

        Visualizing trees

        Parentheses give a linear representation of trees, @@ -867,10 +1144,21 @@ program (from the Graphviz package).

             % dot -Tpng _grph.dot > mytree.png
         
        +

        +You can also visualize parse trees, which show categories and words instead of +function symbols. The command is visualize_parse = vp: +

        +
        +    > parse "this delicious cheese is very Italian" | visualize_parse
        +

        + +

        +

        +

        System commands

        You can give a system command without leaving GF: @@ -882,10 +1170,10 @@ You can give a system command without leaving GF:

    A system command may also receive its argument from -a GF pipes. It then has the name sp = system_pipe: +a GF pipes. It then uses the symbol ?:

    -    > generate_trees -depth=4 | sp -command="wc -l"
    +    > generate_trees -depth=4 | ? wc -l
     

    This command example returns the number of generated trees. @@ -899,6 +1187,7 @@ a system pipe from a GF command into a Unix command.

    +

    An Italian concrete syntax

    @@ -953,6 +1242,7 @@ which are introduced in Lesson 3.)

    +

    Exercises on multilinguality

    1. Write a concrete syntax of Food for some other language. @@ -970,6 +1260,7 @@ after having worked out Lesson 3.

      +

      Free variation

      Semantically indistinguishable ways of expressing a thing. @@ -1017,7 +1308,9 @@ a variant list must be of the same type.

      +

      More application of multilingual grammars

      +

      Multilingual treebanks

      @@ -1041,6 +1334,7 @@ linearizations in different languages:

      +

      Translation quiz

      translation_quiz = tq: @@ -1072,7 +1366,9 @@ answer given in another language.

      +

      Context-free grammars and GF

      +

      The "cf" grammar format

      The grammar FoodEng can be written in a BNF format as follows: @@ -1106,6 +1402,7 @@ The compiler creates separate abstract and concrete modules internally.

      +

      Restrictions of context-free grammars

      Separating concrete and abstract syntax allows @@ -1124,6 +1421,7 @@ copy language {x x | x <- (a|b)*} in GF.

      +

      Modules and files

      GF uses suffixes to recognize different file formats: @@ -1169,7 +1467,9 @@ a second time? Try this in different situations:

      +

      Using operations and resource modules

      +

      Operation definitions

      The golden rule of functional programmin: @@ -1231,6 +1531,7 @@ sugar for abstraction:

      +

      The ``resource`` module type

      The resource module type is used to package @@ -1249,6 +1550,7 @@ The resource module type is used to package

      +

      Opening a resource

      Any number of resource modules can be @@ -1281,6 +1583,7 @@ Any number of resource modules can be

      +

      Partial application

      @@ -1318,6 +1621,7 @@ such that it allows you to write

      +

      Testing resource modules

      Import with the flag -retain, @@ -1336,10 +1640,12 @@ Compute the value with compute_concrete = cc,

      +

      Grammar architecture

      +

      Extending a grammar

      A new module can extend an old one: @@ -1395,6 +1701,7 @@ possible to build resource hierarchies.

      +

      Multiple inheritance

      Extend several grammars at the same time: @@ -1428,6 +1735,7 @@ where

      +

      Lesson 3: Grammars with parameters

      @@ -1456,6 +1764,7 @@ could be left to library implementors.

      +

      The problem: words have to be inflected

      Plural forms are needed in things like @@ -1488,6 +1797,7 @@ adjectives, and verbs can have in some languages that you know.

      +

      Parameters and tables

      We define the parameter type of number in English by @@ -1598,6 +1908,7 @@ module, which you can test by using the command compute_concrete.

      +

      Inflection tables and paradigms

      A morphological paradigm is a formula telling how a class of @@ -1649,6 +1960,7 @@ uses a wild card pattern _.

      +

      Exercises on morphology

      1. Identify cases in which the regNoun paradigm does not @@ -1661,6 +1973,7 @@ considered in earlier exercises.

        +

        Using parameters in concrete syntax

        Purpose: a more radical @@ -1685,6 +1998,7 @@ This will force us to deal with gender-

        +

        Agreement

        In English, the phrase-forming rule @@ -1726,6 +2040,7 @@ Now we can write

        +

        Determiners

        How does an Item subject receive its number? The rules @@ -1795,6 +2110,7 @@ In a more lexicalized grammar, determiners would be a category:

        +

        Parametric vs. inherent features

        Kinds have number as a parametric feature: both singular and plural @@ -1862,6 +2178,7 @@ Notice

        +

        An English concrete syntax for Foods with parameters

        We use some string operations from the library Prelude are used. @@ -1926,6 +2243,7 @@ We use some string operations from the library Prelude are used.

        +

        More on inflection paradigms

        @@ -1939,6 +2257,7 @@ add words to a lexicon.

        +

        Worst-case functions

        We perform data abstraction from the type @@ -2028,6 +2347,7 @@ parameters.

        +

        Smart paradigms

        The regular dog-dogs paradigm has @@ -2094,6 +2414,7 @@ the suffix "oo" prevents bamboo from matching the suffix

        +

        Exercises on regular patterns

        1. The same rules that form plural nouns in English also @@ -2118,6 +2439,7 @@ operation to see whether it correctly changes Arzt to

          +

          Function types with variables

          In Lesson 5, dependent function types need a notation @@ -2173,6 +2495,7 @@ looking like the expected forms:

          +

          Separating operation types and definitions

          In librarues, it is useful to group type signatures separately from @@ -2192,6 +2515,7 @@ With the interface and instance module types

          +

          Overloading of operations

          Overloading: different functions can be given the same name, as e.g. in C++. @@ -2233,6 +2557,7 @@ an overload group.

          +

          Morphological analysis and morphology quiz

          The command morpho_analyse = ma @@ -2269,6 +2594,7 @@ To create a list for later use, use the command morpho_list = ml

          +

          The Italian Foods grammar

          @@ -2406,6 +2732,7 @@ The complete set of linearization rules:

          +

          Exercises on using parameters

          1. Experiment with multilingual generation and translation in the @@ -2425,6 +2752,7 @@ now aiming for complete grammatical correctness by the use of parameters.

            +

            Discontinuous constituents

            A linearization record may contain more strings than one, and those @@ -2462,6 +2790,7 @@ but can be defined in GF by using discontinuous constituents.

            +

            Strings at compile time vs. run time

            Tokens are created in the following ways: @@ -2520,6 +2849,7 @@ This topic will be covered in here.

            +

            Supplementary constructs for concrete syntax

            Record extension and subtyping

            @@ -2581,6 +2911,7 @@ Thus

            +

            Lesson 4: Using the resource grammar library

            @@ -2597,32 +2928,38 @@ Goals:

            +

            The coverage of the library

            -The current 12 resource languages are +The current 16 resource languages (GF version 3.2, December 2010) are

            • Bulgarian
            • Catalan
            • Danish +
            • Dutch
            • English
            • Finnish
            • French
            • German
            • Italian
            • Norwegian +
            • Polish +
            • Ron, Romanian
            • Russian
            • Spanish
            • Swedish +
            • Urdu

            The first three letters (Eng etc) are used in grammar module names -(ISO 639 standard). +(ISO 639-3 standard).

            +

            The structure of the library

            @@ -2644,6 +2981,7 @@ wider coverage than with semantic grammars.

            +

            Lexical vs. phrasal rules

            A resource grammar has two kinds of categories and two kinds of rules: @@ -2671,6 +3009,7 @@ But it is a good discipline to follow.

            +

            Lexical categories

            Two kinds of lexical categories: @@ -2683,8 +3022,7 @@ Two kinds of lexical categories:

          2. structural words / function words, e.g.
                   Conj ;     -- conjunction           e.g. "and"
            -      QuantSg ;  -- singular quantifier   e.g. "this"
            -      QuantPl ;  -- plural quantifier     e.g. "this"
            +      Det ;      -- determiner            e.g. "this"
             

            @@ -2703,13 +3041,13 @@ Two kinds of lexical categories:

            +

            Lexical rules

            Closed classes: module Syntax. In the Foods grammar, we need

            -    this_QuantSg, that_QuantSg : QuantSg ; 
            -    these_QuantPl, those_QuantPl : QuantPl ; 
            +    this_Det, that_Det, these_Det, those_Det : Det ; 
                 very_AdA  : AdA ;
             

            @@ -2735,6 +3073,7 @@ where we use mkN from ParadigmsEng:

            +

            Resource lexicon

            Alternative concrete syntax for @@ -2765,6 +3104,7 @@ Advantages:

            +

            Phrasal categories

            In Foods, we need just four phrasal categories: @@ -2785,14 +3125,14 @@ Common nouns are made into noun phrases by adding determiners.

            +

            Syntactic combinations

            We need the following combinations:

                 mkCl : NP -> AP -> Cl ;      -- e.g. "this pizza is very warm"
            -    mkNP : QuantSg -> CN -> NP ; -- e.g. "this pizza" 
            -    mkNP : QuantPl -> CN -> NP ; -- e.g. "these pizzas"
            +    mkNP : Det -> CN -> NP ;     -- e.g. "this pizza" 
                 mkCN : AP -> CN -> CN ;      -- e.g. "warm pizza"
                 mkAP : AdA -> AP -> AP ;     -- e.g. "very warm" 
             
            @@ -2813,6 +3153,7 @@ Heavy overloading: the current library

            +

            Example syntactic combination

            The sentence @@ -2823,7 +3164,7 @@ can be built as follows:

                 mkCl 
            -      (mkNP these_QuantPl 
            +      (mkNP these_Det 
                      (mkCN (mkAP very_AdA (mkAP warm_A)) (mkCN pizza_CN)))
                   (mkAP italian_AP) 
             
            @@ -2838,6 +3179,7 @@ this syntactic tree gives the value of linearizing the semantic tree

            +

            The resource API

            Language-specific and language-independent parts - roughly, @@ -2854,11 +3196,12 @@ Language-specific and language-independent parts - roughly, Full API documentation on-line: the resource synopsis,

            -grammaticalframework.org/lib/resource/doc/synopsis.html +grammaticalframework.org/lib/doc/synopsis.html

            +

            A miniature resource API: categories

            @@ -2892,16 +3235,11 @@ Full API documentation on-line: the resource synopsis, - - + + - - - - - @@ -2916,6 +3254,7 @@ Full API documentation on-line: the resource synopsis,

            +

            A miniature resource API: rules

            very
            QuantSgsingular quantifierDetdeterminer these
            QuantPlplural quantifierthis
            A one-place adjective warm
            @@ -2930,12 +3269,7 @@ Full API documentation on-line: the resource synopsis, - - - - - - + @@ -2963,6 +3297,7 @@ Full API documentation on-line: the resource synopsis,

            +

            A miniature resource API: structural words

            mkNPQuantSg -> CN -> NPthis old man
            mkNPQuantPl -> CN -> NPDet -> CN -> NP these old man
            @@ -2971,23 +3306,23 @@ Full API documentation on-line: the resource synopsis, - - + + - - + + - - + + - - + + @@ -3000,6 +3335,7 @@ Full API documentation on-line: the resource synopsis,

            +

            A miniature resource API: paradigms

            From ParadigmsEng: @@ -3044,6 +3380,7 @@ From ParadigmsIta:

            +

            A miniature resource API: more paradigms

            From ParadigmsGer: @@ -3108,6 +3445,7 @@ From ParadigmsFin:

            +

            Exercises

            1. Try out the morphological paradigms in different languages. Do @@ -3122,6 +3460,7 @@ as follows:

            +

            Example: English

            @@ -3155,6 +3494,7 @@ Thus the beginning of the module is

            +

            English example: linearization types and combination rules

            As linearization types, we use clauses for Phrase, noun phrases @@ -3173,10 +3513,10 @@ Now the combination rules we need almost write themselves automatically:

                 lin
                   Is item quality = mkCl item quality ;
            -      This kind = mkNP this_QuantSg kind ;
            -      That kind = mkNP that_QuantSg kind ;
            -      These kind = mkNP these_QuantPl kind ;
            -      Those kind = mkNP those_QuantPl kind ;
            +      This kind = mkNP this_Det kind ;
            +      That kind = mkNP that_Det kind ;
            +      These kind = mkNP these_Det kind ;
            +      Those kind = mkNP those_Det kind ;
                   QKind quality kind = mkCN quality kind ;
                   Very quality = mkAP very_AdA quality ;
             
            @@ -3184,6 +3524,7 @@ Now the combination rules we need almost write themselves automatically:

            +

            English example: lexical rules

            We use resource paradigms and lexical insertion rules. @@ -3209,6 +3550,7 @@ The two-place noun paradigm is needed only once, for

            +

            English example: exercises

            1. Compile the grammar FoodsEng and generate @@ -3223,10 +3565,12 @@ grammars presented earlier in this tutorial.

            +

            Functor implementation of multilingual grammars

            +

            New language by copy and paste

            If you write a concrete syntax of Foods for some other @@ -3257,6 +3601,7 @@ Can we avoid this programming by copy-and-paste?

            +

            Functors: functions on the module level

            Functors familiar from the functional programming languages ML and OCaml, @@ -3301,6 +3646,7 @@ we can write a functor instantiation,

            +

            Code for the Foods functor

                 --# -path=.:../foods
            @@ -3313,10 +3659,10 @@ we can write a functor instantiation,
                   Quality = AP ;
                 lin
                   Is item quality = mkCl item quality ;
            -      This kind = mkNP this_QuantSg kind ;
            -      That kind = mkNP that_QuantSg kind ;
            -      These kind = mkNP these_QuantPl kind ;
            -      Those kind = mkNP those_QuantPl kind ;
            +      This kind = mkNP this_Det kind ;
            +      That kind = mkNP that_Det kind ;
            +      These kind = mkNP these_Det kind ;
            +      Those kind = mkNP those_Det kind ;
                   QKind quality kind = mkCN quality kind ;
                   Very quality = mkAP very_AdA quality ;
               
            @@ -3336,6 +3682,7 @@ we can write a functor instantiation,
             

            +

            Code for the LexFoods interface

            @@ -3359,6 +3706,7 @@ we can write a functor instantiation,

            +

            Code for a German instance of the lexicon

                 instance LexFoodsGer of LexFoods = open SyntaxGer, ParadigmsGer in {
            @@ -3379,6 +3727,7 @@ we can write a functor instantiation,
             

            +

            Code for a German functor instantiation

                 --# -path=.:../foods:present
            @@ -3391,6 +3740,7 @@ we can write a functor instantiation,
             

            +

            Adding languages to a functor implementation

            Just two modules are needed: @@ -3416,6 +3766,7 @@ language:

            +

            Example: adding Finnish

            Lexicon instance @@ -3449,6 +3800,7 @@ Functor instantiation

            +

            A design pattern

            This can be seen as a design pattern for multilingual grammars: @@ -3471,6 +3823,7 @@ Of the hand-written modules, only LexDomainL is language-dependent.

            +

            Functors: exercises

            1. Compile and test FoodsGer. @@ -3511,7 +3864,9 @@ The implementation goes in the following phases:

            +

            Restricted inheritance

            +

            A problem with functors

            Problem: a functor only works when all languages use the resource Syntax @@ -3541,6 +3896,7 @@ Problem with this solution:

            +

            Restricted inheritance: include or exclude

            A module may inherit just a selection of names. @@ -3561,6 +3917,7 @@ A concrete syntax of Foodmarket must make the analogous restriction

            +

            The functor problem solved

            The English instantiation inherits the functor @@ -3582,6 +3939,7 @@ is defined in the body instead:

            +

            Grammar reuse

            Abstract syntax modules can be used as interfaces, @@ -3603,6 +3961,7 @@ The following correspondencies are then applied:

            +

            Library exercises

            1. Find resource grammar terms for the following @@ -3627,6 +3986,7 @@ Then translate the phrases to other languages.

            +

            Tenses

            @@ -3718,6 +4078,7 @@ tenses and moods, e.g. the Romance languages.

            +

            Lesson 5: Refining semantics in abstract syntax

            @@ -3745,6 +4106,7 @@ GF = logical framework + concrete syntax.

            +

            Dependent types

            @@ -3772,6 +4134,7 @@ defines voice commands for household appliances.

            +

            A dependent type system

            Ontology: @@ -3800,6 +4163,7 @@ Abstract syntax formalizing this:

            +

            Examples of devices and actions

            Assume the kinds light and fan, @@ -3832,6 +4196,7 @@ but we cannot form the trees

            +

            Linearization and parsing with dependent types

            Concrete syntax does not know if a category is a dependent type. @@ -3874,6 +4239,7 @@ to mark incomplete parts of trees in the syntax editor.

            +

            Solving metavariables

            Use the command put_tree = pt with the option -typecheck: @@ -3896,6 +4262,7 @@ is shown and no tree is returned:

            +

            Polymorphism

            @@ -3928,6 +4295,7 @@ to express Haskell-type library functions:

            +

            Dependent types: exercises

            1. Write an abstract syntax module with above contents @@ -3944,6 +4312,7 @@ and an appropriate English concrete syntax. Try to parse the commands

            +

            Proof objects

            Curry-Howard isomorphism = propositions as types principle: @@ -3988,6 +4357,7 @@ Example: the fact that 2 is less that 4 has the proof object

            +

            Proof-carrying documents

            Idea: to be semantically well-formed, the abstract syntax of a document @@ -4031,6 +4401,7 @@ A legal connection is formed by the function

            +

            Restricted polymorphism

            Above, all Actions were either of @@ -4055,6 +4426,7 @@ The notion of class uses the Curry-Howard isomorphism as follows:

            +

            Example: classes for switching and dimming

            We modify the smart house grammar: @@ -4077,6 +4449,7 @@ Classes for new actions can be added incrementally.

            +

            Variable bindings

            @@ -4110,6 +4483,7 @@ Examples from informal mathematical language:

            +

            Higher-order abstract syntax

            Abstract syntax can use functions as arguments: @@ -4147,6 +4521,7 @@ expressed using higher-order syntactic constructors.

            +

            Higher-order abstract syntax: linearization

            HOAS has proved to be useful in the semantics and computer implementation of @@ -4180,6 +4555,7 @@ If there are more bindings, we add $1, $2, etc.

            +

            Eta expansion

            To make sense of linearization, syntax trees must be @@ -4228,6 +4604,7 @@ The linearization of the variable x is,

            +

            Parsing variable bindings

            GF can treat any one-word string as a variable symbol. @@ -4247,6 +4624,7 @@ Variables must be bound if they are used:

            +

            Exercises on variable bindings

            1. Write an abstract syntax of the whole @@ -4265,6 +4643,7 @@ guarantee non-ambiguity.

            +

            Semantic definitions

            @@ -4303,6 +4682,7 @@ The key word is def:

            +

            Computing a tree

            Computation: follow a chain of definition until no definition @@ -4328,6 +4708,7 @@ Computation in GF is performed with the put_term command and the

            +

            Definitional equality

            Two trees are definitionally equal if they compute into the same tree. @@ -4355,6 +4736,7 @@ so that an object of one also is an object of the other.

            +

            Judgement forms for constructors

            The judgement form data tells that a category has @@ -4384,6 +4766,7 @@ marked as data will be treated as variables.

            +

            Exercises on semantic definitions

            1. Implement an interpreter of a small functional programming @@ -4399,6 +4782,7 @@ Type checking can be invoked with put_term -transform=solve.

            +

            Lesson 6: Grammars of formal languages

            @@ -4415,6 +4799,7 @@ Goals:

            +

            Arithmetic expressions

            We construct a calculator with addition, subtraction, multiplication, and @@ -4445,6 +4830,7 @@ grammars are not allowed to declare functions with Int as value typ

            +

            Concrete syntax: a simple approach

            We begin with a @@ -4486,6 +4872,7 @@ First problems:

            +

            Lexing and unlexing

            @@ -4538,6 +4925,7 @@ In linearization, we use a corresponding unlexer:

            +

            Most common lexers and unlexers

            In English
            this_QuantSgQuantSgthis_DetDet this
            that_QuantSgQuantSgthat_DetDet that
            these_QuantPlQuantPlthese_DetDet this
            those_QuantPlQuantPlthose_DetDet that
            @@ -4575,6 +4963,7 @@ In linearization, we use a corresponding unlexer:

            +

            Precedence and fixity

            Arithmetic expressions should be unambiguous. If we write @@ -4613,6 +5002,7 @@ The usual precedence rules:

            +

            Precedence as a parameter

            Precedence can be made into an inherent feature of expressions: @@ -4657,6 +5047,7 @@ This idea is encoded in the operation

            +

            Fixities

            We can define left-associative infix expressions: @@ -4697,6 +5088,7 @@ Now we can write the whole concrete syntax of Calculator compactly:

            +

            Exercises on precedence

            1. Define non-associative and right-associative infix operations @@ -4710,6 +5102,7 @@ Test parsing with and without a pipe to pt -transform=compute.

            +

            Code generation as linearization

            Translate arithmetic (infix) to JVM (postfix): @@ -4739,6 +5132,7 @@ Just give linearization rules for JVM:

            +

            Programs with variables

            A straight code programming language, with @@ -4787,6 +5181,7 @@ of the extension is Prog.

            +

            Exercises on code generation

            1. Define a C-like concrete syntax of the straight-code language. @@ -4827,6 +5222,7 @@ point literals as arguments.

            +

            Lesson 7: Embedded grammars

            @@ -4844,6 +5240,7 @@ Goals:

            +

            Functionalities of an embedded grammar format

            GF grammars can be used as parts of programs written in other programming @@ -4860,16 +5257,17 @@ This facility is based on several components:

            +

            The portable grammar format

            The portable format is called PGF, "Portable Grammar Format".

            -This format is produced by the GF batch compiler gf, -executable from the operative system shell: +This format is produced by using GF as batch compiler, with the option -make, +from the operative system shell:

            -    % gf --make SOURCE.gf
            +    % gf -make SOURCE.gf
             

            PGF is the recommended format in @@ -4887,6 +5285,7 @@ general-purpose programming (or bytecode in Java).

            +

            Haskell: the EmbedAPI module

            The Haskell API contains (among other things) the following types and functions: @@ -4915,6 +5314,7 @@ It is available as a part of the GF distribution, in the file

            +

            First application: a translator

            Let us first build a stand-alone translator, which can translate @@ -4941,7 +5341,7 @@ in any multilingual grammar between any languages in the grammar. To run the translator, first compile it by

            -    % ghc --make -o trans Translator.hs 
            +    % ghc -make -o trans Translator.hs 
             

            For this, you need the Haskell compiler GHC. @@ -4949,13 +5349,14 @@ For this, you need the Haskell compiler GHC

            +

            Producing PGF for the translator

            Then produce a PGF file. For instance, the Food grammar set can be compiled as follows:

            -    % gf --make FoodEng.gf FoodIta.gf
            +    % gf -make FoodEng.gf FoodIta.gf
             

            This produces the file Food.pgf (its name comes from the abstract syntax). @@ -4976,6 +5377,7 @@ The result is given in all languages except the input language.

            +

            A translator loop

            To avoid starting the translator over and over again: @@ -4997,6 +5399,7 @@ is quit.

            +

            A question-answer system

            @@ -5041,6 +5444,7 @@ To reply in the same language as the question:

            +

            Abstract syntax of the query system

            Input: abstract syntax judgements @@ -5067,6 +5471,7 @@ Input: abstract syntax judgements

            +

            Exporting GF datatypes to Haskell

            To make it easy to define a transfer function, we export the @@ -5079,7 +5484,7 @@ abstract syntax to a system of Haskell datatypes: It is also possible to produce the Haskell file together with PGF, by

            -    % gf --make --output-format=haskell QueryEng.gf
            +    % gf -make --output-format=haskell QueryEng.gf
             

            The result is a file named Query.hs, containing a @@ -5117,6 +5522,7 @@ The Haskell module name is the same as the abstract syntax name.

            +

            The question-answer function

            Haskell's type checker guarantees that the functions are well-typed also with @@ -5140,6 +5546,7 @@ respect to GF.

            +

            Converting between Haskell and GF trees

            The generated Haskell module also contains @@ -5172,6 +5579,7 @@ For the programmer, it is enougo to know:

            +

            Putting it all together: the transfer definition

               module TransferDef where
            @@ -5205,6 +5613,7 @@ For the programmer, it is enougo to know:
             

            +

            Putting it all together: the Main module

            Here is the complete code in the Haskell file TransferLoop.hs. @@ -5236,13 +5645,14 @@ Here is the complete code in the Haskell file TransferLoop.hs.

            +

            Putting it all together: the Makefile

            To automate the production of the system, we write a Makefile as follows:

               all:
            -          gf --make --output-format=haskell QueryEng
            +          gf -make --output-format=haskell QueryEng
                       ghc --make -o ./math TransferLoop.hs
                       strip math
             
            @@ -5273,6 +5683,7 @@ Just to summarize, the source of the application consists of the following files

            +

            Web server applications

            PGF files can be used in web servers, for which there is a Haskell library included @@ -5291,6 +5702,7 @@ is an example of its application to the Foods grammars.

            +

            JavaScript applications

            JavaScript is a programming language that has interpreters built in in most @@ -5304,13 +5716,14 @@ program compiled from GF grammars as run on an iPhone.

            +

            Compiling to JavaScript

            JavaScript is one of the output formats of the GF batch compiler. Thus the following command generates a JavaScript file from two Food grammars.

            -    % gf --make --output-format=js FoodEng.gf FoodIta.gf
            +    % gf -make --output-format=js FoodEng.gf FoodIta.gf
             

            The name of the generated file is Food.js, derived from the top-most abstract @@ -5319,6 +5732,7 @@ syntax name. This file contains the multilingual grammar as a JavaScript object.

            +

            Using the JavaScript grammar

            To perform parsing and linearization, the run-time library @@ -5344,6 +5758,7 @@ With these changes, the translator works for any multilingual grammar.

            +

            Language models for speech recognition

            The standard way of using GF in speech recognition is by building @@ -5361,7 +5776,7 @@ GSL is produced from GF by running gf with the flag Example: GSL generated from FoodsEng.gf.

            -    % gf --make --output-format=gsl FoodsEng.gf
            +    % gf -make --output-format=gsl FoodsEng.gf
                 % more FoodsEng.gsl
               
                 ;GSL2.0
            @@ -5390,6 +5805,7 @@ Example: GSL  generated from FoodsEng.gf.
             

            +

            More speech recognition grammar formats

            Other formats available via the --output-format flag include: @@ -5438,5 +5854,5 @@ All currently available formats can be seen with gf --help.

            - + -- cgit v1.2.3