Skip to content
Florian Freudenberg edited this page Jul 4, 2013 · 25 revisions

Document Status: Draft / Work in Progress

This article is a draft and work in progress. Feel free to comment and discuss on the issue tracker.

Remarks

Several questions have to be addressed:

  • What is the definition of "id“, "num", "basic", "real" und "string"?
  • What is the semantic of "if (assign)" for "assign" -> "loc = assign"?
  • The grammar is ambiguous for "if else" (Dangling Else). How to solve it (prioritoes)?
  • What is the difference between "id" and "loc.id" for "loc"?
  • What is the semantic for "break" outside of a loop?

(Author: Sven, Translator: Flo)

Grammar rules

The provided grammmar can be found here: https://github.com/swp-uebersetzerbau-ss13/common/blob/master/doc/quellsprache.pdf?raw=true

program -> decls stmts
block -> {decls stmts}
decls -> decls decl | ϵ
decl -> type *id*;
type -> type [*num*] | *basic* | *record* {decls}
stmts -> stmts stmt | ϵ

stmt -> assign;
     | *if*(assign) stmt
     | *if*( assign ) stmt *else* stmt
     | *while*( assign ) stmt
     | *do* stmt *while* ( assign );
     | *break*;
     | *return*;
     | *return* loc;
     | *print* loc;
     | block
loc -> loc [assign] | *id* | loc.*id*

assign -> loc=assign | bool
bool -> bool||join | join
join -> join&&equality | equality
equality -> equality==rel | equality!=rel | rel
rel -> expr<expr | expr<=expr | expr>=expr | expr>expr | expr
expr -> expr+term | expr-term | term
term -> term*unary | term/unary | unary
unary -> !unary | -unary | factor
factor -> (assign) | loc | *num* | *real* | *true* | *false* | *string*

basic -> long | double | string | bool

string -> " (.*) " //?? escaping?

id -> ...

Note: *...* marks terminals

terminal definition

digit -> [0-9]
digits -> digit+
integralDigits -> -? digits
exponent -> ((e | E) integralDigits)
num -> digits exponent?
real -> digits . digits exponent?
basic -> (long | double | string | bool)
alpha -> [a-zA-Z]
alphaNumeric -> [a-zA-Z0-9]
id -> alpha alphaNumeric*

Semantic

Definition of semantic of each term (if necessary)

Variables

All variables are initialized with a default value:

  • long - 0
  • double - 0.0
  • bool - false
  • string - ""

All arrays and records are initialized similar to this.

Return

The return-statement may have a parameter of type LONG but no other. The value returns by non-parameter return is always 0 (and if there is no return defined and the last statement executed, the program also returns 0. An execution path with statements after the return is invalid.

! (unary not)

The unary not ! may only be used on bool-type values.

- (unary minus)

The unary minus - may only be used on long- or double-type values.

if - else

The condition value of the if-statement always has to be of bool-type and an else always belongs to the last defined if which has no associated else.

while / do-while

The condition value of the loop-statements always has to be of bool-type.

break

The break-statement is only valid inside of a loop. It aborts the loop execution of the directly containing loop.

+ - binary add

The binary add works for all primitive type by following rules:

  • Two long values results into a long value.
  • A double and a long or two double values results into a long value.

print

Print writes the value of basic type identifier to stdout.

Arrays

Index of arrays starts by 0. Declaration arrays and accessing its elements works in reverse ordering. If there is a multidimensional array like long[2][3][4] array; this is to be read as 4 arrays of 3 arrays of 2 longs. If it gets accessed by array[3][2][1] it works the other way around: access the last of the four 3 arrays of 2 longs, get than the last of the three 2 longs arrays and than the second long.