updated tutorial up to lexers ; fixed lexcode in GF (was wrong due to a typo)

This commit is contained in:
aarne
2008-11-10 10:59:13 +00:00
parent eedd209458
commit 87c6bebf29
4 changed files with 44 additions and 29 deletions

View File

@@ -3601,13 +3601,8 @@ tenses and moods, e.g. the Romance languages.
=Lesson 5: Refining semantics in abstract syntax=
**NOTICE**: The methods described in this lesson are not yet fully supported
in GF 3.0 beta. Use GF 2.9 to get all functionalities.
#Lchapsix
Goals:
- include semantic conditions in grammars, by using
- **dependent types**
@@ -3730,18 +3725,23 @@ to mark incomplete parts of trees in the syntax editor.
===Solving metavariables===
Use the command ``put_tree = pt`` with the flag ``-transform=solve``:
Use the command ``put_tree = pt`` with the option ``-typecheck``:
```
> parse "dim the light" | put_tree -transform=solve
> parse "dim the light" | put_tree -typecheck
CAction light dim (DKindOne light)
```
The ``solve`` process may fail, in which case no tree is returned:
The ``typecheck`` process may fail, in which case an error message
is shown and no tree is returned:
```
> parse "dim the fan" | put_tree -transform=solve
no tree found
> parse "dim the fan" | put_tree -typecheck
Error in tree UCommand (CAction ? 0 dim (DKindOne fan)) :
(? 0 <> fan) (? 0 <> light)
```
#NEW
==Polymorphism==
@@ -4016,14 +4016,17 @@ The linearization of the variable ``x`` is,
===Parsing variable bindings===
GF needs to know what strings are parsed as variable symbols.
This is defined in a special lexer,
GF can treat any one-word string as a variable symbol.
```
> p -cat=Prop -lexer=codevars "(All x)(x = x)"
> p -cat=Prop "( All x ) ( x = x )"
All (\x -> Eq x x)
```
More details on lexers #Rseclexing.
Variables must be bound if they are used:
```
> p -cat=Prop "( All x ) ( x = y )"
no tree found
```
@@ -4161,9 +4164,6 @@ Type checking can be invoked with ``put_term -transform=solve``.
==Lesson 6: Grammars of formal languages==
**NOTICE**: The methods described in this lesson are not yet fully supported
in GF 3.0 beta. Use GF 2.9 to get all functionalities.
#Lchapseven
@@ -4253,21 +4253,26 @@ The proper way would be
Moreover, the tokens ``"12"``, ``"3"``, and ``"4"`` should be recognized as
integer literals - they cannot be found in the grammar.
We choose a proper with a flag:
#NEW
Lexers are invoked by flags to the command ``put_string = ps``.
```
> parse -cat=Exp -lexer=codelit "(2 + (3 * 4))"
> put_string -lexcode "(2 + (3 * 4))"
( 2 + ( 3 * 4 ) )
```
This can be piped into a parser, as usual:
```
> ps -lexcode "(2 + (3 * 4))" | parse
EPlus (EInt 2) (ETimes (EInt 3) (EInt 4))
```
We could also put the flag into the grammar (concrete syntax):
```
flags lexer = codelit ;
```
In linearization, we use a corresponding **unlexer**:
```
> l -unlexer=code EPlus (EInt 2) (ETimes (EInt 3) (EInt 4))
> linearize EPlus (EInt 2) (ETimes (EInt 3) (EInt 4)) | ps -unlexcode
(2 + (3 * 4))
```
#NEW
===Most common lexers and unlexers===
@@ -4289,6 +4294,10 @@ In linearization, we use a corresponding **unlexer**:
| ``codelit`` | like code, but remove string literal quotes
| ``concat`` | remove all spaces
%TODO: update the names
%TODO: also on alphabet encodings - although somewhere else
#NEW
@@ -4501,7 +4510,7 @@ the prefix is ``f`` instead of ``i``, and that ``fconst`` takes floating
point literals as arguments.
%TODO: fix embedded grammars lesson
#NEW

View File

@@ -4,8 +4,9 @@ all: magnet
magnet:
# gfc --make -src --parser=off --name=fre BronzeageFre.gf +RTS -K100M
gfc --make -src --erasing=on --name=bul BronzeageBul.gf
gfc --make -src --parser=off --name=fin BronzeageFin.gf
gfc --make -src --erasing=on --name=ger BronzeageGer.gf
gfc --make -src BronzeageEng.gf BronzeageSwe.gf BronzeageIta.gf
gfc --make --name=grammar fin.pgf ger.pgf Bronzeage.pgf
gfc --make --name=grammar bul.pgf fin.pgf ger.pgf Bronzeage.pgf

View File

@@ -1,6 +1,6 @@
--# -path=.:../foods:present:prelude
instance LexFoodsFre of LexFoods = open SyntaxFre,ParadigmsFre in {
instance LexFoodsFre of LexFoods = open SyntaxFre,ParadigmsFre,IrregFre in {
oper
wine_N = mkN "vin" ;
pizza_N = mkN "pizza" feminine ;
@@ -12,4 +12,9 @@ instance LexFoodsFre of LexFoods = open SyntaxFre,ParadigmsFre in {
expensive_A = mkA "cher" ;
delicious_A = mkA "délicieux" ;
boring_A = mkA "ennuyeux" ;
drink_V2 = boire_V2 ;
eat_V2 = mkV2 (mkV "manger") ;
pay_V2 = mkV2 (mkV "payer") ;
gentleman_N = mkN "monsieur" "messieurs" masculine ;
lady_N = mkN "madame" "mesdames" feminine ;
}

View File

@@ -13,7 +13,7 @@ stringOp :: String -> Maybe (String -> String)
stringOp name = case name of
"chars" -> Just $ appLexer (filter (not . all isSpace) . map return)
"lextext" -> Just $ appLexer lexText
"lexcode" -> Just $ appLexer lexText
"lexcode" -> Just $ appLexer lexCode
"lexmixed" -> Just $ appLexer lexMixed
"words" -> Just $ appLexer words
"bind" -> Just $ appUnlexer bindTok