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

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

@=~
~p maximum_input_line_length = infinity

This file contains the specification of a language
for sequences of declarations and statements. 
Try to generate a processor for it. 

If the parser generation indicates problems, analyse their
reasons and eliminate them. 

The following file contains correct input for the processor:
Give an example for a correct input:
~O~<Expression.ok~>~{~-
int (b);
h(a);
bool (c);
string (s);
a = 42;
g(a);
f(a+1);
~}

The following file specifies the concrete syntax:
~O~<Expression.ambig~>~{
Program:	Declarations Statements.
Declarations:	Declarations Declaration / Declaration.
Statements:	Statements Statement / Statement.

Declaration:	Type '(' Ident ')' ';'.
Type:		'int' / 'bool' / Ident.

Statement:	Assignment / Call.
Assignment:	VarIdent '=' Expression ';'.
VarIdent:	Ident.
Expression:	Expression '+' Operand / Operand.
Operand:	Number / VarIdent.
Call:		Ident '(' Expression ')' ';'.
~}
The grammar is ambiguous.

The parser generator indicates that a construct "Ident ( Ident );" 
can be reduced to a Declaration or to a Statement.
If it occurs after some number of Declarations, it may be reduced 
to the one or the other nonterminal.

The problem can be solved: Restrict Declarations to those which
have not the form of a call. Then specify a sequence which begins
with Declarations and Calls, after the first Assignment only
Assignments and calls may follow. 

The language of the following grammar is a superset of the language
of the ambiguous grammar. The constructs which could be reduced
to a Declaration or to a Statement have to be checked by semantic
analysis, whether they are a Statement or a Declaration, and are 
allowed at their position in the sequence.
~O~<Expression.con~>~{
Program:	A.
A:		Declaration B / Call B.
B:		Declaration B / Call B / Assignment C / .
C:		Assignment C / Call C / .

Declaration:	PlainType '(' Ident ')' ';'.
PlainType:	'int' / 'bool'.

Call:		Ident '(' Expression ')' ';'.

Assignment:	VarIdent '=' Expression ';'.
VarIdent:	Ident.
Expression:	Expression '+' Operand / Operand.
Operand:	Number / VarIdent.
~}


The following file specifies the non-literal tokens:
~O~<Expression.gla~>~{
Ident: C_IDENTIFIER
Number:     C_INTEGER
~}

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