From 8f83b43cd6813f83dd88bd4be9140631a6e73c6f Mon Sep 17 00:00:00 2001 From: Wen HU Date: Mon, 5 Dec 2022 08:38:18 -0800 Subject: [PATCH 01/11] new literal.spec based on C11 specification, syntax only --- src/MapleFE/c/literal.spec | 216 ++++++++++++++++++++++++------------- 1 file changed, 140 insertions(+), 76 deletions(-) diff --git a/src/MapleFE/c/literal.spec b/src/MapleFE/c/literal.spec index 013f438418..87f6f3f6fd 100644 --- a/src/MapleFE/c/literal.spec +++ b/src/MapleFE/c/literal.spec @@ -11,108 +11,172 @@ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR # FIT FOR A PARTICULAR PURPOSE. # See the Mulan PSL v2 for more details. +# +# A literal is the source code representation of a value of a primitive type, the +# String type, or the null type. +# +# NOTE : Make sure there is a 'rule Literal'. This is the official rule recognized +# by autogen. + +# based on C11 specification A.1 Lexical grammar + +rule UniversalCharacterName : ONEOF( + '\' + 'u' + HexQuad + '\' + 'U' + HexQuad + HexQuad) + +rule HexQuad : ONEOF( + HexadecimalDigit + HexadecimalDigit + HexadecimalDigit + HexadecimalDigit) + +rule Constant : ONEOF( + IntegerConstant + FloatingConstant + EnumerationConstant + CharacterConstant) + +rule IntegerConstant : ONEOF( + DecimalConstant + ZEROORONE(IntegerSuffix) + OctalConstant + ZEROORONE(IntegerSuffix) + HexadecimalConstant + ZEROORONE(IntegerSuffix)) + +rule DecimalConstant : ONEOF( + NonzeroDigit + DecimalConstant + Digit) + +rule OctalConstant : ONEOF( + '0' + OctalConstant + OctalDigit) + +rule HexadecimalConstant : ONEOF( + HexadecimalPrefix + HexadecimalDigit + HexadecimalConstant + HexadecimalDigit) -######################################################################### -## Integer ## -######################################################################### +rule HexadecimalPrefix : ONEOF("0x", "0X") -### Decimal rules +rule NonzeroDigit : ONEOF('1', '2', '3', '4', '5', '6', '7', '8', '9') -rule NonZeroDigit : ONEOF('1', '2', '3', '4', '5', '6', '7', '8', '9') -rule Digit : ONEOF('0', NonZeroDigit) -rule DecimalNumeral : ONEOF('0', NonZeroDigit + ZEROORONE(Digit)) +rule OctalDigit : ONEOF('0', '1', '2', '3', '4', '5', '6', '7') -### Hexadecimal rules +rule HexadecimalDigit : ONEOF('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'A', 'B', 'C', 'D', 'E', 'F') -rule HexDigit : ONEOF('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', - 'a', 'b', 'c', 'd', 'e', 'f', - 'A', 'B', 'C', 'D', 'E', 'F') -rule HexNumeral : ONEOF("0x" + HexDigit, "0X" + HexDigit) +rule IntegerSuffix : ONEOF( + UnsignedSuffix + ZEROORONE(LongSuffix) + UnsignedSuffix + LongLongSuffix + LongSuffix + ZEROORONE(UnsignedSuffix) + LongLongSuffix + ZEROORONE(UnsignedSuffix)) -### Octal rules +rule UnsignedSuffix : ONEOF('u', 'U') -rule OctalDigit : ONEOF('0', '1', '2', '3', '4', '5', '6', '7') -rule OctalNumeral : ONEOF('0' + OctalDigit) +rule LongSuffix : ONEOF('l', 'L') -rule IntegerTypeSuffix : ONEOF('L', 'l') -rule DecimalIntegerLiteral: DecimalNumeral + ZEROORONE(IntegerTypeSuffix) -rule HexIntegerLiteral : HexNumeral + ZEROORONE(IntegerTypeSuffix) -rule OctalIntegerLiteral : OctalNumeral + ZEROORONE(IntegerTypeSuffix) +rule LongLongSuffix : ONEOF("ll", "LL") -rule IntegerLiteral: ONEOF(DecimalIntegerLiteral, - HexIntegerLiteral, - OctalIntegerLiteral) +rule FloatingConstant : ONEOF( + DecimalFloatingConstant + HexadecimalFloatingConstant) -######################################################################### -## Floating Point ## -######################################################################### +rule DecimalFloatingConstant : ONEOF( + FractionalConstant + ZEROORONE(ExponentPart) + ZEROORONE(FloatingSuffix) + DigitSequence + ExponentPart + ZEROORONE(FloatingSuffix)) -##### Decimal floating point literal +rule HexadecimalFloatingConstant : ONEOF( + HexadecimalPrefix + HexadecimalFractionalConstant + BinaryExponentPart + ZEROORONE(FloatingSuffix) + HexadecimalPrefix + HexadecimalDigitSequence + BinaryExponentPart + ZEROORONE(FloatingSuffix)) + +rule FractionalConstant : ONEOF( + ZEROORONE(DigitSequence) + '.' + DigitSequence + DigitSequence '.') + +rule ExponentPart : ONEOF( + 'e' + ZEROORONE(Sign) + DigitSequence + 'E' + ZEROORONE(Sign) + DigitSequence) rule Sign : ONEOF('+', '-') -rule FloatTypeSuffix : ONEOF('f', 'F') -rule ExponentIndicator : ONEOF('e', 'E') -rule SignedInteger : ZEROORONE(Sign) + Digit -rule ExponentPart : ExponentIndicator + SignedInteger -rule DecFPLiteral : ONEOF(Digit + '.' + ZEROORONE(Digit) + ZEROORONE(ExponentPart) + ZEROORONE(FloatTypeSuffix), - '.'+Digit + ZEROORONE(ExponentPart) + ZEROORONE(FloatTypeSuffix), - Digit + ExponentPart + ZEROORONE(FloatTypeSuffix), - Digit + ZEROORONE(ExponentPart)) +rule DigitSequence : ONEOF( + Digit + DigitSequence + Digit) -####### Hex floating point literal +rule HexadecimalFractionalConstant : ONEOF( + ZEROORONE(HexadecimalDigitSequence) '.' + HexadecimalDigitSequence + HexadecimalDigitSequence '.') -rule BinaryExponentIndicator : ONEOF('p', 'P') -rule BinaryExponent : BinaryExponentIndicator + SignedInteger -rule HexSignificand : ONEOF(HexNumeral + ZEROORONE('.'), - "0x" + ZEROORONE(HexDigit) + '.' + HexDigit, - "0X" + ZEROORONE(HexDigit) + '.' + HexDigit) -rule HexFPLiteral: HexSignificand + BinaryExponent + ZEROORONE(FloatTypeSuffix) +rule BinaryExponentPart : ONEOF( + 'p' + ZEROORONE(Sign) + DigitSequence + 'P' + ZEROORONE(Sign) + DigitSequence) -###### Floating Point Literal +rule HexadecimalDigitSequence : ONEOF( + HexadecimalDigit + HexadecimalDigitSequence + HexadecimalDigit) -rule FPLiteral : ONEOF(DecFPLiteral, HexFPLiteral) +rule FloatingSuffix : ONEOF('f', 'l', 'F', 'L') -######################################################################### -## Boolean ## -######################################################################### +rule EnumerationConstant : Identifier -rule BooleanLiteral : ONEOF ("true", "false") +rule CharacterConstant : ONEOF( + ''' + CCharSequence ''' + 'L' + ''' + CCharSequence + ''' + 'U' + ''' + CCharSequence + ''' + 'U' + ''' + CCharSequence + ''') + +rule CCharSequence : ONEOF( + CChar + CCharSequence + CChar) + +rule EscapeSequence : ONEOF( + SimpleEscapeSequence + OctalEscapeSequence + HexadecimalEscapeSequence + UniversalCharacterName) -######################################################################### -## Character ## -## ESCAPE is a reserved rule in reserved.spec. ## -######################################################################### +rule SimpleEscapeSequence : ONEOF( + '\' + ''', '\' + '"', '\' + '?', '\' + '\', + '\' + 'a', '\' + 'b', '\' + 'f', '\' + 'n', '\' + 'r', '\' + 't', '\' + 'v') -rule UnicodeEscape: '\' + 'u' + HEXDIGIT + HEXDIGIT + HEXDIGIT + HEXDIGIT -rule RawInputCharacter : ONEOF(ASCII, ''', ESCAPE) -rule SingleCharacter: ONEOF(UnicodeEscape, RawInputCharacter) +rule OctalEscapeSequence : ONEOF( + '\' + OctalDigit + '\' + OctalDigit + OctalDigit + '\' + OctalDigit + OctalDigit + OctalDigit) -rule OctalEscape : ONEOF('\' + '0', '\' + '1') -rule EscapeSequence : ONEOF(ESCAPE, OctalEscape) -rule CharacterLiteral : ''' + ONEOF(SingleCharacter, EscapeSequence) + ''' +rule HexadecimalEscapeSequence : ONEOF( + '\' + 'x' + HexadecimalDigit + HexadecimalEscapeSequence + HexadecimalDigit) -######################################################################### -## String ## -######################################################################### -# The UnicodeEscape is limited from \u0000 to \u00ff. -rule StringUnicodeEscape: '\' + 'u' + '0' + '0' + HEXDIGIT + HEXDIGIT -rule StringCharater: ONEOF(StringUnicodeEscape, RawInputCharacter) -rule StringLiteral : '"' + ZEROORMORE(StringCharater) + '"' +rule StringLiteral : ONEOF( + ZEROORONE(EncodingPrefix) + '"' + ZEROORONE(SCharSequence) + '"') -######################################################################### -## Null ## -######################################################################### +rule EncodingPrefix : ONEOF ( + "u8", 'u', 'U', 'L') + +rule SCharSequence : ONEOF( + SChar + SCharSequence + SChar) + +rule SChar : ONEOF( + CChar + EscapeSequence) + +####################################### +#rule CChar : ONEOF( +# CHAR +# '_' +# EscapeSequence) + +rule Digit : ONEOF(DIGIT) rule NullLiteral : "NULL" +rule FPLiteral : FloatingConstant +rule BooleanLiteral : ONEOF ("true", "false") +rule CharacterLiteral : CharacterConstant +rule IntegerLiteral : IntegerConstant -######################################################################### -## Literal ## -######################################################################### +rule Literal : ONEOF( + IntegerLiteral + FPLiteral + EnumerationConstant + CharacterLiteral + StringLiteral + NullLiteral) -rule Literal : ONEOF(IntegerLiteral, - FPLiteral, - BooleanLiteral, - CharacterLiteral, - StringLiteral, - NullLiteral) \ No newline at end of file -- Gitee From 8cc025dcceda6a769540faaf57165ced65c4f670 Mon Sep 17 00:00:00 2001 From: Wen HU Date: Wed, 7 Dec 2022 07:42:31 -0800 Subject: [PATCH 02/11] new stmt.spec for C --- src/MapleFE/c/literal.spec | 2 +- src/MapleFE/c/stmt.spec | 619 ++++++++++++++++++++++++++++++------- 2 files changed, 516 insertions(+), 105 deletions(-) diff --git a/src/MapleFE/c/literal.spec b/src/MapleFE/c/literal.spec index 87f6f3f6fd..2142416614 100644 --- a/src/MapleFE/c/literal.spec +++ b/src/MapleFE/c/literal.spec @@ -1,5 +1,5 @@ # -# Copyright (c) [2021] Huawei Technologies Co.,Ltd.All rights reserved. +# Copyright (c) [2021-2022] Huawei Technologies Co.,Ltd.All rights reserved. # # OpenArkCompiler is licensed under Mulan PSL v2. # You can use this software according to the terms and conditions of the Mulan PSL v2. diff --git a/src/MapleFE/c/stmt.spec b/src/MapleFE/c/stmt.spec index f71d36b960..a8fb287f68 100644 --- a/src/MapleFE/c/stmt.spec +++ b/src/MapleFE/c/stmt.spec @@ -1,5 +1,5 @@ # -# Copyright (c) [2021] Huawei Technologies Co.,Ltd.All rights reserved. +# Copyright (c) [2021-2022] Huawei Technologies Co.,Ltd.All rights reserved. # # OpenArkCompiler is licensed under Mulan PSL v2. # You can use this software according to the terms and conditions of the Mulan PSL v2. @@ -12,152 +12,563 @@ # FIT FOR A PARTICULAR PURPOSE. # See the Mulan PSL v2 for more details. -###################################################################### -# Expression # -###################################################################### +# based "on" C11 specification A.2 Phrase structure grammar rule PrimaryExpression : ONEOF( - Literal, - Identifier + Identifier + Constant + StringLiteral + '(' + Expression + ')' + GenericSelection ) -rule DimExprs : DimExpr + ZEROORMORE(DimExpr) +rule GenericSelection : ONEOF( + "_Generic" + '(' + AssignmentExpression + ',' + GenericAssocList + ')' +) -rule DimExpr : '[' + Expression + ']' +rule GenericAssocList : ONEOF( + GenericAssociation + GenericAssocList + ',' + GenericAssociation +) -rule Expression : ONEOF( - PrimaryExpression, - UnaryExpression) +rule GenericAssociation : ONEOF( + TypeName + ':' + AssignmentExpression + "default" + ':' + AssignmentExpression +) + +rule PostfixExpression : ONEOF( + PrimaryExpression + PostfixExpression + '[' + Expression + ']' + PostfixExpression + '(' + ZEROORONE(ArgumentExpressionList)+ ')' + PostfixExpression + '.' + Identifier + PostfixExpression + "->" + Identifier + PostfixExpression + "++" + PostfixExpression + "--" + '(' + TypeName + ')' + '{' + InitializerList + '}' + '(' + TypeName + ')' + '{' + InitializerList + ',' + '}' +) + +rule ArgumentExpressionList : ONEOF( + AssignmentExpression + ArgumentExpressionList + ',' + AssignmentExpression +) rule UnaryExpression : ONEOF( - PreIncrementExpression, - PreDecrementExpression, - PostIncrementExpression, - PostDecrementExpression) + PostfixExpression + "++" + UnaryExpression + "--" + UnaryExpression + UnaryOperator + CastExpression + "sizeof" + UnaryExpression + "sizeof" + '(' + TypeName + ')' + "_Alignof" + '(' + TypeName + ')' +) + +rule UnaryOperator: ONEOF( + '&', '*', '+', '-', '~', '!' +) + +rule CastExpression : ONEOF( + UnaryExpression + '(' + TypeName + ')' + CastExpression +) + +rule MultiplicativeExpression : ONEOF( + CastExpression + MultiplicativeExpression + '*' + CastExpression + MultiplicativeExpression + '/' + CastExpression + MultiplicativeExpression + '%' + CastExpression +) + +rule AdditiveExpression : ONEOF( + MultiplicativeExpression + AdditiveExpression + '+' + MultiplicativeExpression + AdditiveExpression + '-' + MultiplicativeExpression +) + +rule ShiftExpression : ONEOF( + AdditiveExpression + ShiftExpression + "<<" + AdditiveExpression + ShiftExpression + ">>" + AdditiveExpression +) + +rule RelationalExpression : ONEOF( + ShiftExpression + RelationalExpression + '<' + ShiftExpression + RelationalExpression + '>' + ShiftExpression + RelationalExpression + "<=" + ShiftExpression + RelationalExpression + ">=" + ShiftExpression +) + +rule EqualityExpression : ONEOF( + RelationalExpression + EqualityExpression + "==" + RelationalExpression + EqualityExpression + "!=" + RelationalExpression +) + +rule ANDExpression : ONEOF( + EqualityExpression + ANDExpression + '&' + EqualityExpression +) + +rule ExclusiveORExpression : ONEOF( + ANDExpression + ExclusiveORExpression + '^' + ANDExpression +) + +rule InclusiveORExpression : ONEOF( + ExclusiveORExpression + InclusiveORExpression + '|' + ExclusiveORExpression +) + +rule LogicalANDExpression : ONEOF( + InclusiveORExpression + LogicalANDExpression + "&&" + InclusiveORExpression +) + +rule LogicalORExpression : ONEOF( + LogicalANDExpression + LogicalORExpression + "||" + LogicalANDExpression +) + +rule ConditionalExpression : ONEOF( + LogicalORExpression + LogicalORExpression + '?' + Expression + ':' + ConditionalExpression +) + +rule AssignmentExpression : ONEOF( + ConditionalExpression + UnaryExpression + AssignmentOperator + AssignmentExpression +) + +rule AssignmentOperator: ONEOF( + '=', "*=", "/=", "%=", "+=", "-=", "<<=", ">>=", "&=", "^=", "|=" +) + +rule Expression : ONEOF( + AssignmentExpression + Expression + ',' + AssignmentExpression +) + +rule ConstantExpression : ONEOF( + ConditionalExpression +) + +rule Declaration : ONEOF( + DeclarationSpecifiers + ZEROORONE(InitDeclaratorList)+ ';' + Static_assertDeclaration +) + +rule DeclarationSpecifiers : ONEOF( + StorageClassSpecifier + ZEROORONE(DeclarationSpecifiers) + TypeSpecifier + ZEROORONE(DeclarationSpecifiers) + TypeQualifier + ZEROORONE(DeclarationSpecifiers) + FunctionSpecifier + ZEROORONE(DeclarationSpecifiers) + AlignmentSpecifier + ZEROORONE(DeclarationSpecifiers) +) + +rule InitDeclaratorList : ONEOF( + InitDeclarator + InitDeclaratorList + ',' + InitDeclarator +) + +rule InitDeclarator : ONEOF( + Declarator + Declarator + '=' + Initializer +) + +rule StorageClassSpecifier : ONEOF( + "typedef" + "extern" + "static" + "_Thread_local" + "auto" + "register" +) + +rule TypeSpecifier : ONEOF( + "void" + "char" + "short" + "int" + "long" + "float" + "double" + "signed" + "unsigned" + "_Bool" + "_Complex" + AtomicTypeSpecifier + StructOrUnionSpecifier + EnumSpecifier + TypedefName +) + +rule StructOrUnionSpecifier : ONEOF( + StructOrUnion + ZEROORONE(Identifier)+ '{' + StructDeclarationList + '}' + StructOrUnion + Identifier +) + +rule StructOrUnion : ONEOF( + "struct" + "union" +) + +rule StructDeclarationList : ONEOF( + StructDeclaration + StructDeclarationList + StructDeclaration +) + +rule StructDeclaration : ONEOF( + SpecifierQualifierList + ZEROORONE(StructDeclaratorList)+ ';' + Static_assertDeclaration +) + +rule SpecifierQualifierList : ONEOF( + TypeSpecifier + ZEROORONE(SpecifierQualifierList) + TypeQualifier + ZEROORONE(SpecifierQualifierList) +) + +rule StructDeclaratorList : ONEOF( + StructDeclarator + StructDeclaratorList + ',' + StructDeclarator +) + +rule StructDeclarator : ONEOF( + Declarator + ZEROORONE(Declarator)+ ':' + ConstantExpression +) + +rule EnumSpecifier : ONEOF( + "enum" + ZEROORONE(Identifier)+ '{' + EnumeratorList + '}' + "enum" + ZEROORONE(Identifier)+ '{' + EnumeratorList + ',' + '}' + "enum" + Identifier +) + +rule EnumeratorList : ONEOF( + Enumerator + EnumeratorList + ',' + Enumerator +) + +rule Enumerator : ONEOF( + EnumerationConstant + EnumerationConstant + '=' + ConstantExpression +) + +rule AtomicTypeSpecifier : ONEOF( + "_Atomic" + '(' + TypeName + ')' +) + +rule TypeQualifier : ONEOF( + "const" + "restrict" + "volatile" + "_Atomic" +) + +rule FunctionSpecifier : ONEOF( + "inline" + "_Noreturn" +) + +rule AlignmentSpecifier : ONEOF( + "_Alignas" + '(' + TypeName + ')' + "_Alignas" + '(' + ConstantExpression + ')' +) + +rule Declarator : ONEOF( + ZEROORONE(Pointer)+ DirectDeclarator +) + +rule DirectDeclarator : ONEOF( + Identifier + '(' + Declarator + ')' + DirectDeclarator + '[' + ZEROORONE(TypeQualifierList)+ ZEROORONE(AssignmentExpression)+ ']' + DirectDeclarator + '[' + "static" + ZEROORONE(TypeQualifierList)+ AssignmentExpression + ']' + DirectDeclarator + '[' + TypeQualifierList + "static" + AssignmentExpression + ']' + DirectDeclarator + '[' + ZEROORONE(TypeQualifierList)+ '*' + ']' + DirectDeclarator + '(' + ParameterTypeList + ')' + DirectDeclarator + '(' + ZEROORONE(IdentifierList)+ ')' +) + +rule Pointer : ONEOF( + '*' + ZEROORONE(TypeQualifierList) + '*' + ZEROORONE(TypeQualifierList)+ Pointer +) + +rule TypeQualifierList : ONEOF( + TypeQualifier + TypeQualifierList + TypeQualifier +) + +rule ParameterTypeList : ONEOF( + ParameterList + ParameterList + ',' + "..." +) + +rule ParameterList : ONEOF( + ParameterDeclaration + ParameterList + ',' + ParameterDeclaration +) + +rule ParameterDeclaration : ONEOF( + DeclarationSpecifiers + Declarator + DeclarationSpecifiers + ZEROORONE(AbstractDeclarator) +) + +rule IdentifierList : ONEOF( + Identifier + IdentifierList + ',' + Identifier +) + +rule TypeName : ONEOF( + SpecifierQualifierList + ZEROORONE(AbstractDeclarator) +) + +rule AbstractDeclarator : ONEOF( + Pointer + ZEROORONE(Pointer)+ DirectAbstractDeclarator +) + +rule DirectAbstractDeclarator : ONEOF( + '(' + AbstractDeclarator + ')' + ZEROORONE(DirectAbstractDeclarator)+ '[' + ZEROORONE(TypeQualifierList) + ZEROORONE(AssignmentExpression)+ ']' + ZEROORONE(DirectAbstractDeclarator)+ '[' + "static" + ZEROORONE(TypeQualifierList) + AssignmentExpression + ']' + ZEROORONE(DirectAbstractDeclarator)+ '[' + TypeQualifierList + "static" + AssignmentExpression + ']' + ZEROORONE(DirectAbstractDeclarator)+ '[' + '*' + ']' + ZEROORONE(DirectAbstractDeclarator)+ '(' + ZEROORONE(ParameterTypeList)+ ')' +) + +rule TypedefName : ONEOF( + Identifier +) + +rule Initializer : ONEOF( + AssignmentExpression + '{' + InitializerList + '}' + '{' + InitializerList + ',' + '}' +) + +rule InitializerList : ONEOF( + ZEROORONE(Designation)+ Initializer + InitializerList + ',' + ZEROORONE(Designation)+ Initializer +) + +rule Designation : ONEOF( + DesignatorList + '=' +) + +rule DesignatorList : ONEOF( + Designator + DesignatorList + Designator +) -rule PreIncrementExpression : "++" + PrimaryExpression - attr.action : BuildUnaryOperation(%1, %2) +rule Designator : ONEOF( + '[' + ConstantExpression + ']' + '.' + Identifier +) -rule PreDecrementExpression : "--" + PrimaryExpression - attr.action : BuildUnaryOperation(%1, %2) +rule Static_assertDeclaration : ONEOF( + "_Static_assert" + '(' + ConstantExpression + ',' + StringLiteral + ')' + ';' +) -rule PostIncrementExpression : PrimaryExpression + "++" - attr.action : BuildPostfixOperation(%2, %1) +rule Statement : ONEOF( + LabeledStatement + CompoundStatement + ExpressionStatement + SelectionStatement + IterationStatement + JumpStatement +) -rule PostDecrementExpression : PrimaryExpression + "--" - attr.action : BuildPostfixOperation(%2, %1) +rule LabeledStatement : ONEOF( + Identifier + ':' + Statement + "case" + ConstantExpression + ':' + Statement + "default" + ':' + Statement +) -###################################################################### -# Variable # -###################################################################### +rule CompoundStatement : ONEOF( + '{' + ZEROORONE(BlockItemList)+ '}' +) -rule GlobalVariableDeclarationStatement : VariableDeclaration + ';' - attr.property : Top +rule BlockItemList : ONEOF( + BlockItem + BlockItemList + BlockItem +) -rule LocalVariableDeclarationStatement : VariableDeclaration + ';' +rule BlockItem : ONEOF( + Declaration + Statement +) -rule VariableDeclaration : ZEROORMORE(VariableModifier) + Type + VariableDeclaratorList - attr.action: BuildDecl(%2, %3) - attr.action: AddModifier(%1) +rule ExpressionStatement : ONEOF( + ZEROORONE(Expression)+ ';' +) -rule VariableModifier : ONEOF( - "static", - "const", - "volatile", - "restrict") +rule SelectionStatement : ONEOF( + "if" + '(' + Expression + ')' + Statement + "if" + '(' + Expression + ')' + Statement + "else" + Statement + "switch" + '(' + Expression + ')' + Statement +) -rule VariableDeclaratorList : VariableDeclarator + ZEROORMORE(',' + VariableDeclarator) - attr.action: BuildVarList(%1, %2) +rule IterationStatement : ONEOF( + "while" + '(' + Expression + ')' + Statement + "do" + Statement + "while" + '(' + Expression + ')' + ';' + "for" + '(' + ZEROORONE(Expression)+ ';' + ZEROORONE(Expression)+ ';' + ZEROORONE(Expression)+ ')' + Statement + "for" + '(' + Declaration + ZEROORONE(Expression)+ ';' + ZEROORONE(Expression)+ ')' + Statement +) -rule VariableDeclarator : VariableDeclaratorId + ZEROORONE('=' + VariableInitializer) - attr.action: AddInitTo(%1, %2) +rule JumpStatement : ONEOF( + "goto" + Identifier + ';' + "continue" + ';' + "break" + ';' + "return" + ZEROORONE(Expression)+ ';' +) -rule VariableDeclaratorId : Identifier + ZEROORONE(Dims) - attr.action: AddDimsTo(%1, %2) +rule TranslationUnit : ONEOF( + ExternalDeclaration + TranslationUnit + ExternalDeclaration +) -rule VariableInitializer : ONEOF( - Expression, - ArrayInitializer) +rule ExternalDeclaration : ONEOF( + FunctionDefinition + Declaration +) -rule ArrayInitializer : '{' + ZEROORONE(VariableInitializerList) + ZEROORONE(',') + '}' +rule FunctionDefinition : ONEOF( + DeclarationSpecifiers + Declarator + ZEROORONE(DeclarationList)+ CompoundStatement +) -rule VariableInitializerList: VariableInitializer + ZEROORMORE(',' + VariableInitializer) +rule DeclarationList : ONEOF( + Declaration + DeclarationList + Declaration +) -rule Dims : Dim + ZEROORMORE(Dim) - attr.action: BuildDims(%1, %2) +rule preprocessingFile: ONEOF( + ZEROORONE(Group) +) -rule Dim : '[' + ']' - attr.action: BuildDim(%1) +rule Group: ONEOF( + GroupPart + Group + GroupPart +) -###################################################################### -# statement # -###################################################################### +rule GroupPart: ONEOF( + IfSection + ControlLine + TextLine + '#' + NonDirective +) -rule Statement : ONEOF(LocalVariableDeclarationStatement, - ExpressionStatement, - ReturnStatement) - attr.property: Single +rule IfSection: ONEOF( + IfGroup + ZEROORONE(ElifGroups) + ZEROORONE(ElseGroup) + EndifLine +) -rule ExpressionStatement : StatementExpression + ';' +rule IfGroup: ONEOF( + '#' + "if" + ConstantExpression + NewLine + ZEROORONE(Group) + '#' + "ifdef" + Identifier + NewLine + ZEROORONE(Group) + '#' + "ifndef" + Identifier + NewLine + ZEROORONE(Group) +) -rule StatementExpression : ONEOF( - PreIncrementExpression, - PreDecrementExpression, - PostIncrementExpression, - PostDecrementExpression, - ) - attr.property: Single +rule ElifGroups: ONEOF( + ElifGroup + ElifGroups + ElifGroup +) -rule ReturnStatement : "return" + ZEROORONE(Expression) + ';' - attr.action : BuildReturn(%2) +rule ElifGroup: ONEOF( + '#' + "elif" + ConstantExpression + NewLine + ZEROORONE(Group) +) +rule ElseGroup: ONEOF( + '#' + "else" + NewLine + ZEROORONE(Group) +) -###################################################################### -# Function # -###################################################################### +rule EndifLine: ONEOF( + '#' + "endif" + NewLine +) -rule GlobalFuncDeclaration : FuncDeclaration - attr.property : Top +rule ControlLine: ONEOF( + '#' + "include" + PpTokens + NewLine + '#' + "define" + Identifier + ReplacementList + NewLine + '#' + "define" + Identifier + Lparen + ZEROORONE(IdentifierList) + ')' + ReplacementList + NewLine + '#' + "define" + Identifier + Lparen + "..." + ')' + ReplacementList + NewLine + '#' + "define" + Identifier + Lparen + IdentifierList + ',' + "..." + ')' + ReplacementList + NewLine + '#' + "undef" + Identifier + NewLine + '#' + "line" + PpTokens + NewLine + '#' + "error" + ZEROORONE(PpTokens) + NewLine + '#' + "pragma" + ZEROORONE(PpTokens) + NewLine + '#' + NewLine +) -rule FuncDeclaration : ZEROORMORE(FuncModifier) + FuncHeader + FuncBody - attr.action: AddModifierTo(%2, %1) - attr.action: AddFunctionBodyTo(%2, %3) +rule TextLine: ONEOF( + ZEROORONE(PpTokens) + NewLine +) -rule FuncBody : ONEOF(Block, ';') - attr.property : Single +rule NonDirective: ONEOF( + PpTokens + NewLine +) -rule FuncHeader : ONEOF(Result + FuncDeclarator) - attr.action.%1: AddType(%2, %1) - attr.property : Single +rule Lparen: ONEOF( + '(' +) -rule Result : ONEOF(Type, "void") - attr.property : Single +rule ReplacementList: ONEOF( + ZEROORONE(PpTokens) +) -rule FuncDeclarator : Identifier + '(' + ZEROORONE(FormalParameters) + ')' - attr.action: BuildFunction(%1) - attr.action: AddParams(%3) +rule PpTokens: ONEOF( + PreprocessingToken + PpTokens + PreprocessingToken +) -rule FuncAttr : ONEOF("const", "static") - attr.property : Single +rule NewLine: ONEOF( + "\n" +) -rule FuncModifier : ONEOF(FuncAttr) - attr.property : Single +rule PreprocessingToken: ONEOF( + HeaderName + Identifier + PpNumber + CharacterConstant + StringLiteral + Punctuator + #Each nonWhiteSpace character that cannot be one of the above +) -rule FormalParameters : ONEOF(FormalParameter + ZEROORMORE(',' + FormalParameter)) - attr.property : Single +rule Punctuator: ONEOF('[', ']', '(', ')', '{', '}', '.', "->" "++", "--", '&', '*', '+', '-', '~', '!', '/', '%', "<<", ">>", '<', '>', "<=", ">=", "==", "!=", '^', '|', "&&", "||", '?', ':', ';', "...", '=', "*=", "/=", "%=", "+=", "-=", "<<=", ">>=", "&=", "^=", "|=", ',', '#', "##", "<:", ":>", "<%", "%>", "%:", "%:%:") -rule FormalParameter : ZEROORMORE(VariableModifier) + Type + VariableDeclaratorId - attr.action: BuildDecl(%2, %3) - attr.action: AddModifier(%1) +rule HeaderName: ONEOF( + '<' + HCharSequence + '>' + '"' + QCharSequence + '"' +) -###################################################################### -# Block # -###################################################################### +rule HCharSequence: ONEOF( + HChar + HCharSequence + HChar +) -rule BlockStatement : ONEOF(LocalVariableDeclarationStatement, Statement) - attr.property : Single +rule HChar: ONEOF(CHAR, DIGIT, '-', '_') + +rule QCharSequence: ONEOF( + QChar + QCharSequence + QChar +) + +rule QChar: ONEOF(CHAR, DIGIT, '-', '_') + +rule PpNumber: ONEOF( + DIGIT + '.' + DIGIT + PpNumber + DIGIT + PpNumber + ONEOF(CHAR, '_') + PpNumber + 'e' + Sign0 + PpNumber + 'E' + Sign0 + PpNumber + 'p' + Sign0 + PpNumber + 'P' + Sign0 + PpNumber + '.' +) -rule BlockStatements : BlockStatement + ZEROORMORE(BlockStatement) +rule Sign0 : ONEOF('+', '-') -rule Block : '{' + ZEROORONE(BlockStatements) + '}' - attr.action: BuildBlock(%2) -- Gitee From c5a9a6ecb1f87014b953e9cb51c4062dfa9989c0 Mon Sep 17 00:00:00 2001 From: Wen HU Date: Mon, 12 Dec 2022 09:22:50 -0800 Subject: [PATCH 03/11] add more C keywards --- src/MapleFE/c/keyword.spec | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/MapleFE/c/keyword.spec b/src/MapleFE/c/keyword.spec index 2e62b807aa..36b95e836b 100644 --- a/src/MapleFE/c/keyword.spec +++ b/src/MapleFE/c/keyword.spec @@ -46,6 +46,13 @@ STRUCT KeyWord : ((auto), (void), (volatile), (while), + (_Alignas), + (_Alignof), + (_Atomic), (_Bool), (_Complex), - (_Imaginary)) + (_Imaginary), + (_Generic), + (_Noreturn), + (_Static_assert), + (_Thread_local)) -- Gitee From e89b1a7618e27731de646bdf8ba4d8f73cff6167 Mon Sep 17 00:00:00 2001 From: Wen HU Date: Mon, 12 Dec 2022 09:24:08 -0800 Subject: [PATCH 04/11] fix a bug --- src/MapleFE/shared/src/ruletable_util.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/MapleFE/shared/src/ruletable_util.cpp b/src/MapleFE/shared/src/ruletable_util.cpp index 65dc85557f..a459ac70cb 100644 --- a/src/MapleFE/shared/src/ruletable_util.cpp +++ b/src/MapleFE/shared/src/ruletable_util.cpp @@ -239,7 +239,7 @@ bool LookAheadEqual(LookAhead la_a, LookAhead la_b) { size_t len_b = strlen(la_b.mData.mString); if (len_a != len_b) return false; - return strncmp(la_a.mData.mString, la_b.mData.mString, len_a); + return (strncmp(la_a.mData.mString, la_b.mData.mString, len_a) == 0); } else if (la_a.mType == LA_Identifier && la_b.mType == LA_Identifier) { return true; } else if (la_a.mType == LA_Literal && la_b.mType == LA_Literal) { -- Gitee From f5197dedbc9797e5ed9f9905f50fd54c77609adf Mon Sep 17 00:00:00 2001 From: Wen HU Date: Mon, 12 Dec 2022 15:34:31 -0800 Subject: [PATCH 05/11] escape for ' and \ --- src/MapleFE/ladetect/la_detect.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/MapleFE/ladetect/la_detect.cpp b/src/MapleFE/ladetect/la_detect.cpp index 49757bab47..87fce46b50 100644 --- a/src/MapleFE/ladetect/la_detect.cpp +++ b/src/MapleFE/ladetect/la_detect.cpp @@ -28,8 +28,13 @@ std::string GetLookAheadString(LookAhead la) { std::string str = "{"; switch (la.mType) { case LA_Char: - str += "LA_Char, "; + str += "LA_Char, \'"; + // need escape + if (la.mData.mChar == '\\' || la.mData.mChar == '\'') { + str += "\\"; + } str += la.mData.mChar; + str += "\'"; break; case LA_String: str += "LA_String, \""; -- Gitee From 786fac3fcfed421fdbc297ffa6a0b9dd130ba8e8 Mon Sep 17 00:00:00 2001 From: Wen HU Date: Mon, 12 Dec 2022 16:44:04 -0800 Subject: [PATCH 06/11] emit source line during lexer trace --- src/MapleFE/shared/src/lexer.cpp | 4 ++-- src/MapleFE/shared/src/parser.cpp | 11 +++++++---- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/MapleFE/shared/src/lexer.cpp b/src/MapleFE/shared/src/lexer.cpp index 694f86a8ed..8b0b4a9d12 100644 --- a/src/MapleFE/shared/src/lexer.cpp +++ b/src/MapleFE/shared/src/lexer.cpp @@ -633,7 +633,7 @@ bool Lexer::TraverseTableData(TableData *data) { switch (data->mType) { - // The first thinking is I also want to check if the next text after 'curidx' is a separtor + // The first thought is to check if the next text after 'curidx' is a separtor // or operator. // // This is the case in parsing a DT_String. However, we have many rules handling DT_Char @@ -815,7 +815,7 @@ bool Lexer::TraverseSecondTry(const RuleTable *rule_table) { // 3. We need find the longest match. // These four are the final result if success. - // [NOTE] The reason I use 'the one after' as xxx_end, is to check if TraverseTableData() + // [NOTE] The reason we use 'the one after' as xxx_end, is to check if TraverseTableData() // really moves the curidx. Or in another word, if it matches anything or nother. // If it matches something, xxx_end will be greater than xxx_start, or else they are equal. unsigned yyy_start = curidx; diff --git a/src/MapleFE/shared/src/parser.cpp b/src/MapleFE/shared/src/parser.cpp index a3f8139a95..834fed0274 100644 --- a/src/MapleFE/shared/src/parser.cpp +++ b/src/MapleFE/shared/src/parser.cpp @@ -66,7 +66,7 @@ SmallVector gTemplateLiteralNodes; // 3. Left Recursion // // MapleFE is an LL parser, and left recursion has to be handled if we allow language -// designer to write left recursion. I personally believe left recursion is a much +// designer to write left recursion. We believe left recursion is a much // simpler, more human friendly and stronger way to describe language spec. To // provide this juicy feature, parser has to do extra job. // @@ -361,6 +361,9 @@ unsigned Parser::LexOneLine() { return mActiveTokens.GetNum() - mCurToken; while (!token_num) { + if (mLexer->GetTrace() && !mLexer->EndOfLine() && line_begin) { + std::cout << "\n" << mLexer->GetLine() + mLexer->GetCuridx() << std::endl; + } // read until end of line while (!mLexer->EndOfLine() && !mLexer->EndOfFile()) { t = mLexer->LexToken(); @@ -960,7 +963,7 @@ bool Parser::TraverseRuleTable(RuleTable *rule_table, AppealNode *parent, Appeal // The affected can be succ later. So there is possibility both succ and fail // exist at the same time. // - // I still keep this assertion. We will see. Maybe we'll remove it. + // We still keep this assertion. We will see. Maybe we'll remove it. MASSERT(!WasFailed(rule_table, mCurToken)); // set the apppeal node @@ -996,7 +999,7 @@ bool Parser::TraverseRuleTable(RuleTable *rule_table, AppealNode *parent, Appeal bool in_group = FindRecursionGroup(rule_table, group_id); // 1. In a recursion, a rule could fail in the first a few instances, - // but could match in a later instance. So I need check is_done. + // but could match in a later instance. So We need check is_done. // Here is an example. Node A is one of the circle node. // (a) In the first recursion instance, A is failed, but luckly it // gets appealed due to lead node is 2ndOf1st. @@ -1139,7 +1142,7 @@ bool Parser::TraverseRuleTable(RuleTable *rule_table, AppealNode *parent, Appeal // It's a regular (non leadnode) table, either inside or outside of a // recursion, we just need do the regular traversal. // If it's inside a Left Recursion, it will finally goes to that - // recursion. I don't need take care here. + // recursion. We don't need take care here. bool matched = TraverseRuleTableRegular(rule_table, appeal); if (rec_tra) -- Gitee From f425b5d4dbb07e2544bd8f6840482a76d98ba00b Mon Sep 17 00:00:00 2001 From: Wen HU Date: Tue, 13 Dec 2022 09:54:51 -0800 Subject: [PATCH 07/11] add color for a successful match during trace table --- src/MapleFE/shared/src/parser.cpp | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src/MapleFE/shared/src/parser.cpp b/src/MapleFE/shared/src/parser.cpp index 834fed0274..2c57eae775 100644 --- a/src/MapleFE/shared/src/parser.cpp +++ b/src/MapleFE/shared/src/parser.cpp @@ -32,6 +32,12 @@ namespace maplefe { +#define RESET "\x1B[0m" +#define BOLD "\x1B[1m" +#define RED "\x1B[31m" +#define GRN "\x1B[32m" +#define YEL "\x1B[33m" + SmallVector gTemplateLiteralNodes; ////////////////////////////////////////////////////////////////////////////////// @@ -781,22 +787,28 @@ void Parser::DumpExitTable(const char *table_name, unsigned indent, AppealStatus reason, AppealNode *appeal) { for (unsigned i = 0; i < indent; i++) std::cout << " "; + if (reason == SuccWasSucc || + reason == SuccStillWasSucc || + reason == Succ || + reason == SuccASI) { + std::cout << GRN; + } std::cout << "Exit " << table_name << "@" << mCurToken; if (reason == SuccWasSucc) { std::cout << " succ@WasSucc" << "}"; DumpSuccTokens(appeal); - std::cout << std::endl; + std::cout << RESET << std::endl; } else if (reason == SuccStillWasSucc) { std::cout << " succ@StillWasSucc" << "}"; DumpSuccTokens(appeal); - std::cout << std::endl; + std::cout << RESET << std::endl; } else if (reason == Succ) { std::cout << " succ" << "}"; DumpSuccTokens(appeal); - std::cout << std::endl; + std::cout << RESET << std::endl; } else if (reason == SuccASI) { std::cout << " succASI" << "}"; - std::cout << std::endl; + std::cout << RESET << std::endl; } else if (reason == FailWasFailed) std::cout << " fail@WasFailed" << "}" << std::endl; else if (reason == FailNotRightToken) -- Gitee From 535fcc0465a57d0a4c97dfa2c7202f008f8e55db Mon Sep 17 00:00:00 2001 From: Wen HU Date: Wed, 14 Dec 2022 11:49:18 -0800 Subject: [PATCH 08/11] update C spec --- src/MapleFE/c/literal.spec | 211 ++++++++------- src/MapleFE/c/stmt.spec | 511 +++++++++++++++++++------------------ 2 files changed, 387 insertions(+), 335 deletions(-) diff --git a/src/MapleFE/c/literal.spec b/src/MapleFE/c/literal.spec index 2142416614..677b17ca75 100644 --- a/src/MapleFE/c/literal.spec +++ b/src/MapleFE/c/literal.spec @@ -21,162 +21,195 @@ # based on C11 specification A.1 Lexical grammar rule UniversalCharacterName : ONEOF( - '\' + 'u' + HexQuad - '\' + 'U' + HexQuad + HexQuad) + '\' + 'u' + HexQuad, + '\' + 'U' + HexQuad + HexQuad +) rule HexQuad : ONEOF( - HexadecimalDigit + HexadecimalDigit + HexadecimalDigit + HexadecimalDigit) + HexadecimalDigit + HexadecimalDigit + HexadecimalDigit + HexadecimalDigit +) -rule Constant : ONEOF( - IntegerConstant - FloatingConstant - EnumerationConstant - CharacterConstant) - -rule IntegerConstant : ONEOF( - DecimalConstant + ZEROORONE(IntegerSuffix) - OctalConstant + ZEROORONE(IntegerSuffix) - HexadecimalConstant + ZEROORONE(IntegerSuffix)) +rule IntegerLiteral : ONEOF( + DecimalConstant + ZEROORONE(IntegerSuffix), +# OctalConstant + ZEROORONE(IntegerSuffix), +# HexadecimalConstant + ZEROORONE(IntegerSuffix) +) rule DecimalConstant : ONEOF( - NonzeroDigit - DecimalConstant + Digit) + '0', NonzeroDigit + ZEROORMORE(DIGIT) +) rule OctalConstant : ONEOF( - '0' - OctalConstant + OctalDigit) + '0', + OctalConstant + OctalDigit +) rule HexadecimalConstant : ONEOF( - HexadecimalPrefix + HexadecimalDigit - HexadecimalConstant + HexadecimalDigit) + HexadecimalPrefix + HexadecimalDigit, + HexadecimalConstant + HexadecimalDigit +) -rule HexadecimalPrefix : ONEOF("0x", "0X") +rule HexadecimalPrefix : ONEOF( + '0' + 'x', '0' + 'X' +) -rule NonzeroDigit : ONEOF('1', '2', '3', '4', '5', '6', '7', '8', '9') +rule NonzeroDigit : ONEOF( + '1', '2', '3', '4', '5', '6', '7', '8', '9' +) -rule OctalDigit : ONEOF('0', '1', '2', '3', '4', '5', '6', '7') +rule OctalDigit : ONEOF( + '0', '1', '2', '3', '4', '5', '6', '7' +) -rule HexadecimalDigit : ONEOF('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'A', 'B', 'C', 'D', 'E', 'F') +rule HexadecimalDigit : ONEOF( + '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'A', 'B', 'C', 'D', 'E', 'F' +) rule IntegerSuffix : ONEOF( - UnsignedSuffix + ZEROORONE(LongSuffix) - UnsignedSuffix + LongLongSuffix - LongSuffix + ZEROORONE(UnsignedSuffix) - LongLongSuffix + ZEROORONE(UnsignedSuffix)) + UnsignedSuffix + ZEROORONE(LongSuffix), + UnsignedSuffix + LongLongSuffix, + LongSuffix + ZEROORONE(UnsignedSuffix), + LongLongSuffix + ZEROORONE(UnsignedSuffix) +) -rule UnsignedSuffix : ONEOF('u', 'U') +rule UnsignedSuffix : ONEOF( + 'u', 'U' +) -rule LongSuffix : ONEOF('l', 'L') +rule LongSuffix : ONEOF( + 'l', 'L' +) -rule LongLongSuffix : ONEOF("ll", "LL") +rule LongLongSuffix : ONEOF( + 'l' + 'l', 'L' + 'L' +) rule FloatingConstant : ONEOF( - DecimalFloatingConstant - HexadecimalFloatingConstant) + DecimalFloatingConstant, + HexadecimalFloatingConstant +) rule DecimalFloatingConstant : ONEOF( - FractionalConstant + ZEROORONE(ExponentPart) + ZEROORONE(FloatingSuffix) - DigitSequence + ExponentPart + ZEROORONE(FloatingSuffix)) + FractionalConstant + ZEROORONE(ExponentPart) + ZEROORONE(FloatingSuffix), + DigitSequence + ExponentPart + ZEROORONE(FloatingSuffix) +) rule HexadecimalFloatingConstant : ONEOF( - HexadecimalPrefix + HexadecimalFractionalConstant - BinaryExponentPart + ZEROORONE(FloatingSuffix) - HexadecimalPrefix + HexadecimalDigitSequence - BinaryExponentPart + ZEROORONE(FloatingSuffix)) + HexadecimalPrefix + HexadecimalFractionalConstant, + BinaryExponentPart + ZEROORONE(FloatingSuffix), + HexadecimalPrefix + HexadecimalDigitSequence, + BinaryExponentPart + ZEROORONE(FloatingSuffix) +) rule FractionalConstant : ONEOF( - ZEROORONE(DigitSequence) + '.' + DigitSequence - DigitSequence '.') + ZEROORONE(DigitSequence) + '.' + DigitSequence, + DigitSequence '.' +) rule ExponentPart : ONEOF( - 'e' + ZEROORONE(Sign) + DigitSequence - 'E' + ZEROORONE(Sign) + DigitSequence) + 'e' + ZEROORONE(Sign) + DigitSequence, + 'E' + ZEROORONE(Sign) + DigitSequence +) -rule Sign : ONEOF('+', '-') +rule Sign : ONEOF( + '+', '-' +) rule DigitSequence : ONEOF( - Digit - DigitSequence + Digit) + Digit, + DigitSequence + Digit +) rule HexadecimalFractionalConstant : ONEOF( - ZEROORONE(HexadecimalDigitSequence) '.' - HexadecimalDigitSequence - HexadecimalDigitSequence '.') + ZEROORONE(HexadecimalDigitSequence) + '.', + HexadecimalDigitSequence, + HexadecimalDigitSequence + '.' +) rule BinaryExponentPart : ONEOF( - 'p' + ZEROORONE(Sign) + DigitSequence - 'P' + ZEROORONE(Sign) + DigitSequence) + 'p' + ZEROORONE(Sign) + DigitSequence, + 'P' + ZEROORONE(Sign) + DigitSequence +) rule HexadecimalDigitSequence : ONEOF( - HexadecimalDigit - HexadecimalDigitSequence + HexadecimalDigit) + HexadecimalDigit, + HexadecimalDigitSequence + HexadecimalDigit +) -rule FloatingSuffix : ONEOF('f', 'l', 'F', 'L') +rule FloatingSuffix : ONEOF( + 'f', 'l', 'F', 'L' +) rule EnumerationConstant : Identifier rule CharacterConstant : ONEOF( - ''' + CCharSequence ''' - 'L' + ''' + CCharSequence + ''' - 'U' + ''' + CCharSequence + ''' - 'U' + ''' + CCharSequence + ''') + ''' + SCharSequence + ''', + 'L' + ''' + SCharSequence + ''', + 'U' + ''' + SCharSequence + ''', + 'U' + ''' + SCharSequence + ''' +) rule CCharSequence : ONEOF( - CChar - CCharSequence + CChar) + CChar, + CCharSequence + CChar +) rule EscapeSequence : ONEOF( - SimpleEscapeSequence - OctalEscapeSequence - HexadecimalEscapeSequence - UniversalCharacterName) - -rule SimpleEscapeSequence : ONEOF( - '\' + ''', '\' + '"', '\' + '?', '\' + '\', - '\' + 'a', '\' + 'b', '\' + 'f', '\' + 'n', '\' + 'r', '\' + 't', '\' + 'v') + ESCAPE, + OctalEscapeSequence, + HexadecimalEscapeSequence, + UniversalCharacterName +) rule OctalEscapeSequence : ONEOF( - '\' + OctalDigit - '\' + OctalDigit + OctalDigit - '\' + OctalDigit + OctalDigit + OctalDigit) + '\' + OctalDigit, + '\' + OctalDigit + OctalDigit, + '\' + OctalDigit + OctalDigit + OctalDigit +) rule HexadecimalEscapeSequence : ONEOF( - '\' + 'x' + HexadecimalDigit - HexadecimalEscapeSequence + HexadecimalDigit) + '\' + 'x' + HexadecimalDigit, + HexadecimalEscapeSequence + HexadecimalDigit +) rule StringLiteral : ONEOF( - ZEROORONE(EncodingPrefix) + '"' + ZEROORONE(SCharSequence) + '"') + ZEROORONE(EncodingPrefix) + '"' + ZEROORONE(SCharSequence) + '"' +) -rule EncodingPrefix : ONEOF ( - "u8", 'u', 'U', 'L') +rule EncodingPrefix : ONEOF( + 'u' + '8', 'u', 'U', 'L' +) rule SCharSequence : ONEOF( - SChar - SCharSequence + SChar) + SChar, + SCharSequence + SChar +) rule SChar : ONEOF( - CChar - EscapeSequence) + CChar, + EscapeSequence +) ####################################### #rule CChar : ONEOF( # CHAR # '_' -# EscapeSequence) +# EscapeSequence +#) -rule Digit : ONEOF(DIGIT) +rule Digit : DIGIT rule NullLiteral : "NULL" rule FPLiteral : FloatingConstant -rule BooleanLiteral : ONEOF ("true", "false") +rule BooleanLiteral : ONEOF("true", "false") rule CharacterLiteral : CharacterConstant -rule IntegerLiteral : IntegerConstant rule Literal : ONEOF( - IntegerLiteral - FPLiteral - EnumerationConstant - CharacterLiteral - StringLiteral - NullLiteral) + IntegerLiteral, + FPLiteral, + EnumerationConstant, + CharacterLiteral, + StringLiteral, + NullLiteral +) diff --git a/src/MapleFE/c/stmt.spec b/src/MapleFE/c/stmt.spec index a8fb287f68..51f23d9077 100644 --- a/src/MapleFE/c/stmt.spec +++ b/src/MapleFE/c/stmt.spec @@ -15,435 +15,447 @@ # based "on" C11 specification A.2 Phrase structure grammar rule PrimaryExpression : ONEOF( - Identifier - Constant - StringLiteral - '(' + Expression + ')' - GenericSelection + Identifier, + Literal, + StringLiteral, + '(' + Expression + ')', + GenericSelection ) rule GenericSelection : ONEOF( - "_Generic" + '(' + AssignmentExpression + ',' + GenericAssocList + ')' + "_Generic" + '(' + AssignmentExpression + ',' + GenericAssocList + ')' ) rule GenericAssocList : ONEOF( - GenericAssociation - GenericAssocList + ',' + GenericAssociation + GenericAssociation, + GenericAssocList + ',' + GenericAssociation ) rule GenericAssociation : ONEOF( - TypeName + ':' + AssignmentExpression - "default" + ':' + AssignmentExpression + TypeName + ':' + AssignmentExpression, + "default" + ':' + AssignmentExpression ) rule PostfixExpression : ONEOF( - PrimaryExpression - PostfixExpression + '[' + Expression + ']' - PostfixExpression + '(' + ZEROORONE(ArgumentExpressionList)+ ')' - PostfixExpression + '.' + Identifier - PostfixExpression + "->" + Identifier - PostfixExpression + "++" - PostfixExpression + "--" - '(' + TypeName + ')' + '{' + InitializerList + '}' - '(' + TypeName + ')' + '{' + InitializerList + ',' + '}' + PrimaryExpression, + PostfixExpression + '[' + Expression + ']', + PostfixExpression + '(' + ZEROORONE(ArgumentExpressionList) + ')', + PostfixExpression + '.' + Identifier, + PostfixExpression + "->" + Identifier, + PostfixExpression + "++", + PostfixExpression + "--", + '(' + TypeName + ')' + '{' + InitializerList + '}', + '(' + TypeName + ')' + '{' + InitializerList + ',' + '}' ) rule ArgumentExpressionList : ONEOF( - AssignmentExpression - ArgumentExpressionList + ',' + AssignmentExpression + AssignmentExpression, + ArgumentExpressionList + ',' + AssignmentExpression ) rule UnaryExpression : ONEOF( - PostfixExpression - "++" + UnaryExpression - "--" + UnaryExpression - UnaryOperator + CastExpression - "sizeof" + UnaryExpression - "sizeof" + '(' + TypeName + ')' - "_Alignof" + '(' + TypeName + ')' + PostfixExpression, + "++" + UnaryExpression, + "--" + UnaryExpression, + UnaryOperator + CastExpression, + "sizeof" + UnaryExpression, + "sizeof" + '(' + TypeName + ')', + "_Alignof" + '(' + TypeName + ')' ) rule UnaryOperator: ONEOF( - '&', '*', '+', '-', '~', '!' + '&', '*', '+', '-', '~', '!' ) rule CastExpression : ONEOF( - UnaryExpression - '(' + TypeName + ')' + CastExpression + UnaryExpression, + '(' + TypeName + ')' + CastExpression ) rule MultiplicativeExpression : ONEOF( - CastExpression - MultiplicativeExpression + '*' + CastExpression - MultiplicativeExpression + '/' + CastExpression - MultiplicativeExpression + '%' + CastExpression + CastExpression, + MultiplicativeExpression + '*' + CastExpression, + MultiplicativeExpression + '/' + CastExpression, + MultiplicativeExpression + '%' + CastExpression ) rule AdditiveExpression : ONEOF( - MultiplicativeExpression - AdditiveExpression + '+' + MultiplicativeExpression - AdditiveExpression + '-' + MultiplicativeExpression + MultiplicativeExpression, + AdditiveExpression + '+' + MultiplicativeExpression, + AdditiveExpression + '-' + MultiplicativeExpression ) rule ShiftExpression : ONEOF( - AdditiveExpression - ShiftExpression + "<<" + AdditiveExpression - ShiftExpression + ">>" + AdditiveExpression + AdditiveExpression, + ShiftExpression + "<<" + AdditiveExpression, + ShiftExpression + ">>" + AdditiveExpression ) rule RelationalExpression : ONEOF( - ShiftExpression - RelationalExpression + '<' + ShiftExpression - RelationalExpression + '>' + ShiftExpression - RelationalExpression + "<=" + ShiftExpression - RelationalExpression + ">=" + ShiftExpression + ShiftExpression, + RelationalExpression + '<' + ShiftExpression, + RelationalExpression + '>' + ShiftExpression, + RelationalExpression + "<=" + ShiftExpression, + RelationalExpression + ">=" + ShiftExpression ) rule EqualityExpression : ONEOF( - RelationalExpression - EqualityExpression + "==" + RelationalExpression - EqualityExpression + "!=" + RelationalExpression + RelationalExpression, + EqualityExpression + "==" + RelationalExpression, + EqualityExpression + "!=" + RelationalExpression ) rule ANDExpression : ONEOF( - EqualityExpression - ANDExpression + '&' + EqualityExpression + EqualityExpression, + ANDExpression + '&' + EqualityExpression ) rule ExclusiveORExpression : ONEOF( - ANDExpression - ExclusiveORExpression + '^' + ANDExpression + ANDExpression, + ExclusiveORExpression + '^' + ANDExpression ) rule InclusiveORExpression : ONEOF( - ExclusiveORExpression - InclusiveORExpression + '|' + ExclusiveORExpression + ExclusiveORExpression, + InclusiveORExpression + '|' + ExclusiveORExpression ) rule LogicalANDExpression : ONEOF( - InclusiveORExpression - LogicalANDExpression + "&&" + InclusiveORExpression + InclusiveORExpression, + LogicalANDExpression + "&&" + InclusiveORExpression ) rule LogicalORExpression : ONEOF( - LogicalANDExpression - LogicalORExpression + "||" + LogicalANDExpression + LogicalANDExpression, + LogicalORExpression + "||" + LogicalANDExpression ) rule ConditionalExpression : ONEOF( - LogicalORExpression - LogicalORExpression + '?' + Expression + ':' + ConditionalExpression + LogicalORExpression, + LogicalORExpression + '?' + Expression + ':' + ConditionalExpression ) rule AssignmentExpression : ONEOF( - ConditionalExpression - UnaryExpression + AssignmentOperator + AssignmentExpression + ConditionalExpression, + UnaryExpression + AssignmentOperator + AssignmentExpression ) rule AssignmentOperator: ONEOF( - '=', "*=", "/=", "%=", "+=", "-=", "<<=", ">>=", "&=", "^=", "|=" + '=', "*=", "/=", "%=", "+=", "-=", "<<=", ">>=", "&=", "^=", "|=" ) + attr.property : Single rule Expression : ONEOF( - AssignmentExpression - Expression + ',' + AssignmentExpression + AssignmentExpression, + Expression + ',' + AssignmentExpression ) rule ConstantExpression : ONEOF( - ConditionalExpression + ConditionalExpression ) rule Declaration : ONEOF( - DeclarationSpecifiers + ZEROORONE(InitDeclaratorList)+ ';' - Static_assertDeclaration + DeclarationSpecifiers + ZEROORONE(InitDeclaratorList) + ';', + Static_assertDeclaration ) + attr.property : Single rule DeclarationSpecifiers : ONEOF( - StorageClassSpecifier + ZEROORONE(DeclarationSpecifiers) - TypeSpecifier + ZEROORONE(DeclarationSpecifiers) - TypeQualifier + ZEROORONE(DeclarationSpecifiers) - FunctionSpecifier + ZEROORONE(DeclarationSpecifiers) - AlignmentSpecifier + ZEROORONE(DeclarationSpecifiers) + StorageClassSpecifier + ZEROORONE(DeclarationSpecifiers), + TypeSpecifier + ZEROORONE(DeclarationSpecifiers), + TypeQualifier + ZEROORONE(DeclarationSpecifiers), + FunctionSpecifier + ZEROORONE(DeclarationSpecifiers), + AlignmentSpecifier + ZEROORONE(DeclarationSpecifiers) ) rule InitDeclaratorList : ONEOF( - InitDeclarator - InitDeclaratorList + ',' + InitDeclarator + InitDeclarator, + InitDeclaratorList + ',' + InitDeclarator ) rule InitDeclarator : ONEOF( - Declarator - Declarator + '=' + Initializer + Declarator, + Declarator + '=' + Initializer ) rule StorageClassSpecifier : ONEOF( - "typedef" - "extern" - "static" - "_Thread_local" - "auto" - "register" + "typedef", + "extern", + "static", + "_Thread_local", + "auto", + "register" ) + attr.property : Single rule TypeSpecifier : ONEOF( - "void" - "char" - "short" - "int" - "long" - "float" - "double" - "signed" - "unsigned" - "_Bool" - "_Complex" - AtomicTypeSpecifier - StructOrUnionSpecifier - EnumSpecifier - TypedefName -) + "void", + "char", + "short", + "int", + "long", + "float", + "double", + "signed", + "unsigned", + "_Bool", + "_Complex", + AtomicTypeSpecifier, + StructOrUnionSpecifier, + EnumSpecifier, + TypedefName +) + attr.property : Single rule StructOrUnionSpecifier : ONEOF( - StructOrUnion + ZEROORONE(Identifier)+ '{' + StructDeclarationList + '}' - StructOrUnion + Identifier + StructOrUnion + ZEROORONE(Identifier) + '{' + StructDeclarationList + '}', + StructOrUnion + Identifier ) rule StructOrUnion : ONEOF( - "struct" - "union" + "struct", + "union" ) + attr.property : Single rule StructDeclarationList : ONEOF( - StructDeclaration - StructDeclarationList + StructDeclaration + StructDeclaration, + StructDeclarationList + StructDeclaration ) rule StructDeclaration : ONEOF( - SpecifierQualifierList + ZEROORONE(StructDeclaratorList)+ ';' - Static_assertDeclaration + SpecifierQualifierList + ZEROORONE(StructDeclaratorList) + ';', + Static_assertDeclaration ) rule SpecifierQualifierList : ONEOF( - TypeSpecifier + ZEROORONE(SpecifierQualifierList) - TypeQualifier + ZEROORONE(SpecifierQualifierList) + TypeSpecifier + ZEROORONE(SpecifierQualifierList), + TypeQualifier + ZEROORONE(SpecifierQualifierList) ) rule StructDeclaratorList : ONEOF( - StructDeclarator - StructDeclaratorList + ',' + StructDeclarator + StructDeclarator, + StructDeclaratorList + ',' + StructDeclarator ) rule StructDeclarator : ONEOF( - Declarator - ZEROORONE(Declarator)+ ':' + ConstantExpression + Declarator, + ZEROORONE(Declarator) + ':' + ConstantExpression ) rule EnumSpecifier : ONEOF( - "enum" + ZEROORONE(Identifier)+ '{' + EnumeratorList + '}' - "enum" + ZEROORONE(Identifier)+ '{' + EnumeratorList + ',' + '}' - "enum" + Identifier + "enum" + ZEROORONE(Identifier) + '{' + EnumeratorList + '}', + "enum" + ZEROORONE(Identifier) + '{' + EnumeratorList + ',' + '}', + "enum" + Identifier ) rule EnumeratorList : ONEOF( - Enumerator - EnumeratorList + ',' + Enumerator + Enumerator, + EnumeratorList + ',' + Enumerator ) rule Enumerator : ONEOF( - EnumerationConstant - EnumerationConstant + '=' + ConstantExpression + EnumerationConstant, + EnumerationConstant + '=' + ConstantExpression ) rule AtomicTypeSpecifier : ONEOF( - "_Atomic" + '(' + TypeName + ')' + "_Atomic" + '(' + TypeName + ')' ) rule TypeQualifier : ONEOF( - "const" - "restrict" - "volatile" - "_Atomic" + "const", + "restrict", + "volatile", + "_Atomic" ) rule FunctionSpecifier : ONEOF( - "inline" - "_Noreturn" + "inline", + "_Noreturn" ) rule AlignmentSpecifier : ONEOF( - "_Alignas" + '(' + TypeName + ')' - "_Alignas" + '(' + ConstantExpression + ')' + "_Alignas" + '(' + TypeName + ')', + "_Alignas" + '(' + ConstantExpression + ')' ) rule Declarator : ONEOF( - ZEROORONE(Pointer)+ DirectDeclarator + ZEROORONE(Pointer) + DirectDeclarator ) rule DirectDeclarator : ONEOF( - Identifier - '(' + Declarator + ')' - DirectDeclarator + '[' + ZEROORONE(TypeQualifierList)+ ZEROORONE(AssignmentExpression)+ ']' - DirectDeclarator + '[' + "static" + ZEROORONE(TypeQualifierList)+ AssignmentExpression + ']' - DirectDeclarator + '[' + TypeQualifierList + "static" + AssignmentExpression + ']' - DirectDeclarator + '[' + ZEROORONE(TypeQualifierList)+ '*' + ']' - DirectDeclarator + '(' + ParameterTypeList + ')' - DirectDeclarator + '(' + ZEROORONE(IdentifierList)+ ')' + Identifier, + '(' + Declarator + ')', + DirectDeclarator + '[' + ZEROORONE(TypeQualifierList) + ZEROORONE(AssignmentExpression) + ']', + DirectDeclarator + '[' + "static" + ZEROORONE(TypeQualifierList) + AssignmentExpression + ']', + DirectDeclarator + '[' + TypeQualifierList + "static" + AssignmentExpression + ']', + DirectDeclarator + '[' + ZEROORONE(TypeQualifierList) + '*' + ']', + DirectDeclarator + '(' + ParameterTypeList + ')', + DirectDeclarator + '(' + ZEROORONE(IdentifierList) + ')' ) rule Pointer : ONEOF( - '*' + ZEROORONE(TypeQualifierList) - '*' + ZEROORONE(TypeQualifierList)+ Pointer + '*' + ZEROORONE(TypeQualifierList), + '*' + ZEROORONE(TypeQualifierList) + Pointer ) rule TypeQualifierList : ONEOF( - TypeQualifier - TypeQualifierList + TypeQualifier + TypeQualifier, + TypeQualifierList + TypeQualifier ) rule ParameterTypeList : ONEOF( - ParameterList - ParameterList + ',' + "..." + ParameterList, + ParameterList + ',' + "..." ) rule ParameterList : ONEOF( - ParameterDeclaration - ParameterList + ',' + ParameterDeclaration + ParameterDeclaration, + ParameterList + ',' + ParameterDeclaration ) rule ParameterDeclaration : ONEOF( - DeclarationSpecifiers + Declarator - DeclarationSpecifiers + ZEROORONE(AbstractDeclarator) + DeclarationSpecifiers + Declarator, + DeclarationSpecifiers + ZEROORONE(AbstractDeclarator) ) rule IdentifierList : ONEOF( - Identifier - IdentifierList + ',' + Identifier + Identifier, + IdentifierList + ',' + Identifier ) rule TypeName : ONEOF( - SpecifierQualifierList + ZEROORONE(AbstractDeclarator) + SpecifierQualifierList + ZEROORONE(AbstractDeclarator) ) rule AbstractDeclarator : ONEOF( - Pointer - ZEROORONE(Pointer)+ DirectAbstractDeclarator + Pointer, + ZEROORONE(Pointer) + DirectAbstractDeclarator ) rule DirectAbstractDeclarator : ONEOF( - '(' + AbstractDeclarator + ')' - ZEROORONE(DirectAbstractDeclarator)+ '[' + ZEROORONE(TypeQualifierList) - ZEROORONE(AssignmentExpression)+ ']' - ZEROORONE(DirectAbstractDeclarator)+ '[' + "static" + ZEROORONE(TypeQualifierList) - AssignmentExpression + ']' - ZEROORONE(DirectAbstractDeclarator)+ '[' + TypeQualifierList + "static" - AssignmentExpression + ']' - ZEROORONE(DirectAbstractDeclarator)+ '[' + '*' + ']' - ZEROORONE(DirectAbstractDeclarator)+ '(' + ZEROORONE(ParameterTypeList)+ ')' + '(' + AbstractDeclarator + ')', + ZEROORONE(DirectAbstractDeclarator) + '[' + ZEROORONE(TypeQualifierList), + ZEROORONE(AssignmentExpression) + ']', + ZEROORONE(DirectAbstractDeclarator) + '[' + "static" + ZEROORONE(TypeQualifierList), + AssignmentExpression + ']', + ZEROORONE(DirectAbstractDeclarator) + '[' + TypeQualifierList + "static", + AssignmentExpression + ']', + ZEROORONE(DirectAbstractDeclarator) + '[' + '*' + ']', + ZEROORONE(DirectAbstractDeclarator) + '(' + ZEROORONE(ParameterTypeList) + ')' ) rule TypedefName : ONEOF( - Identifier + Identifier ) rule Initializer : ONEOF( - AssignmentExpression - '{' + InitializerList + '}' - '{' + InitializerList + ',' + '}' + AssignmentExpression, + '{' + InitializerList + '}', + '{' + InitializerList + ',' + '}' ) rule InitializerList : ONEOF( - ZEROORONE(Designation)+ Initializer - InitializerList + ',' + ZEROORONE(Designation)+ Initializer + ZEROORONE(Designation) + Initializer, + InitializerList + ',' + ZEROORONE(Designation) + Initializer ) rule Designation : ONEOF( - DesignatorList + '=' + DesignatorList + '=' ) rule DesignatorList : ONEOF( - Designator - DesignatorList + Designator + Designator, + DesignatorList + Designator ) rule Designator : ONEOF( - '[' + ConstantExpression + ']' - '.' + Identifier + '[' + ConstantExpression + ']', + '.' + Identifier ) rule Static_assertDeclaration : ONEOF( - "_Static_assert" + '(' + ConstantExpression + ',' + StringLiteral + ')' + ';' + "_Static_assert" + '(' + ConstantExpression + ',' + StringLiteral + ')' + ';' ) rule Statement : ONEOF( - LabeledStatement - CompoundStatement - ExpressionStatement - SelectionStatement - IterationStatement - JumpStatement + LabeledStatement, + CompoundStatement, + ExpressionStatement, + SelectionStatement, + IterationStatement, + JumpStatement ) + attr.property : Top rule LabeledStatement : ONEOF( - Identifier + ':' + Statement - "case" + ConstantExpression + ':' + Statement - "default" + ':' + Statement + Identifier + ':' + Statement, + "case" + ConstantExpression + ':' + Statement, + "default" + ':' + Statement ) + attr.property : Single rule CompoundStatement : ONEOF( - '{' + ZEROORONE(BlockItemList)+ '}' + '{' + ZEROORONE(BlockItemList) + '}' ) rule BlockItemList : ONEOF( - BlockItem - BlockItemList + BlockItem + BlockItem, + BlockItemList + BlockItem ) rule BlockItem : ONEOF( - Declaration - Statement + Declaration, + Statement ) rule ExpressionStatement : ONEOF( - ZEROORONE(Expression)+ ';' + ZEROORONE(Expression) + ';' ) rule SelectionStatement : ONEOF( - "if" + '(' + Expression + ')' + Statement - "if" + '(' + Expression + ')' + Statement + "else" + Statement - "switch" + '(' + Expression + ')' + Statement + "if" + '(' + Expression + ')' + Statement, + "if" + '(' + Expression + ')' + Statement + "else" + Statement, + "switch" + '(' + Expression + ')' + Statement ) + attr.property : Single rule IterationStatement : ONEOF( - "while" + '(' + Expression + ')' + Statement - "do" + Statement + "while" + '(' + Expression + ')' + ';' - "for" + '(' + ZEROORONE(Expression)+ ';' + ZEROORONE(Expression)+ ';' + ZEROORONE(Expression)+ ')' + Statement - "for" + '(' + Declaration + ZEROORONE(Expression)+ ';' + ZEROORONE(Expression)+ ')' + Statement + "while" + '(' + Expression + ')' + Statement, + "do" + Statement + "while" + '(' + Expression + ')' + ';', + "for" + '(' + ZEROORONE(Expression) + ';' + ZEROORONE(Expression) + ';' + ZEROORONE(Expression) + ')' + Statement, + "for" + '(' + Declaration + ZEROORONE(Expression) + ';' + ZEROORONE(Expression) + ')' + Statement ) + attr.property : Single rule JumpStatement : ONEOF( - "goto" + Identifier + ';' - "continue" + ';' - "break" + ';' - "return" + ZEROORONE(Expression)+ ';' + "goto" + Identifier + ';', + "continue" + ';', + "break" + ';', + "return" + ZEROORONE(Expression) + ';' ) + attr.property : Single rule TranslationUnit : ONEOF( - ExternalDeclaration - TranslationUnit + ExternalDeclaration + ExternalDeclaration, + TranslationUnit + ExternalDeclaration ) rule ExternalDeclaration : ONEOF( - FunctionDefinition - Declaration + Declaration, + FunctionDefinition ) + attr.property : Top + attr.property : Single rule FunctionDefinition : ONEOF( - DeclarationSpecifiers + Declarator + ZEROORONE(DeclarationList)+ CompoundStatement + DeclarationSpecifiers + Declarator + ZEROORONE(DeclarationList) + CompoundStatement ) rule DeclarationList : ONEOF( - Declaration - DeclarationList + Declaration + Declaration, + DeclarationList + Declaration ) rule preprocessingFile: ONEOF( @@ -451,14 +463,14 @@ rule preprocessingFile: ONEOF( ) rule Group: ONEOF( - GroupPart + GroupPart, Group + GroupPart ) rule GroupPart: ONEOF( - IfSection - ControlLine - TextLine + IfSection, + ControlLine, + TextLine, '#' + NonDirective ) @@ -467,13 +479,14 @@ rule IfSection: ONEOF( ) rule IfGroup: ONEOF( - '#' + "if" + ConstantExpression + NewLine + ZEROORONE(Group) - '#' + "ifdef" + Identifier + NewLine + ZEROORONE(Group) + '#' + "if" + ConstantExpression + NewLine + ZEROORONE(Group), + '#' + "ifdef" + Identifier + NewLine + ZEROORONE(Group), '#' + "ifndef" + Identifier + NewLine + ZEROORONE(Group) ) + attr.property : Single rule ElifGroups: ONEOF( - ElifGroup + ElifGroup, ElifGroups + ElifGroup ) @@ -490,17 +503,18 @@ rule EndifLine: ONEOF( ) rule ControlLine: ONEOF( - '#' + "include" + PpTokens + NewLine - '#' + "define" + Identifier + ReplacementList + NewLine - '#' + "define" + Identifier + Lparen + ZEROORONE(IdentifierList) + ')' + ReplacementList + NewLine - '#' + "define" + Identifier + Lparen + "..." + ')' + ReplacementList + NewLine - '#' + "define" + Identifier + Lparen + IdentifierList + ',' + "..." + ')' + ReplacementList + NewLine - '#' + "undef" + Identifier + NewLine - '#' + "line" + PpTokens + NewLine - '#' + "error" + ZEROORONE(PpTokens) + NewLine - '#' + "pragma" + ZEROORONE(PpTokens) + NewLine + '#' + "include" + PpTokens + NewLine, + '#' + "define" + Identifier + ReplacementList + NewLine, + '#' + "define" + Identifier + Lparen + ZEROORONE(IdentifierList) + ')' + ReplacementList + NewLine, + '#' + "define" + Identifier + Lparen + "..." + ')' + ReplacementList + NewLine, + '#' + "define" + Identifier + Lparen + IdentifierList + ',' + "..." + ')' + ReplacementList + NewLine, + '#' + "undef" + Identifier + NewLine, + '#' + "line" + PpTokens + NewLine, + '#' + "error" + ZEROORONE(PpTokens) + NewLine, + '#' + "pragma" + ZEROORONE(PpTokens) + NewLine, '#' + NewLine ) + attr.property : Single rule TextLine: ONEOF( ZEROORONE(PpTokens) + NewLine @@ -519,54 +533,59 @@ rule ReplacementList: ONEOF( ) rule PpTokens: ONEOF( - PreprocessingToken + PreprocessingToken, PpTokens + PreprocessingToken ) -rule NewLine: ONEOF( - "\n" -) +rule NewLine: '\' + 'n' rule PreprocessingToken: ONEOF( - HeaderName - Identifier - PpNumber - CharacterConstant - StringLiteral - Punctuator + HeaderName, + Identifier, + PpNumber, + CharacterLiteral, + StringLiteral, + Punctuator, #Each nonWhiteSpace character that cannot be one of the above ) -rule Punctuator: ONEOF('[', ']', '(', ')', '{', '}', '.', "->" "++", "--", '&', '*', '+', '-', '~', '!', '/', '%', "<<", ">>", '<', '>', "<=", ">=", "==", "!=", '^', '|', "&&", "||", '?', ':', ';', "...", '=', "*=", "/=", "%=", "+=", "-=", "<<=", ">>=", "&=", "^=", "|=", ',', '#', "##", "<:", ":>", "<%", "%>", "%:", "%:%:") +rule Punctuator: ONEOF( + '[', ']', '(', ')', '{', '}', '.', "->", + "++", "--", '&', '*', '+', '-', '~', '!', '/', '%', + "<<", ">>", '<', '>', "<=", ">=", "==", "!=", '^', '|', "&&", "||", + '?', ':', ';', "...", + '=', "*=", "/=", "%=", "+=", "-=", "<<=", ">>=", "&=", "^=", "|=", + ',', '#', "##", "<:", ":>", "<%", "%>", "%:", "%:%:" +) rule HeaderName: ONEOF( - '<' + HCharSequence + '>' + '<' + HCharSequence + '>', '"' + QCharSequence + '"' ) rule HCharSequence: ONEOF( - HChar + HChar, HCharSequence + HChar ) rule HChar: ONEOF(CHAR, DIGIT, '-', '_') rule QCharSequence: ONEOF( - QChar + QChar, QCharSequence + QChar ) rule QChar: ONEOF(CHAR, DIGIT, '-', '_') rule PpNumber: ONEOF( - DIGIT - '.' + DIGIT - PpNumber + DIGIT - PpNumber + ONEOF(CHAR, '_') - PpNumber + 'e' + Sign0 - PpNumber + 'E' + Sign0 - PpNumber + 'p' + Sign0 - PpNumber + 'P' + Sign0 + DIGIT, + '.' + DIGIT, + PpNumber + DIGIT, + PpNumber + ONEOF(CHAR, '_'), + PpNumber + 'e' + Sign0, + PpNumber + 'E' + Sign0, + PpNumber + 'p' + Sign0, + PpNumber + 'P' + Sign0, PpNumber + '.' ) -- Gitee From f17d7252c6347a869503642246a1eedc5577aa84 Mon Sep 17 00:00:00 2001 From: Wen HU Date: Mon, 19 Dec 2022 08:35:16 -0800 Subject: [PATCH 09/11] adjust emitted message --- src/MapleFE/shared/src/parser.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/MapleFE/shared/src/parser.cpp b/src/MapleFE/shared/src/parser.cpp index 2c57eae775..2014939669 100644 --- a/src/MapleFE/shared/src/parser.cpp +++ b/src/MapleFE/shared/src/parser.cpp @@ -367,9 +367,11 @@ unsigned Parser::LexOneLine() { return mActiveTokens.GetNum() - mCurToken; while (!token_num) { - if (mLexer->GetTrace() && !mLexer->EndOfLine() && line_begin) { - std::cout << "\n" << mLexer->GetLine() + mLexer->GetCuridx() << std::endl; + if (mLexer->GetTrace() && !mLexer->EndOfLine() && !mLexer->EndOfFile() && line_begin) { + std::cout << "line: " << mLexer->GetLineNum() << " : " + << mLexer->GetLine() + mLexer->GetCuridx() << std::endl; } + // read until end of line while (!mLexer->EndOfLine() && !mLexer->EndOfFile()) { t = mLexer->LexToken(); @@ -400,8 +402,6 @@ unsigned Parser::LexOneLine() { if (line_begin) { t->mLineBegin = true; line_begin = false; - if (mLexer->GetTrace()) - DUMP0("Set as Line First."); } mActiveTokens.PushBack(t); @@ -426,7 +426,7 @@ unsigned Parser::LexOneLine() { if (token_num) { last_token->mLineEnd = true; if (mLexer->GetTrace()) - DUMP0("Set as Line End."); + DUMP0("Set as Line End.\n"); } return token_num; -- Gitee From ac13a1805862e4938e7fb731a94417a97a210c41 Mon Sep 17 00:00:00 2001 From: Wen HU Date: Mon, 19 Dec 2022 14:09:56 -0800 Subject: [PATCH 10/11] match float/double constants --- src/MapleFE/c/literal.spec | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/MapleFE/c/literal.spec b/src/MapleFE/c/literal.spec index 677b17ca75..306c8a1dfb 100644 --- a/src/MapleFE/c/literal.spec +++ b/src/MapleFE/c/literal.spec @@ -84,7 +84,7 @@ rule LongLongSuffix : ONEOF( 'l' + 'l', 'L' + 'L' ) -rule FloatingConstant : ONEOF( +rule FPLiteral : ONEOF( DecimalFloatingConstant, HexadecimalFloatingConstant ) @@ -116,9 +116,10 @@ rule Sign : ONEOF( ) rule DigitSequence : ONEOF( - Digit, - DigitSequence + Digit + DIGIT, + DIGIT + ZEROORMORE(DIGIT) + DIGIT ) + attr.property.%2 : SecondTry rule HexadecimalFractionalConstant : ONEOF( ZEROORONE(HexadecimalDigitSequence) + '.', @@ -200,7 +201,6 @@ rule SChar : ONEOF( rule Digit : DIGIT rule NullLiteral : "NULL" -rule FPLiteral : FloatingConstant rule BooleanLiteral : ONEOF("true", "false") rule CharacterLiteral : CharacterConstant -- Gitee From 4150b5502dc3d16f89468bcd57dad364f162573d Mon Sep 17 00:00:00 2001 From: Wen HU Date: Tue, 20 Dec 2022 11:35:16 -0800 Subject: [PATCH 11/11] match char and string literals --- src/MapleFE/c/literal.spec | 53 ++++++++++++++++++++++---------------- 1 file changed, 31 insertions(+), 22 deletions(-) diff --git a/src/MapleFE/c/literal.spec b/src/MapleFE/c/literal.spec index 306c8a1dfb..3c3e1037d5 100644 --- a/src/MapleFE/c/literal.spec +++ b/src/MapleFE/c/literal.spec @@ -143,16 +143,33 @@ rule FloatingSuffix : ONEOF( rule EnumerationConstant : Identifier -rule CharacterConstant : ONEOF( - ''' + SCharSequence + ''', - 'L' + ''' + SCharSequence + ''', - 'U' + ''' + SCharSequence + ''', - 'U' + ''' + SCharSequence + ''' +rule CharacterLiteral : ONEOF( + ''' + CCharSequence + ''', + 'L' + ''' + CCharSequence + ''', + 'U' + ''' + CCharSequence + ''', + 'U' + ''' + CCharSequence + ''' ) rule CCharSequence : ONEOF( - CChar, - CCharSequence + CChar + CharChar, + CharChar + ZEROORMORE(CharChar) + CharChar +) + attr.property.%2 : SecondTry + +rule CharChar : ONEOF( #except ' \ and \n + PrintableChar, + '"', + #EscapeSequence +) + +# printalbe characters except ' and " +rule PrintableChar : ONEOF( + CHAR, + DIGIT, + ' ', '!', '#', '$', '%', '&', '(', ')', '*', '+', ',', '-', '.', '/', + ':', ';', '<', '=', '>', '?', '@', + '[', ']', '^', '_', '`', + '{', '|', '}', '~' ) rule EscapeSequence : ONEOF( @@ -182,27 +199,19 @@ rule EncodingPrefix : ONEOF( ) rule SCharSequence : ONEOF( - SChar, - SCharSequence + SChar + StringChar, + StringChar + ZEROORMORE(StringChar) + StringChar ) + attr.property.%2 : SecondTry -rule SChar : ONEOF( - CChar, - EscapeSequence +rule StringChar : ONEOF( #except " \ and \n + PrintableChar, + ''', + #EscapeSequence ) -####################################### -#rule CChar : ONEOF( -# CHAR -# '_' -# EscapeSequence -#) - -rule Digit : DIGIT - rule NullLiteral : "NULL" rule BooleanLiteral : ONEOF("true", "false") -rule CharacterLiteral : CharacterConstant rule Literal : ONEOF( IntegerLiteral, -- Gitee