diff --git a/src/MapleFE/c/keyword.spec b/src/MapleFE/c/keyword.spec index 2e62b807aaaf736015788008e0c36e2b1843bc51..36b95e836b0a60677cfb7edefaac605a30e5560e 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)) diff --git a/src/MapleFE/c/literal.spec b/src/MapleFE/c/literal.spec index 013f438418239a299bcd63ca4285bc00efd532c6..3c3e1037d51f76486234e2c60ed4520c42317d54 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. @@ -11,108 +11,214 @@ # 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. - -######################################################################### -## Integer ## -######################################################################### - -### Decimal rules - -rule NonZeroDigit : ONEOF('1', '2', '3', '4', '5', '6', '7', '8', '9') -rule Digit : ONEOF('0', NonZeroDigit) -rule DecimalNumeral : ONEOF('0', NonZeroDigit + ZEROORONE(Digit)) - -### Hexadecimal rules - -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) - -### Octal rules - -rule OctalDigit : ONEOF('0', '1', '2', '3', '4', '5', '6', '7') -rule OctalNumeral : ONEOF('0' + OctalDigit) - -rule IntegerTypeSuffix : ONEOF('L', 'l') -rule DecimalIntegerLiteral: DecimalNumeral + ZEROORONE(IntegerTypeSuffix) -rule HexIntegerLiteral : HexNumeral + ZEROORONE(IntegerTypeSuffix) -rule OctalIntegerLiteral : OctalNumeral + ZEROORONE(IntegerTypeSuffix) - -rule IntegerLiteral: ONEOF(DecimalIntegerLiteral, - HexIntegerLiteral, - OctalIntegerLiteral) - -######################################################################### -## Floating Point ## -######################################################################### - -##### Decimal floating point literal - -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)) - -####### Hex floating point literal - -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) - -###### Floating Point Literal - -rule FPLiteral : ONEOF(DecFPLiteral, HexFPLiteral) - -######################################################################### -## Boolean ## -######################################################################### - -rule BooleanLiteral : ONEOF ("true", "false") - -######################################################################### -## Character ## -## ESCAPE is a reserved rule in reserved.spec. ## -######################################################################### - -rule UnicodeEscape: '\' + 'u' + HEXDIGIT + HEXDIGIT + HEXDIGIT + HEXDIGIT -rule RawInputCharacter : ONEOF(ASCII, ''', ESCAPE) -rule SingleCharacter: ONEOF(UnicodeEscape, RawInputCharacter) - -rule OctalEscape : ONEOF('\' + '0', '\' + '1') -rule EscapeSequence : ONEOF(ESCAPE, OctalEscape) -rule CharacterLiteral : ''' + ONEOF(SingleCharacter, EscapeSequence) + ''' - -######################################################################### -## 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) + '"' - -######################################################################### -## Null ## -######################################################################### +# +# 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 IntegerLiteral : ONEOF( + DecimalConstant + ZEROORONE(IntegerSuffix), +# OctalConstant + ZEROORONE(IntegerSuffix), +# HexadecimalConstant + ZEROORONE(IntegerSuffix) +) + +rule DecimalConstant : ONEOF( + '0', NonzeroDigit + ZEROORMORE(DIGIT) +) + +rule OctalConstant : ONEOF( + '0', + OctalConstant + OctalDigit +) + +rule HexadecimalConstant : ONEOF( + HexadecimalPrefix + HexadecimalDigit, + HexadecimalConstant + HexadecimalDigit +) + +rule HexadecimalPrefix : ONEOF( + '0' + 'x', '0' + 'X' +) + +rule NonzeroDigit : ONEOF( + '1', '2', '3', '4', '5', '6', '7', '8', '9' +) + +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 IntegerSuffix : ONEOF( + UnsignedSuffix + ZEROORONE(LongSuffix), + UnsignedSuffix + LongLongSuffix, + LongSuffix + ZEROORONE(UnsignedSuffix), + LongLongSuffix + ZEROORONE(UnsignedSuffix) +) + +rule UnsignedSuffix : ONEOF( + 'u', 'U' +) + +rule LongSuffix : ONEOF( + 'l', 'L' +) + +rule LongLongSuffix : ONEOF( + 'l' + 'l', 'L' + 'L' +) + +rule FPLiteral : ONEOF( + DecimalFloatingConstant, + HexadecimalFloatingConstant +) + +rule DecimalFloatingConstant : ONEOF( + FractionalConstant + ZEROORONE(ExponentPart) + ZEROORONE(FloatingSuffix), + DigitSequence + ExponentPart + ZEROORONE(FloatingSuffix) +) + +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 DigitSequence : ONEOF( + DIGIT, + DIGIT + ZEROORMORE(DIGIT) + DIGIT +) + attr.property.%2 : SecondTry + +rule HexadecimalFractionalConstant : ONEOF( + ZEROORONE(HexadecimalDigitSequence) + '.', + HexadecimalDigitSequence, + HexadecimalDigitSequence + '.' +) + +rule BinaryExponentPart : ONEOF( + 'p' + ZEROORONE(Sign) + DigitSequence, + 'P' + ZEROORONE(Sign) + DigitSequence +) + +rule HexadecimalDigitSequence : ONEOF( + HexadecimalDigit, + HexadecimalDigitSequence + HexadecimalDigit +) + +rule FloatingSuffix : ONEOF( + 'f', 'l', 'F', 'L' +) + +rule EnumerationConstant : Identifier + +rule CharacterLiteral : ONEOF( + ''' + CCharSequence + ''', + 'L' + ''' + CCharSequence + ''', + 'U' + ''' + CCharSequence + ''', + 'U' + ''' + CCharSequence + ''' +) + +rule CCharSequence : ONEOF( + 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( + ESCAPE, + OctalEscapeSequence, + HexadecimalEscapeSequence, + UniversalCharacterName +) + +rule OctalEscapeSequence : ONEOF( + '\' + OctalDigit, + '\' + OctalDigit + OctalDigit, + '\' + OctalDigit + OctalDigit + OctalDigit +) + +rule HexadecimalEscapeSequence : ONEOF( + '\' + 'x' + HexadecimalDigit, + HexadecimalEscapeSequence + HexadecimalDigit +) + +rule StringLiteral : ONEOF( + ZEROORONE(EncodingPrefix) + '"' + ZEROORONE(SCharSequence) + '"' +) + +rule EncodingPrefix : ONEOF( + 'u' + '8', 'u', 'U', 'L' +) + +rule SCharSequence : ONEOF( + StringChar, + StringChar + ZEROORMORE(StringChar) + StringChar +) + attr.property.%2 : SecondTry + +rule StringChar : ONEOF( #except " \ and \n + PrintableChar, + ''', + #EscapeSequence +) rule NullLiteral : "NULL" +rule BooleanLiteral : ONEOF("true", "false") -######################################################################### -## 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 diff --git a/src/MapleFE/c/stmt.spec b/src/MapleFE/c/stmt.spec index f71d36b960266b2d4a5e42e799f73bf60a0223ff..51f23d90778c6104b8f354b2c83fc97be7cfd2ae 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,582 @@ # 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( + Identifier, Literal, - Identifier + StringLiteral, + '(' + Expression + ')', + GenericSelection +) + +rule GenericSelection : ONEOF( + "_Generic" + '(' + AssignmentExpression + ',' + GenericAssocList + ')' ) -rule DimExprs : DimExpr + ZEROORMORE(DimExpr) +rule GenericAssocList : ONEOF( + GenericAssociation, + GenericAssocList + ',' + GenericAssociation +) -rule DimExpr : '[' + Expression + ']' +rule GenericAssociation : ONEOF( + TypeName + ':' + AssignmentExpression, + "default" + ':' + AssignmentExpression +) -rule Expression : ONEOF( +rule PostfixExpression : ONEOF( PrimaryExpression, - UnaryExpression) + 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 PreIncrementExpression : "++" + PrimaryExpression - attr.action : BuildUnaryOperation(%1, %2) +rule UnaryOperator: ONEOF( + '&', '*', '+', '-', '~', '!' +) -rule PreDecrementExpression : "--" + PrimaryExpression - attr.action : BuildUnaryOperation(%1, %2) +rule CastExpression : ONEOF( + UnaryExpression, + '(' + TypeName + ')' + CastExpression +) -rule PostIncrementExpression : PrimaryExpression + "++" - attr.action : BuildPostfixOperation(%2, %1) +rule MultiplicativeExpression : ONEOF( + CastExpression, + MultiplicativeExpression + '*' + CastExpression, + MultiplicativeExpression + '/' + CastExpression, + MultiplicativeExpression + '%' + CastExpression +) -rule PostDecrementExpression : PrimaryExpression + "--" - attr.action : BuildPostfixOperation(%2, %1) +rule AdditiveExpression : ONEOF( + MultiplicativeExpression, + AdditiveExpression + '+' + MultiplicativeExpression, + AdditiveExpression + '-' + MultiplicativeExpression +) -###################################################################### -# Variable # -###################################################################### +rule ShiftExpression : ONEOF( + AdditiveExpression, + ShiftExpression + "<<" + AdditiveExpression, + ShiftExpression + ">>" + AdditiveExpression +) -rule GlobalVariableDeclarationStatement : VariableDeclaration + ';' - attr.property : Top +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 LocalVariableDeclarationStatement : VariableDeclaration + ';' +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 VariableDeclaration : ZEROORMORE(VariableModifier) + Type + VariableDeclaratorList - attr.action: BuildDecl(%2, %3) - attr.action: AddModifier(%1) +rule AssignmentOperator: ONEOF( + '=', "*=", "/=", "%=", "+=", "-=", "<<=", ">>=", "&=", "^=", "|=" +) + attr.property : Single + +rule Expression : ONEOF( + AssignmentExpression, + Expression + ',' + AssignmentExpression +) + +rule ConstantExpression : ONEOF( + ConditionalExpression +) + +rule Declaration : ONEOF( + DeclarationSpecifiers + ZEROORONE(InitDeclaratorList) + ';', + Static_assertDeclaration +) + attr.property : Single -rule VariableModifier : ONEOF( +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" +) + attr.property : Single + +rule TypeSpecifier : ONEOF( + "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 +) + +rule StructOrUnion : ONEOF( + "struct", + "union" +) + attr.property : Single + +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", - "restrict") + "_Atomic" +) -rule VariableDeclaratorList : VariableDeclarator + ZEROORMORE(',' + VariableDeclarator) - attr.action: BuildVarList(%1, %2) +rule FunctionSpecifier : ONEOF( + "inline", + "_Noreturn" +) -rule VariableDeclarator : VariableDeclaratorId + ZEROORONE('=' + VariableInitializer) - attr.action: AddInitTo(%1, %2) +rule AlignmentSpecifier : ONEOF( + "_Alignas" + '(' + TypeName + ')', + "_Alignas" + '(' + ConstantExpression + ')' +) -rule VariableDeclaratorId : Identifier + ZEROORONE(Dims) - attr.action: AddDimsTo(%1, %2) +rule Declarator : ONEOF( + ZEROORONE(Pointer) + DirectDeclarator +) -rule VariableInitializer : ONEOF( - Expression, - ArrayInitializer) +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 ArrayInitializer : '{' + ZEROORONE(VariableInitializerList) + ZEROORONE(',') + '}' +rule Pointer : ONEOF( + '*' + ZEROORONE(TypeQualifierList), + '*' + ZEROORONE(TypeQualifierList) + Pointer +) -rule VariableInitializerList: VariableInitializer + ZEROORMORE(',' + VariableInitializer) +rule TypeQualifierList : ONEOF( + TypeQualifier, + TypeQualifierList + TypeQualifier +) -rule Dims : Dim + ZEROORMORE(Dim) - attr.action: BuildDims(%1, %2) +rule ParameterTypeList : ONEOF( + ParameterList, + ParameterList + ',' + "..." +) -rule Dim : '[' + ']' - attr.action: BuildDim(%1) +rule ParameterList : ONEOF( + ParameterDeclaration, + ParameterList + ',' + ParameterDeclaration +) -###################################################################### -# statement # -###################################################################### +rule ParameterDeclaration : ONEOF( + DeclarationSpecifiers + Declarator, + DeclarationSpecifiers + ZEROORONE(AbstractDeclarator) +) -rule Statement : ONEOF(LocalVariableDeclarationStatement, - ExpressionStatement, - ReturnStatement) - attr.property: Single +rule IdentifierList : ONEOF( + Identifier, + IdentifierList + ',' + Identifier +) -rule ExpressionStatement : StatementExpression + ';' +rule TypeName : ONEOF( + SpecifierQualifierList + ZEROORONE(AbstractDeclarator) +) -rule StatementExpression : ONEOF( - PreIncrementExpression, - PreDecrementExpression, - PostIncrementExpression, - PostDecrementExpression, - ) - attr.property: Single +rule AbstractDeclarator : ONEOF( + Pointer, + ZEROORONE(Pointer) + DirectAbstractDeclarator +) -rule ReturnStatement : "return" + ZEROORONE(Expression) + ';' - attr.action : BuildReturn(%2) +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 +) -###################################################################### -# Function # -###################################################################### +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 Designator : ONEOF( + '[' + ConstantExpression + ']', + '.' + Identifier +) + +rule Static_assertDeclaration : ONEOF( + "_Static_assert" + '(' + ConstantExpression + ',' + StringLiteral + ')' + ';' +) -rule GlobalFuncDeclaration : FuncDeclaration +rule Statement : ONEOF( + LabeledStatement, + CompoundStatement, + ExpressionStatement, + SelectionStatement, + IterationStatement, + JumpStatement +) attr.property : Top -rule FuncDeclaration : ZEROORMORE(FuncModifier) + FuncHeader + FuncBody - attr.action: AddModifierTo(%2, %1) - attr.action: AddFunctionBodyTo(%2, %3) +rule LabeledStatement : ONEOF( + Identifier + ':' + Statement, + "case" + ConstantExpression + ':' + Statement, + "default" + ':' + Statement +) + attr.property : Single + +rule CompoundStatement : ONEOF( + '{' + ZEROORONE(BlockItemList) + '}' +) + +rule BlockItemList : ONEOF( + BlockItem, + BlockItemList + BlockItem +) + +rule BlockItem : ONEOF( + Declaration, + Statement +) + +rule ExpressionStatement : ONEOF( + ZEROORONE(Expression) + ';' +) -rule FuncBody : ONEOF(Block, ';') +rule SelectionStatement : ONEOF( + "if" + '(' + Expression + ')' + Statement, + "if" + '(' + Expression + ')' + Statement + "else" + Statement, + "switch" + '(' + Expression + ')' + Statement +) attr.property : Single -rule FuncHeader : ONEOF(Result + FuncDeclarator) - attr.action.%1: AddType(%2, %1) +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 +) attr.property : Single -rule Result : ONEOF(Type, "void") +rule JumpStatement : ONEOF( + "goto" + Identifier + ';', + "continue" + ';', + "break" + ';', + "return" + ZEROORONE(Expression) + ';' +) attr.property : Single -rule FuncDeclarator : Identifier + '(' + ZEROORONE(FormalParameters) + ')' - attr.action: BuildFunction(%1) - attr.action: AddParams(%3) +rule TranslationUnit : ONEOF( + ExternalDeclaration, + TranslationUnit + ExternalDeclaration +) -rule FuncAttr : ONEOF("const", "static") +rule ExternalDeclaration : ONEOF( + Declaration, + FunctionDefinition +) + attr.property : Top attr.property : Single -rule FuncModifier : ONEOF(FuncAttr) - attr.property : Single +rule FunctionDefinition : ONEOF( + DeclarationSpecifiers + Declarator + ZEROORONE(DeclarationList) + CompoundStatement +) + +rule DeclarationList : ONEOF( + Declaration, + DeclarationList + Declaration +) + +rule preprocessingFile: ONEOF( + ZEROORONE(Group) +) -rule FormalParameters : ONEOF(FormalParameter + ZEROORMORE(',' + FormalParameter)) +rule Group: ONEOF( + GroupPart, + Group + GroupPart +) + +rule GroupPart: ONEOF( + IfSection, + ControlLine, + TextLine, + '#' + NonDirective +) + +rule IfSection: ONEOF( + IfGroup + ZEROORONE(ElifGroups) + ZEROORONE(ElseGroup) + EndifLine +) + +rule IfGroup: ONEOF( + '#' + "if" + ConstantExpression + NewLine + ZEROORONE(Group), + '#' + "ifdef" + Identifier + NewLine + ZEROORONE(Group), + '#' + "ifndef" + Identifier + NewLine + ZEROORONE(Group) +) attr.property : Single -rule FormalParameter : ZEROORMORE(VariableModifier) + Type + VariableDeclaratorId - attr.action: BuildDecl(%2, %3) - attr.action: AddModifier(%1) +rule ElifGroups: ONEOF( + ElifGroup, + ElifGroups + ElifGroup +) -###################################################################### -# Block # -###################################################################### +rule ElifGroup: ONEOF( + '#' + "elif" + ConstantExpression + NewLine + ZEROORONE(Group) +) + +rule ElseGroup: ONEOF( + '#' + "else" + NewLine + ZEROORONE(Group) +) + +rule EndifLine: ONEOF( + '#' + "endif" + NewLine +) -rule BlockStatement : ONEOF(LocalVariableDeclarationStatement, Statement) +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 +) attr.property : Single -rule BlockStatements : BlockStatement + ZEROORMORE(BlockStatement) +rule TextLine: ONEOF( + ZEROORONE(PpTokens) + NewLine +) + +rule NonDirective: ONEOF( + PpTokens + NewLine +) + +rule Lparen: ONEOF( + '(' +) + +rule ReplacementList: ONEOF( + ZEROORONE(PpTokens) +) + +rule PpTokens: ONEOF( + PreprocessingToken, + PpTokens + PreprocessingToken +) + +rule NewLine: '\' + 'n' + +rule PreprocessingToken: ONEOF( + HeaderName, + Identifier, + PpNumber, + CharacterLiteral, + StringLiteral, + Punctuator, + #Each nonWhiteSpace character that cannot be one of the above +) + +rule Punctuator: ONEOF( + '[', ']', '(', ')', '{', '}', '.', "->", + "++", "--", '&', '*', '+', '-', '~', '!', '/', '%', + "<<", ">>", '<', '>', "<=", ">=", "==", "!=", '^', '|', "&&", "||", + '?', ':', ';', "...", + '=', "*=", "/=", "%=", "+=", "-=", "<<=", ">>=", "&=", "^=", "|=", + ',', '#', "##", "<:", ":>", "<%", "%>", "%:", "%:%:" +) + +rule HeaderName: ONEOF( + '<' + HCharSequence + '>', + '"' + QCharSequence + '"' +) + +rule HCharSequence: ONEOF( + HChar, + HCharSequence + HChar +) + +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 Sign0 : ONEOF('+', '-') -rule Block : '{' + ZEROORONE(BlockStatements) + '}' - attr.action: BuildBlock(%2) diff --git a/src/MapleFE/ladetect/la_detect.cpp b/src/MapleFE/ladetect/la_detect.cpp index 49757bab478a9dc1923ba914f572f824646fe25b..87fce46b508029ac40736bb712a75fce335e87e1 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, \""; diff --git a/src/MapleFE/shared/src/lexer.cpp b/src/MapleFE/shared/src/lexer.cpp index 694f86a8edafedea9ff0584a140a9b0e25777951..8b0b4a9d12e9a8f5f3524db35643b2d91133fd35 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 a3f8139a95310f5d692004b8e687b0ca5c6bbca6..20149396699ed87c4bab28ee76a6ef0b2e0700d6 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; ////////////////////////////////////////////////////////////////////////////////// @@ -66,7 +72,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 +367,11 @@ unsigned Parser::LexOneLine() { return mActiveTokens.GetNum() - mCurToken; while (!token_num) { + 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(); @@ -391,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); @@ -417,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; @@ -778,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) @@ -960,7 +975,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 +1011,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 +1154,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) diff --git a/src/MapleFE/shared/src/ruletable_util.cpp b/src/MapleFE/shared/src/ruletable_util.cpp index 65dc85557fe634d3dece1f2f5ae82b1f5d576f18..a459ac70cb2a449efca64b81d0afbcf4ab52254e 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) {