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 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
|
COMPILER C
(* An attempt to describe a subset of C *)
CHARACTERS
letter = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" .
digit = "0123456789" .
hexdigit = digit + "ABCDEFabcdef" .
tab = CHR(9) .
lf = CHR(10) .
eol = CHR(13) .
files = letter + digit + ":\." .
chars = CHR(32) .. CHR(255) - "'" .
strings = CHR(32) .. CHR(255) - '"'.
macros = ANY - eol .
COMMENTS FROM "/*" TO "*/"
IGNORE
tab + eol + lf
TOKENS
identifier = ( "_" | letter) { "_" | letter | digit } .
number = digit { digit } [ "." { digit }] [ "U" | "u" | "L" | "l" ] .
hexnumber = "0" ( "x" | "X" ) hexdigit { hexdigit } [ "U" | "u" | "L" | "l" ] .
string = '"' { strings } '"' .
char = "'" [ "\" ] chars "'" .
library = "<" files { files } ">" .
PRAGMAS
PreProcessor = "#" {macros} .
PRODUCTIONS
C = { Definition } EOF .
(* Variable and Type Definitions *)
Definition = [ StorageClass ] Type { "*" } identifier
( FunctionDefinition | VarList ";" ) .
StorageClass = "auto" | "extern" | "register" | "static" .
Type = "short" [ "int" ]
| "long" [ "int" | "float" ]
| "unsigned" [ "char" | "int" | "long" ]
| "char" | "int" | "float" | "double" | "void" .
VarList = ArraySize { "," { "*" } identifier ArraySize } .
ArraySize = { "[" [ ConstExpression ] "]" } .
(* Function Definitions *)
FunctionDefinition = FunctionHeader ( ";" | FunctionBody ) .
FunctionHeader = "(" [ FormalParamList ] ")" .
FunctionBody = CompoundStatement .
FormalParamList = FormalParameter { "," FormalParameter } .
FormalParameter = Type { "*" } [ identifier ] ArraySize .
(* Statements *)
Statement = { Label }
( AssignmentExpression | BreakStatement
| CompoundStatement | ContinueStatement
| DoStatement | ForStatement
| IfStatement | NullStatement
| ReturnStatement | SwitchStatement
| WhileStatement ) .
Label = "case" ConstExpression ":" | "default" ":" .
(* There is no requirement that a switch statement be followed by a compound
statement. Actually labels may be even more general *)
AssignmentExpression = Expression ";" .
BreakStatement = "break" ";" .
CompoundStatement = "{" { LocalDeclaration } { Statement } "}" .
ContinueStatement = "continue" ";" .
DoStatement = "do" Statement "while" "(" Expression ")" ";" .
ForStatement = "for" "(" [ Expression ] ";" [ Expression ] ";" [ Expression ] ")" Statement .
IfStatement = "if" "(" Expression ")" Statement [ "else" Statement ] .
NullStatement = ";" .
ReturnStatement = "return" [ Expression ] ";" .
(* Expression usually in parentheses *)
SwitchStatement = "switch" "(" Expression ")" Statement .
WhileStatement = "while" "(" Expression ")" Statement .
(* LocalDeclarations *)
LocalDeclaration = [ StorageClass ] Type { "*" } identifier
( FunctionHeader | VarList ) ";" .
(* Expressions, based on Kernighan and Ritchie: "The C Programming Language".
There does not seem to be a way to make this work in an LL(1) fashion,
but this generates a "working" parser *)
ConstExpression = Expression .
Expression = Conditional { AssignmentOperator Expression }.
Conditional = LogORExp .
LogORExp = LogANDExp { "||" LogANDExp } .
LogANDExp = InclORExp { "&&" InclORExp }.
InclORExp = ExclORExp { "|" ExclORExp } .
ExclORExp = ANDExp { "^" ANDExp } .
ANDExp = EqualExp { "&" EqualExp } .
EqualExp = RelationExp { ( "==" | "!=" ) RelationExp } .
RelationExp = ShiftExp { ( "<" | ">" | "<=" | ">=" ) ShiftExp }.
ShiftExp = AddExp { ( "<<" | ">>" ) AddExp } .
AddExp = MultExp { ( "+" | "-" ) MultExp } .
MultExp = CastExp { ( "*" | "/" | "%" ) CastExp } .
CastExp = UnaryExp .
(* we should really add
| "(" identifier ")" CastExp .
but this breaks it badly *)
UnaryExp = PostFixExp
| ( "++" | "--" ) UnaryExp
| UnaryOperator CastExp .
(* we should really add
| "sizeof" ( UnaryExp | "(" Type ")" ) .
but this breaks it badly *)
PostFixExp = Primary
{ "[" Expression "]"
| FunctionCall
| "." identifier
| "->" identifier
| "++"
| "--"
} .
Primary = identifier | string | char | number | "(" Expression ")" .
FunctionCall = "(" [ ActualParameters ] ")" .
ActualParameters = Expression { "," Expression } .
AssignmentOperator = "=" | "*=" | "/=" | "%=" | "+=" | "-=" | "&="
| "^=" | "|=" | "<<=" | ">>=" .
UnaryOperator = "+" | "-" | "*" | "!" | "&" | "~" .
END C.
|