From b76b27773c5da7a5d5c0185b692058cc5ac5fd75 Mon Sep 17 00:00:00 2001 From: aarne Date: Sat, 16 Apr 2005 16:02:03 +0000 Subject: [PATCH] arrived to the module system --- doc/tutorial/Paleolithic.gf | 16 ++ doc/tutorial/PaleolithicEng.gf | 31 ++++ doc/tutorial/PaleolithicIta.gf | 31 ++++ doc/tutorial/gf-tutorial2.html | 328 +++++++++++++++++++++++++++++++-- doc/tutorial/neolithic.cf | 26 --- 5 files changed, 394 insertions(+), 38 deletions(-) create mode 100644 doc/tutorial/Paleolithic.gf create mode 100644 doc/tutorial/PaleolithicEng.gf create mode 100644 doc/tutorial/PaleolithicIta.gf delete mode 100644 doc/tutorial/neolithic.cf diff --git a/doc/tutorial/Paleolithic.gf b/doc/tutorial/Paleolithic.gf new file mode 100644 index 000000000..dadfd5140 --- /dev/null +++ b/doc/tutorial/Paleolithic.gf @@ -0,0 +1,16 @@ +abstract Paleolithic = { +cat + S ; NP ; VP ; CN ; A ; V ; TV ; + +fun + PredVP : NP -> VP -> S ; + UseV : V -> VP ; + ComplTV : TV -> NP -> VP ; + UseA : A -> VP ; + This, That, Def, Indef : CN -> NP ; + ModA : A -> CN -> CN ; + Bird, Boy, Man, Louse, Snake, Worm : CN ; + Big, Green, Rotten, Thick, Warm : A ; + Laugh, Sleep, Swim : V ; + Eat, Kill, Wash : TV ; +} \ No newline at end of file diff --git a/doc/tutorial/PaleolithicEng.gf b/doc/tutorial/PaleolithicEng.gf new file mode 100644 index 000000000..4bbf81707 --- /dev/null +++ b/doc/tutorial/PaleolithicEng.gf @@ -0,0 +1,31 @@ +concrete PaleolithicEng of Paleolithic = { +lincat + S, NP, VP, CN, A, V, TV = {s : Str} ; +lin + PredVP np vp = {s = np.s ++ vp.s} ; + UseV v = v ; + ComplTV tv np = {s = tv.s ++ np.s} ; + UseA a = {s = "is" ++ a.s} ; + This cn = {s = "this" ++ cn.s} ; + That cn = {s = "that" ++ cn.s} ; + Def cn = {s = "the" ++ cn.s} ; + Indef cn = {s = "a" ++ cn.s} ; + ModA a cn = {s = a.s ++ cn.s} ; + Bird = {s = "bird"} ; + Boy = {s = "boy"} ; + Louse = {s = "louse"} ; + Man = {s = "man"} ; + Snake = {s = "snake"} ; + Worm = {s = "worm"} ; + Big = {s = "big"} ; + Green = {s = "green"} ; + Rotten = {s = "rotten"} ; + Thick = {s = "thick"} ; + Warm = {s = "warm"} ; + Laugh = {s = "laughs"} ; + Sleep = {s = "sleeps"} ; + Swim = {s = "swims"} ; + Eat = {s = "eats"} ; + Kill = {s = "kills"} ; + Wash = {s = "washes"} ; +} \ No newline at end of file diff --git a/doc/tutorial/PaleolithicIta.gf b/doc/tutorial/PaleolithicIta.gf new file mode 100644 index 000000000..2b7648767 --- /dev/null +++ b/doc/tutorial/PaleolithicIta.gf @@ -0,0 +1,31 @@ +concrete PaleolithicIta of Paleolithic = { +lincat + S, NP, VP, CN, A, V, TV = {s : Str} ; +lin + PredVP np vp = {s = np.s ++ vp.s} ; + UseV v = v ; + ComplTV tv np = {s = tv.s ++ np.s} ; + UseA a = {s = "è" ++ a.s} ; + This cn = {s = "questo" ++ cn.s} ; + That cn = {s = "quello" ++ cn.s} ; + Def cn = {s = "il" ++ cn.s} ; + Indef cn = {s = "un" ++ cn.s} ; + ModA a cn = {s = cn.s ++ a.s} ; + Bird = {s = "uccello"} ; + Boy = {s = "ragazzo"} ; + Louse = {s = "pidocchio"} ; + Man = {s = "uomo"} ; + Snake = {s = "serpente"} ; + Worm = {s = "verme"} ; + Big = {s = "grande"} ; + Green = {s = "verde"} ; + Rotten = {s = "marcio"} ; + Thick = {s = "grosso"} ; + Warm = {s = "caldo"} ; + Laugh = {s = "ride"} ; + Sleep = {s = "dorme"} ; + Swim = {s = "nuota"} ; + Eat = {s = "mangia"} ; + Kill = {s = "uccide"} ; + Wash = {s = "lava"} ; +} diff --git a/doc/tutorial/gf-tutorial2.html b/doc/tutorial/gf-tutorial2.html index 22490f8dd..2c1a036c4 100644 --- a/doc/tutorial/gf-tutorial2.html +++ b/doc/tutorial/gf-tutorial2.html @@ -44,7 +44,7 @@ It will guide you -

