CompilerDesignIFLR1/CompilerDesignIflr1/files/if-grammar.grammar

118 lines
2.0 KiB
Plaintext
Raw Normal View History

2024-12-23 01:42:58 +08:00
@top Program {
2024-12-20 20:20:34 +08:00
StatementList
}
StatementList {
2024-12-24 16:17:20 +08:00
LBrace Statement* RBrace
2024-12-20 20:20:34 +08:00
}
@skip { Whitespace }
IfStatement {
2024-12-24 16:17:20 +08:00
If ConditionPart PartIfStatement Else Statement | If ConditionPart Statement
2024-12-20 20:20:34 +08:00
}
PartIfStatement {
2024-12-24 16:17:20 +08:00
If ConditionPart PartIfStatement Else PartIfStatement | NoIfStatement
}
ConditionPart {
LParen Condition RParen
}
Condition {
ConditionalExpression (LogicalOperator ConditionalExpression)*
2024-12-20 20:20:34 +08:00
}
ConditionalExpression {
Expression Operator Expression | Expression
}
Expression {
Term (AddLike Term)*
}
Statement {
IfStatement | NoIfStatement
}
NoIfStatement {
2024-12-23 01:42:58 +08:00
AssignmentStatement Semicolon | VariableDefinition Semicolon | LBrace Statement* RBrace | ConstantDefinition Semicolon | Semicolon
2024-12-20 20:20:34 +08:00
}
AssignmentStatement {
Identifier Equal Expression
}
Term {
Factor (MultiplyLike Factor)*
}
ConstantDefinition {
Const VariableDefinition
}
2024-12-23 01:42:58 +08:00
VariableDefinition {
2024-12-24 16:17:20 +08:00
Type (Identifier | AssignmentStatement) (Comma Identifier | Comma AssignmentStatement)*
2024-12-23 01:42:58 +08:00
}
2024-12-20 20:20:34 +08:00
Type {
Int | Char
}
Factor {
Identifier | Number | Character | LParen Expression RParen
}
AddLike {
Plus | Minus
}
MultiplyLike {
Multiply | Divide | Modulo
}
Number {
UnsignedNumber | Minus UnsignedNumber | Plus UnsignedNumber
}
2024-12-23 01:42:58 +08:00
Operator {
2024-12-24 16:17:20 +08:00
EqualTo | NotEqualTo | LessThan | GreaterThan | LessThanOrEqual | GreaterThanOrEqual
}
LogicalOperator {
And | Or
2024-12-23 01:42:58 +08:00
}
2024-12-20 20:20:34 +08:00
@tokens {
If { "!if" }
Else { "!else" }
Int {"!int"}
Char {"!char"}
Const { "!const" }
Plus { "+" }
Minus { "-" }
Multiply { "*" }
Divide { "/" }
2024-12-27 01:25:50 +08:00
Modulo { "%" }
2024-12-20 20:20:34 +08:00
LParen { "(" }
RParen { ")" }
LBrace { "{" }
RBrace { "}" }
2024-12-23 01:42:58 +08:00
Comma { "," }
2024-12-20 20:20:34 +08:00
Semicolon { ";" }
Identifier { $[a-zA-Z_]$[a-zA-Z0-9_]* }
UnsignedNumber { $[0-9]+ }
String { "\"" $[\x00-\x7F]* "\"" }
Character { "'" $[\x00-\x7F] "'" }
2024-12-24 16:17:20 +08:00
EqualTo { "==" }
2024-12-23 01:42:58 +08:00
NotEqualTo { "!=" }
LessThan { "<" }
GreaterThan { ">" }
LessThanOrEqual { "<=" }
GreaterThanOrEqual { ">=" }
2024-12-20 20:20:34 +08:00
Equal { "=" }
Whitespace { $[\t\n\r]+ }
2024-12-24 16:17:20 +08:00
And { "&&" }
Or { "||" }
2024-12-20 20:20:34 +08:00
}