wgtdkp / wgtcc

A small C11 compiler
MIT License
765 stars 130 forks source link

Grammar railroad diagram #65

Open mingodad opened 3 years ago

mingodad commented 3 years ago

I'm trying to translate this project parser to a CocoR parser (https://github.com/mingodad/CocoR-CPP) and I'm having a some difficult following the parser code (see bellow what I've got so far). Just in case someone want to help create a better EBNF description of this project parser.

I've done a experimental tool to convert CocoR grammars to a kind of EBNF understood by https://www.bottlecaps.de/rr/ui to generate railroad diagrams see bellow the converted to allow view it at https://www.bottlecaps.de/rr/ui the order of the rules could be changed to a better view of the railroad diagrams. Copy and paste the EBNF bellow on https://www.bottlecaps.de/rr/ui tab Edit Grammar then switch to the tab View Diagram.

Cheers !

//
// EBNF generated by CocoR parser generator to be viewed with https://www.bottlecaps.de/rr/ui
//

//
// productions
//

WGTCC ::= ( ParseTranslationUnit )* EOF 
ParseTranslationUnit ::= ( ";" | ParseStaticAssert | ParseDeclSpec ParseDeclarator ( ( ParseFuncDef | ParseInitDeclarator ( "," ParseDirectDeclarator ParseInitDeclarator )* ) )? ";" | TryAttributeSpecList ) 
ParseStaticAssert ::= Token_STATIC_ASSERT "(" ParseAssignExpr "," ConcatLiterals ")" ";" 
ParseDeclSpec ::= ( FunctionSpecifier | AlignmentSpecifier | StorageSpecifier | TypeQualifier | TypeSpecifier ) 
ParseDeclarator ::= ( ParsePointer )? ( "(" ParseDeclarator ")" | Token_IDENTIFIER ( TryAttributeSpecList )? ) ParseArrayFuncDeclarator 
ParseFuncDef ::= "{" ( ParseCompoundStmt )* "}" 
ParseInitDeclarator ::= "=" ParseInitDeclaratorSub 
ParseDirectDeclarator ::= ParseDeclarator 
TryAttributeSpecList ::= ParseAttributeSpec 
ParseAssignExpr ::= ParseConditionalExpr ( ( Token_MUL_ASSIGN | Token_DIV_ASSIGN | Token_MOD_ASSIGN | Token_ADD_ASSIGN | Token_SUB_ASSIGN | Token_LEFT_ASSIGN | Token_RIGHT_ASSIGN | Token_AND_ASSIGN | Token_XOR_ASSIGN | Token_OR_ASSIGN | "=" ) ParseAssignExpr )? 
ConcatLiterals ::= Token_LITERAL ( Token_LITERAL )* 
ParseCompoundStmt ::= "{" ( ( ParseDecl | ParseStmt ) )* "}" 
ParseDecl ::= ( ParseStaticAssert | ParseDeclSpec ( ParseDIDeclarator ( "," ParseDIDeclarator )* )? ";" ) 
ParseStmt ::= ( ";" | TryAttributeSpecList | ParseCompoundStmt | ParseIfStmt | ParseSwitchStmt | ParseWhileStmt | ParseDoStmt | ParseForStmt | ParseGotoStmt | ParseContinueStmt | ParseBreakStmt | ParseReturnStmt | ParseCaseStmt | ParseDefaultStmt | ParseLabelStmt | ParseExpr ) 
ParseDIDeclarator ::= ParseDirectDeclarator ParseInitDeclarator 
ParseIfStmt ::= Token_IF "(" ParseExpr ")" ParseStmt ( Token_ELSE ParseStmt )? 
ParseSwitchStmt ::= Token_SWITCH "(" ParseExpr ")" ParseStmt 
ParseWhileStmt ::= Token_WHILE "(" ParseExpr ")" ParseStmt 
ParseDoStmt ::= Token_DO ParseStmt Token_WHILE "(" ParseExpr ")" ";" 
ParseForStmt ::= Token_FOR "(" ( ( ParseDecl | ParseExpr ) )? "," ( ParseExpr )? ";" ( ParseExpr )? ")" ParseStmt 
ParseGotoStmt ::= Token_GOTO Token_IDENTIFIER ";" 
ParseContinueStmt ::= Token_CONTINUE ";" 
ParseBreakStmt ::= Token_BREAK ";" 
ParseReturnStmt ::= Token_RETURN ( ParseExpr )? ";" 
ParseCaseStmt ::= ParseCaseChoice ParseStmt 
ParseDefaultStmt ::= Token_DEFAULT ":" ParseStmt 
ParseLabelStmt ::= Token_IDENTIFIER ":" 
ParseExpr ::= ParseCommaExpr 
FunctionSpecifier ::= ( Token_INLINE | Token_NORETURN ) 
AlignmentSpecifier ::= Token_ALIGNAS "(" ( IsTypeName | ParseExpr ) ")" 
StorageSpecifier ::= ( Token_TYPEDEF | Token_EXTERN | Token_STATIC | Token_THREAD | Token_AUTO | Token_REGISTER ) 
TypeQualifier ::= ( Token_CONST | Token_RESTRICT | Token_VOLATILE | Token_ATOMIC ) 
TypeSpecifier ::= ( Token_SIGNED | Token_UNSIGNED | Token_VOID | Token_CHAR | Token_SHORT | Token_INT | Token_LONG | Token_FLOAT | Token_DOUBLE | Token_BOOL | Token_COMPLEX | ( Token_STRUCT | Token_UNION ) ParseStructUnionSpec | Token_ENUM ParseEnumSpec ) 
IsTypeName ::= Token_IDENTIFIER 
ParseStructUnionSpec ::= ( Token_IDENTIFIER )? "{" ParseStructUnionDecl ( "," ParseStructUnionDecl )* "}" ( TryAttributeSpecList )? 
ParseEnumSpec ::= ( TryAttributeSpecList )? ( Token_IDENTIFIER )? "{" ParseEnumerator ( "," ParseEnumerator )* "}" 
ParseStructUnionDecl ::= ( ParseStaticAssert | ParseDeclSpec ( ParseDeclarator ( ":" ParseBitField )? )* ) 
ParseBitField ::= Token_I_CONSTANT ( ParseAssignExpr )? 
ParseEnumerator ::= Token_IDENTIFIER ( TryAttributeSpecList )? ( "=" ParseAssignExpr )? 
ParsePointer ::= "*" ( "*" )* 
ParseArrayFuncDeclarator ::= ( "[" ( ParseArrayLength )? "]" | "(" ( ParseParamList )? ")" ) ParseArrayFuncDeclarator 
ParseArrayLength ::= ParseAssignExpr 
ParseParamList ::= ParseParamDecl ( "," ( ParseParamDecl | Token_ELLIPSIS ) )* 
ParseParamDecl ::= ParseDeclSpec ParseDeclarator 
ParseAttributeSpec ::= Token_ATTRIBUTE "(" "(" ParseAttribute ( "," ParseAttribute )* ")" ")" 
ParseAttribute ::= Token_IDENTIFIER "(" ( Token_IDENTIFIER ( "," Token_IDENTIFIER )* )? ")" 
ParseInitDeclaratorSub ::= ParseInitializer 
ParseInitializer ::= "=" "{" ( ( ParseLiteralInitializer | ParseAssignExpr | ParseArrayInitializer ) )? "}" 
ParseLiteralInitializer ::= Token_LITERAL 
ParseArrayInitializer ::= "{" ( ParseArrayInitializerItem ( "," ParseArrayInitializerItem )* )? "}" 
ParseArrayInitializerItem ::= ( "[" ParseAssignExpr "]" )? ParseInitializer 
ParseCommaExpr ::= ParseAssignExpr ( "," ParseAssignExpr )* 
ParseConditionalExpr ::= ParseLogicalOrExpr ( "?" ParseExpr ":" ParseConditionalExpr )? 
ParseLogicalOrExpr ::= ParseLogicalAndExpr ( Token_LOGICAL_OR ParseLogicalAndExpr )* 
ParseLogicalAndExpr ::= ParseBitwiseOrExpr ( Token_LOGICAL_AND ParseBitwiseOrExpr )* 
ParseBitwiseOrExpr ::= ParseBitwiseXorExpr ( "|" ParseBitwiseXorExpr )* 
ParseBitwiseXorExpr ::= ParseBitiwiseAndExpr ( "^" ParseBitiwiseAndExpr )* 
ParseBitiwiseAndExpr ::= ParseEqualityExpr ( "&" ParseEqualityExpr )* 
ParseEqualityExpr ::= ParseRelationalExpr ( ( Token_EQ | Token_NE ) ParseRelationalExpr )* 
ParseRelationalExpr ::= ParseShiftExpr ( ( Token_LE | Token_GE | "<" | ">" ) ParseShiftExpr )* 
ParseShiftExpr ::= ParseAdditiveExpr ( ( Token_LEFT | Token_RIGHT ) ParseAdditiveExpr )* 
ParseAdditiveExpr ::= ParseMultiplicativeExpr ( ( "+" | "-" ) ParseMultiplicativeExpr )* 
ParseMultiplicativeExpr ::= ParseCastExpr ( ( "*" | "/" | "%" ) ParseCastExpr )* 
ParseCastExpr ::= ( "(" ParseTypeName ")" ( ParseCompoundLiteral ( ParsePostfixExprTail )? )? ParseCastExpr )? ParseUnaryExpr 
ParseTypeName ::= ParseSpecQual ( ParseAbstractDeclarator )? 
ParseCompoundLiteral ::= ParseInitDeclaratorSub 
ParsePostfixExprTail ::= ( ParseSubScripting | ParseFuncCall | ParseMemberRef | ParsePostfixIncDec ) 
ParseUnaryExpr ::= ( ( ParseAlignof | ParseSizeof | ParsePrefixIncDec | ParseUnaryOp ) )? ParsePostfixExpr 
ParseAlignof ::= Token_ALIGNOF 
ParseSizeof ::= Token_SIZEOF 
ParsePrefixIncDec ::= ( Token_INC | Token_DEC ) 
ParseUnaryOp ::= ( "&" | "*" | "+" | "-" | "~" | "!" ) 
ParsePostfixExpr ::= ( TryCompoundLiteral )? ParsePrimaryExpr ParsePostfixExprTail 
ParseSpecQual ::= ParseDeclSpec 
ParseAbstractDeclarator ::= ParseDeclarator 
TryCompoundLiteral ::= "(" ParseTypeName ")" "{" ParseCompoundLiteral "}" ParsePostfixExprTail 
ParsePrimaryExpr ::= ( "(" ParseExpr ")" | Token_IDENTIFIER | ParseConstant | ConcatLiterals | ParseGeneric ) 
ParseConstant ::= ( Token_I_CONSTANT | Token_C_CONSTANT | Token_C_DOUBLE ) 
ParseGeneric ::= "(" ParseAssignExpr ( "," ( Token_DEFAULT | ParseTypeName ) ":" ParseAssignExpr )* ")" 
ParseSubScripting ::= "[" ParseExpr "]" 
ParseFuncCall ::= "(" ( ParseAssignExpr ( "," ParseAssignExpr )* )? ")" 
ParseMemberRef ::= ( Token_PTR | "." ) Token_IDENTIFIER 
ParsePostfixIncDec ::= ( Token_INC | Token_DEC ) 
ParseCaseChoice ::= Token_CASE ParseAssignExpr ( Token_ELLIPSIS ParseAssignExpr )? ":" 

//
// tokens
//

Token_AUTO ::= "auto"
Token_BREAK ::= "break"
Token_CASE ::= "case"
Token_CHAR ::= "char"
Token_CONST ::= "const"
Token_CONTINUE ::= "continue"
Token_DEFAULT ::= "default"
Token_DO ::= "do"
Token_DOUBLE ::= "double"
Token_ELSE ::= "else"
Token_ENUM ::= "enum"
Token_EXTERN ::= "extern"
Token_FLOAT ::= "float"
Token_FOR ::= "for"
Token_GOTO ::= "goto"
Token_IF ::= "if"
Token_INLINE ::= "inline"
Token_INT ::= "int"
Token_LONG ::= "long"
Token_SIGNED ::= "signed"
Token_UNSIGNED ::= "unsigned"
Token_REGISTER ::= "register"
Token_RESTRICT ::= "restrict"
Token_RETURN ::= "return"
Token_SHORT ::= "short"
Token_SIZEOF ::= "sizeof"
Token_STATIC ::= "static"
Token_STRUCT ::= "struct"
Token_SWITCH ::= "switch"
Token_TYPEDEF ::= "typedef"
Token_UNION ::= "union"
Token_VOID ::= "void"
Token_VOLATILE ::= "volatile"
Token_WHILE ::= "while"
Token_ALIGNAS ::= "_Alignas"
Token_ALIGNOF ::= "_Alignof"
Token_ATOMIC ::= "_Atomic"
Token_ATTRIBUTE ::= "__attribute__"
Token_BOOL ::= "_Bool"
Token_COMPLEX ::= "_Complex"
Token_GENERIC ::= "_Generic"
Token_IMAGINARY ::= "_Imaginary"
Token_NORETURN ::= "_Noreturn"
Token_STATIC_ASSERT ::= "_Static_assert"
Token_THREAD ::= "_Thread_local"
Token_DSHARP ::= "##"
Token_PTR ::= "->"
Token_INC ::= "++"
Token_DEC ::= "--"
Token_LEFT ::= "<<"
Token_RIGHT ::= ">>"
Token_LE ::= "<="
Token_GE ::= ">="
Token_EQ ::= "=="
Token_NE ::= "!="
Token_LOGICAL_AND ::= "&&"
Token_LOGICAL_OR ::= "||"
Token_MUL_ASSIGN ::= "*="
Token_DIV_ASSIGN ::= "/="
Token_MOD_ASSIGN ::= "%="
Token_ADD_ASSIGN ::= "+="
Token_SUB_ASSIGN ::= "-="
Token_LEFT_ASSIGN ::= "<<="
Token_RIGHT_ASSIGN ::= ">>="
Token_AND_ASSIGN ::= "&="
Token_XOR_ASSIGN ::= "^="
Token_OR_ASSIGN ::= "|="
Token_ELLIPSIS ::= "..."