The GF program

+

The GF program

The program is open-source free software, which you can download from the GF Homepage:
@@ -79,7 +79,7 @@ Now you are ready to try out your first grammar. We start with one that is not written in GF language, but in the EBNF notation (Extended Backus Naur Form), which GF can also understand. Type (or copy) the following lines in a file named -stoneage.ebnf: +paleolithic.ebnf:
   S   ::= NP VP ;
   VP  ::= V | TV NP | "is" A ;
@@ -93,12 +93,12 @@ understand. Type (or copy) the following lines in a file named
 
 
 
-

Importing grammars and parsing strings

+

Importing grammars and parsing strings

The first GF command when using a grammar is to import it. The command has a long name, import, and a short name, i.
-  import stoneage.gf
+  import paleolithic.gf
 
The GF program now compiles your grammar into an internal representation, and shows a new prompt when it is ready. @@ -131,7 +131,7 @@ you imported. Try parsing something else, and you fail -

Generating trees and strings

+

Generating trees and strings

You can also use GF for linearizing (linearize = l). This is the inverse of @@ -158,7 +158,7 @@ a pipe. -

Some random-generated sentences

+

Some random-generated sentences

Random generation can be quite amusing. So you may want to generate ten strings with one and the same command: @@ -178,7 +178,7 @@ generate ten strings with one and the same command: -

Systematic generation

+

Systematic generation

To generate all sentence that a grammar can generate, use the command generate_trees = gt. @@ -203,7 +203,7 @@ trees in your grammar, it would never terminate. Why? -

More on pipes; tracing

+

More on pipes; tracing

A pipe of GF commands can have any length, but the "output type" (either string or tree) of one command must always match the "input type" @@ -227,7 +227,7 @@ contains strings that can be parsed in more than one way. -

Writing and reading files

+

Writing and reading files

To save the outputs of GF commands into a file, you can pipe it to the write_file = wf command, @@ -248,9 +248,7 @@ a sentence but a sequence of ten sentences. -

Labelled context-free grammars

- -

Rules and labels

+

Labelled context-free grammars

The syntax trees returned by GF's parser in the previous examples are not so nice to look at. The identifiers of form Mks @@ -280,7 +278,313 @@ is the label of the rule prefixing this to a common noun, Mks_18 is the label of the adjective thick, and so on. + +

The labelled context-free format

+The labelled context-free grammar format permits user-defined +labels to each rule. GF recognizes files of this format by the suffix +.cf. Let us include the following rules in the file +paleolithic.cf. +
+  PredVP.  S   ::= NP VP ;
+  UseV.    VP  ::= V ;
+  ComplTV. VP  ::= TV NP ;
+  UseA.    VP  ::= "is" A ;
+  This.    NP  ::= "this" CN ; 
+  That.    NP  ::= "that" CN ; 
+  Def.     NP  ::= "the" CN ;
+  Indef.   NP  ::= "a" CN ;  
+  ModA.    CN  ::= A CN ;
+  Bird.    CN  ::= "bird" ;
+  Boy.     CN  ::= "boy" ;
+  Man.     CN  ::= "man" ;
+  Louse.   CN  ::= "louse" ;
+  Snake.   CN  ::= "snake" ;
+  Worm.    CN  ::= "worm" ;
+  Big.     A   ::= "big" ;
+  Green.   A   ::= "green" ;
+  Rotten.  A   ::= "rotten" ;
+  Thick.   A   ::= "thick" ;
+  Warm.    A   ::= "warm" ;
+  Laugh.   V   ::= "laughs" ;
+  Sleep.   V   ::= "sleeps" ;
+  Swim.    V   ::= "swims" ;
+  Eat.     TV  ::= "eats" ;
+  Kill.    TV  ::= "kills" 
+  Wash.    TV  ::= "washes" ;
+
+ + +

