Newer
Older
zweic / doc / concrete_syntax.txt
@glproj03 glproj03 on 22 Nov 2005 2 KB Added grammar description
################################################################################
                             LEXICAL GRAMMAR
################################################################################

input           = { inputelement }

inputelement    = whitespace
                | comment
                | token

token           = ident
                | number
                | string
                | '('
                | ')'
                | ...         /* to complete */

comment         = '/' '/' { cchar }

ident           = letter { letter | digit | '_' }

number          = '0'
                | digit1 { digit }

string          = '"' { schar } '"'

whitespace      = ' ' | '\t' | '\f' | '\n'

letter          = 'a' | ... | 'z' | 'A' | ... | 'Z'

digit           = '0' | digit1
digit1          = '1' | ... | '9'

cchar           = all characters except for '\n'
schar           = all characters except for '\n' and '"'


################################################################################
                           SYNTACTICAL GRAMMAR
################################################################################

Program        = { ClassDecl } Expression

ClassDecl      = "class" ident [ "extends" ident ] "{" { Member } "}"

Member         = FieldDecl
               | MethodDef

FieldDecl      = Formal ";"

MethodDef      = Formal "(" [ Formal { "," Formal} ] ")" Block

Formal         = Type ident

Block          = "{" { Statement } [ "return" Expression ] "}"

Type           = "Int"
               | "Null"
               | ident

Statement      = "while" "(" Expression ")" "{" { Statement } "}"
               | Formal "=" Expression ";"
               | ident "=" Expression ";"
               | Expression ";"
               | "printInt" "(" Expression ")" ";"
               | "printChar" "(" Expression ")" ";"

Expression     = "if" "(" Expression ")" Expression [ "else" Expression ]
               | CmpExpression

CmpExpression  = [ SumExpression CompOp ] SumExpression

CompOp         = "=="
               | "!="
               | "<"
               | ">"
               | "<="
               | ">="

SumExpression  = Term
               | SumExpression SumOp Term

SumOp          = "+"
               | "-"
               | "||"

Term           = [ Term ProdOp ] [ NegateOp ] Factor

NegateOp       = "-"
               | "!"

ProdOp         = "*"
               | "/"
               | "%"
               | "&&"

Factor         = ident
               | number
               | string
               | "true"
               | "false"
               | "this"
               | "null"
               | "readInt"
               | "readChar"
               | "(" Expression ")"
               | Block 
               | "new" ident Params
               | Factor "." ident
               | Factor "." ident Params

Params         = "(" Expressions ")"

Expressions    = [ Expression { "," Expression } ]

################################################################################
$Id$