記一次寫的編譯原理文法屎山

Q·K發表於2024-05-15

哎,,,,看了南大的感覺被薄紗。
文法:
/* Program */
Program->Blocks
Blocks->{Declarations Statements}
| Type id(Declarations) {Declarations Statements}
| Declarations
| StructDeclarations Blocks

/* Declaration */
Declarations->Declarations Declaration | ε
Declaration->Type idArray; | Type Optional; | Type idArray = {List};
StructDeclarations -> StructDeclaration StructDeclarations | ε
StructDeclaration -> struct id { Fields } ;
Fields -> FieldDeclaration MoreFields
MoreFields -> FieldDeclaration MoreFields | ε
FieldDeclaration -> Type idArray ;
Optional->id=Bool
Array->[num]Array | ε
List->num , List | num
Type-> Basic | Basic *
Basic-> float | int | double | char | id

/* Statement */
Statements->Statements Statement
| ε
Statement-> Location=Bool;
| if(Bool) Statement
| if(Bool) Statement else Statement
| while (Bool) Statement
| do Statement while(Bool);
| break;
| Blocks
| return num;
Location->Location[num] | Location[id] | id

/* Condition Expressions */
Bool->Bool || Join | Join
Join->Join && Equality | Equality
Equality->Equality == Relationship
| Equality != Relationship
| Relationship
Relationship-> Expression<Expression
| Expression<=Expression
| Expression>=Expression
| Expression>Expression
| Expression

/* Arithmetic Expressions /
Expression->Expression+Term | Expression-Term | Term
Term->Term
Unary | Term/Unary | Unary
Unary->!Unary | -Unary | Factor
Factor->(Bool) | Location | num | real | true | false

相關文章