Using the labelled context-free format

+ +The GF commands for the .cf format are +exactly the same as for the .ebnf format. +Just the syntax trees become nicer to read and +to remember. Notice that before reading in +a new grammar in GF you often (but not always, +as we will see later) have first to give the +command (empty = e), which removes the +old grammar from the GF shell state. +
+  > empty
+
+  > i paleolithic.cf
+
+  > p "the boy eats a snake"
+  PredVP (Def Boy) (ComplTV Eat (Indef Snake))
+
+  > gr -tr | l
+  PredVP (Indef Louse) (UseA Big)
+  a louse is big
+
+ + + +

The GF grammar format

+ +To see what there really is in GF's shell state when a grammar +has been imported, you can give the plain command +print_grammar = pg. +
+  > print_grammar
+
+The output is quite unreadable at this stage, and you may feel happy that +you did not need to write the grammar in that notation, but that the +GF grammar compiler produced it. + +

+ +However, we will now start to show how GF's own notation gives you +much more expressive power than the .cf and .ebnf +formats. We will introduce the .gf format by presenting +one more way of defining the same grammar as in +paleolithic.cf and paleolithic.ebnf. +Then we will show how the full GF grammar format enables you +to do things that are not possible in the weaker formats. + + + +

Abstract and concrete syntax

+ +A GF grammar consists of two main parts: + +The EBNF and CF formats fuse these two things together, but it is possible +to take them apart. For instance, the verb phrase predication rule +
+  PredVP. S ::= NP VP ;
+
+is interpreted as the following pair of rules: +
+  fun PredVP : NP -> VP -> S ;
+  lin PredVP x y = {s = x.s ++ y.s} ;
+
+The former rule, with the keyword fun, belongs to the abstract syntax. +It defines the function +PredVP which constructs syntax trees of form +(PredVP x y). + +

+ +The latter rule, with the keyword lin, belongs to the concrete syntax. +It defines the linearization function for +syntax trees of form (PredVP x y). + + + +

Judgement forms

+ +Rules in a GF grammar are called judgements, and the keywords +fun and lin are used for distinguishing between two +judgement forms. Here is a summary of the most important +judgement forms: + +We return to the precise meanings of these judgement forms later. +First we will look at how judgements are grouped into modules, and +show how the grammar paleolithic.cf is +expressed by using modules and judgements. + + + +

Module types

+ +A GF grammar consists of modules, +into which judgements are grouped. The most important +module forms are + + + +

An abstract syntax example

+ +Each nonterminal occurring in paleolithic.cf is +introduced by a cat judgement. Each +rule label is introduced by a fun judgement. +
+abstract Paleolithic = {
+cat 
+  S ; NP ; VP ; CN ; A ; V ; TV ; 
+fun
+  PredVP  : NP -> VP -> S ;
+  UseV    : V -> VP ;
+  ComplTV : TV -> NP -> VP ;
+  UseA    : A -> VP ;
+  ModA    : A -> CN -> CN ;
+  This, That, Def, Indef : CN -> NP ; 
+  Bird, Boy, Man, Louse, Snake, Worm : CN ;
+  Big, Green, Rotten, Thick, Warm : A ;
+  Laugh, Sleep, Swim : V ;
+  Eat, Kill, Wash : TV ;
+}
+
+Notice the use of shorthands permitting the sharing of +the keyword in subsequent judgements, and of the type +in subsequent fun judgements. + + + +

A concrete syntax example

