CpSc 423, Language Processor Techniques

Grammar for Programming Assignments

Introduction

This is the grammar for the language we will use for Programming Assignments this semester. It is a simplified and slightly altered version of the Oberon-0 language defined in Niklaus Wirth's book Compiler Construction (Addison Wesley, 1996).

The language also allows comments (* delimited like this *). Comments are not included in the grammar because they are usually removed during the lexical analysis phase.

EBNF

Words and symbols appearing in "QUOTES" are terminals, and should be interpreted literally. Words without quotes are non-terminals; you should find each on the left-hand side of a production. Items appearing in [square brackets] are optional. Items in {curly braces} may be repeated any number of times (including zero). Alternatives are separated by vertical bar (|) characters. Parentheses (like these) are used for grouping.

Grammar

module = "MODULE" ident ";" declarations ["BEGIN" StatementSequence] "END" ident "."

ident = letter {letter | digit}

declarations = ["VAR" {IdentList ":" "INTEGER" ";"}]

StatementSequence = statement {";" statement}

letter = "A" | "B" | "C" | "D" | "E" | "F" | "G" | "H" | "I" | "J" | "K" | "L" | "M" | "N" | "O" | "P" | "Q" | "R" | "S" | "T" | "U" | "V" | "W" | "X" | "Y" | "Z" | "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | "i" | "j" | "k" | "l" | "m" | "n" | "o" | "p" | "q" | "r" | "s" | "t" | "u" | "v" | "w" | "x" | "y" | "z"

digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"

identList = ident { "," ident }

statement = [assignment | IfStatement | WhileStatement | WriteStatement]

assignment = ident ":=" expression

IfStatement = "IF" expression "THEN" StatementSequence { "ELSIF" expression "THEN" StatementSequence } [ "ELSE" StatementSequence ] "END"

WhileStatement = "WHILE" expression "DO" StatementSequence "END"

WriteStatement = "WriteLn" "(" expression ")"

expression = SimpleExpression [("=" | "<>" | "<" | ">") SimpleExpression]

SimpleExpression = ["+" | "-"] term {("+" | "-" | "OR") term}

term = factor {("*" | "DIV" | "MOD" | "AND") factor }

factor = ident | integer | "(" expression ")" | "NOT" factor

integer = digit {digit}