|
# [The "BSD licence"] |
|
# Copyright (c) 2013 Sam Harwell |
|
# All rights reserved. |
|
# |
|
# Redistribution and use in source and binary forms, with or without |
|
# modification, are permitted provided that the following conditions |
|
# are met: |
|
# 1. Redistributions of source code must retain the above copyright |
|
# notice, this list of conditions and the following disclaimer. |
|
# 2. Redistributions in binary form must reproduce the above copyright |
|
# notice, this list of conditions and the following disclaimer in the |
|
# documentation and/or other materials provided with the distribution. |
|
# 3. The name of the author may not be used to endorse or promote products |
|
# derived from this software without specific prior written permission. |
|
# |
|
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
|
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
|
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
|
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
|
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
|
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
|
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
|
|
# C 2011 grammar built from the C11 Spec |
|
# Converted from https://github.com/antlr/grammars-v4/blob/master/c/C.g4 |
|
# by @ochafik using https://claude.ai (Opus) |
|
|
|
root ::= (translationUnit)? |
|
|
|
translationUnit ::= (externalDeclaration)+ |
|
|
|
externalDeclaration ::= functionDefinition | |
|
declaration | |
|
";" |
|
|
|
functionDefinition ::= (declarationSpecifiers)? declarator (declarationList)? compoundStatement |
|
|
|
declarationList ::= (declaration)+ |
|
|
|
declaration ::= declarationSpecifiers (initDeclaratorList)? ";" | |
|
staticAssertDeclaration |
|
|
|
declarationSpecifiers ::= (declarationSpecifier)+ |
|
|
|
declarationSpecifiers2 ::= (declarationSpecifier)+ |
|
|
|
declarationSpecifier ::= storageClassSpecifier | |
|
typeSpecifier | |
|
typeQualifier | |
|
functionSpecifier | |
|
alignmentSpecifier |
|
|
|
initDeclaratorList ::= initDeclarator ("," initDeclarator)* |
|
|
|
initDeclarator ::= declarator ("=" initializer)? |
|
|
|
storageClassSpecifier ::= "typedef" | |
|
"extern" | |
|
"static" | |
|
"_Thread_local" | |
|
"auto" | |
|
"register" |
|
|
|
typeSpecifier ::= "void" | |
|
"char" | |
|
"short" | |
|
"int" | |
|
"long" | |
|
"float" | |
|
"double" | |
|
"signed" | |
|
"unsigned" | |
|
"_Bool" | |
|
"_Complex" | |
|
"__m128" | |
|
"__m128d" | |
|
"__m128i" | |
|
("__extension__" "(" ("__m128" | "__m128d" | "__m128i") ")") | |
|
atomicTypeSpecifier | |
|
structOrUnionSpecifier | |
|
enumSpecifier | |
|
typedefName | |
|
("__typeof__" "(" constantExpression ")") |
|
|
|
structOrUnionSpecifier ::= structOrUnion (Identifier)? "{" structDeclarationList "}" | |
|
structOrUnion Identifier |
|
|
|
structOrUnion ::= "struct" | |
|
"union" |
|
|
|
structDeclarationList ::= (structDeclaration)+ |
|
|
|
structDeclaration ::= specifierQualifierList structDeclaratorList ";" | |
|
specifierQualifierList ";" | |
|
staticAssertDeclaration |
|
|
|
specifierQualifierList ::= ((typeSpecifier | typeQualifier) (specifierQualifierList)?)? |
|
|
|
structDeclaratorList ::= structDeclarator ("," structDeclarator)* |
|
|
|
structDeclarator ::= declarator | |
|
(declarator)? ":" constantExpression |
|
|
|
enumSpecifier ::= "enum" (Identifier)? "{" enumeratorList (",")? "}" | |
|
"enum" Identifier |
|
|
|
enumeratorList ::= enumerator ("," enumerator)* |
|
|
|
enumerator ::= enumerationConstant ("=" constantExpression)? |
|
|
|
enumerationConstant ::= Identifier |
|
|
|
atomicTypeSpecifier ::= "_Atomic" "(" typeName ")" |
|
|
|
typeQualifier ::= "const" | |
|
"restrict" | |
|
"volatile" | |
|
"_Atomic" |
|
|
|
functionSpecifier ::= "inline" | |
|
"_Noreturn" | |
|
"__inline__" | |
|
"__stdcall" | |
|
gccAttributeSpecifier | |
|
("__declspec" "(" Identifier ")") |
|
|
|
alignmentSpecifier ::= "_Alignas" "(" (typeName | constantExpression) ")" |
|
|
|
declarator ::= (pointer)? directDeclarator (gccDeclaratorExtension)* |
|
|
|
directDeclarator ::= Identifier | |
|
("(" declarator ")") | |
|
(directDeclarator "[" (typeQualifierList)? (assignmentExpression)? "]") | |
|
(directDeclarator "[" "static" (typeQualifierList)? assignmentExpression "]") | |
|
(directDeclarator "[" typeQualifierList "static" assignmentExpression "]") | |
|
(directDeclarator "[" (typeQualifierList)? "*" "]") | |
|
(directDeclarator "(" parameterTypeList ")") | |
|
(directDeclarator "(" (identifierList)? ")") | |
|
(Identifier ":" DigitSequence) | |
|
(vcSpecificModifer Identifier) | |
|
("(" vcSpecificModifer declarator ")") |
|
|
|
vcSpecificModifer ::= "__cdecl" | |
|
"__clrcall" | |
|
"__stdcall" | |
|
"__fastcall" | |
|
"__thiscall" | |
|
"__vectorcall" |
|
|
|
gccDeclaratorExtension ::= ("__asm" "(" (StringLiteral)+ ")") | |
|
gccAttributeSpecifier |
|
|
|
gccAttributeSpecifier ::= ("__attribute__" "(" "(" gccAttributeList ")" ")") |
|
|
|
gccAttributeList ::= (gccAttribute)? ("," (gccAttribute)?)* |
|
|
|
gccAttribute ::= (([^,] | "(" | ")") (("(" argumentExpressionList? ")")?)) |
|
|
|
nestedParenthesesBlock ::= (([^(] | ")" | ("(" nestedParenthesesBlock ")"))*)? |
|
|
|
pointer ::= ((("*" | "^") typeQualifierList?)+) |
|
|
|
typeQualifierList ::= (typeQualifier)+ |
|
|
|
parameterTypeList ::= parameterList ("," "...")? |
|
|
|
parameterList ::= parameterDeclaration ("," parameterDeclaration)* |
|
|
|
parameterDeclaration ::= |
|
declarationSpecifiers declarator | |
|
declarationSpecifiers2 (abstractDeclarator)? |
|
|
|
identifierList ::= Identifier ("," Identifier)* |
|
|
|
typeName ::= specifierQualifierList (abstractDeclarator)? |
|
|
|
abstractDeclarator ::= pointer | |
|
(pointer)? directAbstractDeclarator (gccDeclaratorExtension)* |
|
|
|
directAbstractDeclarator ::= (("(" abstractDeclarator ")") (gccDeclaratorExtension)*) | |
|
("[" (typeQualifierList)? (assignmentExpression)? "]") | |
|
("[" "static" (typeQualifierList)? assignmentExpression "]") | |
|
("[" typeQualifierList "static" assignmentExpression "]") | |
|
("[" "*" "]") | |
|
(("(" (parameterTypeList)? ")") (gccDeclaratorExtension)*) | |
|
((directAbstractDeclarator "[" (typeQualifierList)? (assignmentExpression)? "]")) | |
|
((directAbstractDeclarator "[" "static" (typeQualifierList)? assignmentExpression "]")) | |
|
((directAbstractDeclarator "[" typeQualifierList "static" assignmentExpression "]")) | |
|
((directAbstractDeclarator "[" "*" "]")) | |
|
((directAbstractDeclarator "(" (parameterTypeList)? ")") (gccDeclaratorExtension)*) |
|
|
|
typedefName ::= Identifier |
|
|
|
initializer ::= assignmentExpression | |
|
("{" initializerList (",")? "}") |
|
|
|
initializerList ::= (designation)? initializer ("," (designation)? initializer)* |
|
|
|
designation ::= designatorList "=" |
|
|
|
designatorList ::= (designator)+ |
|
|
|
designator ::= |
|
("[" constantExpression "]") | |
|
("." Identifier) |
|
|
|
staticAssertDeclaration ::= "_Static_assert" "(" constantExpression "," (StringLiteral)+ ")" ";" |
|
|
|
statement ::= labeledStatement | |
|
compoundStatement | |
|
expressionStatement | |
|
selectionStatement | |
|
iterationStatement | |
|
jumpStatement | |
|
(("__asm" | "__asm__") ("volatile" | "__volatile__") "(" ((logicalOrExpression ("," logicalOrExpression)*))? ((":" (logicalOrExpression ("," logicalOrExpression)*)*))* ")" ";") |
|
|
|
labeledStatement ::= (Identifier ":" (statement)?) | |
|
("case" constantExpression ":" statement) | |
|
("default" ":" statement) |
|
|
|
compoundStatement ::= "{" (blockItemList)? "}" |
|
|
|
blockItemList ::= (blockItem)+ |
|
|
|
blockItem ::= statement | |
|
declaration |
|
|
|
expressionStatement ::= (expression)? ";" |
|
|
|
selectionStatement ::= ("if" "(" expression ")" statement ("else" statement)?) | |
|
("switch" "(" expression ")" statement) |
|
|
|
iterationStatement ::= (While "(" expression ")" statement) | |
|
(Do statement While "(" expression ")" ";") | |
|
(For "(" forCondition ")" statement) |
|
|
|
forCondition ::= ((forDeclaration | expression?)? ";" (forExpression)? ";" (forExpression)?) |
|
|
|
forDeclaration ::= declarationSpecifiers (initDeclaratorList)? |
|
|
|
forExpression ::= assignmentExpression ("," assignmentExpression)* |
|
|
|
jumpStatement ::= (( |
|
("goto" Identifier) |
|
| "continue" |
|
| "break" |
|
| ("return" (expression)?) |
|
| ("goto" unaryExpression) |
|
) ";") |
|
|
|
compilationUnit ::= (translationUnit)? |
|
|
|
primaryExpression ::= Identifier | |
|
Constant | |
|
(StringLiteral)+ | |
|
("(" expression ")") | |
|
genericSelection | |
|
("__extension__"? "(" compoundStatement ")") | |
|
("__builtin_va_arg" "(" unaryExpression "," typeName ")") | |
|
("__builtin_offsetof" "(" typeName "," unaryExpression ")") |
|
|
|
genericSelection ::= "_Generic" "(" assignmentExpression "," genericAssocList ")" |
|
|
|
genericAssocList ::= genericAssociation ("," genericAssociation)* |
|
|
|
genericAssociation ::= (typeName | "default") ":" assignmentExpression |
|
|
|
postfixExpression ::= ((primaryExpression | "__extension__"? "(" typeName ")" "{" initializerList (",")? "}") ( |
|
("[" expression "]") |
|
| ("(" (argumentExpressionList)? ")") |
|
| (("." | "->") Identifier) |
|
| "++" |
|
| "--" |
|
)*) |
|
|
|
argumentExpressionList ::= assignmentExpression ("," assignmentExpression)* |
|
|
|
unaryExpression ::= (("++" | "--" | "sizeof")* ( |
|
postfixExpression |
|
| (unaryOperator castExpression) |
|
| (("sizeof" | "_Alignof") "(" typeName ")") |
|
| ("&&" Identifier) |
|
)) |
|
|
|
unaryOperator ::= "&" | |
|
"*" | |
|
"+" | |
|
"-" | |
|
"~" | |
|
"!" |
|
|
|
castExpression ::= ("__extension__"? "(" typeName ")" castExpression) | |
|
unaryExpression | |
|
DigitSequence |
|
|
|
multiplicativeExpression ::= castExpression ((("*" | "/" | "%") castExpression))* |
|
|
|
additiveExpression ::= multiplicativeExpression ((("+"|"-") multiplicativeExpression))* |
|
|
|
shiftExpression ::= additiveExpression ((("<<"|">>") additiveExpression))* |
|
|
|
relationalExpression ::= shiftExpression ((("<"|">"|"<="|">=") shiftExpression))* |
|
|
|
equalityExpression ::= relationalExpression ((("=="|"!=") relationalExpression))* |
|
|
|
andExpression ::= equalityExpression ("&" equalityExpression)* |
|
|
|
exclusiveOrExpression ::= andExpression ("^" andExpression)* |
|
|
|
inclusiveOrExpression ::= exclusiveOrExpression ("|" exclusiveOrExpression)* |
|
|
|
logicalAndExpression ::= inclusiveOrExpression ("&&" inclusiveOrExpression)* |
|
|
|
logicalOrExpression ::= logicalAndExpression ("||" logicalAndExpression)* |
|
|
|
conditionalExpression ::= logicalOrExpression (("?" expression ":" conditionalExpression)?)? |
|
|
|
assignmentExpression ::= conditionalExpression | |
|
(unaryExpression assignmentOperator assignmentExpression) | |
|
DigitSequence |
|
|
|
assignmentOperator ::= "=" | |
|
"*=" | |
|
"/=" | |
|
"%=" | |
|
"+=" | |
|
"-=" | |
|
"<<=" | |
|
">>=" | |
|
"&=" | |
|
"^=" | |
|
"|=" |
|
|
|
expression ::= assignmentExpression ("," assignmentExpression)* |
|
|
|
constantExpression ::= conditionalExpression |
|
|
|
Auto ::= "auto" |
|
|
|
Break ::= "break" |
|
|
|
Case ::= "case" |
|
|
|
Char ::= "char" |
|
|
|
Const ::= "const" |
|
|
|
Continue ::= "continue" |
|
|
|
Default ::= "default" |
|
|
|
Do ::= "do" |
|
|
|
Double ::= "double" |
|
|
|
Else ::= "else" |
|
|
|
Enum ::= "enum" |
|
|
|
Extern ::= "extern" |
|
|
|
Float ::= "float" |
|
|
|
For ::= "for" |
|
|
|
Goto ::= "goto" |
|
|
|
If ::= "if" |
|
|
|
Inline ::= "inline" |
|
|
|
Int ::= "int" |
|
|
|
Long ::= "long" |
|
|
|
Register ::= "register" |
|
|
|
Restrict ::= "restrict" |
|
|
|
Return ::= "return" |
|
|
|
Short ::= "short" |
|
|
|
Signed ::= "signed" |
|
|
|
Sizeof ::= "sizeof" |
|
|
|
Static ::= "static" |
|
|
|
Struct ::= "struct" |
|
|
|
Switch ::= "switch" |
|
|
|
Typedef ::= "typedef" |
|
|
|
Union ::= "union" |
|
|
|
Unsigned ::= "unsigned" |
|
|
|
Void ::= "void" |
|
|
|
Volatile ::= "volatile" |
|
|
|
While ::= "while" |
|
|
|
Alignas ::= "_Alignas" |
|
|
|
Alignof ::= "_Alignof" |
|
|
|
Atomic ::= "_Atomic" |
|
|
|
Bool ::= "_Bool" |
|
|
|
Complex ::= "_Complex" |
|
|
|
Generic ::= "_Generic" |
|
|
|
Imaginary ::= "_Imaginary" |
|
|
|
Noreturn ::= "_Noreturn" |
|
|
|
StaticAssert ::= "_Static_assert" |
|
|
|
ThreadLocal ::= "_Thread_local" |
|
|
|
LeftParen ::= "(" |
|
|
|
RightParen ::= ")" |
|
|
|
LeftBracket ::= "[" |
|
|
|
RightBracket ::= "]" |
|
|
|
LeftBrace ::= "{" |
|
|
|
RightBrace ::= "}" |
|
|
|
Less ::= "<" |
|
|
|
LessEqual ::= "<=" |
|
|
|
Greater ::= ">" |
|
|
|
GreaterEqual ::= ">=" |
|
|
|
LeftShift ::= "<<" |
|
|
|
RightShift ::= ">>" |
|
|
|
Plus ::= "+" |
|
|
|
PlusPlus ::= "++" |
|
|
|
Minus ::= "-" |
|
|
|
MinusMinus ::= "--" |
|
|
|
Star ::= "*" |
|
|
|
Div ::= "/" |
|
|
|
Mod ::= "%" |
|
|
|
And ::= "&" |
|
|
|
Or ::= "|" |
|
|
|
AndAnd ::= "&&" |
|
|
|
OrOr ::= "||" |
|
|
|
Caret ::= "^" |
|
|
|
Not ::= "!" |
|
|
|
Tilde ::= "~" |
|
|
|
Question ::= "?" |
|
|
|
Colon ::= ":" |
|
|
|
Semi ::= ";" |
|
|
|
Comma ::= "," |
|
|
|
Assign ::= "=" |
|
|
|
StarAssign ::= "*=" |
|
|
|
DivAssign ::= "/=" |
|
|
|
ModAssign ::= "%=" |
|
|
|
PlusAssign ::= "+=" |
|
|
|
MinusAssign ::= "-=" |
|
|
|
LeftShiftAssign ::= "<<=" |
|
|
|
RightShiftAssign ::= ">>=" |
|
|
|
AndAssign ::= "&=" |
|
|
|
XorAssign ::= "^=" |
|
|
|
OrAssign ::= "|=" |
|
|
|
Equal ::= "==" |
|
|
|
NotEqual ::= "!=" |
|
|
|
Arrow ::= "->" |
|
|
|
Dot ::= "." |
|
|
|
Ellipsis ::= "..." |
|
|
|
Identifier ::= IdentifierNondigit ((IdentifierNondigit | Digit)*)? |
|
|
|
IdentifierNondigit ::= (Nondigit | UniversalCharacterName) |
|
|
|
Nondigit ::= [a-zA-Z_] |
|
|
|
Digit ::= [0-9] |
|
|
|
UniversalCharacterName ::= ("\\u" HexQuad) | |
|
("\\U" HexQuad HexQuad) |
|
|
|
HexQuad ::= HexadecimalDigit HexadecimalDigit HexadecimalDigit HexadecimalDigit |
|
|
|
Constant ::= IntegerConstant | |
|
FloatingConstant | |
|
CharacterConstant |
|
|
|
IntegerConstant ::= (DecimalConstant (IntegerSuffix)?) | |
|
(OctalConstant (IntegerSuffix)?) | |
|
(HexadecimalConstant (IntegerSuffix)?) | |
|
BinaryConstant |
|
|
|
BinaryConstant ::= "0" [bB] [0-1]+ |
|
|
|
DecimalConstant ::= NonzeroDigit (Digit)* |
|
|
|
OctalConstant ::= "0" (OctalDigit)* |
|
|
|
HexadecimalConstant ::= HexadecimalPrefix (HexadecimalDigit)+ |
|
|
|
HexadecimalPrefix ::= "0" [xX] |
|
|
|
NonzeroDigit ::= [1-9] |
|
|
|
OctalDigit ::= [0-7] |
|
|
|
HexadecimalDigit ::= [0-9a-fA-F] |
|
|
|
IntegerSuffix ::= (UnsignedSuffix (LongSuffix)?) | |
|
(UnsignedSuffix LongLongSuffix) | |
|
(LongSuffix (UnsignedSuffix)?) | |
|
(LongLongSuffix (UnsignedSuffix)?) |
|
|
|
UnsignedSuffix ::= [uU] |
|
|
|
LongSuffix ::= [lL] |
|
|
|
LongLongSuffix ::= "ll" | |
|
"LL" |
|
|
|
FloatingConstant ::= DecimalFloatingConstant | |
|
HexadecimalFloatingConstant |
|
|
|
DigitSequence ::= (Digit)+ |
|
|
|
DecimalFloatingConstant ::= (FractionalConstant (ExponentPart)? (FloatingSuffix)?) | |
|
(DigitSequence ExponentPart (FloatingSuffix)?) |
|
|
|
HexadecimalFloatingConstant ::= (HexadecimalPrefix ((HexadecimalFractionalConstant | HexadecimalDigitSequence)) BinaryExponentPart (FloatingSuffix)?) |
|
|
|
FractionalConstant ::= ((DigitSequence)? "." DigitSequence) | |
|
(DigitSequence ".") |
|
|
|
ExponentPart ::= [eE] (Sign)? DigitSequence |
|
|
|
Sign ::= [+-] |
|
|
|
HexadecimalFractionalConstant ::= ((HexadecimalDigitSequence)? "." HexadecimalDigitSequence) | |
|
(HexadecimalDigitSequence ".") |
|
|
|
BinaryExponentPart ::= [pP] (Sign)? DigitSequence |
|
|
|
HexadecimalDigitSequence ::= (HexadecimalDigit)+ |
|
|
|
FloatingSuffix ::= [flFL] |
|
|
|
CharacterConstant ::= ("'" CCharSequence "'") | |
|
("L'" CCharSequence "'") | |
|
("u'" CCharSequence "'") | |
|
("U'" CCharSequence "'") |
|
|
|
EscapeSequence ::= SimpleEscapeSequence | |
|
OctalEscapeSequence | |
|
HexadecimalEscapeSequence | |
|
UniversalCharacterName |
|
|
|
SimpleEscapeSequence ::= "\\" ["'?abfnrtv] |
|
|
|
OctalEscapeSequence ::= "\\" OctalDigit (OctalDigit OctalDigit?)? |
|
|
|
HexadecimalEscapeSequence ::= "\\x" (HexadecimalDigit)+ |
|
|
|
StringLiteral ::= (EncodingPrefix)? ["] (SCharSequence)? ["] |
|
|
|
EncodingPrefix ::= "u8" | |
|
"u" | |
|
"U" | |
|
"L" |
|
|
|
CCharSequence ::= (CChar)+ |
|
|
|
SCharSequence ::= (SChar)+ |
|
|
|
CChar ::= |
|
[^'\r\n] | |
|
EscapeSequence |
|
|
|
SChar ::= [^"\r\n] | |
|
EscapeSequence | |
|
"\\\n" | |
|
"\\\r\n" |