1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
|
COMPILER CMinus
(* Simple CMinus grammar. P.D. Terry, Rhodes University, 1995 *)
CHARACTERS
eol = CHR(13) .
letter = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" .
digit = "0123456789" .
noquote1 = ANY - "'" - eol .
noquote2 = ANY - '"' - eol .
IGNORE CHR(9) .. CHR(13)
COMMENTS FROM "//" TO eol
TOKENS
identifier = letter {letter | digit} .
number = digit { digit} .
string = '"' (noquote2 | '\"') { noquote2 | '\"' } '"' .
char = "'" ["\"] noquote1 "'" | "'\''" .
PRODUCTIONS
(* The main program can nest procedures, but procedures cannot nest procedures
themselves. This can be handled in a context free way by having a
MainBlock and a ProcBlock non-terminal. *)
CMinus = "void" identifier FormalParameters
MainBlock .
MainBlock = "{"
{ ConstDeclaration
| ProcDeclaration
| VarDeclarations }
{ Statement }
"}" .
ConstDeclaration = "const" identifier "=" number ";" .
VarDeclarations = "int"
identifier [ UpperBound ]
{ "," identifier [ UpperBound ] } ";" .
UpperBound = "[" ( number | identifier ) "]" .
ProcDeclaration = ( "function" | "void" ) identifier FormalParameters
( ProcBlock | ";" ) .
FormalParameters = "(" [ FormalPar { "," FormalPar } ] ")" .
FormalPar = identifier [ "[" "]" ] .
ProcBlock = "{"
{ ConstDeclaration | VarDeclarations }
{ Statement }
"}" .
(* The rule for semicolons is very different from Pascal/Modula, where they
are separators. *)
CompoundStatement = "{" { Statement } "}" .
Statement = CompoundStatement | AssignmentOrCall
| IfStatement | WhileStatement | ForStatement
| LoopStatement | ExitStatement | WriteStatement
| ReadStatement | ReturnStatement | ";" .
AssignmentOrCall = Designator
( "=" Expression | ActualParameters | "++" | "--" ) ";" .
Designator = identifier [ "[" Expression "]" ] .
ActualParameters = "(" [ ActualPar { "," ActualPar } ] ")" .
ActualPar = Expression .
IfStatement = "if" "(" Expression ")" Statement
[ "else" Statement ] .
WhileStatement = "while" "(" Expression ")" Statement .
ForStatement = "for" "(" [ Initial ] ";" Expression ";" [ Final ] ")"
Statement .
Initial = Designator "=" Expression .
Final = Designator ( "++" | "--" ) .
LoopStatement = "loop" Statement .
ExitStatement = "exit" ";" .
WriteStatement = "cout" "<<" Expression { "<<" Expression } ";" .
ReadStatement = "cin" ">>" Designator { ">>" Designator } ";" .
ReturnStatement = "return" [ Expression ] ";" .
Expression = SimpleExpression [ RelOp SimpleExpression ] .
SimpleExpression = [ Sign ] Term { AddOp Term } .
Term = Factor { MulOp Factor } .
Factor = Designator [ ActualParameters ]
| number | char | string
| "(" Expression ")"
| "!" Factor .
Sign = "+" | "-" .
AddOp = "+" | "-" | "||" .
MulOp = "*" | "/" | "%" | "&&" .
RelOp = "==" | "!=" | "<" | "<=" | ">" | ">=" .
END CMinus.
|