| ghc -iparsecDir -fglasgow-exts -o mimic -fallow-undecidable-instances -fallow-overlapping-instances --make mimico |
Flags that indicate Haskell extensions are necessary because Mímico uses Parsec. Parsec uses a Haskell extension — the forall keyword — to "simulate" parametrized modules (by "records" with polymorphic fields). parsecDir is where parsec's input files are, say /home/somedir/parsec, or say c:\parsec.
| mimic foo |
You can also type mimic foo.gram.
The executable file mimic must of course be accessible (i.e. it must be in one of the directories defined by the $PATH environment variable).
This generates file foo.hs, which contains a Haskell program of a recursive descent monadic parser that "mimicks the specification" — i.e. "mimicks" the behavior of the parser specified in the input file, foo.gram.
| ghci -fglasgow-exts -imimicoDir:parsecDir -fallow-undecidable-instances -fallow-overlapping-instances |
mimicoDir is needed because Mímico slightly extends a few parsec files, which are put in mimico's source directory.
Then run foo by typing either:
| compile "str" |
| compileFile "f" |
| ghc -fglasgow-exts -imimicoDir:parsecDir -fallow-undecidable-instance -fallow-overlapping-instances -o foo foo.hs |
| foo "f" |
• Predefined Parsers
The prefix d- (for "delimited") denotes that some sequence of symbols (that following the prefix) must be followed by spaces.
| Predefined parser name | Informal meaning | Parsec equivalent |
| _n | A d-sequence of digits | lexeme (many1 digit) |
| _N | A sequence of digits | many1 digit |
| _digit | A d-digit | lexeme digit |
| _Digit | A digit | digit |
| _char | A d-character | lexeme anyChar |
| _Char | A character | anyChar |
| _letter | A d-letter | lexeme letter |
| _Letter | A letter | letter |
| _lower | A d-lower case letter | lexeme lower |
| _Lower | A lowercase letter | lower |
| _upper | A d-uppercase letter | lexeme upper |
| _Upper | An uppercase letter | upper |
| _ident | A d-Haskell-style identifier | lexeme ident |
| _int | A d-integer literal | integer |
| _float | A d-floating point literal (Haskell style) | float |
| _nat | A d-natural number literal | natural |
| _eoln | d-character '\n' | lexeme (satisfy ('\\n')) |
| _Eoln | The character '\n' | satisfy ('\\n') |
• Associativity
Usually Mímico uses the Pastor rule (parse as specified by the textual order). However, for left recursive productions, the left recursive alternative is always tried first. In this case, left and right associativity may be explicitly specified (the default is right associativity), as shown in the following example for adding and subtracting integer literals.
| e | = e r | {| r e |} |
| .| int_ | {| int_ |} | |
| r | = + e | {| \ e' −> e' + e |} |
| | − e | {| \ e' −> e' − e |} |
".|" indicates left-associativity. Thus, according to this grammar, 1−2+3 is parsed as (1−2)+3.
"|." or the absence of a dot in left-recursive alternatives means right-associativity (the default).