Universität Paderborn - Home Universität Paderborn
Die Universität der Informationsgesellschaft

Generating Software from Specifications WS 2013/14 - File TryScan.fw

@=~
~p maximum_input_line_length = infinity

The concrete grammar specifies a sequence of Elements
separated by commas. The elements are named terminals.
~O~<TryScan.con~>~{
Specification:	Sequence.
Sequence:	Sequence ',' Element / Element.
Element:	Ident / Number.
~}

Two tokens are specified here.
Replace them by your favorite token notations and
test the generated scanner and parser:
~O~<TryScan.gla~>~{
Ident:  $[a-zA-Z][a-zA-Z0-9]* [myToken]
Number: $[0-9]+               [myToken]
~}

Here is a correct input file:
~O~<in.ok~>~{~-

~}

Here is an erroneous input file:
~O~<in.err~>~{~-

~}

The following rules of the abstract syntax are used in Exercise 6c
to output token encodings.
~O~<TokenSpecs.lido~>~{
RULE p1: Element ::= Ident COMPUTE
/*
  printf ("p1: token code %d in line %d\n", Ident, LINE);
*/
END;

RULE p2: Element ::= Number COMPUTE
/*
  printf ("p2: token code %d in line %d\n", Number, LINE);
*/
END;
~}

The following three macros implement a little C module.
It defines functions to be called as a token encoding functions.
In the present state myToken only prints some information on every
accepted token:
~O~<TryScan.HEAD.phi~>~{
#include "csm.h"
#include "TryScan.h"
~}
~O~<TryScan.h~>~{
extern void myToken (char *start, int length, int *class, int *intrinsic);
extern void mkDay (char *start, int length, int *class, int *intrinsic);
~}
~O~<TryScan.c~>~{
#include <stdio.h>
#include "TryScan.h"

int count = 0;

void myToken (char *start, int length, int *class, int *intrinsic)
{
  *intrinsic=count++;
  printf ("token %d first char >%c<\n", count, *start);
}

void mkDay (char *d, int l, int *c, int *i)
{
    switch (d[0]) {
        case 'F': *i = 5; break;
        case 'M': *i = 1; break;
        case 'W': *i = 3; break;
        case 'S': *i = (d[1] == 'a'? 6 : 7); break;
        case 'T': *i = (d[1] == 'u' ? 2 : 4); break;
    }
}
~}

Generiert mit Camelot | Probleme mit Camelot? | Geändert am: 30.10.2013