GF Quick Reference Aarne Ranta April 4, 2006 % NOTE: this is a txt2tags file. % Create an html file from this file using: % txt2tags -thtml gf-reference.t2t %!style:../css/style.css %!target:html %!options: --toc %!postproc(html):
-- tuple, same as {p1=p ; p2=q} p | q -- disjunction, binds to first match x@p -- binds x to what p matches - p -- negation p + "s" -- sequence of two string patterns p* -- repetition of a string pattern ``` ===Sample library functions=== ``` -- lib/prelude/Predef.gf drop : Int -> Tok -> Tok -- drop prefix of length take : Int -> Tok -> Tok -- take prefix of length tk : Int -> Tok -> Tok -- drop suffix of length dp : Int -> Tok -> Tok -- take suffix of length occur : Tok -> Tok -> PBool -- test if substring occurs : Tok -> Tok -> PBool -- test if any char occurs show : (P:Type) -> P ->Tok -- param to string read : (P:Type) -> Tok-> P -- string to param toStr : (L:Type) -> L ->Str -- find "first" string -- lib/prelude/Prelude.gf param Bool = True | False oper SS : Type -- the type {s : Str} ss : Str -> SS -- construct SS cc2 : (_,_ : SS) -> SS -- concat SS's optStr : Str -> Str -- string or empty strOpt : Str -> Str -- empty or string bothWays : Str -> Str -> Str -- X++Y or Y++X init : Tok -> Tok -- all but last char last : Tok -> Tok -- last char prefixSS : Str -> SS -> SS postfixSS : Str -> SS -> SS infixSS : Str -> SS -> SS -> SS if_then_else : (A : Type) -> Bool -> A -> A -> A if_then_Str : Bool -> Str -> Str -> Str ``` ===Flags=== Flags can appear, with growing priority, - in files, judgement ``flags`` and without dash (``-``) - as flags to ``gf`` when invoked, with dash - as flags to various GF commands, with dash Some common flags used in grammars: ``` startcat=cat use this category as default lexer=literals int and string literals recognized lexer=code like program code lexer=text like text: spacing, capitals lexer=textlit text, unknowns as string lits unlexer=code like program code unlexer=codelit code, remove string lit quotes unlexer=text like text: punctuation, capitals unlexer=textlit text, remove string lit quotes unlexer=concat remove all spaces unlexer=bind remove spaces around "&+" optimize=all_subs best for almost any concrete optimize=values good for lexicon concrete optimize=all usually good for resource optimize=noexpand for resource, if =all too big ``` For the full set of values for ``FLAG``, use on-line ``h -FLAG``. ===File paths=== Colon-separated lists of directories searched in the given order: ``` --# -path=.:../abstract:../common:prelude ``` This can be (in order of growing preference), as first line in the top file, as flag to ``gf`` when invoked, or as flag to the ``i`` command. The prefix ``--#`` is used only in files. If the environment variabls ``GF_LIB_PATH`` is defined, its value is automatically prefixed to each directory to extend the original search path. ===Alternative grammar formats=== **Old GF** (before GF 2.0): all judgements in any kinds of modules, division into files uses ``include``s. A file ``Foo.gf`` is recognized as the old format if it lacks a module header. **Context-free** (file ``foo.cf``). The form of rules is e.g. ``` Fun. S ::= NP "is" AP ; ``` If ``Fun`` is omitted, it is generated automatically. Rules must be one per line. The RHS can be empty. **Extended BNF** (file ``foo.ebnf``). The form of rules is e.g. ``` S ::= (NP+ ("is" | "was") AP | V NP*) ; ``` where the RHS is a regular expression of categories and quoted tokens: ``"foo", CAT, T U, T|U, T*, T+, T?``, or empty. Rule labels are generated automatically. **Probabilistic grammars** (not a separate format). You can set the probability of a function ``f`` (in its value category) by ``` --# prob f 0.009 ``` These are put into a file given to GF using the ``probs=File`` flag on command line. This file can be the grammar file itself. **Example-based grammars** (file ``foo.gfe``). Expressions of the form ``` in Cat "example string" ``` are preprocessed by using a parser given by the flag ``` --# -resource=File ``` and the result is written to ``foo.gf``. ===References=== [GF Homepage http://www.grammaticalframework.org/] A. Ranta, Grammatical Framework: A Type-Theoretical Grammar Formalism. //The Journal of Functional Programming//, vol. 14:2. 2004, pp. 145-189.