+ +Each category introduced in Paleolithic.gf is +given a lincat rule, and each +function is given a fun rule. Similar shorthands +apply as in abstract modules. +
+concrete PaleolithicEng of Paleolithic = {
+lincat 
+  S, NP, VP, CN, A, V, TV = {s : Str} ; 
+lin
+  PredVP np vp  = {s = np.s ++ vp.s} ;
+  UseV   v      = v ;
+  ComplTV tv np = {s = tv.s ++ np.s} ;
+  UseA   a   = {s = "is" ++ a.s} ;
+  This  cn   = {s = "this" ++ cn.s} ; 
+  That  cn   = {s = "that" ++ cn.s} ; 
+  Def   cn   = {s = "the" ++ cn.s} ;
+  Indef cn   = {s = "a" ++ cn.s} ; 
+  ModA  a cn = {s = a.s ++ cn.s} ;
+  Bird   = {s = "bird"} ;
+  Boy    = {s = "boy"} ;
+  Louse  = {s = "louse"} ;
+  Man    = {s = "man"} ;
+  Snake  = {s = "snake"} ;
+  Worm   = {s = "worm"} ;
+  Big    = {s = "big"} ;
+  Green  = {s = "green"} ;
+  Rotten = {s = "rotten"} ;
+  Thick  = {s = "thick"} ;
+  Warm   = {s = "warm"} ;
+  Laugh  = {s = "laughs"} ;
+  Sleep  = {s = "sleeps"} ;
+  Swim   = {s = "swims"} ;
+  Eat    = {s = "eats"} ;
+  Kill   = {s = "kills"} ; 
+  Wash   = {s = "washes"} ;
+}
+
+ + + +

Modules and files

+ +Module name + .gf = file name + +

+ +Each module is compiled into a .gfc file. + +

+ +Import PaleolithicEng.gf and try what happens +

+
+
+Nothing more than before, except that the GFC files +are generated. + + + +

An Italian concrete syntax

+ +
+concrete PaleolithicIta of Paleolithic = {
+lincat 
+  S, NP, VP, CN, A, V, TV = {s : Str} ; 
+lin
+  PredVP np vp  = {s = np.s ++ vp.s} ;
+  UseV   v      = v ;
+  ComplTV tv np = {s = tv.s ++ np.s} ;
+  UseA   a   = {s = "è" ++ a.s} ;
+  This  cn   = {s = "questo" ++ cn.s} ; 
+  That  cn   = {s = "quello" ++ cn.s} ; 
+  Def   cn   = {s = "il" ++ cn.s} ;
+  Indef cn   = {s = "un" ++ cn.s} ; 
+  ModA  a cn = {s = cn.s ++ a.s} ;
+  Bird   = {s = "uccello"} ;
+  Boy    = {s = "ragazzo"} ;
+  Louse  = {s = "pidocchio"} ;
+  Man    = {s = "uomo"} ;
+  Snake  = {s = "serpente"} ;
+  Worm   = {s = "verme"} ;
+  Big    = {s = "grande"} ;
+  Green  = {s = "verde"} ;
+  Rotten = {s = "marcio"} ;
+  Thick  = {s = "grosso"} ;
+  Warm   = {s = "caldo"} ;
+  Laugh  = {s = "ride"} ;
+  Sleep  = {s = "dorme"} ;
+  Swim   = {s = "nuota"} ;
+  Eat    = {s = "mangia"} ;
+  Kill   = {s = "uccide"} ; 
+  Wash   = {s = "lava"} ;
+}
+
+ + +

Using a multilingual grammar

+ +Import without first emptying +
+
+
+Try generation now: +
+
+
+Translate by using a pipe: +
+
+
+Inspect the shell state (print_options = po): +
+  > print_options
+  main abstract :     Paleolithic
+  main concrete :     PaleolithicIta
+  all concretes :     PaleolithicIta PaleolithicEng
+
+ + + +

Extending the grammar

+ +Neolithic diff --git a/doc/tutorial/neolithic.cf b/doc/tutorial/neolithic.cf deleted file mode 100644 index d9869a257..000000000 --- a/doc/tutorial/neolithic.cf +++ /dev/null @@ -1,26 +0,0 @@ -PredVP. S ::= NP VP ; -UseV. VP ::= V ; -ComplTV. VP ::= TV NP ; -UseA. VP ::= "is" A ; -This. NP ::= "this" CN ; -That. NP ::= "that" CN ; -Def. NP ::= "the" CN ; -Indef. NP ::= "a" CN ; -ModA. CN ::= A CN ; -Bird. CN ::= "bird" ; -Boy. CN ::= "boy" ; -Man. CN ::= "man" ; -Louse. CN ::= "louse" ; -Snake. CN ::= "snake" ; -Worm. CN ::= "worm" ; -Big. A ::= "big" ; -Green. A ::= "green" ; -Rotten. A ::= "rotten" ; -Thick. A ::= "thick" ; -Warm. A ::= "warm" ; -Laugh. V ::= "laughs" ; -Sleep. V ::= "sleeps" ; -Swim. V ::= "swims" ; -Eat. TV ::= "eats" ; -Kill. TV ::= "kills" -Wash. TV ::= "washes" ;